Robotics C++ Physics II AP Physics B Electronics Java Astronomy Other Courses Summer Session  

Exercises

 

 

1. Lighting - Duplicate Graphics Provided

2. Explain Code

3. LIghting - Duplicate Graphics Provided

4. Multiple Lights 1

5. Multiple Lights 2

 

Exercise 1: Lighting

Duplicate the following

 

     

 

Exercise 2

Explain the code highlighted in red below

 

//permit use of OpenGL graphics library utilities toolkit

#include <GL/glut.h>

void init(void)

{

   //clear colors and set shading model

   glClearColor (0.0, 0.0, 0.0, 0.0);

   glShadeModel (GL_SMOOTH);

   //defining ambient and diffuse properties and position of light0

   GLfloat light0_ambient[] = {0.2, 0.2, 0.2, 1.0};        

   GLfloat light0_diffuse[] = {1.0, 1.0, 1.0, 1.0};      

   GLfloat light0_position [] = {1.0, 0.0, 0.0,0.0};     

   //defining properties of light0

   glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);     

   glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);

   glLightfv(GL_LIGHT0, GL_POSITION, light0_position);   

   //enable lighting

   glEnable(GL_LIGHTING);

   glEnable(GL_LIGHT0);         

}

void display(void)

{

   glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

   glutSolidSphere (1.0, 20, 16);

   glFlush ();

}

void reshape (int w, int h)

{

   glViewport (0, 0, (GLsizei) w, (GLsizei) h);

   glMatrixMode (GL_PROJECTION);

   glLoadIdentity();

   //EXPLAIN THE FOLLOWING 6 LINES OF CODE - WHAT, IN DETAIL, IS BEING ACCOMPLISHED LINE BY LINE

   if (w <= h)

      glOrtho (-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,

         1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);

   else

      glOrtho (-1.5*(GLfloat)w/(GLfloat)h,

         1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);

   glMatrixMode(GL_MODELVIEW);

   glLoadIdentity();

}

//allows program to quit upon pressing escape key

void keyboard(unsigned char key, int x, int y)

{

   switch (key)

   {

      case 27:

      exit(0);

      break;

   }

}

 

int main(int argc, char** argv)

{

   //initialize glut

   glutInit(&argc, argv);

   //set the display mode (single buffer, RGB (instead of color index, use depth buffer)

   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);

   //set window size

   glutInitWindowSize (500, 500);

   //set window position

   glutInitWindowPosition (100, 100);

   //create the window

   glutCreateWindow (argv[0]);

   //call the init() function above

   init ();

   //call the glut(DisplayFunc and pass it display - function defined above

   //used whenever glut determines that window needs to be redisplayed

   glutDisplayFunc(display);

   //indicates what action should be taken when window is resized

   //calls reshape function above

   glutReshapeFunc(reshape);

 

   //must be last function called, windows opened and event processing begins

   glutMainLoop();

   return 0;

}

 

 

Exercise 3: Shapes

Duplicate the following

4. Multiple Lights

     Not using material properties

 

Ø Place a blue teapot at the origin of a three-D coordinate sytem. Rotate it so that the spout is pointing to the right (same orientation as in the above graphic).

Ø Place the camera  out along the positive z axis, elevated slightly, looking toward the origin, positive direction along the y axis.

Ø Use/place the following 4 lights (do not use material properties)

       ] Light0: Ambient light – place up positive y axis 5 units

       ] Light1: specular and diffuse light placed out positive x axis 5 units from origin

       ] Light2: specular and diffuse light placed out negative x axis 5 units from origin

       ] Light3: specular and difuse light placed out negative z axis 5 units from origin

 

5. Multiple Lights

Using material properties

Run exercise 4 above with a material property for the teapot - pick and identify one 

The following code uses material properties. Run the code and explain why the teapot you demonstrated yesterday (second teapot on the righ belowt) produces a transparent teapot and the following code does not (output is first teapot on right).

#include <GL/glut.h>

void init(void)

{

   GLfloat mat_specular[] = { 3000.0, 3000.0, 3000.0, 3000.0 };

   GLfloat mat_shininess[] = { 100.0 };

   GLfloat mat_surface[] = { 1.0, 1.0, 0.0, 0.0 };

 

   GLfloat white_light[] = { 1.0, 1.0, 1.0, 1.0 };

   GLfloat light_position0[] = { 1.0, 1.0, 1.0, 0.0 };

   GLfloat light_position1[] = { -1.0, -1.0, 1.0, 0.0 };

 

   glClearColor (0.0, .5, 0.0, 0.0);

   glShadeModel (GL_SMOOTH);

 

   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);

   glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);

   glMaterialfv(GL_FRONT, GL_AMBIENT, mat_surface);

 

   glLightfv(GL_LIGHT0, GL_POSITION, light_position0);

   glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);

   glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);

   glLightfv(GL_LIGHT1, GL_POSITION, light_position1);

   glLightfv(GL_LIGHT1, GL_DIFFUSE, white_light);

   glLightfv(GL_LIGHT1, GL_SPECULAR, white_light);

 

   glEnable(GL_LIGHTING);

   glEnable(GL_LIGHT0);

   glEnable(GL_LIGHT1);

   glEnable(GL_DEPTH_TEST);        //THE LINE!

}

void display(void)

