# CS3162 Introduction to Computer Graphics Helena Wong, 2000

Simple Programming with OpenGL
In Week 9 tutorial, you have already seen an OpenGL program. You should make sure that you understand the geometry of the viewing system and the modelling system. You should also know that now we don’t specify colors like (255,0,0), but we specify as (1.0,0,0), for red color. Consult your classmates or your instructor if necessary. However, this program is very simple. It does not include lighting and material properties. And the construction of the object is done by clumsy construction of polygons. In this handout, you’ll see some more examples, which involves rendering of animated lighting and objects with material properties. 1. Drawing with OpenGL Auxiliary Library Functions In previous example, the cube was drawn using clumsy “glBegin() .. glVertex3f .. glVertex3f .. glEnd()” instructions. Indeed we can use more advanced functions in the OpenGL Auxiliary Library to draw the cube. void DoPainting(CDC *pDC,int WhichCallInCurrentCycle) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear with background color glPushMatrix(); glTranslatef(0.0, 0.0, -5.5); glRotatef(gObjectRotationX, 1.0, 0.0, 0.0); glRotatef(gObjectRotationY, 0.0, 1.0, 0.0); glRotatef(gObjectRotationZ, 0.0, 0.0, 1.0); gObjectRotationX += 1.0; gObjectRotationY += 10.0; gObjectRotationZ += 5.0; glColor3f(1.0,0,1.0); //specify a pink color auxSolidCube(1.0); //draw a solid cube glPopMatrix(); } In the OpenGL Auxiliary Library, there are also other advanced functions for drawing some primitive shapes: void auxWireSphere(GLdouble); void auxSolidSphere(GLdouble); void auxWireCube(GLdouble); void auxSolidCube(GLdouble); void auxWireBox(GLdouble, GLdouble, GLdouble); void auxSolidBox(GLdouble, GLdouble, GLdouble); void auxWireTorus(GLdouble, GLdouble); void auxSolidTorus(GLdouble, GLdouble); void auxWireCylinder(GLdouble, GLdouble); void auxSolidCylinder(GLdouble, GLdouble); void auxWireIcosahedron(GLdouble); void auxSolidIcosahedron(GLdouble); void auxWireOctahedron(GLdouble); void auxSolidOctahedron(GLdouble); void auxWireTetrahedron(GLdouble); void auxSolidTetrahedron(GLdouble); void auxWireDodecahedron(GLdouble); void auxSolidDodecahedron(GLdouble); void auxWireCone(GLdouble, GLdouble); void auxSolidCone(GLdouble, GLdouble); void auxWireTeapot(GLdouble); void auxSolidTeapot(GLdouble); //define a translation //define a rotation transformation // with rotation angle and vector // of rotation (eg. 1,0,0 = x-axis) //prepare for bigger rotation in next picture

1

