CSC Graphic Programming Lecture 2 OpenGL Lightning
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};
Setup and enable light 0 glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight); glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
Enable Lightings glEnable(GL_LIGHTING); glEnable(GL_LIGHT0);
Points in 2D GLfloat lightPos[]={-1.5f, 2.0f, 5.5f, 1.0f}; glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
Enable color tracking glEnable(GL_COLOR_MATERIAL); // set material properties to follow glColor values glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
// 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);
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();
// 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();
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.
void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar);
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);
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(); }
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(); }
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, , 0.0, 0.0, , 0.0, 1.0, 0.0); }