{

   gluLookAt (6.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

   glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

   glutSolidTeapot (0.8);

   glFlush ();

}

void reshape (int w, int h)

{

   glViewport (0, 0, (GLsizei) w, (GLsizei) h);

   glMatrixMode (GL_PROJECTION);

   glLoadIdentity();

   if (w <= h)

      glOrtho (-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,

         1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);

   else

      glOrtho (-1.5*(GLfloat)w/(GLfloat)h,

         1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);

   glMatrixMode(GL_MODELVIEW);

   glLoadIdentity();

}

int main(int argc, char** argv)

{

   glutInit(&argc, argv);

   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);

   glutInitWindowSize (400, 400);

   glutInitWindowPosition (100, 100);

   glutCreateWindow ("Why Am I Not Transparent?");

   init ();

   glutDisplayFunc(display);

   glutReshapeFunc(reshape);

   glutMainLoop();

   return 0;

}

 




 

//permit use of OpenGL graphics library utilities toolkit

#include <GL/glut.h>

 

void init(void)

{

   //clear colors and set shading model

   glClearColor (0.0, 0.0, 0.0, 0.0);

   glShadeModel (GL_SMOOTH);

   //defining ambient and diffuse properties of light0 - simplies property listed following

   GLfloat light0_ambient[] = {0.2, 0.2, 0.2, 1.0};

//   GLfloat light0_diffuse[] = {1.0, 1.0, 1.0, 1.0};

   //defining positioin of light0

   GLfloat light0_position [] = {0.0, 5.0, 0.0,0.0};                         //out along the x axis looking toward origin

   //defining properties of light0

   glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);

//   glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);

   //defining positon of first light

   glLightfv(GL_LIGHT0, GL_POSITION, light0_position);

 

   //defining ambient and diffuse properties of light0 - simplies property listed following

//   GLfloat light1_ambient[] = {0.2, 0.2, 0.2, 1.0};

   GLfloat light1_diffuse[] = {1.0, 1.0, 1.0, 1.0};

   //defining positioin of light0

   GLfloat light1_position [] = {5.0, 0.0, 0.0,0.0};                         //out along the x axis looking toward origin

   //defining properties of light0

//   glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);

   glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);

   //defining positon of first light

   glLightfv(GL_LIGHT1, GL_POSITION, light1_position);

 

   //defining ambient and diffuse properties of light0 - simplies property listed following

//   GLfloat light1_ambient[] = {0.2, 0.2, 0.2, 1.0};

   GLfloat light2_diffuse[] = {1.0, 1.0, 1.0, 1.0};

   //defining positioin of light0

   GLfloat light2_position [] = {-5.0, 0.0, 0.0,0.0};                         //out along the x axis looking toward origin

   //defining properties of light0

//   glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);

   glLightfv(GL_LIGHT2, GL_DIFFUSE, light2_diffuse);

   //defining positon of first light

   glLightfv(GL_LIGHT2, GL_POSITION, light2_position);

 

   //defining ambient and diffuse properties of light0 - simplies property listed following

//   GLfloat light1_ambient[] = {0.2, 0.2, 0.2, 1.0};

   GLfloat light3_diffuse[] = {1.0, 1.0, 1.0, 1.0};

   //defining positioin of light0

   GLfloat light3_position [] = {0.0, 0.0, -5.0,0.0};                         //out along the x axis looking toward origin

   //defining properties of light0

//   glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);

   glLightfv(GL_LIGHT3, GL_DIFFUSE, light3_diffuse);

   //defining positon of first light

   glLightfv(GL_LIGHT3, GL_POSITION, light3_position);

 

   //enable lighting

   glEnable(GL_LIGHTING);

   glEnable(GL_LIGHT0);

   glEnable(GL_LIGHT1);

   glEnable(GL_LIGHT2);

   glEnable(GL_LIGHT3);

}

 

void display(void)

{                                                            //******************************************

   gluLookAt (0.0, 2.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);  //This is the line that is giving me trouble

   glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);      //******************************************

   glutSolidTeapot (0.8);                                                   //one of the shapes included with OpenGL

   glFlush ();

}

 

void reshape (int w, int h)

{

   glViewport (0, 0, (GLsizei) w, (GLsizei) h);

   glMatrixMode (GL_PROJECTION);

   glLoadIdentity();

   //following lines of code part of lab assignment

   if (w <= h)

      glOrtho (-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,

         1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);

   else

      glOrtho (-1.5*(GLfloat)w/(GLfloat)h,

         1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);

   glMatrixMode(GL_MODELVIEW);

   glLoadIdentity();

}

//allows program to quit upon pressing escape key

void keyboard(unsigned char key, int x, int y)                  //unsigned means positive values only

{

   switch (key)

   {

      case 27:                        //ASCII Table

      exit(0);

      break;

   }

}

 

int main(int argc, char** argv)

{

   //initialize glut

   glutInit(&argc, argv);

   //set the display mode (single buffer, RGB (instead of color index, use depth buffer)

   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);

   //set window size

   glutInitWindowSize (500, 500);

   //set window position

   glutInitWindowPosition (100, 100);

   //create the window

   glutCreateWindow (argv[0]);

   //call the init() function above

   init ();

   //call the glut(DisplayFunc and pass it display - function defined above

   //used whenever glut determines that window needs to be redisplayed

   glutDisplayFunc(display);

   //indicates what action should be taken when window is resized - calls reshape function above

   glutReshapeFunc(reshape);

   glutKeyboardFunc(keyboard);

   //must be last function called, windows opened and event processing begins

   glutMainLoop();

   return 0;

}