0. glColorMaterial(GL_FRONT. 0.0. 0.0}. //clear with background color glEnable(GL_LIGHTING).0).0. auxSolidTorus(0. 0. Drawing with Lighting Effects However.0.4. Defining Specific Lighting Behaviour //define a translation //define a rotation transformation // with rotation angle and vector // of rotation (eg.DiffuseLight_color).5). 0.GL_DIFFUSE). glPushMatrix(). 1.GL_DIFFUSE). gObjectRotationY += 10. glEnable(GL_LIGHT0).0. //enable a light source. glRotatef(gObjectRotationY.GL_DIFFUSE.1.0. glColor3f(1. 0.0. { //enable the lighting system glEnable(GL_LIGHT0). glColor3f(1. so the rendered image is not realistic.0.1.0.GL_LIGHT7 GLfloat DiffuseLight_color[] = {1.0. 1.1.0. Other sources are GL_LIGHT1.1. so we have to specify our own design: void DoPainting(CDC *pDC.-4.0. // with rotation angle and vector ….0.0}. //define a rotation transformation glRotatef(gObjectRotationY. 0.. glRotatef(gObjectRotationX. glPopMatrix(). 1.0.int WhichCallInCurrentCycle) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT).0.0.0). 0. Other sources are GL_LIGHT1.0).0.0. } 3.0). } 2 //specify a pink color //draw a solid torus .0). glTranslatef(0..0.0. To apply lighting effects.0).0). //enable the lighting system //enable a light source.light_position).CS3162 Introduction to Computer Graphics Helena Wong.5). see the following example: void DoPainting(CDC *pDC.9). //clear with background color glEnable(GL_LIGHTING).0.4. 1.GL_LIGHT7 //enable the object material coloring //the following specified material color is for diffuse reflection glEnable(GL_COLOR_MATERIAL).9)..0 = x-axis) //prepare for bigger rotation in next picture //specify a pink color //draw a solid torus However.0. we still not applied lighting effects. glColorMaterial(GL_FRONT. gObjectRotationZ += 5. glRotatef(gObjectRotationZ.1.. auxSolidTorus(0.GL_POSITION. 2000 2. GLfloat light_position[] = {-1. 1. glPopMatrix().0.int WhichCallInCurrentCycle) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT).0. //define a translation glRotatef(gObjectRotationX.0. glLightfv(GL_LIGHT0. 0. //Specify the color of light glLightfv(GL_LIGHT0.0. glPushMatrix(). -5. the default light setting for GL_LIGHT0 may not be satisfactory. //Specify the position of light source } glEnable(GL_COLOR_MATERIAL).1.. 1.0. gObjectRotationX += 1. 0. 0. 0.. 0.0. glTranslatef(0.0. -5.0.1.

