Lab(7) - Graphics

Objectives:

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 2005 */

#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

    glBegin(GL_POLYGON);
            if(thiscolor%2 ==0)
                   Material(0);

            else
                   Material(1);
            thiscolor++;

            glVertex2f(row,col);
            glVertex2f(row+1,col);
            glVertex2f(row+1,col+1);
            glVertex2f(row,col+1);
    glEnd();   

}

void display(void)
{
    int i,j;
    glEnable(GL_DEPTH_TEST);  
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
     gluLookAt(9, 14,9,4, 4 , 0 , 0,0,1);

    for(i=0;i<2;i++)
    {
        thiscolor++;
        for(j=0;j<2;j++){
            glPushMatrix();
            square(i,j);
            glPopMatrix();
            
        }
    }
    glFlush();
}
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_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_DEPTH_TEST); /* enable z buffer */
    glEnable(GL_NORMALIZE);
    glMatrixMode(GL_PROJECTION);
    // 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);
    glMatrixMode(GL_MODELVIEW);
}

void main(int argc, char **argv){
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH);
    glEnable(GL_DEPTH_TEST);  
    glutInitWindowSize(500, 500);
    glutCreateWindow("2-by-2 grid");
    myinit();
    glutDisplayFunc(display);
    glutMainLoop();
}
 

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.

Imag1_2x2

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

Image2_8x8

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 

Usage

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

Description

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 

Usage

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

Description

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.

Image3_checkers

Submit a hard copy of your program and a printout of the scene in the lab.  Make sure your name appers at the top of your program and the display.

Only if you are really stock use this hint: hint

Post-Lab - Due: 9:00 pm Tuesday April 26
(No late submission will be graded)

Here is a challenging work for you to complete.  Add additional code segment to your program such that when you left click on any of the pieces three things can happen (Only straight moves, no diagonal):
1) The piece moves by one toward the opposite side if there is a free space in front of it.
2) It jumps by one over the opposite member if the cell immediately after the opposite member is free, and removes the opposite member (the one it just jumped over) from the board and up its own score by 1.
3) It does not do anything if the next two cells in front of it are taken. So, if you click on such a member, nothing will happen until you click on a member that is eligible. Note that you cannot jump over your own members.
4) It keeps track of players' turn, so if the red member has just made a move, it cannot not make another move again until the white one does. In case of a deadluck, the current ihigh score wins.

Complete this by: 9:00 pm on April 26.  E-mail the program and please make sure you have your name on your programs. No late submission will be accepted.