Computer Graphics Cube Assignment
William Easton : 04/08/2008
/*
* Name: William R Easton
* Course: Computer Graphics
* Assignment: Project 3
* Purpose: To create a rotating cube in space. Then add texture to
* sides of cube to in this case make an image of dice...
*/
#include <GL/glut.h> /* glut.h includes gl.h and glu.h*/
#include <math.h>
#include "dice1.c"
#include "dice2.c"
#include "dice3.c"
#include "dice4.c"
#include "dice5.c"
double tx = 5.0; //starting point
double tz = 5.0;
/*
* mksq acceptys vert1, vert2, vert3 and vert4 in order to
* create a square in a 3D space given preset
* coordinates of a cube...
*
* Precondition : The vert are given clockwise order
* starting from top left. In order to
* properly display textures...
*/
void mksq(int vert1, int vert2, int vert3, int vert4) {
GLfloat vertices[][3] =
{
{-0.5, -0.5, -0.5}, {0.5, -0.5, -0.5},
{0.5, 0.5, -0.5}, {-0.5, 0.5, -0.5},
{-0.5, -0.5, 0.5}, {0.5, -0.5, 0.5},
{0.5, 0.5, 0.5}, {-0.5, 0.5, 0.5}
};
glBegin(GL_TRIANGLES);
glTexCoord2f(0.0, 0.0);
glVertex3fv(vertices[vert2]);
glTexCoord2f(1.0, 1.0);
glVertex3fv(vertices[vert4]);
glTexCoord2f(1.0, 0.0);
glVertex3fv(vertices[vert1]);
glTexCoord2f(0.0, 0.0);
glVertex3fv(vertices[vert2]);
glTexCoord2f(0.0, 1.0);
glVertex3fv(vertices[vert3]);
glTexCoord2f(1.0, 1.0);
glVertex3fv(vertices[vert4]);
glEnd();
}
/*
* Function for rotating the camera around the Y axis
* until 360 then starts over again at 0.0...
*/
void roteye() {
tz = (tz*(0.999999939)) - (tx*(0.000349066));
tx = (tz*(0.000349066)) + (tx*(0.999999939));
gluLookAt( tx, 5.0, tz,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0);
}
/*
* Display Method for creating camera point and
* making triangles.
*
*/
void display(void)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0, 1.0, 1.0, 10000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
roteye();
glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT);
//Face 1
mksq(7, 3, 0, 4);
//Face 2
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, dice3.pixel_data);
mksq(6, 7, 4, 5);
//Face 3 - TOP
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, dice1.pixel_data);
mksq(7, 6, 2, 3);
//Face 4
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, dice4.pixel_data);
mksq(3, 2, 1, 0);
//Face 5
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, dice5.pixel_data);
mksq(2, 6, 5, 1);
//Face 6 - BOTTOM
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, dice2.pixel_data);
mksq(0, 1, 5, 4);
glutSwapBuffers();
}
/*
* Main Program
* Added GL_DEPTH_TEST and Looped main to
* avoid infinite loop in function...
*/
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glCullFace(GL_FRONT_AND_BACK);
glutInitDisplayMode (GLUT_DOUBLE
| GLUT_RGBA
| GLUT_DEPTH);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Easton : Cube");
glutDisplayFunc(display);
glutIdleFunc(glutPostRedisplay);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, dice2.pixel_data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glutMainLoop(); //loopback main
}
//End--------------------------------------------------->