glLightfv(GL_LIGHT1.int WhichCallInCurrentCycle) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT).1. } glEnable(GL_COLOR_MATERIAL). 1. glPushMatrix(). -5.0. auxSolidTorus(0. //enable a light source..1.GL_DIFFUSE).1. //Specify the color of light glLightfv(GL_LIGHT0. glLightfv(GL_LIGHT0. auxSolidSphere(0. glColor3f(1.0.0.5).0.0. //define a translation glRotatef(gObjectRotationX.0)..5).0}.1.1.0).0.. 2000 4.0. //enable 2nd light source GLfloat DiffuseLight_color[] = {0. glColor3f(1. //clear with background color glEnable(GL_LIGHTING).int WhichCallInCurrentCycle) { .1.light_position).0..0}. glPopMatrix().0.-4. // with rotation angle and vector ….0.DiffuseLight_color). } //define a translation //specify a pink color //draw a solid sphere 3 .0.GL_LIGHT7 GLfloat DiffuseLight_color[] = {1. GLfloat light_position[] = {-1.0.5).9).1. Add one more object The following example simply adds one more sphere.1. 0. 0.1.0.0. 0.0.0.4. glPushMatrix(). auxSolidTorus(0.0. //define a translation .0}.0.0).GL_DIFFUSE.0).4.0.0.. but which does not move.GL_DIFFUSE. glPopMatrix(). Other sources are GL_LIGHT1.1. //Specify the position of light source } { glEnable(GL_LIGHT1).0).0.0}. //draw a solid torus glPopMatrix().-4. glTranslatef(0. 0.0.GL_POSITION. } //specify a pink color //draw a solid torus //Specify the position of light source 5.0.0. 1.0.4). glPushMatrix(). -5. { //enable the lighting system glEnable(GL_LIGHT0). //Specify the color of light glLightfv(GL_LIGHT1.0. glTranslatef(-1.0. void DoPainting(CDC *pDC.DiffuseLight_color). Add One More Light Source We can add variation by changing the existing light source to red color and adding the 2nd blue color light source. glColorMaterial(GL_FRONT. glTranslatef(1. 0.0.0.CS3162 Introduction to Computer Graphics Helena Wong. glColor3f(1.0.light_position). 0. GLfloat light_position[] = {1. void DoPainting(CDC *pDC.9). //define a rotation transformation glRotatef(gObjectRotationY. 0. -5.0.0.GL_POSITION.0.

-4. glPushMatrix(). { //clear with background color //enable the lighting system glEnable(GL_LIGHT0).1.9).GL_POSITION. -5.GL_LIGHT7 GLfloat DiffuseLight_color[] = {1.9).0. and keep on changing the scale of the solid sphere. } 4 .1. 0..5). glPopMatrix().0.5).0.0. glColorMaterial(GL_FRONT.1. 0. 0.GL_POSITION. The following example animate the position of 2nd light source.0}. scaling the size of objects.GL_DIFFUSE.GL_POSITION. //enable 2nd light source GLfloat DiffuseLight_color[] = {0.light_position1).0.0. 0.9.0.0. //draw a solid sphere glPopMatrix().1. -5. //Specify the color of light glLightfv(GL_LIGHT0.0).-4.0). //Specify 1st position of light source GLfloat light_position2[] = {-1. void DoPainting(CDC *pDC.0.light_position).0. 1.0.0.9.0.0.0.0.4).0. Other sources are GL_LIGHT1. glLightfv(GL_LIGHT0.1.0}. 2000 6.0).0. 1.0.-4. } if (WhichCallInCurrentCycle==7 || //the sphere is changed to red only for the 7th picture WhichCallInCurrentCycle>10) //or the 10th or later pictures in a cycle { glColor3f(1.0. as well as occasionally changing its color.0.0.GL_DIFFUSE). Animating Position of Light Source.int WhichCallInCurrentCycle) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT).0. gObjectRotationZ += 5. //Specify 2nd position of light source if ((WhichCallInCurrentCycle%2)==0) { glLightfv(GL_LIGHT1.0.1. 0. and the coloring of light or objects etc.1.0.0.1. GLfloat light_position1[] = {1.0.0. glTranslatef(-1.0). 0. 0. auxSolidTorus(0. 1.1.GL_DIFFUSE.0}.0. glColor3f(1.0.0. 0. // with rotation angle and vector glRotatef(gObjectRotationZ.0.0}.DiffuseLight_color).0.0}. //define a rotation transformation glRotatef(gObjectRotationY.0. //define a translation if ((WhichCallInCurrentCycle%2)==0) //occasionally scale down the sphere { glScalef(0.0.1. 1.0. } } glEnable(GL_COLOR_MATERIAL). glTranslatef(1. } else { glColor3f(1.0).CS3162 Introduction to Computer Graphics Helena Wong. //Specify the position of light source } { glEnable(GL_LIGHT1). // of rotation (eg. //define a translation glRotatef(gObjectRotationX. //draw a solid torus glPushMatrix(). Sizes of objects Special effect can be produced by animating the position of light sources. GLfloat light_position[] = {-1. //Specify the color of light glLightfv(GL_LIGHT1. } else { glLightfv(GL_LIGHT1.0 = x-axis) gObjectRotationX += 1.1.0.light_position2)..0.. glEnable(GL_LIGHTING).4.0).0.DiffuseLight_color). //prepare for bigger rotation in next picture gObjectRotationY += 10. //enable a light source. //specify a pink color } auxSolidSphere(0.0.0.

8.GL_POSITION.5.8.0. glTranslatef(0.int WhichCallInCurrentCycle) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT).0).0). } A) B) 5 . A. //enable a light source.int WhichCallInCurrentCycle) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT). GLfloat light_position[] = {-3.0. } B. Only diffuse reflection void DoPainting(CDC *pDC.1. -5.5. glColor3f(1.2.1. glLightfv(GL_LIGHT0.1.1. //define a translation //the following glColor3f sets diffuse reflection property //specify a pink color: it can reflect red and blue light glColorMaterial(GL_FRONT.GL_AMBIENT. //define a translation //the following glColor3f sets diffuse reflection property //specify a pink color: it can reflect red and blue light glColorMaterial(GL_FRONT. //clear with background color glEnable(GL_LIGHTING). We can add different kinds of reflections.DiffuseLight_color).8.8.GL_LIGHT7 GLfloat DiffuseLight_color[] = {0. 0.GL_DIFFUSE). //clear with background color glEnable(GL_LIGHTING). glPushMatrix().5).8.1. GLfloat AmbientLight_color[] = {0.0.0. auxSolidSphere(1.GL_LIGHT7 GLfloat DiffuseLight_color[] = {0.0.0.GL_DIFFUSE.GL_DIFFUSE).0. { //enable the lighting system //the diffuse color component of the light //set the light's diffuse color component //the position of light source //set the position of light source glEnable(GL_LIGHT0).DiffuseLight_color). glColor3f(1.2. //enable the object material coloring //define an object to be displayed. Let's start from a simple example.5). Different reflections So far we have been dealing with only diffuse reflection.. 2000 7.1. //draw a solid cube glPopMatrix(). -5.0. Diffuse + ambient reflection void DoPainting(CDC *pDC.0.0..1. glLightfv(GL_LIGHT0. which we have a static sphere.0..0}.. glLightfv(GL_LIGHT0. //draw a solid cube glPopMatrix(). GLfloat light_position[] = {-3.0.0}.. Other sources are GL_LIGHT1.0.0.2. } glEnable(GL_COLOR_MATERIAL).light_position).0.0. { //enable the lighting system //the diffuse color component of the light //the ambient color component of the light //set the light's diffuse color component //set the light's ambient color component //the position of light source //set the position of light source glEnable(GL_LIGHT0).0.0. Other sources are GL_LIGHT1. glTranslatef(0.0.0}.AmbientLight_color).0).8. } glEnable(GL_COLOR_MATERIAL).0..0}. //enable a light source.0}. glLightfv(GL_LIGHT0.0).CS3162 Introduction to Computer Graphics Helena Wong. auxSolidSphere(1.light_position).GL_POSITION.GL_DIFFUSE. 0. glLightfv(GL_LIGHT0. //enable the object material coloring //define an object to be displayed. glPushMatrix().5.5.

