Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 307 1.0 Graphic Programming Lecture 2 OpenGL Lightning.

Similar presentations


Presentation on theme: "CSC 307 1.0 Graphic Programming Lecture 2 OpenGL Lightning."— Presentation transcript:

1 CSC 307 1.0 Graphic Programming Kasun@sjp.ac.lk Lecture 2 OpenGL Lightning

2 Kasun@sjp.ac.lk2 Set color of the light  ///spotlight  GLfloat ambientLight[]={0.3f,0.3f,0.3f,1.0f};  GLfloat diffuseLight[]={0.7f,0.7f,0.7f,1.0f};  GLfloat specular[]={1.0f,1.0f,1.0f,1.0f};

3 Kasun@sjp.ac.lk3 Setup and enable light 0  glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);  glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);  glLightfv(GL_LIGHT0, GL_SPECULAR, specular);

4 Kasun@sjp.ac.lk4 Enable Lightings  glEnable(GL_LIGHTING);  glEnable(GL_LIGHT0);

5 Kasun@sjp.ac.lk5 Points in 2D  GLfloat lightPos[]={-1.5f, 2.0f, 5.5f, 1.0f};  glLightfv(GL_LIGHT0, GL_POSITION, lightPos);

6 Kasun@sjp.ac.lk6 Enable color tracking glEnable(GL_COLOR_MATERIAL); // set material properties to follow glColor values glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

7 Kasun@sjp.ac.lk7

8 Kasun@sjp.ac.lk8 // Bright white light – full intensity RGB values GLfloat ambientLight[] = { 1.0f, 1.0f, 1.0f, 1.0f }; // Enable lighting glEnable(GL_LIGHTING); // Set light model to use ambient light specified by ambientLight[] glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);

9 Kasun@sjp.ac.lk9 Glfloat gray[] = { 0.75f, 0.75f, 0.75f, 1.0f }; glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray); glBegin(GL_TRIANGLES); glVertex3f(-15.0f,0.0f,30.0f); glVertex3f(0.0f, 15.0f, 30.0f); glVertex3f(0.0f, 0.0f, -56.0f); glEnd();

10 Kasun@sjp.ac.lk10 // Enable color tracking glEnable(GL_COLOR_MATERIAL); // Front material ambient and diffuse colors track glColor glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); glcolor3f(0.75f, 0.75f, 0.75f); glBegin(GL_TRIANGLES); glVertex3f(-15.0f,0.0f,30.0f); glVertex3f(0.0f, 15.0f, 30.0f); glVertex3f(0.0f, 0.0f, -56.0f); glEnd();

11 Kasun@sjp.ac.lk11 void glCullFace(GLenum mode);  Indicates which polygons should be discarded (culled) before they’re converted to screen coordinates.  The mode is either GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK to indicate front−facing,  back−facing, or all polygons. To take effect, culling must be enabled using glEnable() with  GL_CULL_FACE; it can be disabled with glDisable() and the same argument.

12 Kasun@sjp.ac.lk12 void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar);

13 Kasun@sjp.ac.lk13 Viewport Transformation  gluPerspective(myFovy, 2.0, myNear, myFar);  glViewport (0, 0, 400, 400);  To avoid the distortion, this line could be used:  glViewport(0, 0, 400, 200);  void glDepthRange(GLclampd near, GLclampd far);

14 Kasun@sjp.ac.lk14 void Resize(GLsizei w, GLsizei h) { GLfloat nRange = 100.0f; if(h == 0) h = 1; glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Establish clipping volume (left, right, bottom, top, near, far) if (w <= h) glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, - nRange, nRange); else glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, - nRange, nRange); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); }

15 Kasun@sjp.ac.lk15 void Reshape(GLsizei w, GLsizei h) {if(h == 0) h = 1; // Prevent a divide by zero glViewport(0, 0, w, h); // Set viewport to window size Glfloat fAspect = (GLfloat)w/(GLfloat)h; glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Field of view of 45 degrees, near and far planes 1.0 and 425 gluPerspective(45.0f, fAspect, 1.0, 425.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); }

16 Kasun@sjp.ac.lk16 Reshape  glutReshapeFunc(reshape); void reshape(int w, int h) { glViewport(0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); //gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0); glOrtho(100.0,-100.0,150.0,-150.0,100.0,-100.0); // glFrustum (-4.0, 4.0, -4.0, 4.0, 2, 10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //gluLookAt(0.0, 0.0, -100.0, 0.0, 0.0, -100.0, 0.0, 1.0, 0.0); }


Download ppt "CSC 307 1.0 Graphic Programming Lecture 2 OpenGL Lightning."

Similar presentations


Ads by Google