/*************** ************** * * VR Juggler is (C) Copyright 1998, 1999, 2000, 2001, 2002 * by Iowa State University * * Original Authors: * Allen Bierbaum, Christopher Just, * Patrick Hartling, Kevin Meinert, * Carolina Cruz-Neira, Albert Baker * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. * * ----------------------------------------------------------------- * File: torusApp.cpp,v * Date modified: 2002/05/12 19:17:18 * Version: 1.7.6.1 * ----------------------------------------------------------------- * *************** ***************/ #include #include #include #include // Header File For The GLaux Library static void doughnut(GLfloat r, GLfloat R, GLint nsides, GLint rings); static void drawwall(float tx, float ty, float tz, float rx, float ry, float rz, int bx, int by); static void drawcube(float x, float y, float z); double TimeDiff() { static clock_t OldTime; clock_t NewTime; double returnval; NewTime = clock(); returnval = (double)(NewTime - OldTime) / CLOCKS_PER_SEC; OldTime = NewTime; return returnval; } //---------------------------------------------- // Draw the scene. A bunch of boxes of differing color and stuff //---------------------------------------------- void torusApp::draw() { vjCoord wand_pos(*mWand->getData()); vjCoord head_pos(*mHead->getData()); vjCoord keyb_pos(*mKeyb->getData()); // Move user in Audio env. g_Env.AbsPos(head_pos.pos[0], head_pos.pos[1]); /* cout << "H " << head_pos.pos.vec[0] << " " << head_pos.pos.vec[1] << " " << head_pos.pos.vec[2]; cout << " W " << wand_pos.pos.vec[0] << " " << wand_pos.pos.vec[1] << " " << wand_pos.pos.vec[2] << endl; cout << " K " << keyb_pos.pos.vec[0] << " " << keyb_pos.pos.vec[1] << " " << keyb_pos.pos.vec[2] << endl; */ // change AV state float td = (float) TimeDiff(); if (td != 0) { g_Env.SetFPS(1/td); } g_Env.DrawBuffer(td); g_Env.PlaceCamera(); glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Set light position GLfloat light0_position[] = {6.0, 6,0, 6.0, 1.0}; //glLightfv(GL_LIGHT0, GL_POSITION, light0_position); glLightfv(GL_LIGHT0, GL_POSITION, wand_pos.pos.vec); //// Draw a doughnut on the wand // Set material color float onyx_red = 255.0f/255.0f; float onyx_blue = 57.0f/255.0f; GLfloat mat_ambient[] = { onyx_red, 0.0, onyx_blue, 0.5}; GLfloat mat_diffuse[] = { onyx_red, 0.0, onyx_blue, 0.5}; GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0}; GLfloat mat_shininess[] = { 50.0}; glMaterialfv( GL_FRONT, GL_AMBIENT, mat_ambient ); glMaterialfv( GL_FRONT, GL_DIFFUSE, mat_diffuse ); glMaterialfv( GL_FRONT, GL_SPECULAR, mat_specular ); glMaterialfv( GL_FRONT, GL_SHININESS, mat_shininess ); glPushMatrix(); glTranslatef(-6.0f, 0.0f, -6.0f); glRotatef(30, 0,1,0); // Around Y glRotatef(-30, 1,0,0); // Around X //doughnut(1.5f,9.0f,300,300); //doughnut(1.5f,6.0f,120,120); doughnut(1.5f,3.0f,30,30); //doughnut(0.5f,1.0f,100,100); glPopMatrix(); // Draw a wall of cubes GLfloat mat_ambient2[] = { 1.0, onyx_red, onyx_blue, 0.5}; GLfloat mat_diffuse2[] = { 1.0, onyx_red, onyx_blue, 0.5}; GLfloat mat_specular2[] = { 1.0, 1.0, 1.0, 1.0}; GLfloat mat_shininess2[] = { 50.0}; glMaterialfv( GL_FRONT, GL_AMBIENT, mat_ambient2 ); glMaterialfv( GL_FRONT, GL_DIFFUSE, mat_diffuse2 ); glMaterialfv( GL_FRONT, GL_SPECULAR, mat_specular2 ); glMaterialfv( GL_FRONT, GL_SHININESS, mat_shininess2 ); // Draw the virtual world glDisable(GL_LIGHTING); drawroom(); } void torusApp::initGLState() { GLfloat light0_ambient[] = { .2, .2, .2, 1.0}; GLfloat light0_diffuse[] = { 1.0, 1.0, 1.0, 1.0}; GLfloat light0_specular[] = { 1.0, 1.0, 1.0, 1.0}; GLfloat light0_position[] = {6.0, 6,0, 6.0, 1.0}; glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular); glLightfv(GL_LIGHT0, GL_POSITION, light0_position); glEnable(GL_DEPTH_TEST); glEnable(GL_NORMALIZE); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glDisable(GL_COLOR_MATERIAL); glShadeModel(GL_SMOOTH); glMatrixMode(GL_MODELVIEW); // texture map additions /* if (!LoadGLTextures()) // Jump To Texture Loading Routine ( NEW ) { cout << "ERROR: texture map did not load." << endl; } glEnable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW ) glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations */ } // Draw a doughnut. // Borrowed from GLUT static void doughnut(GLfloat r, GLfloat R, GLint nsides, GLint rings) { int i, j; GLfloat theta, phi, theta1; GLfloat cosTheta, sinTheta; GLfloat cosTheta1, sinTheta1; GLfloat ringDelta, sideDelta; ringDelta = 2.0 * M_PI / rings; sideDelta = 2.0 * M_PI / nsides; theta = 0.0; cosTheta = 1.0; sinTheta = 0.0; for (i = rings - 1; i >= 0; i--) { theta1 = theta + ringDelta; cosTheta1 = cos(theta1); sinTheta1 = sin(theta1); glBegin(GL_QUAD_STRIP); phi = 0.0; for (j = nsides; j >= 0; j--) { GLfloat cosPhi, sinPhi, dist; phi += sideDelta; cosPhi = cos(phi); sinPhi = sin(phi); dist = R + r * cosPhi; glNormal3f(cosTheta1 * cosPhi, -sinTheta1 * cosPhi, sinPhi); glVertex3f(cosTheta1 * dist, -sinTheta1 * dist, r * sinPhi); glNormal3f(cosTheta * cosPhi, -sinTheta * cosPhi, sinPhi); glVertex3f(cosTheta * dist, -sinTheta * dist, r * sinPhi); } glEnd(); theta = theta1; cosTheta = cosTheta1; sinTheta = sinTheta1; } } void torusApp::drawroom() { //void drawwall(float tx, float ty, float tz, // float rx, float ry, float rz, // int length, int height); int rmx = 8; int rmy = 9; int ht = 5; drawwall(0.0f, 0.0f, 0.0f, 90.0f, 0.0f, 0.0f, rmx, ht); drawwall(0.0f, 0.0f, 0.0f, 90.0f, 90.0f, 0.0f, rmy, ht); // Make floor drawwall(1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, rmx, 2*rmy); // Make far wall drawwall(float(2*rmx), 0.0f, 0.0f, 90.0f, 90.0f, 0.0f, rmy, ht); // last wall drawwall(0.0f, float(2*rmy), 0.0f, 90.0f, 0.0f, 0.0f, rmx, ht); // Extra features in the room // Dividing wall glPushMatrix(); glTranslatef(float(rmx), 0.0f, 0.0f); //glRotatef(90, 1,0,0); // rotate around x //glRotatef(90, 0,1,0); // rotate around y glScalef(1.0f, -(float(rmy*1.2)), float(ht)); drawcube(0.0f, 0.0f, 0.0f); glPopMatrix(); // Dividing wall glPushMatrix(); glTranslatef(float(rmx), 0.0f, 0.0f); //glRotatef(90, 1,0,0); // rotate around x //glRotatef(90, 0,1,0); // rotate around y glScalef(1.0f, -(float(rmy*1.2)), float(ht)); drawcube(0.0f, 0.0f, 0.0f); glPopMatrix(); glPushMatrix(); //glBindTexture(GL_TEXTURE_2D, texture[0]); // Select Our Texture glTranslatef(float(rmx+3), float(rmy/2.0), float(ht/2.0)); glRotatef(45, 1,0,0); // rotate around x glRotatef(45, 0,1,0); // rotate around y //glScalef(1.0f, -(float(rmy*1.2)), float(ht)); drawcube(0.0f, 0.0f, 0.0f); glPopMatrix(); } void drawwall(float tx, float ty, float tz, float rx, float ry, float rz, int bx, int by){ int x,y; glPushMatrix(); glTranslatef(tx, ty, tz); glRotatef(rx, 1,0,0); // rotate around x glRotatef(ry, 0,1,0); // rotate around y glRotatef(rz, 0,0,1); // rotate around z for(y=0; y<=by; y=y++) { for(x=0; x<=bx; x=x++) { drawcube(float((x*2)+(y%2)), float(y),0.0f); } } glPopMatrix(); } // x,y,z -- lower left corner of cube void drawcube(float x, float y, float z) { glBegin(GL_QUADS); // Start Drawing The Cube glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green glVertex3f( x, y, z); glVertex3f( x+1.0, y, z); glVertex3f( x+1.0, y, z+1.0); glVertex3f( x, y, z+1.0f); glColor3f(1.0f,0.5f,0.0f); // Set The Color To Orange glVertex3f( x, y-1.0, z); glVertex3f( x+1.0, y-1.0, z); glVertex3f( x+1.0, y-1.0, z+1.0); glVertex3f( x, y-1.0, z+1.0f); glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red glVertex3f( x, y, z); glVertex3f( x, y-1.0, z); glVertex3f( x, y-1.0, z+1.0); glVertex3f( x, y, z+1.0f); glColor3f(1.0f,1.0f,0.0f); // Set The Color To Yellow glVertex3f( x+1.0f, y, z); glVertex3f( x+1.0f, y-1.0, z); glVertex3f( x+1.0f, y-1.0, z+1.0); glVertex3f( x+1.0f, y, z+1.0f); glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue glVertex3f( x, y, z); glVertex3f( x, y-1.0, z); glVertex3f( x+1.0f, y-1.0, z); glVertex3f( x+1.0f, y, z); glColor3f(1.0f,0.0f,1.0f); // Set The Color To Violet glVertex3f( x, y, z+1.0f); glVertex3f( x, y-1.0, z+1.0f); glVertex3f( x+1.0f, y-1.0, z+1.0f); glVertex3f( x+1.0f, y, z+1.0f); glEnd(); } // texture mapping /* AUX_RGBImageRec *LoadBMP(char *Filename) // Loads A Bitmap Image { FILE *File=NULL; if (!Filename) // Make Sure A Filename Was Given { return NULL; // If Not Return NULL } File=fopen(Filename,"r"); // Check To See If The File Exists if (File) // Does The File Exist? { fclose(File); // Close The Handle return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer } return NULL; // If Load Failed Return NULL } int torusApp::LoadGLTextures() // Load Bitmaps And Convert To Textures { int Status=FALSE; // Status Indicator AUX_RGBImageRec *TextureImage[1]; // Create Storage Space For The Texture memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL // Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit if (TextureImage[0]=LoadBMP("warning.bmp")) { Status=TRUE; // Set The Status To TRUE glGenTextures(1, &texture[0]); // Create The Texture // Typical Texture Generation Using Data From The Bitmap glBindTexture(GL_TEXTURE_2D, texture[0]); // Generate The Texture glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Linear Filtering glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Linear Filtering } if (TextureImage[0]) // If Texture Exists { if (TextureImage[0]->data) // If Texture Image Exists { free(TextureImage[0]->data); // Free The Texture Image Memory } free(TextureImage[0]); // Free The Image Structure } return Status; // Return The Status } */ /* int InitGL(GLvoid) // All Setup For OpenGL Goes Here { if (!LoadGLTextures()) // Jump To Texture Loading Routine ( NEW ) { return FALSE; // If Texture Didn't Load Return FALSE ( NEW ) } glEnable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW ) glShadeModel(GL_SMOOTH); // Enable Smooth Shading glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background glClearDepth(1.0f); // Depth Buffer Setup glEnable(GL_DEPTH_TEST); // Enables Depth Testing glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations return TRUE; // Initialization Went OK } int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer glLoadIdentity(); // Reset The Current Matrix glTranslatef(0.0f,0.0f,-5.0f); // Move Into The Screen 5 Units glBindTexture(GL_TEXTURE_2D, texture[0]); // Select Our Texture */