//define how shiny the object is (valid value: 0.5. GLfloat AmbientLight_color[] = {0.0. 0.2.0).. -5.0. glLightfv(GL_LIGHT0.0.1.. glMaterialf(GL_FRONT.0. Diffuse + ambient + specular reflection void DoPainting(CDC *pDC.5. //enable a light source.5.0).0.0. //define a translation //the following glColor3f sets diffuse reflection property //specify a pink color: it can reflect red and blue light glColorMaterial(GL_FRONT.b light auxSolidSphere(1.1. glLightfv(GL_LIGHT0.it can reflect all r.1. //the following glColor3f sets specular reflection property glColor3f(1.SpecularLight_color). Other sources are GL_LIGHT1.5.. glPushMatrix(). glTranslatef(0.0.0. //specify a white color . } C) 6 .2.GL_DIFFUSE. glColor3f(1.5).2.0. glLightfv(GL_LIGHT0.128.GL_DIFFUSE).0.0) glColorMaterial(GL_FRONT.GL_SPECULAR.0).4.GL_SHININESS. //the diffuse color component of the light //the ambient color component of the light //the specular color component of the light //set the light's diffuse color component //set the light's ambient color component //set the light's specular color component //the position of light source //set the position of light source } glEnable(GL_COLOR_MATERIAL).0}.0 . 2000 C. GLfloat SpecularLight_color[] = {0.0}.0.0.0.AmbientLight_color).8.g.8.0}. GLfloat light_position[] = {-3.1.8. //clear with background color glEnable(GL_LIGHTING). //enable the object material coloring //define an object to be displayed.light_position). glLightfv(GL_LIGHT0.5.0}.int WhichCallInCurrentCycle) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT).0.0.DiffuseLight_color).1.CS3162 Introduction to Computer Graphics Helena Wong.1.GL_AMBIENT. { //enable the lighting system glEnable(GL_LIGHT0).GL_POSITION.1.GL_SPECULAR). //draw a solid cube glPopMatrix().GL_LIGHT7 GLfloat DiffuseLight_color[] = {0.0).