/* Chad Brewbaker *CS147 * room.c * Final Project: * This program draws a texture mapped gallery of some of the works I have * done this semester with wierd stuff at the ends of the hallway. */ #include #include #include #include #include #include #include "sgi.h" //for the texture bindings: #define CELING 0 #define FLOOR 1 #define WALL 2 #define BEARD 3 #define SERPENSKI 4 #define TREE 5 #define HEX 6 #define STIPPOLY 7 #define MATERIAL 8 #define AAPOLY 9 #define RECURSE 10 #define MENU_NULL 0 #define MENU_QUIT 3 #define PI 3.1415926 //Assuming all images have the same height,width,depth... int iheight, iwidth, idepth; unsigned char* celingImg = NULL; unsigned char* floorImg = NULL; unsigned char* wallImg = NULL; unsigned char* beardImg = NULL; unsigned char* serpenskiImg = NULL; unsigned char* treeImg = NULL; unsigned char* hexImg = NULL; unsigned char* stipPolyImg = NULL; unsigned char* materialImg = NULL; unsigned char* aapolyImg =NULL; unsigned char* recurseImg =NULL; GLdouble lookVec[]={0.0, 0.0, -.9, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0}; /* 0--1 Back | | | | 3--2 4--5 Front | | 7--6 */ float room[][8] = { { -2.,2.,-10.},{ 2.,2.,-10.}, {2.,-1.,-10.}, {-2.,-1.,-10.}, { -2.,2.,1.},{ 2.,2.,1.}, {2.,-1.,1.}, {-2.,-1.,1.}}; //this generates the paintings void paint1(unsigned char* theImage, int bindNum) { glEnable(GL_TEXTURE_2D); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glBindTexture(GL_TEXTURE_2D, bindNum); 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); glTexImage2D(GL_TEXTURE_2D, 0, idepth,iwidth,iheight,0, idepth== 4 ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE,theImage); glBegin(GL_POLYGON); glColor3f(.5,.5,.5); glTexCoord2f(0.0,0.0); glVertex3f(-1.,-1.,.0); glTexCoord2f(1.0,0.0); glVertex3f(1.,-1.,.0); glTexCoord2f(1.0,1.0); glVertex3f(1.,1.,.0); glTexCoord2f(0.0,1.0); glVertex3f(-1.,1,.0); glEnd(); glDisable(GL_TEXTURE_2D); } void theCeling() { //top glEnable(GL_TEXTURE_2D); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glBindTexture(GL_TEXTURE_2D, CELING); 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); glTexImage2D(GL_TEXTURE_2D, 0, idepth,iwidth,iheight,0, idepth== 4 ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE,celingImg); glColor3f(1.0,0.0,0.0); glBegin(GL_POLYGON); glTexCoord2f(0.0,0.0); glVertex3fv(room[0]); glTexCoord2f(4.0,0.0); glVertex3fv(room[1]); glTexCoord2f(4.0,4.0); glVertex3fv(room[5]); glTexCoord2f(0.0,4.0); glVertex3fv(room[4]); glEnd(); glDisable(GL_TEXTURE_2D); } void theFloor() { glEnable(GL_TEXTURE_2D); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glBindTexture(GL_TEXTURE_2D, FLOOR); 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); glTexImage2D(GL_TEXTURE_2D, 0, idepth,iwidth,iheight,0, idepth== 4 ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE,floorImg); //bottom glColor4f(.3,1.,.4,1.0); glBegin(GL_POLYGON); glTexCoord2f(0.0,0.0); glVertex3fv(room[3]); glTexCoord2f(4.0,0.0); glVertex3fv(room[2]); glTexCoord2f(4.0,4.0); glVertex3fv(room[6]); glTexCoord2f(0.0,4.0); glVertex3fv(room[7]); glEnd(); glDisable(GL_TEXTURE_2D); } void theWalls() { glEnable(GL_TEXTURE_2D); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glBindTexture(GL_TEXTURE_2D, WALL); 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); glTexImage2D(GL_TEXTURE_2D, 0, idepth,iwidth,iheight,0, idepth== 4 ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE,wallImg); //draw the walls //back glColor4f(.5,.3,.7,1.0); glBegin(GL_POLYGON); glTexCoord2f(0.0,0.0); glVertex3fv(room[0]); glTexCoord2f(4.0,0.0); glVertex3fv(room[1]); glTexCoord2f(4.0,4.0); glVertex3fv(room[2]); glTexCoord2f(0.0,4.0); glVertex3fv(room[3]); glEnd(); //front glColor4f(.6,.3,.5,1.0); glBegin(GL_POLYGON); glTexCoord2f(0.0,0.0); glVertex3fv(room[4]); glTexCoord2f(4.0,0.0); glVertex3fv(room[5]); glTexCoord2f(4.0,4.0); glVertex3fv(room[6]); glTexCoord2f(0.0,4.0); glVertex3fv(room[7]); glEnd(); //left glColor4f(1.0,0.0,0.0,1.0); glBegin(GL_POLYGON); glTexCoord2f(0.0,0.0); glVertex3fv(room[0]); glTexCoord2f(4.0,0.0); glVertex3fv(room[3]); glTexCoord2f(4.0,4.0); glVertex3fv(room[7]); glTexCoord2f(0.0,4.0); glVertex3fv(room[4]); glEnd(); //right //glColor4f(0.0,1.0,0.0,1.0); glBegin(GL_POLYGON); glTexCoord2f(0.0,0.0); glVertex3fv(room[1]); glTexCoord2f(4.0,0.0); glVertex3fv(room[2]); glTexCoord2f(4.0,4.0); glVertex3fv(room[6]); glTexCoord2f(0.0,4.0); glVertex3fv(room[5]); glEnd(); //glFlush(); glDisable(GL_TEXTURE_2D); } void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); //glLoadIdentity(); theCeling(); theFloor(); theWalls(); //back wall glPushMatrix(); glTranslatef(0.0,0.2,-9.95); paint1(recurseImg, RECURSE); glPopMatrix(); //1st glPushMatrix(); glTranslatef(1.95,0.2,-8); glRotatef(-90,0,1,0); paint1(materialImg, MATERIAL); glPopMatrix(); glPushMatrix(); glTranslatef(-1.95,0.2,-8); glRotatef(-90,0,1,0); paint1(treeImg, TREE); glPopMatrix(); //2nd glPushMatrix(); glTranslatef(-1.95,0.2,-5.); glRotatef(90,0,1,0); paint1(serpenskiImg, SERPENSKI); glPopMatrix(); glPushMatrix(); glTranslatef(1.95,0.2,-5.); glRotatef(90,0,1,0); paint1(hexImg, HEX); glPopMatrix(); //3rd glPushMatrix(); glTranslatef(-1.95,0.2,-2.); glRotatef(90,0,1,0); paint1(stipPolyImg, STIPPOLY); glPopMatrix(); glPushMatrix(); glTranslatef(1.95,0.2,-2.); glRotatef(90,0,1,0); paint1(aapolyImg, AAPOLY); glPopMatrix(); //back picture glPushMatrix(); glTranslatef(.98,0.2,.95); glRotatef(180,0,1,0); paint1(beardImg, BEARD); glPopMatrix(); glPushMatrix(); glTranslatef(-.98,0.2,.95); paint1(beardImg, BEARD); glPopMatrix(); glFlush(); reshape(600,600 ); glutSwapBuffers(); return; } void rotateCenterOfVision(float theta) { float xcord, zcord; //translate to origin xcord = lookVec[0]-lookVec[3]; zcord = lookVec[2]-lookVec[5]; //rotate theta in the xz plane xcord = ( xcord*cos(theta)) - (zcord*sin(theta)); zcord = (zcord*cos(theta)) +(xcord*sin(theta)); //translate back lookVec[0] = xcord + lookVec[3]; lookVec[2] = zcord + lookVec[5]; } void move(float amount) { float xzSlope,deltaX,deltaZ,distance,xAmount, zAmount, tempX; deltaX =lookVec[0]-lookVec[3]; deltaZ =lookVec[2]-lookVec[5]; xzSlope = deltaZ/deltaX; //the equation for line is y == xzSlope*(x-lookVec[0])+lookVec[2] //now find the distance between (x0,z0) and (x1,z1) distance = sqrt( (deltaX*deltaX)+(deltaZ*deltaZ)); //use the ratio deltaX/distance == X/amount xAmount=(deltaX*amount)/distance; zAmount=(deltaZ*amount)/distance; //Change center lookVec[2] += zAmount; lookVec[0] += xAmount; //Change eye lookVec[5] += zAmount; lookVec[3] += xAmount; } void keyboard(unsigned char key, int x, int y) { if (key ==27) /* Escape Key */ exit(0); //turn left if(key =='a') rotateCenterOfVision(-.02); //turn right if(key=='d') rotateCenterOfVision(.02); //backwards if(key =='s') move(.05); //forwards if(key == 'w') move(-.05); } void init() { srand(clock()); printf("\n*****Fall 2000 Gallery ver 1.0***** "); printf("\n by Chad Brewbaker"); printf("\n a->Left,d->Right"); printf("\n w->Forward, s->Backward"); printf("\nRightClick for menu options\n"); } void twist() { display(); } void myMenu(int menuID) { switch(menuID) { case MENU_QUIT: exit(0); } } void reshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, 1.0, .1, 20.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(lookVec[0], lookVec[1], lookVec[2], lookVec[3],lookVec[4], lookVec[5],lookVec[6],lookVec[7],lookVec[8]); } int main (int argc, char ** argv) { beardImg = read_sgi("data/beard.sgi",&iwidth,&iheight,&idepth); wallImg = read_sgi("data/background.sgi",&iwidth,&iheight,&idepth); celingImg = read_sgi("data/wood.sgi",&iwidth,&iheight,&idepth); treeImg = read_sgi("data/displayListHack.sgi",&iwidth,&iheight,&idepth); serpenskiImg = read_sgi("data/serpenski.sgi",&iwidth,&iheight,&idepth); floorImg = read_sgi("data/redCarpet.sgi",&iwidth,&iheight,&idepth); hexImg = read_sgi("data/hex.sgi",&iwidth,&iheight,&idepth); stipPolyImg = read_sgi("data/stipPoly.sgi",&iwidth,&iheight,&idepth); materialImg = read_sgi("data/material.sgi",&iwidth,&iheight,&idepth); aapolyImg = read_sgi("data/aapoly.sgi",&iwidth,&iheight,&idepth); recurseImg = read_sgi("data/recursive.sgi",&iwidth,&iheight,&idepth); glutInit(& argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowPosition(50, 50); glutInitWindowSize(600, 600); glutCreateWindow("Fall 2000 Gallery"); glutIdleFunc(twist); init(); glClearColor(0.0,0.0,0.0,0.0); glShadeModel(GL_FLAT); glEnable(GL_DEPTH_TEST); glDisable(GL_CULL_FACE); glutKeyboardFunc(keyboard); glutDisplayFunc(display); glutReshapeFunc(reshape); glutCreateMenu(myMenu); glutAddMenuEntry("FALL 2000 Gallery by Chad Brewbaker",MENU_NULL); glutAddMenuEntry("Quit",MENU_QUIT); glutAttachMenu(GLUT_RIGHT_BUTTON); glutMainLoop(); return 0; }