/* simple_cube3.c This is a very basic CAVE program, that shows how a * simple object can be drawn multiple times. */ /* parts of this code originally came from Dave Pape, EVL, UIC. */ #include #include void init_gl(void); void glDraw(void); void DrawFloor(void); void navigate(void); void DrawCube(void); main(int argc,char **argv) { CAVEConfigure(&argc,argv,NULL); CAVEFar = 1000; CAVEInit(); CAVEInitApplication(init_gl,0); CAVEDisplay(glDraw,0); while (!CAVEgetbutton(CAVE_ESCKEY)) { navigate(); sginap(1); } CAVEExit(); } #define SPEED 5.0f /* Max navigation speed in feet per second */ void navigate(void) { float jx=CAVE_JOYSTICK_X,jy=CAVE_JOYSTICK_Y,dt,t; static float prevtime = 0; t = CAVEGetTime(); dt = t - prevtime; prevtime = t; if (fabs(jy)>0.2) { float wandFront[3]; CAVEGetVector(CAVE_WAND_FRONT,wandFront); CAVENavTranslate(wandFront[0]*jy*SPEED*dt, 0, wandFront[2]*jy*SPEED*dt); } if (fabs(jx)>0.2) CAVENavRot(-jx*90.0f*dt,'y'); } void init_gl(void) { glEnable(GL_LIGHT0); } void glDraw(void) { glClearColor(0., 0., 0., 1.); glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); glEnable(GL_LIGHTING); /* Lets put one cube before we call CAVENavTransform, and see what happens. */ glPushMatrix(); glTranslatef(2, 1, -3); DrawCube(); glPopMatrix(); CAVENavTransform(); DrawFloor(); /* Using glPushMatrix and glPopMatrix, we can draw several cubes independant of each other. */ glPushMatrix(); glTranslatef(0, 4, -5); glRotatef(45.0, 0, 1, 0); DrawCube(); glPopMatrix(); glPushMatrix(); glTranslatef(5, 7, -12); glRotatef(45.0, 0, 1, 0); DrawCube(); glPopMatrix(); glPushMatrix(); glTranslatef(-4, 3, -8); glRotatef(45.0, 0, 1, 0); DrawCube(); glPopMatrix(); glPushMatrix(); glTranslatef(-4, 30, -18); glRotatef(45.0, 0, 1, 0); glRotatef(15.0, 0, 0, 1); DrawCube(); glPopMatrix(); glPushMatrix(); glTranslatef(25, 2, -8); DrawCube(); glPopMatrix(); glPushMatrix(); glTranslatef(-4, 3, 42); DrawCube(); glPopMatrix(); glPushMatrix(); glTranslatef(22, 7, -16); glRotatef(45.0, 1, 0, 0); glRotatef(170.0, 0, 0, 1); DrawCube(); glPopMatrix(); glPushMatrix(); glTranslatef(-71, 2, 78); glRotatef(70.0, 1, 0, 0); glScalef(1.1, 9.5, 7.5); DrawCube(); glPopMatrix(); glPushMatrix(); glTranslatef(54, 7, -16); glRotatef(45.0, 1, 0, 0); glRotatef(170.0, 0, 0, 1); glScalef(4.6, 1.3, 1.5); DrawCube(); glPopMatrix(); glPushMatrix(); glTranslatef(35, 4, 20); glRotatef(15.0, 1, 0, 0); glRotatef(56.0, 0, 0, 1); glScalef(4.5, 1.0, 3.2); DrawCube(); glPopMatrix(); glPushMatrix(); glTranslatef(-42, 2, 34); glRotatef(240.0, 0, 0, 1); glRotatef(75.0, 0, 1, 0); glScalef(0.2, 0.3, 3.5); DrawCube(); glPopMatrix(); glPushMatrix(); glTranslatef(28, 1, -70); glRotatef(60.0, 1, 0, 0); glScalef(2.2, 1.3, 0.5); DrawCube(); glPopMatrix(); glPushMatrix(); glTranslatef(-82, 2, -64); glRotatef(20.0, 0, 0, 1); glRotatef(125.0, 0, 1, 0); glScalef(12.2, 1.3, 30.5); DrawCube(); glPopMatrix(); glDisable(GL_LIGHTING); } void DrawCube(void) { static float redMaterial[] = { 1, 0, 0, 1 }; static float greenMaterial[] = { 0, 1, 0, 1 }; static float blueMaterial[] = { 0, 0, 1, 1 }; static float yellowMaterial[] = { 1, 1, 0, 1 }; static float purpleMaterial[] = { 1, 0, 1, 1 }; static float whiteMaterial[] = { 1, 1, 1, 1 }; glBegin(GL_QUADS); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, redMaterial); glNormal3f(0, 0, -1); glVertex3f(1, 1, -1); glVertex3f(-1, 1, -1); glVertex3f(-1, -1, -1); glVertex3f(1, -1, -1); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, greenMaterial); glNormal3f(-1, 0, 0); glVertex3f(-1, 1, 1); glVertex3f(-1, -1, 1); glVertex3f(-1, -1, -1); glVertex3f(-1, 1, -1); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blueMaterial); glNormal3f(0, -1, 0); glVertex3f(-1, -1, -1); glVertex3f(1, -1, -1); glVertex3f(1, -1, 1); glVertex3f(-1, -1, 1); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, yellowMaterial); glNormal3f(0, 0, 1); glVertex3f(1, 1, 1); glVertex3f(-1, 1, 1); glVertex3f(-1, -1, 1); glVertex3f(1, -1, 1); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, purpleMaterial); glNormal3f(0, 1, 0); glVertex3f(-1, 1, -1); glVertex3f(-1, 1, 1); glVertex3f(1, 1, 1); glVertex3f(1, 1, -1); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, whiteMaterial); glNormal3f(1, 0, 0); glVertex3f(1, 1, -1); glVertex3f(1, 1, 1); glVertex3f(1, -1, 1); glVertex3f(1, -1, -1); glEnd(); glEnable(GL_LIGHTING); } void DrawFloor(void) { int i, j; static float blueMaterial[] = { 0, 0, 1, 1 }; static float whiteMaterial[] = { 1, 1, 1, 1 }; glPushMatrix(); glBegin(GL_QUADS); glNormal3f(0, 1, 0); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, whiteMaterial); glVertex3f(-100, 0, -100); glVertex3f(100, 0, -100); glVertex3f(100, 0, 100); glVertex3f(-100, 0, 100); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blueMaterial); for (i = -100; i <100; i+=20) { for(j = -100; j <100; j+=20) { glVertex3f(i, 0.1, j); glVertex3f(i+10, 0.1, j); glVertex3f(i+10, 0.1, j+10); glVertex3f(i, 0.1, j+10); } } for (i = -90; i < 100; i+=20) { for(j = -90; j < 100; j+=20) { glVertex3f(i, 0.1, j); glVertex3f(i+10, 0.1, j); glVertex3f(i+10, 0.1, j+10); glVertex3f(i, 0.1, j+10); } } glEnd(); glPopMatrix(); }