#include #include #include #include #include "textures.h" void init(void); void frame(void); void display(void); void draw_cube(void); void init_terrain(void); void init_texture(void); image get_bmp_data(char * fileName); /* declare globals */ char file1[50] = {"height2_128.bmp"}; char file2[50] = {"texture_512.bmp"}; GLuint texture; image img; float pos[3]; bool START = true; float scaleX = 17.501; float scaleY = 85.0002; float scaleZ = 58.8006; float xpos = 0.0; float ypos = 0.0; float zpos = 0.0; int main(int argc,char **argv) { /* Initialize the CAVE */ CAVEConfigure(&argc,argv,NULL); /* Give the library a pointer to the GL initialization function */ CAVEInitApplication(init,0); /* Give the library a pointer to the drawing function */ CAVEDisplay(display,0); /* Create the multiple processes/threads and start the display loop */ CAVEInit(); /* Wait for the escape key to be hit */ while (!CAVEgetbutton(CAVE_ESCKEY)) { if (CAVEBUTTON1 && CAVEgetbutton(CAVE_AKEY)) { /*Zoom In*/ scaleX -= 0.1; cout << "x scale: " << scaleX << " y scale: " << scaleY << " z scale: " << scaleZ << endl; } if (CAVEBUTTON2 && CAVEgetbutton(CAVE_AKEY)) { /*Zoom Out*/ scaleY -= 0.1; cout << "x scale: " << scaleX << " y scale: " << scaleY << " z scale: " << scaleZ << endl; } if (CAVEBUTTON3 && CAVEgetbutton(CAVE_AKEY)) { scaleZ -= 0.1; cout << "x scale: " << scaleX << " y scale: " << scaleY << " z scale: " << scaleZ << endl; } if (CAVEBUTTON1 && CAVEgetbutton(CAVE_ZKEY)) { /*Zoom In*/ scaleX += 0.1; cout << "x scale: " << scaleX << " y scale: " << scaleY << " z scale: " << scaleZ << endl; } if (CAVEBUTTON2 && CAVEgetbutton(CAVE_ZKEY)) { /*Zoom Out*/ scaleY += 0.1; cout << "x scale: " << scaleX << " y scale: " << scaleY << " z scale: " << scaleZ << endl; } if (CAVEBUTTON3 && CAVEgetbutton(CAVE_ZKEY)) { scaleZ += 0.1; cout << "x scale: " << scaleX << " y scale: " << scaleY << " z scale: " << scaleZ << endl; } CAVEUSleep(10); } /* Clean up & exit */ CAVEExit(); return 0; } void init(void) { CAVEFar = 30000; glEnable (GL_DEPTH_TEST); glEnable (GL_SMOOTH); glShadeModel (GL_SMOOTH); glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glPolygonMode (GL_FRONT, GL_FILL); init_terrain(); init_texture(); } /* * init terrain */ void init_terrain(void) { img = get_bmp_data(file1); } /* * init texture */ void init_texture() { glGenTextures(1, &texture); LoadBMPTexture (file2, 1, texture); } /* * called once per frame before display - once and only once per frame! */ void frame(void) { } /* DISPLAY * call your command and function calls that handle drawing here */ void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if (START) { CAVEGetPosition(CAVE_HEAD,pos); CAVENavTranslate(-1.0*pos[0],-1.0*pos[1],-1.0*pos[2]); START = false; cout << pos[0] << ',' << pos[1] << ',' << pos[2] << endl; } float xSpace = 1.0/img.width; float zSpace = 1.0/img.height; glEnable (GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texture); for (int j = 0; j < img.height-1; j++) { for (int i = 0; i < img.width-1; i++) { glBegin(GL_QUADS); /* lower lefthand corner -> i,j+1 */ glColor3f ((img.data[i+(j+1)*img.height]/256.0),(img.data[i+(j+1)*img.height]/256.0),(img.data[i+(j+1)*img.height]/256.0)); glTexCoord2f(i*xSpace,(j+1)*zSpace); glVertex3f(i/scaleX,img.data[i+(j+1)*img.height]/scaleY,(j+1)/scaleZ); //glTexCoord2f(0.0,0.0); //glVertex3f(i/scaleX,0.0,(j+1)/scaleZ); /* upper lefthand corner -> i,j */ glColor3f((img.data[i+j*img.height]/256.0),(img.data[i+j*img.height]/256.0),(img.data[i+j*img.height]/256.0)); glTexCoord2f(i*xSpace,j*zSpace); glVertex3f(i/scaleX,img.data[i+j*img.height]/scaleY,j/scaleZ); //glTexCoord2f(0.0,1.0); //glVertex3f(i/scaleX,0.0,j/scaleZ); /* upper righthand corner -> i+1,j */ glColor3f((img.data[(i+1)+j*img.height]/256.0),(img.data[(i+1)+j*img.height]/256.0),(img.data[(i+1)+j*img.height]/256.0)); glTexCoord2f((i+1)*xSpace,j*zSpace); glVertex3f((i+1)/scaleX,img.data[(i+1)+j*img.height]/scaleY,j/scaleZ); //glTexCoord2f(1.0,1.0); //glVertex3f((i+1)/scaleX,0.0,j/scaleZ); /* lower righthand corner -> i+1,j+1 */ glColor3f((img.data[(i+1)+(j+1)*img.height]/256.0),(img.data[(i+1)+(j+1)*img.height]/256.0),(img.data[(i+1)+(j+1)*img.height]/256.0)); glTexCoord2f((i+1)*xSpace,(j+1)*zSpace); glVertex3f((i+1)/scaleX,img.data[(i+1)+(j+1)*img.height]/scaleY,(j+1)/scaleZ); //glTexCoord2f(0.0,1.0); //glVertex3f((i+1)/scaleX,0.0,(j+1)/scaleZ); glEnd(); } } } image get_bmp_data(char * fileName) { FILE * file; short plane, bpp; int temp; image img; if ((file = fopen (fileName, "r")) == NULL) { cout << "problem opening " << fileName << endl; CAVEExit(); } /* skip irrelavant info - 18 bytes worth */ fseek (file, 18, SEEK_SET); img.width = GetInt (file); img.height = GetInt (file); plane = GetShort (file); bpp = GetShort (file); /* 8 bit images for some reason require this 1048 byte jump */ fseek (file, 1048, SEEK_CUR); if ((img.data = (unsigned char *) malloc (img.height * img.width)) == NULL) { cout << "run out of memory" << endl; fclose (file); CAVEExit(); } fread (img.data, img.height * img.width, 1, file); cout << "BMP Info: " << endl; cout << "Height = " << img.height << endl; cout << "Width = " << img.width << endl; int check; for (int i = 0; i < img.width; i++) { for (int j = 0; j < img.height; j++) { check = (int)img.data[j + (i * img.height)]; if (check == 255){ cout <