Download openGL2.cpp to
the project folder. Open the file from the VC7 interface and right-click to add it to the project.
Build the project and run it. The application opens two windows; one is a console window and the
other is an OpenGL window. The latter shows a purple sphere, though it looks more like a circle because
there is no lighting in the scene. We'll add that next.
GLfloat white[] = {1,1,1,1}; // light color
GLfloat lightPosition[]={1,1,-8,1}; // light position
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); // setlight position
glLightfv(GL_LIGHT0, GL_DIFFUSE, white); // set diffuse light color
glLightfv(GL_LIGHT0, GL_SPECULAR, white); // set specular light color
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
The glLightfv() command takes three arguments. The first is the
light number, which should be one of the OpenGL constants GL_LIGHTi. The second is
the lighting parameter to be set, also one of the OpenGL constants. The final argument
is the
value the parameter should be set to. In this case, the value is a vector of floats, as specified by the
fv (f for float and v for vector) in the command glLightfv(). (See Woo for a full description of argument specifications.)
GLfloat lightPosition[]={x,y,z, type};
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
specify the type of light and its position/direction. If type=1, the light is a point light while type=0 specifies a
directional light. For a point light, (x,y,z) specifies the position of the light. For a directional light,
< x,y,z > is the light direction.
(I know... I'm just the messenger!)glLightfv(GL_LIGHT0, GL_DIFFUSE, white); // set diffuse light color glLightfv(GL_LIGHT0, GL_SPECULAR, white); // set specular light colorSets the color of our light. Note that OpenGL lighting is more flexible than the model we used for raytracing in that a light can provide different colors for diffuse and specular.
GLfloat white[] = {1,1,1,1}; // white
GLfloat purple[] = {1,0,1,1}; // purple
glMaterialfv(GL_FRONT, GL_DIFFUSE, purple);
glMaterialfv(GL_FRONT, GL_SPECULAR, white);
glMateriali(GL_FRONT,GL_SHININESS,50);
Just as with colors, the most recently defined properties apply to an object when it is drawn.
float const=1.0; // constant attenuation float linear=0.5; // linear attenuation float quadratic=0.0; // quadratic attenuation glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, const); // set constant attenuation term glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, const); // set linear attenuation term glLightf(GL_LIGHT0, GL_QUQADRATIC_ATTENUATION, const); // set quadratic attenuation termNote that we are using glLightf, where the f signifies a float argument.
GLfloat lightPosition[]={1,1,-8,1}; // light position
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); // setlight position
The light position is now transformed by the translate command. Recompile and rerun your program. You should see a big change in the
lighting. You may think that OpenGL realizes that the sphere is occluding the light. Unfortunately, OpenGL is not that smart --
in fact shadows are quite difficult to achieve in OpenGL (we'll talk more about that in a future tutorial). We'll explore what
is going on here next but remove the two lines we
happened in the next section but remove the two lines you added above before going on.
glBegin(GL_TRIANGLE_STRIP); glVertex3f(-20.0,-1.0,-40.0); glVertex3f(-20.0,-1.0,0.0); glVertex3f(20.0,-1.0,-40.0); glVertex3f(20.0,-1.0,0.0); glEnd();Recompile and run your program.