Lab(5) - Graphics


To use the lighting, predefined shapes, and material properties to create a game scene

The following program creates a 2x2 red-white grid.  The gluLookAt function is used to give it the view shown in the first figure below the code and the material properties and a light source are used to give it the lighting and color combinations that  appears in that  figure.  

/* This program creates a 2x2 red-white grid - RT Spring 2003 */

#include <GL/glut.h>
typedef float point[3];

int thiscolor = 1;

void Material(bool Red){
    // set up material properties for red or white
    GLfloat mat_amb_diff_red[] = { 1, 0, 0, 1.0 };
    GLfloat mat_amb_diff_white[] = { 1,1,1, 1.0 };
    if(Red)glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,mat_amb_diff_red);
    else glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,mat_amb_diff_white);

void square(int row,int col)
// Draw square at given logical coordinates (0 - 7, 0 - 7), setting color as appropriate
// If one of first two or last two rows, draw piece of appropriate color

            if(thiscolor%2 ==0)




void display(void)
    int i,j;
     gluLookAt(9, 14,9,4, 4 , 0 , 0,0,1);

void myinit()
    GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
    GLfloat mat_shininess[] = { 50.0 };

    GLfloat light_position[] = { 0, 0, 0};

    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
    glEnable(GL_DEPTH_TEST); /* enable z buffer */
    // gluPerspective is an alternative to glFrustum in which
    // the 'lens' is specified by angle and aspect ratio rather
    // than frustum coordinates
    // The near and far planes have the same meaning as before.
    gluPerspective( /* field of view in degree */ 45.0,
        /* aspect ratio */ 1.0,
        /* Z near */ .10, /* Z far */ 100);

void main(int argc, char **argv){
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(500, 500);
    glutCreateWindow("2-by-2 grid");

Activity (1)
The first thing you are to do is to cut and paste the above code and make sure you are able to create the pattern that is shown below.


Activity (2)
Make a very few changes in the program to create the 8-by-8 grid of the same color pattern shown below.  


Now, take a moment to go through the code to determine:
    1) How the red-white color grids are created?
    2) How the lighting is done?
    3) How this view of the board is created?

So far most of the objects that we have created have been home-made.  We created a square using its 8 vertices.  We recently created a sphere using subdivision of a tetrahedron.  In some cases, we can use the pre-defined shapes in OpenGL.  For example, you can use the following calls to create a cone and sphere respectively.

    glutSolidCone(0.4, 2, 40, 40);
    glutSolidSphere(0.4, 40, 40);

You can find the definition of these two function calls at:

 glutSolidSphere : a solid sphere 


void glutSolidSphere(GLdouble radius,
GLint slices, GLint stacks);
The radius of the sphere.
The number of subdivisions around the Z axis (similar to lines of longitude).
The number of subdivisions along the Z axis (similar to lines of latitude).


Renders a sphere centered at the modeling coordinates origin of the specified radius. The sphere is subdivided around the Z axis into slices and along the Z axis into stacks.

glutSolidCone and  a solid cone 


void glutSolidCone(GLdouble base, GLdouble height,
GLint slices, GLint stacks);
The radius of the base of the cone.
The height of the cone.
The number of subdivisions around the Z axis.
The number of subdivisions along the Z axis.


glutSolidCone render a solid cone oriented along the Z axis. The base of the cone is placed at Z = 0, and the top at Z = height. The cone is subdivided around the Z axis into slices, and along the Z axis into stacks.

Now that you have seen these definitions, you can determine what each one of the following lines are doing.

    glutSolidCone(0.4, 2, 40, 40);
    glutSolidSphere(0.4, 40, 40);

What is next?

Activity (3)
You are to put all pieces together to create the following view of game scene.  There are 8 rows and 8 columns.  In the middle of each square of the first and the last two rows there is a piece that is created by putting together a cone and a sphere.  Note that to create a piece, we are to move to the center of each square, then create a cone (I have used 0.4 for radius and 2 for the height), then at that height create a sphere (I have used radius of .4).  To get the following view of the board, you need to play with the gluLookAt function.   To get this view, I assume the up is y-axis, I have looked at the center of the board at z =0, and then adjusted the eye position to get the scene that is shown below.  You need to be patient and give it a few try, or sit  back and figure that out then plug in the numbers.


Submit a hard copy of your program and a printout of the scene in the lab.

Only if you are really stock use this hint: hint


Here is a challenging work for you to complete.  Add additional code segment to your program such that when you click on any of the pieces that has a free square in front of it, it moves forward toward the opposite side by one box.  Note that this will only happen when there is nothing in the box in front of the piece.  If there something, then nothing will happen when you click on a piece.  Complete this by the next lab, Wednesday April 30.  E-mail the program and submit hard copy in class, please make sure you have your name on your programs.