00001 /*! @file BeoSub/test-BeoSubSimulator.C [put description here] */ 00002 00003 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/BeoSub/test-BeoSubSimulator.C $ 00004 // $Id: test-BeoSubSimulator.C 11565 2009-08-09 02:14:40Z rand $ 00005 00006 // compile with: -lm -lglut -lGLU -lGL -lX11 -lXext 00007 00008 00009 #include <cmath> 00010 #ifdef HAVE_GL_GLUT_H 00011 #include <GL/glut.h> 00012 #endif 00013 #include <GL/gl.h> 00014 #include <GL/glu.h> 00015 #include <cstdio> 00016 #include <cstring> 00017 #include <cstdlib> 00018 #include <iostream> 00019 #include <pthread.h> 00020 #include <unistd.h> 00021 00022 #include "Image/Pixels.H" 00023 #include "Image/ColorOps.H" 00024 #include "Image/Image.H" 00025 #include "Image/MathOps.H" 00026 #include "Image/Transforms.H" 00027 #include "Image/FilterOps.H" 00028 #include "BeoSub/BeoSubCanny.H" 00029 #include "BeoSub/ColorTracker.H" 00030 #include "CannyModel.H" 00031 #include "BeoSub/BeoSubSim.H" 00032 00033 #ifdef HAVE_GL_GLUT_H 00034 00035 bool start = true; 00036 00037 ModelManager submgr("BeoSubSim Tester"); 00038 nub::soft_ref<BeoSubSim> SimSub(new BeoSubSim(submgr)); 00039 00040 Angle heading(0.0), deltaHeading(0.0); 00041 float ratio, dtemp; 00042 Angle pitch(0.0), deltaPitch(0.0); 00043 Angle roll(0.0); 00044 float deltaStrafeX = 0; 00045 float leftThrustValue = 0, rightThrustValue = 0; 00046 bool setForward = false; 00047 00048 float x=-30.0f,y=20.0f,z=10.0f; // init postion of the camera 00049 float targetZ = z, targetX = x; 00050 float subX= 2.0f, subY=3.5f, subZ = 2.0f; 00051 float lx=0.0f,ly=0.0f,lz=-0.1f, ty=0.0f; 00052 float l[] = {0.0,80.0,0.0}; float n[]={0.0,-1.0,0.0}; float e[] = {0.0,-60.0,0.0}; 00053 float shadowColor[] = {0.0,0.0,0.0}; 00054 GLfloat fogColor[4] = {0.35,0.5,0.5,1.0}; 00055 int sw=320,sh=240; 00056 int deltaMove = 0,h=740,w=325, border=5; 00057 int deltaDepth = 0; 00058 int counter=0, actionCount=0; 00059 void* font=GLUT_BITMAP_8_BY_13; 00060 int bitmapHeight=13; 00061 int imgCounter = 0; 00062 GLfloat TCx = 45.0f,TCz = -20.0f; 00063 GLfloat TAx = -15.0f, TAy=4.0f, TAz=0.5f; 00064 int frame,frametime,outframe, outtime,timebase=0,atime, atimebase=0, decode=0; 00065 char s[30]; 00066 int mainWindow, subWindow1,subWindow2,subWindow3, subWindow4, subWindow5, subWindow6; 00067 bool set3 = false; 00068 bool fogAct = true, isSphere = true; 00069 void initWindow(); 00070 00071 byte *image1 = new byte[sw*sh*3]; 00072 byte *image2 = new byte[sw*sh*3]; 00073 byte *image3 = new byte[sw*sh*3]; 00074 00075 // instantiate a model manager for the shape detector module: 00076 LINFO("CREATING MANAGER"); 00077 ModelManager manager("BeoSubCanny Tester"); 00078 nub::soft_ref<ColorTracker> ct(new ColorTracker(manager)); 00079 //Test with a circle 00080 rutz::shared_ptr<ShapeModel> shape; 00081 double *darray = (double*)calloc(4, sizeof(double)); 00082 00083 Image< PixRGB<byte> > simulateCamera(const byte *data, const int w, const int h); 00084 Image< PixRGB<byte> > ima1, ima2, ima3; 00085 00086 // texture mapping 00087 struct simTexture 00088 { 00089 unsigned long sizeX; 00090 unsigned long sizeY; 00091 char *data; 00092 }; 00093 typedef struct simTexture Texture; 00094 int t[50]; 00095 // make shadow in the pool 00096 void glShadowProjection(float *l, float *n, float *e) 00097 { 00098 float d, c, mat[16]; 00099 d = n[0]*l[0] + n[1]*l[1] + n[2]*l[2]; 00100 c = e[0]*n[0] + e[1]*n[1] + e[2]*n[2]-d; 00101 00102 mat[0] = n[0]*l[0] + c; 00103 mat[4] = n[1]*l[0]; 00104 mat[8] = n[2]*l[0]; 00105 mat[12]= -l[0]*c - l[0]*d; 00106 00107 mat[1] = n[0]*l[1]; 00108 mat[5] = n[1]*l[1]+c; 00109 mat[9] = n[2]*l[1]; 00110 mat[13]= -l[1]*c - l[1]*d; 00111 00112 mat[2] = n[0]*l[2]; 00113 mat[6] = n[1]*l[2]; 00114 mat[10]= n[2]*l[2]+c; 00115 mat[14]= -l[2]*c - l[2]*d; 00116 00117 mat[3] = n[0]; 00118 mat[7] = n[1]; 00119 mat[11]= n[2]; 00120 mat[15]= -d; 00121 glMultMatrixf(mat); 00122 00123 } 00124 00125 // quick and dirty bitmap loader...for 24 bit bitmaps with 1 plane only. 00126 // See http://www.dcs.ed.ac.uk/~mxr/gfx/2d/BMP.txt for more info. 00127 int ImageLoad(char *filename, Texture *image) { 00128 FILE *file; 00129 unsigned long size; // size of the image in bytes. 00130 unsigned long i; // standard counter. 00131 unsigned short int planes; // number of planes in image (must be 1) 00132 unsigned short int bpp; // number of bits per pixel (must be 24) 00133 char temp; // temporary color storage for bgr-rgb conversion. 00134 00135 // make sure the file is there. 00136 if ((file = fopen(filename, "rb"))==NULL) 00137 { 00138 printf("File Not Found : %s\n",filename); 00139 return 0; 00140 } 00141 00142 // seek through the bmp header, up to the width/height: 00143 fseek(file, 18, SEEK_CUR); 00144 00145 // read the width 00146 if ((i = fread(&image->sizeX, 4, 1, file)) != 1) { 00147 printf("Error reading width from %s.\n", filename); 00148 return 0; 00149 } 00150 00151 // read the height 00152 if ((i = fread(&image->sizeY, 4, 1, file)) != 1) { 00153 printf("Error reading height from %s.\n", filename); 00154 return 0; 00155 } 00156 // printf("Height of %s: %lu\n", filename, image->sizeY); 00157 00158 // calculate the size (assuming 24 bits or 3 bytes per pixel). 00159 size = image->sizeX * image->sizeY * 3; 00160 00161 // read the planes 00162 if ((fread(&planes, 2, 1, file)) != 1) { 00163 printf("Error reading planes from %s.\n", filename); 00164 return 0; 00165 } 00166 if (planes != 1) { 00167 printf("Planes from %s is not 1: %u\n", filename, planes); 00168 return 0; 00169 } 00170 00171 // read the bpp 00172 if ((i = fread(&bpp, 2, 1, file)) != 1) { 00173 printf("Error reading bpp from %s.\n", filename); 00174 return 0; 00175 } 00176 00177 if (bpp != 24) { 00178 printf("Bpp from %s is not 24: %u\n", filename, bpp); 00179 return 0; 00180 } 00181 00182 // seek past the rest of the bitmap header. 00183 fseek(file, 24, SEEK_CUR); 00184 00185 // read the data. 00186 image->data = (char *) malloc(size); 00187 if (image->data == NULL) { 00188 printf("Error allocating memory for color-corrected image data"); 00189 return 0; 00190 } 00191 00192 if ((i = fread(image->data, size, 1, file)) != 1) { 00193 printf("Error reading image data from %s.\n", filename); 00194 return 0; 00195 } 00196 00197 for (i=0;i<size;i+=3) { // reverse all of the colors. (bgr -> rgb) 00198 temp = image->data[i]; 00199 image->data[i] = image->data[i+2]; 00200 image->data[i+2] = temp; 00201 } 00202 00203 // we're done. 00204 return 1; 00205 } 00206 00207 // Load Bitmaps And Convert To Textures 00208 void LoadGLTextures(char *filename, int i) { 00209 // Load Texture 00210 Texture *texture; 00211 00212 // allocate space for texture 00213 texture = (Texture *) malloc(sizeof(Texture)); 00214 if (texture == NULL) { 00215 printf("Error allocating space for image"); 00216 exit(0); 00217 } 00218 00219 if (!ImageLoad(filename, texture)) { 00220 exit(1); 00221 } 00222 00223 // Create Texture 00224 glGenTextures(1, (GLuint *)&t[i]); 00225 glBindTexture(GL_TEXTURE_2D, t[i]); // 2d texture (x and y size) 00226 00227 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // scale linearly when image bigger than texture 00228 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // scale linearly when image smalled than textur 00229 00230 // 2d texture, level of detail 0 (normal), 3 components (red, green, blue), x size from image, y size from image, 00231 // border 0 (normal), rgb color data, unsigned byte data, and finally the data itself. 00232 glTexImage2D(GL_TEXTURE_2D, 0, 3, texture->sizeX, texture->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture->data); 00233 } 00234 00235 void initWindow2(void) 00236 { 00237 glClearColor(0.0,0.0,0.0,0.0); 00238 glShadeModel(GL_FLAT); 00239 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 00240 } 00241 /* 00242 void display4(void) 00243 { 00244 glutSetWindow(subWindow4); 00245 glClear(GL_COLOR_BUFFER_BIT); 00246 // glDrawPixels(sw,sh,GL_RGB,GL_UNSIGNED_BYTE, ima1.getArrayPtr()); 00247 // glDrawPixels(sw,sh,GL_RGB,GL_UNSIGNED_BYTE, image1); 00248 glutSwapBuffers(); 00249 } 00250 void display5(void) 00251 { 00252 glutSetWindow(subWindow5); 00253 glClear(GL_COLOR_BUFFER_BIT); 00254 // glDrawPixels(sw,sh,GL_RGB,GL_UNSIGNED_BYTE, ima2.getArrayPtr()); 00255 // glDrawPixels(sw,sh,GL_RGB,GL_UNSIGNED_BYTE, image2); 00256 glutSwapBuffers(); 00257 } 00258 void display6(void) 00259 { 00260 glutSetWindow(subWindow6); 00261 glClear(GL_COLOR_BUFFER_BIT); 00262 // glDrawPixels(sw,sh,GL_RGB,GL_UNSIGNED_BYTE, ima3.getArrayPtr()); 00263 // glDrawPixels(sw,sh,GL_RGB,GL_UNSIGNED_BYTE, image3); 00264 glutSwapBuffers(); 00265 } 00266 */ 00267 void changeSize2(int w1, int h1) 00268 { 00269 00270 // Prevent a divide by zero, when window is too short 00271 // (you cant make a window of zero width). 00272 ratio = 1.0f * w1 / h1; 00273 00274 // Set the viewport to be the entire window 00275 glViewport(0, 0, w1, h1); 00276 00277 // Reset the coordinate system before modifying 00278 glMatrixMode(GL_PROJECTION); 00279 glLoadIdentity(); 00280 00281 // Set the clipping volume 00282 gluPerspective(45,ratio,0.1,1000); 00283 glMatrixMode(GL_MODELVIEW); 00284 } 00285 00286 void changeSize(int w1,int h1) { 00287 if(h1 == 0) 00288 h1 = 1; 00289 00290 w = w1; 00291 h = h1; 00292 //mainx =(w-3*border)/2; 00293 //mainy=2*(h-3*border)/3; 00294 00295 glutSetWindow(subWindow1); 00296 glutPositionWindow(border,border); 00297 glutReshapeWindow(w-border, (h-4*border)/3); 00298 changeSize2(w-border, (h-4*border)/3); 00299 00300 glutSetWindow(subWindow2); 00301 glutPositionWindow(border,2*border+(h-4*border)/3); 00302 glutReshapeWindow(w-border, (h-4*border)/3); 00303 changeSize2(w-border, (h-4*border)/3); 00304 00305 glutSetWindow(subWindow3); 00306 glutPositionWindow(border,3*border+2*(h-4*border)/3); 00307 glutReshapeWindow(w-border, (h-4*border)/3); 00308 changeSize2(w-border, (h-4*border)/3); 00309 00310 // glutSetWindow(subWindow4); 00311 //glutPositionWindow(border+(w-2*border-mainx),border); 00312 //glutReshapeWindow(w-3*border-mainx, (h-4*border)/3); 00313 //changeSize2(w-3*border-mainx, (h-4*border)/3); 00314 00315 //glutSetWindow(subWindow5); 00316 //glutPositionWindow(border+(w-2*border-mainx),2*border+(h-4*border)/3); 00317 //glutReshapeWindow(w-3*border-mainx, (h-4*border)/3); 00318 //changeSize2(w-3*border-mainx, (h-4*border)/3); 00319 00320 //glutSetWindow(subWindow6); 00321 //glutPositionWindow(border+(w-2*border-mainx),3*border+2*(h-4*border)/3); 00322 //glutReshapeWindow(w-3*border-mainx, (h-4*border)/3); 00323 //changeSize2(w-3*border-mainx, (h-4*border)/3); 00324 } 00325 00326 00327 void drawSnowMan() {} 00328 00329 // rotate alone y axis 00330 void drawRecRotate(GLfloat de, GLfloat x, GLfloat y, GLfloat z, GLfloat w, GLfloat d, GLfloat h) 00331 { 00332 // rotation matrix 00333 // cos(de) 0 -sin(de) 00334 // 0 1 0 00335 // sin(de) 0 cos(de) 00336 // newx = cos(de) * x - sin(de) * z 00337 // newz = sin(de) * x + cos(de) * z 00338 // down 00339 GLfloat tempx=x,tempz=z; 00340 x=z=0; 00341 glBegin(GL_TRIANGLES); 00342 glVertex3f( cos(de)*x-sin(de)*z+tempx, y, sin(de)*x+cos(de)*z+tempz); 00343 glVertex3f( cos(de)*(x+w)-sin(de)*z+tempx, y, sin(de)*(x+w)+cos(de)*z+tempz); 00344 glVertex3f( cos(de)*x-sin(de)*(z+d)+tempx, y, sin(de)*x+cos(de)*(z+d)+tempz); 00345 glEnd(); 00346 glBegin(GL_TRIANGLES); 00347 glVertex3f( cos(de)*x-sin(de)*(z+d)+tempx, y, sin(de)*x+cos(de)*(z+d)+tempz); 00348 glVertex3f( cos(de)*(x+w)-sin(de)*z+tempx, y, sin(de)*(x+w)+cos(de)*z+tempz); 00349 glVertex3f( cos(de)*(x+w)-sin(de)*(z+d)+tempx, y, sin(de)*(x+w)+cos(de)*(z+d)+tempz); 00350 glEnd(); 00351 // up 00352 glBegin(GL_TRIANGLES); 00353 glVertex3f( cos(de)*x-sin(de)*z+tempx, y+h, sin(de)*x+cos(de)*z+tempz); 00354 glVertex3f( cos(de)*x-sin(de)*(z+d)+tempx, y+h, sin(de)*x+cos(de)*(z+d)+tempz); 00355 glVertex3f( cos(de)*(x+w)-sin(de)*z+tempx, y+h, sin(de)*(x+w)+cos(de)*z+tempz); 00356 glEnd(); 00357 glBegin(GL_TRIANGLES); 00358 glVertex3f( cos(de)*x-sin(de)*(z+d)+tempx, y+h, sin(de)*x+cos(de)*(z+d)+tempz); 00359 glVertex3f( cos(de)*(x+w)-sin(de)*(z+d)+tempx, y+h, sin(de)*(x+w)+cos(de)*(z+d)+tempz); 00360 glVertex3f( cos(de)*(x+w)-sin(de)*z+tempx, y+h, sin(de)*(x+w)+cos(de)*z+tempz); 00361 glEnd(); 00362 // left 00363 glBegin(GL_TRIANGLES); 00364 glVertex3f( cos(de)*x-sin(de)*(z+d)+tempx, y, sin(de)*x+cos(de)*(z+d)+tempz); 00365 glVertex3f( cos(de)*x-sin(de)*z+tempx, y+h, sin(de)*x+cos(de)*z+tempz); 00366 glVertex3f( cos(de)*x-sin(de)*z+tempx, y, sin(de)*x+cos(de)*z+tempz); 00367 glEnd(); 00368 glBegin(GL_TRIANGLES); 00369 glVertex3f( cos(de)*x-sin(de)*z+tempx, y+h, sin(de)*x+cos(de)*z+tempz); 00370 glVertex3f( cos(de)*x-sin(de)*(z+d)+tempx, y, sin(de)*x+cos(de)*(z+d)+tempz); 00371 glVertex3f( cos(de)*x-sin(de)*(z+d)+tempx, y+h, sin(de)*x+cos(de)*(z+d)+tempz); 00372 glEnd(); 00373 // right 00374 glBegin(GL_TRIANGLES); 00375 glVertex3f( cos(de)*(x+w)-sin(de)*(z+d)+tempx, y, sin(de)*(x+w)+cos(de)*(z+d)+tempz); 00376 glVertex3f( cos(de)*(x+w)-sin(de)*z+tempx, y, sin(de)*(x+w)+cos(de)*z+tempz); 00377 glVertex3f( cos(de)*(x+w)-sin(de)*(z+d)+tempx, y+h, sin(de)*(x+w)+cos(de)*(z+d)+tempz); 00378 glEnd(); 00379 glBegin(GL_TRIANGLES); 00380 glVertex3f( cos(de)*(x+w)-sin(de)*z+tempx, y+h, sin(de)*(x+w)+cos(de)*z+tempz); 00381 glVertex3f( cos(de)*(x+w)-sin(de)*(z+d)+tempx, y+h, sin(de)*(x+w)+cos(de)*(z+d)+tempz); 00382 glVertex3f( cos(de)*(x+w)-sin(de)*z+tempx, y, sin(de)*(x+w)+cos(de)*z+tempz); 00383 glEnd(); 00384 // front 00385 glBegin(GL_TRIANGLES); 00386 glVertex3f( cos(de)*(x+w)-sin(de)*(z+d)+tempx, y, sin(de)*(x+w)+cos(de)*(z+d)+tempz); 00387 glVertex3f( cos(de)*x-sin(de)*(z+d)+tempx, y+h, sin(de)*x+cos(de)*(z+d)+tempz); 00388 glVertex3f( cos(de)*x-sin(de)*(z+d)+tempx, y, sin(de)*x+cos(de)*(z+d)+tempz); 00389 glEnd(); 00390 glBegin(GL_TRIANGLES); 00391 glVertex3f( cos(de)*(x+w)-sin(de)*(z+d)+tempx, y, sin(de)*(x+w)+cos(de)*(z+d)+tempz); 00392 glVertex3f( cos(de)*(x+w)-sin(de)*(z+d)+tempx, y+h, sin(de)*(x+w)+cos(de)*(z+d)+tempz); 00393 glVertex3f( cos(de)*x-sin(de)*(z+d)+tempx, y+h, sin(de)*x+cos(de)*(z+d)+tempz); 00394 glEnd(); 00395 // back 00396 glBegin(GL_TRIANGLES); 00397 glVertex3f( cos(de)*x-sin(de)*z+tempx, y, sin(de)*x+cos(de)*z+tempz); 00398 glVertex3f( cos(de)*(x+w)-sin(de)*z+tempx, y+h, sin(de)*(x+w)+cos(de)*z+tempz); 00399 glVertex3f( cos(de)*(x+w)-sin(de)*z+tempx, y, sin(de)*(x+w)+cos(de)*z+tempz); 00400 glEnd(); 00401 glBegin(GL_TRIANGLES); 00402 glVertex3f( cos(de)*x-sin(de)*z+tempx, y, sin(de)*x+cos(de)*z+tempz); 00403 glVertex3f( cos(de)*x-sin(de)*z+tempx, y+h, sin(de)*x+cos(de)*z+tempz); 00404 glVertex3f( cos(de)*(x+w)-sin(de)*z+tempx, y+h, sin(de)*(x+w)+cos(de)*z+tempz); 00405 glEnd(); 00406 00407 } 00408 void setOrthographicProjection() { 00409 00410 // switch to projection mode 00411 glMatrixMode(GL_PROJECTION); 00412 // save previous matrix which contains the 00413 //settings for the perspective projection 00414 glPushMatrix(); 00415 // reset matrix 00416 glLoadIdentity(); 00417 // set a 2D orthographic projection 00418 gluOrtho2D(0, w, 0, h/2); 00419 // invert the y axis, down is positive 00420 glScalef(1, -1, 1); 00421 // mover the origin from the bottom left corner 00422 // to the upper left corner 00423 glTranslatef(0, -h/2, 0); 00424 glMatrixMode(GL_MODELVIEW); 00425 } 00426 00427 void resetPerspectiveProjection() { 00428 // set the current matrix to GL_PROJECTION 00429 glMatrixMode(GL_PROJECTION); 00430 // restore previous settings 00431 glPopMatrix(); 00432 // get back to GL_MODELVIEW matrix 00433 glMatrixMode(GL_MODELVIEW); 00434 } 00435 00436 void renderBitmapString(float x, float y, void *font,char *string) 00437 { 00438 char *c; 00439 // set position to start drawing fonts 00440 glRasterPos2f(x, y); 00441 // loop all the characters in the string 00442 for (c=string; *c != '\0'; c++) { 00443 glutBitmapCharacter(font, *c); 00444 } 00445 } 00446 void initScene() { 00447 00448 // init the texturemapping 00449 //LoadGLTextures("map.bmp", 0); 00450 /* 00451 LoadGLTextures("pics/bg00.bmp", 0); // Load The Texture(s) 00452 LoadGLTextures("pics/bg01.bmp", 1); 00453 LoadGLTextures("pics/bg02.bmp", 2); 00454 LoadGLTextures("pics/bg03.bmp", 3); 00455 LoadGLTextures("pics/bg04.bmp", 4); 00456 LoadGLTextures("pics/bg05.bmp", 5); 00457 LoadGLTextures("pics/bg06.bmp", 6); 00458 LoadGLTextures("pics/bg07.bmp", 7); 00459 LoadGLTextures("pics/bg08.bmp", 8); 00460 LoadGLTextures("pics/bg09.bmp", 9); 00461 LoadGLTextures("pics/bg10.bmp", 10); 00462 LoadGLTextures("pics/bg11.bmp", 11); 00463 LoadGLTextures("pics/bg12.bmp", 12); 00464 LoadGLTextures("pics/bg13.bmp", 13); 00465 LoadGLTextures("pics/0.bmp", 14); 00466 LoadGLTextures("pics/bg15.bmp", 15); 00467 LoadGLTextures("pics/bg16.bmp", 16); 00468 LoadGLTextures("pics/bg17.bmp", 17); 00469 LoadGLTextures("pics/bg18.bmp", 18); 00470 LoadGLTextures("pics/bg19.bmp", 19); 00471 LoadGLTextures("pics/1.bmp", 20); 00472 LoadGLTextures("pics/b.bmp", 21); 00473 LoadGLTextures("pics/c.bmp", 22); 00474 LoadGLTextures("pics/d.bmp", 23); 00475 LoadGLTextures("pics/e.bmp", 24); 00476 LoadGLTextures("pics/f.bmp", 25); 00477 LoadGLTextures("pics/g.bmp", 26); 00478 LoadGLTextures("pics/h.bmp", 27); 00479 LoadGLTextures("pics/i.bmp", 28); 00480 LoadGLTextures("pics/j.bmp", 29); 00481 LoadGLTextures("pics/k.bmp", 30); 00482 LoadGLTextures("pics/l.bmp", 31); 00483 LoadGLTextures("pics/m.bmp", 32); 00484 LoadGLTextures("pics/n.bmp", 33); 00485 LoadGLTextures("pics/o.bmp", 34); 00486 LoadGLTextures("pics/p.bmp", 35); 00487 LoadGLTextures("pics/q.bmp", 36); 00488 LoadGLTextures("pics/r.bmp", 37); 00489 LoadGLTextures("pics/s.bmp", 38); 00490 LoadGLTextures("pics/2.bmp", 39); 00491 00492 LoadGLTextures("pics/3.bmp", 40); // not used 00493 LoadGLTextures("pics/4.bmp", 41); 00494 LoadGLTextures("pics/5.bmp", 42); 00495 LoadGLTextures("pics/6.bmp", 43); 00496 LoadGLTextures("pics/7.bmp", 44); 00497 LoadGLTextures("pics/8.bmp", 45); 00498 LoadGLTextures("pics/9.bmp", 46); 00499 */ 00500 glClearColor(0.0f, 0.0f, 1.0f, 0.0f); // Clear The Background Color To Blue 00501 glClearDepth(1.0); // Enables Clearing Of The Depth Buffer 00502 glDepthFunc(GL_LESS); // The Type Of Depth Test To Do 00503 glEnable(GL_DEPTH_TEST); // Enables Depth Testing 00504 glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading 00505 // let the texture behind the shadow 00506 glEnable(GL_CULL_FACE); 00507 00508 // add fog effect 00509 glEnable(GL_FOG); 00510 { 00511 glFogi(GL_FOG_MODE, GL_EXP); 00512 glFogfv(GL_FOG_COLOR, fogColor); 00513 glFogf(GL_FOG_DENSITY, 0.10); 00514 glHint(GL_FOG_HINT, GL_DONT_CARE); 00515 glFogf(GL_FOG_START,1.0); 00516 glFogf(GL_FOG_END, 5.0); 00517 } 00518 glClearColor(0.35,0.5,0.5,1.0); 00519 00520 glMatrixMode(GL_PROJECTION); 00521 glLoadIdentity(); // Reset The Projection Matrix 00522 // Set the clipping volume 00523 gluPerspective(45,ratio,0.1,1000); 00524 glMatrixMode(GL_MODELVIEW); 00525 } 00526 00527 void orientMe(Angle theta) 00528 { 00529 lx = sin(theta.getRadians()); 00530 lz = -cos(theta.getRadians()); 00531 } 00532 00533 void moveMeFlat(float i) 00534 { 00535 x = x + i*(lx)*0.24; 00536 z = z + i*(lz)*0.24; 00537 } 00538 00539 void moveMeFlatSmall(float i) 00540 { 00541 x = x + i*(lx)*0.12; 00542 z = z + i*(lz)*0.12; 00543 } 00544 00545 void moveMeVer(float i) 00546 { 00547 y = y + i*0.02; 00548 } 00549 00550 void tiltMe(Angle theta) 00551 { 00552 lz = -cos(theta.getRadians()); 00553 ly = sin(theta.getRadians()); 00554 00555 } 00556 00557 void strafe(float val) 00558 { 00559 x = x + val*lz*0.12; 00560 z = z + val*lx*0.12; 00561 } 00562 00563 void storeImage(const char* s, Image< PixRGB<byte> > im) 00564 { 00565 std::cout << s << "image" << imgCounter << '\n'; 00566 imgCounter++; 00567 Raster::WriteRGB( im, sformat("%simage%d.ppm", s, imgCounter) ); 00568 } 00569 00570 void draw(bool shadow) 00571 { 00572 // draw submarine 00573 /* 00574 glPushMatrix(); 00575 if(shadow) 00576 glColor3f(shadowColor[0],shadowColor[1],shadowColor[2]); 00577 else 00578 glColor3f(1.0,0.0,0.0); 00579 glTranslatef(x,y,z); 00580 glRotatef(180-heading*180.0/3.14,0.0,1.0,0.0); 00581 drawRecRotate(0.0f, 00582 -subX/2.0f, 0.0f,subY, 00583 subX, subY, subZ); 00584 glPopMatrix(); 00585 */ 00586 // draw 5 orange pipes 00587 if(shadow) 00588 glColor3f(shadowColor[0],shadowColor[1],shadowColor[2]); 00589 else 00590 glColor3f(1.0f, 0.5f, 0.0f); 00591 drawRecRotate(0.0f,0.0f,0.85f,0.0f,4.0f,0.5f,0.3f); 00592 drawRecRotate(0.314f,4.3f,0.85f,0.0f,4.0f,0.5f,0.3f); 00593 drawRecRotate(-0.314f,8.2f,0.85f,1.3f,4.0f,0.5f,0.3f); 00594 drawRecRotate(0.5f,-4.7f,0.85f,-1.7f,4.0f,0.5f,0.3f); 00595 drawRecRotate(-0.2f,-9.2f,0.85f,-1.1f,4.0f,0.5f,0.3f); 00596 // draw the black bin 00597 if(shadow) 00598 glColor3f(shadowColor[0],shadowColor[1],shadowColor[2]); 00599 else 00600 glColor3f(0.9f,0.9f,0.9f); 00601 glBegin(GL_POLYGON); 00602 glVertex3f(-1.85f, 1.2f,-0.5f); 00603 00604 glVertex3f(-1.78f, 1.2f,-0.25f); 00605 glVertex3f(-1.965f,1.2f, 0.0f); 00606 glVertex3f(-1.75f,1.2f, 0.15f); 00607 glVertex3f(-1.755f,1.2f, 0.35f); 00608 glVertex3f(-1.878f,1.2f, 0.65f); 00609 glVertex3f(-1.679f,1.2f, 0.95f); 00610 00611 glVertex3f(-1.65f, 1.2f, 1.0f); 00612 00613 glVertex3f(-1.35f,1.2f, 1.42f); 00614 glVertex3f(-1.12f,1.2f, 1.732f); 00615 glVertex3f(-1.03f,1.2f, 1.626f); 00616 00617 glVertex3f( 0.0f, 1.2f, 1.34f); 00618 00619 glVertex3f( 0.412f,1.2f, 0.35f); 00620 glVertex3f( 0.532f,1.2f, 0.24f); 00621 glVertex3f( 0.325f,1.2f, 0.0f); 00622 glVertex3f( 0.65f,1.2f,-0.15f); 00623 glVertex3f( 0.31f, 1.2f,-0.35f); 00624 glVertex3f( 0.46f,1.2f,-0.44f); 00625 00626 glVertex3f( 0.60f, 1.2f,-0.5f); 00627 00628 glVertex3f(-0.11f,1.2f,-0.965f); 00629 glVertex3f(-0.25f,1.2f,-0.6f); 00630 glVertex3f(-0.37f,1.2f,-0.875f); 00631 glVertex3f(-0.65f,1.2f,-0.72f); 00632 glVertex3f(-0.84f,1.2f,-0.966f); 00633 glVertex3f(-0.99f,1.2f,-0.77f); 00634 glVertex3f(-1.23f,1.2f,-0.677f); 00635 00636 glEnd(); 00637 00638 if(shadow) 00639 glColor3f(shadowColor[0],shadowColor[1],shadowColor[2]); 00640 else 00641 glColor3f(0.0f,0.0f,0.0f); 00642 drawRecRotate(0.0f, -1.25f,1.2f,-0.25f,1.0f,1.0f,0.1f); 00643 00644 // draw the octagon 00645 if(shadow) 00646 glColor3f(shadowColor[0],shadowColor[1],shadowColor[2]); 00647 else 00648 glColor3f(1.0f,0.5f,0.0f); 00649 drawRecRotate(0.0f,0.0f+TCx,16.0f,0.0f+TCz,6.21f,0.5f,0.3f); // - 00650 drawRecRotate(0.785f,6.21f+TCx,16.0f,0.0f+TCz,6.21f,0.5f,0.3f); 00651 drawRecRotate(1.57f,10.6f+TCx,16.0f,4.39f+TCz,6.21f,0.5f,0.3f); // | 00652 drawRecRotate(-0.785f,-4.39f+TCx,16.0f,4.39f+TCz,6.21f,0.5f,0.3f); // / 00653 drawRecRotate(1.57f,-3.89f+TCx,16.0f,4.39f+TCz,6.21f,0.5f,0.3f); // | 00654 drawRecRotate(0.785f,-3.89f+TCx,16.0f,10.3f+TCz,6.21f,0.5f,0.3f); 00655 drawRecRotate(-0.785f,6.0f+TCx,16.0f,14.5f+TCz,6.21f,0.5f,0.3f); // / 00656 drawRecRotate(0.0f,0.0f+TCx,16.0f,14.5f+TCz,6.21f,0.5f,0.3f); // - 00657 00658 drawRecRotate(0.0f,0.85f+TCx, 16.0f, 2.75f+TCz,4.14f,0.5f,0.3f); 00659 drawRecRotate(0.785f,5.0f+TCx, 16.0f, 2.75f+TCz,4.14f,0.5f,0.3f); 00660 drawRecRotate(2.355f,1.1f+TCx, 16.0f, 3.0f+TCz,4.14f,0.5f,0.3f); 00661 drawRecRotate(1.57f,7.917f+TCx, 16.0f, 5.677f+TCz,4.14f,0.5f,0.3f); 00662 drawRecRotate(1.57f,-1.8f+TCx, 16.0f, 5.677f+TCz,4.14f,0.5f,0.3f); 00663 drawRecRotate(0.785f,-1.8f+TCx, 16.0f, 9.5f+TCz,4.14f,0.5f,0.3f); 00664 drawRecRotate(2.355f,7.917f+TCx, 16.0f, 9.7f+TCz,4.14f,0.5f,0.3f); 00665 drawRecRotate(0.0f,0.85f+TCx, 16.0f, 12.25f+TCz,4.14f,0.5f,0.3f); 00666 outtime = glutGet(GLUT_ELAPSED_TIME); 00667 // draw the light for task a 00668 if(shadow) 00669 glColor3f(shadowColor[0],shadowColor[1],shadowColor[2]); 00670 else 00671 { 00672 if (outtime % 3 ==0) 00673 glColor3f(1.0,0.0,0.0); 00674 else 00675 glColor3f(1.0,1.0,1.0); 00676 } 00677 glTranslatef(TAx, TAy, TAz); 00678 glutSolidSphere(0.25f,20,20); 00679 00680 // draw the gate 00681 00682 if(shadow) 00683 glColor3f(shadowColor[0],shadowColor[1],shadowColor[2]); 00684 else 00685 glColor3f(1.0f,1.0f,1.0f); 00686 drawRecRotate(0.0f, -10.0f,TAy+1.0,10.0f,0.5f,0.5f,6.0f); 00687 drawRecRotate(0.0f, -10.0f,TAy+1.0,0.0f,0.5f,0.5f,6.0f); 00688 00689 // draw the flashing light box 00690 // frequency = 5 Hz with the red light 00691 if(shadow) 00692 glColor3f(shadowColor[0],shadowColor[1],shadowColor[2]); 00693 else 00694 { 00695 if(decode == 0) 00696 { 00697 if (outtime % 2 ==0) 00698 glColor3f(1.0,0.0,0.0); 00699 else 00700 glColor3f(1.0,1.0,1.0); 00701 } 00702 // frequency = 2 Hz with the red light 00703 else if(decode == 1) 00704 { 00705 if (outtime % 5 ==0) 00706 glColor3f(1.0,0.0,0.0); 00707 else 00708 glColor3f(1.0,1.0,1.0); 00709 } 00710 // frequency = 5 Hz with the green light 00711 else if(decode == 2) 00712 { 00713 if (outtime % 2 ==0) 00714 glColor3f(0.0,1.0,0.0); 00715 else 00716 glColor3f(1.0,1.0,1.0); 00717 } 00718 // frequency - 2 Hz with the green light 00719 else 00720 { 00721 if (outtime % 5 ==0) 00722 glColor3f(0.0,1.0,0.0); 00723 else 00724 glColor3f(1.0,1.0,1.0); 00725 } 00726 } 00727 glTranslatef(-9.15,7.0,9.0); 00728 if(isSphere) 00729 glutSolidSphere(0.1f, 20,20); 00730 else 00731 drawRecRotate(0.0f,0.0f,0.0f,0.0f, 0.2f,0.2f,0.2f); 00732 } 00733 void renderScene2(int currentWindow) { 00734 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer 00735 glLightfv(GL_LIGHT0, GL_POSITION, l); 00736 glDisable(GL_CULL_FACE); 00737 glDisable(GL_LIGHTING); 00738 00739 glColor3f(1.0,1.0,0.0); 00740 glBegin(GL_POINTS); 00741 glVertex3f(l[0],l[1],l[2]); 00742 glEnd(); 00743 00744 // put the texture on the follor 00745 glEnable(GL_TEXTURE_2D); 00746 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 00747 // put pictures in the size of 20*20 in x axis 00748 00749 //TEST for large map 00750 glBindTexture(GL_TEXTURE_2D, t[0]); 00751 glBegin(GL_QUADS); 00752 00753 glTexCoord2f(0.0f, 0.0f); glVertex3f(-100.0f+(80), -1.5f,-40.0f);//Bottom Left 00754 glTexCoord2f(1.0f, 0.0f); glVertex3f(-100.0f+(80), -1.5f,-20.0f);//Bottom Right 00755 glTexCoord2f(1.0f, 1.0f); glVertex3f(-80.0f+(80), -1.5f,-20.0f);//Top Right 00756 glTexCoord2f(0.0f, 1.0f); glVertex3f(-80.0f+(80), -1.5f,-40.0f);//Top Left 00757 00758 /* 00759 glTexCoord2f(0.0f, 0.0f); glVertex3f(-100.0f+(80), -1.5f,-40.0f+(200));//Bottom Left 00760 glTexCoord2f(1.0f, 0.0f); glVertex3f(-100.0f+(80), -1.5f,-20.0f+(200));//Bottom Right 00761 glTexCoord2f(1.0f, 1.0f); glVertex3f(-80.0f+(80), -1.5f,-20.0f+(200));//Top Right 00762 glTexCoord2f(0.0f, 1.0f); glVertex3f(-80.0f+(80), -1.5f,-40.0f+(200));//Top Left 00763 glEnd(); 00764 */ /* 00765 for(int i=0 ; i<10 ; i++) 00766 { 00767 for(int j=0 ; j<4 ; j++) 00768 { 00769 glBindTexture(GL_TEXTURE_2D, t[i*4+j]); 00770 glBegin(GL_QUADS); 00771 glTexCoord2f(0.0f, 0.0f); glVertex3f(-100.0f+(i*20), -1.5f,-40.0f+(j*20));//Bottom Left 00772 glTexCoord2f(1.0f, 0.0f); glVertex3f(-100.0f+(i*20), -1.5f,-20.0f+(j*20));//Bottom Right 00773 glTexCoord2f(1.0f, 1.0f); glVertex3f(-80.0f+(i*20), -1.5f,-20.0f+(j*20));//Top Right 00774 glTexCoord2f(0.0f, 1.0f); glVertex3f(-80.0f+(i*20), -1.5f,-40.0f+(j*20));//Top Left 00775 glEnd(); 00776 } 00777 } 00778 */ 00779 //glDisable(GL_TEXTURE_2D); 00780 00781 // show fog or not 00782 if(fogAct) 00783 glEnable(GL_FOG); 00784 else 00785 glDisable(GL_FOG); 00786 // Draw ground 00787 glColor3f(0.0f, 0.5f, 0.5f); 00788 glBegin(GL_QUADS); 00789 glNormal3f(0.0,1.0,0.0); 00790 glVertex3f(-100.0f, e[1]-0.1f, -100.0f); 00791 glVertex3f(-100.0f, e[1]-0.1f, 100.0f); 00792 glVertex3f( 100.0f, e[1]-0.1f, 100.0f); 00793 glVertex3f( 100.0f, e[1]-0.1f, -100.0f); 00794 glEnd(); 00795 glBegin(GL_QUADS); 00796 glVertex3f(-100.0f, e[1]-0.1f, -100.0f); 00797 glVertex3f( 100.0f, e[1]-0.1f, -100.0f); 00798 glVertex3f( 100.0f, e[1]-0.1f, 100.0f); 00799 glVertex3f(-100.0f, e[1]-0.1f, 100.0f); 00800 glEnd(); 00801 00802 // Draw water surface 00803 glColor3f(0.0f, 0.0f, 0.9f); 00804 glBegin(GL_QUADS); 00805 glNormal3f(0.0,-1.0,0.0); 00806 glVertex3f(-100.0f, 16.5f, -100.0f); 00807 glVertex3f(-100.0f, 16.5f, 100.0f); 00808 glVertex3f( 100.0f, 16.5f, 100.0f); 00809 glVertex3f( 100.0f, 16.5f, -100.0f); 00810 glEnd(); 00811 glBegin(GL_QUADS); 00812 glVertex3f(-200.0f, 16.5f, -200.0f); 00813 glVertex3f( 200.0f, 16.5f, -200.0f); 00814 glVertex3f( 200.0f, 16.5f, 200.0f); 00815 glVertex3f(-200.0f, 16.5f, 200.0f); 00816 glEnd(); 00817 00818 // draw the object that cast th shadow 00819 glPushMatrix(); 00820 // glEnable(GL_LIGHTING); 00821 draw(false); 00822 glPopMatrix(); 00823 00824 // draw the shadow 00825 glPushMatrix(); 00826 glShadowProjection(l,e,n); 00827 glDisable(GL_LIGHTING); 00828 glColor3f(0.5,0.5,0.5); 00829 draw(true); 00830 glPopMatrix(); 00831 00832 //******************** 00833 // glPopMatrix(); 00834 // show the text 00835 if (currentWindow == subWindow1) 00836 { 00837 /* 00838 glColor3f(0.0,1.0,1.0); 00839 setOrthographicProjection(); 00840 glPushMatrix(); 00841 glLoadIdentity(); 00842 sprintf(s,"Position:%.1f,%.1f Depth:%.1f ",x,z,(16-y)); 00843 renderBitmapString(30,15,(void *)font,s); 00844 dtemp = heading/3.14*180; 00845 while(dtemp>360) 00846 dtemp = dtemp -360; 00847 while(dtemp<0) 00848 dtemp = dtemp + 360; 00849 sprintf(s,"Heading:%.1f",dtemp); 00850 renderBitmapString(10,10,(void *)font,s); 00851 renderBitmapString(150,150,(void *)font,"For usrc: Navigate the world by arrow keys,"); 00852 renderBitmapString(30,0,(void *)font,"F1 - Rise, F2 - Dive, ESC - Quit"); 00853 glPopMatrix(); 00854 resetPerspectiveProjection(); 00855 */ 00856 // printf("D:%.1f\n", (16-y)); 00857 frame++; 00858 frametime = glutGet(GLUT_ELAPSED_TIME); 00859 if(frametime - timebase > 1000) // per second 00860 { 00861 printf("FPS:%4.2f\n", frame * 1000.0 / (frametime- timebase)); 00862 timebase = frametime; 00863 frame = 0; 00864 } 00865 00866 } 00867 //=========================================== 00868 // test the glReadPixels() 00869 // unsigned char *image; 00870 // assign 3 images get from cameras to image[] 00871 if(currentWindow==subWindow1) 00872 { 00873 glReadPixels(0,0,sw,sh,GL_RGB,GL_UNSIGNED_BYTE,image1); 00874 ima1 = simulateCamera(image1, sw, sh); 00875 } 00876 if(currentWindow==subWindow2)// && outtime/10 % 10 ==0) 00877 { 00878 glReadPixels(0,0,sw,sh,GL_RGB,GL_UNSIGNED_BYTE,image2); 00879 ima2 = simulateCamera(image2, sw, sh); 00880 // test BeoSubCanny 00881 if(!start) 00882 { 00883 /* 00884 test->setupCanny("Red", ima2, false); 00885 darray[1] = 100.0; 00886 darray[2] = 80.0; 00887 darray[3] = 50.0f; 00888 shape.reset(new CircleShape(25.0, darray); 00889 00890 bool find = test->runCanny(shape); 00891 double *d = shape->getDimensions(); 00892 if(find) 00893 printf("::::::::::::::%f\n",d[1]); 00894 */ 00895 00896 } 00897 00898 } 00899 if(currentWindow==subWindow3)// && outtime/10 % 10 ==0) 00900 { 00901 glReadPixels(0,0,sw,sh,GL_RGB,GL_UNSIGNED_BYTE,image3); 00902 ima3 = simulateCamera(image3, sw, sh); 00903 } 00904 00905 00906 glutSwapBuffers(); 00907 } 00908 void renderScene() { 00909 glutSetWindow(mainWindow); 00910 glClear(GL_COLOR_BUFFER_BIT); 00911 glutSwapBuffers(); 00912 } 00913 00914 void renderScenesw1() { 00915 glutSetWindow(subWindow1); 00916 glLoadIdentity(); 00917 gluLookAt(x, y+0.5, z, 00918 x,y + 1 +ly+ ty,z, 00919 -lx,0,-lz); 00920 renderScene2(subWindow1); 00921 } 00922 00923 void renderScenesw2() { 00924 glutSetWindow(subWindow2); 00925 glLoadIdentity(); 00926 gluLookAt(x, y, z, 00927 x + lx,y + ly + ty,z + lz, 00928 0.0f,1.0f,0.0f); 00929 renderScene2(subWindow2); 00930 } 00931 00932 void renderScenesw3() { 00933 glutSetWindow(subWindow3); 00934 glLoadIdentity(); 00935 gluLookAt(x, y-0.5, z, 00936 x,y - 1 +ly+ ty,z, 00937 lx,0,lz); 00938 renderScene2(subWindow3); 00939 } 00940 /* 00941 void *taska(void *ptr) 00942 { 00943 printf("taska"); 00944 turn(180); 00945 return NULL; 00946 } 00947 */ 00948 //================================ 00949 //define the actions here 00950 void advanceByTime(int steps, bool direction) 00951 { 00952 int d; 00953 if(direction) 00954 d = 1; 00955 else 00956 d = -1; 00957 00958 do 00959 { 00960 atime = glutGet(GLUT_ELAPSED_TIME); 00961 x = x + d*(lx)*0.25; 00962 z = z + d*(lz)*0.25; 00963 // render the screen 00964 renderScenesw1(); 00965 renderScenesw2(); 00966 renderScenesw3(); 00967 // display4(); 00968 // display5(); 00969 //display6(); 00970 }while(atime - atimebase < steps*1000); 00971 atimebase = atime; 00972 00973 } 00974 00975 00976 00977 /*void turn(double dangle) 00978 { 00979 double count = 0.0; 00980 double diff; 00981 dangle = dangle/180 * 3.14; 00982 00983 if(dangle >= 0) 00984 { 00985 diff = 0.0157; 00986 do 00987 { 00988 heading += diff; 00989 lx = sin(heading.getRadians()); 00990 lz = -cos(heading.getRadians()); 00991 // render the screen 00992 renderScenesw1(); 00993 renderScenesw2(); 00994 renderScenesw3(); 00995 display4(); 00996 display5(); 00997 display6(); 00998 count += diff; 00999 }while(count <= dangle); 01000 } 01001 else 01002 { 01003 diff = -0.0157; 01004 do 01005 { 01006 heading += diff; 01007 lx = sin(heading); 01008 lz = -cos(heading); 01009 // render the screen 01010 renderScenesw1(); 01011 renderScenesw2(); 01012 renderScenesw3(); 01013 display4(); 01014 display5(); 01015 display6(); 01016 count += diff; 01017 }while(count >= dangle); 01018 } 01019 //atimebase = atime; 01020 01021 }*/ 01022 //=============================== 01023 // define the action for task a 01024 /* 01025 void taska() 01026 { 01027 float xpos, ypos; 01028 atimebase = glutGet(GLUT_ELAPSED_TIME); 01029 01030 // add the component 01031 if(actionCount == 11) 01032 { 01033 manager.addSubComponent(ct); 01034 manager.start(); 01035 } 01036 // dive first 01037 else if(actionCount == 12) 01038 dive( y - 5, true); 01039 // assume we are close to the target 01040 else if(actionCount == 13) 01041 { 01042 ct->setupTracker("Red", ima2, false); 01043 while(!ct->runTracker(50.0, xpos, ypos)) 01044 { 01045 turn(5); 01046 ct->setupTracker("Red", ima2,false); 01047 } 01048 01049 } 01050 01051 01052 actionCount ++; 01053 } 01054 */ 01055 //================================= 01056 //acions commands in here 01057 void renderSceneAll() { 01058 /* 01059 01060 if(deltaHeading.getVal() > 0 && !(SimSub->targetReached())) { 01061 heading += deltaHeading; 01062 orientMe(heading); 01063 std::cout << "Heading: " << SimSub->getHeading().getVal() << '\n'; 01064 deltaHeading = 1; 01065 } 01066 01067 if(deltaStrafeX) { 01068 strafe(deltaStrafeX); 01069 std::cout << "sub x: " << x << " sub z: " << z << '\n'; 01070 deltaStrafeX = 0; 01071 */ 01072 01073 01074 //if(!setDive) { 01075 // SimSub->diveAbs(3); 01076 //} 01077 01078 if(!setForward) { 01079 // SimSub->strafeRel(5); 01080 01081 SimSub->getThrusters(leftThrustValue, rightThrustValue); 01082 01083 if(leftThrustValue != 0) { 01084 targetZ = -(z + SimSub->getRelDist() * 3.2808 * lz); 01085 targetX = x - SimSub->getRelDist() * 3.2808 * lx; 01086 setForward = true; 01087 } 01088 else if(SimSub->isStrafing()) { 01089 targetZ = -(z + SimSub->getRelDist() * 3.2808 * lx); 01090 targetX = x - SimSub->getRelDist() * 3.2808 * lz; 01091 setForward = true; 01092 } 01093 01094 } 01095 01096 01097 std::cout << "goalValueZ: " << targetZ * .3048 << "goalValueX: " << targetX * .3048<< '\n'; 01098 std::cout << z*.3048 << " " << x*.3048 << '\n'; 01099 //advance forward/reverse 01100 if(leftThrustValue != 0 && fabs(targetZ - z) >= 0.2 && fabs(targetX - x) >=0.2) 01101 moveMeFlat(3.2808f * leftThrustValue); 01102 01103 if(SimSub->isStrafing() && fabs(targetZ - z) >= 0.2 && fabs(targetX - x) >= 0.2) 01104 strafe(3.2808); 01105 01106 if(fabs(targetZ - z) < 0.2 && fabs(targetX - x) < 0.2) { 01107 std::cout << "done advancing\n"; 01108 SimSub->thrust(0.0F, 0.0F); 01109 SimSub->setStrafing(); 01110 } 01111 01112 01113 01114 //this code works turning right/left 01115 if(fabs(SimSub->getTargetAttitude().heading.getRadians() - 01116 SimSub->getCurrentAttitude().heading.getRadians()) > 0.2) { 01117 std::cout << "turning\n"; 01118 std::cout << "heading target: " << SimSub->getTargetAttitude().heading.getVal() << '\n'; 01119 if(SimSub->getTargetAttitude().heading.getRadians() > 0) 01120 heading+=1; 01121 else 01122 heading-=1; 01123 01124 orientMe(heading); 01125 } 01126 else 01127 std::cout << "done turning\n"; 01128 01129 01130 //this code works for diving/surfacing 01131 if(fabs(SimSub->getTargetAttitude().depth - SimSub->getCurrentAttitude().depth) > 0.2) { 01132 std::cout << "diving\n"; 01133 std::cout << "depth target: " << SimSub->getTargetAttitude().depth << "m\n"; 01134 01135 if(SimSub->getTargetAttitude().depth > SimSub->getCurrentAttitude().depth) 01136 moveMeVer(-3.2808f); 01137 else 01138 moveMeVer(3.2808f); 01139 } 01140 else 01141 std::cout << "done diving\n"; 01142 01143 if(SimSub->getUpGrabFlag()) { 01144 storeImage("up", ima1); 01145 SimSub->setUpGrabFlag(); 01146 } 01147 01148 if(SimSub->getFrontGrabFlag()) { 01149 storeImage("front", ima2); 01150 SimSub->setFrontGrabFlag(); 01151 } 01152 01153 if(SimSub->getDownGrabFlag()) { 01154 storeImage("up", ima3); 01155 SimSub->setDownGrabFlag(); 01156 } 01157 01158 //this code works 01159 SimSub->updateCompass(heading, pitch, roll); 01160 SimSub->updateDepth((20 - y) * 0.3048f); 01161 SimSub->updatePosition(z * 0.3048f, x * 0.3048f); 01162 std::cout << "heading: " << SimSub->getHeading().getVal() 01163 << " depth: " << SimSub->getCurrentAttitude().depth << "m\n"; 01164 std::cout << "z: " << SimSub->getCurrentZ() << "m x: " << SimSub->getCurrentX() << "m\n"; 01165 01166 01167 01168 renderScenesw1(); 01169 renderScenesw2(); 01170 renderScenesw3(); 01171 // display4(); 01172 //display5(); 01173 //display6(); 01174 } 01175 01176 void processNormalKeys(unsigned char key, int x, int y) { 01177 if (key == 27) 01178 exit(0); 01179 } 01180 01181 void mouseButton(int button, int state, int x, int y) { 01182 if(button == GLUT_LEFT_BUTTON) 01183 SimSub->diveRel(3); 01184 else if(button == GLUT_RIGHT_BUTTON) 01185 SimSub->turnRel(90); 01186 else if(button == GLUT_MIDDLE_BUTTON) 01187 SimSub->TaskA(); 01188 } 01189 01190 // these keys will be used to choose which task to do, 01191 // i.e. F1 is TaskA() 01192 void pressKey(int key, int x, int y) { 01193 switch (key) { 01194 case GLUT_KEY_LEFT : { 01195 std::cout << "ADVANCE!\n"; 01196 SimSub->advanceRel(5); break; 01197 } 01198 case GLUT_KEY_RIGHT : SimSub->diveRel(5) ;break; 01199 case GLUT_KEY_UP : SimSub->turnRel(5); break; 01200 case GLUT_KEY_DOWN : deltaMove = -1;break; 01201 case GLUT_KEY_F1 : deltaDepth = 1;break; 01202 case GLUT_KEY_F2 : deltaDepth = -1;break; 01203 case GLUT_KEY_F3 : deltaStrafeX = 1; break; 01204 //decode++; 01205 //if(decode >= 4) 01206 //decode = 0; 01207 //break; 01208 case GLUT_KEY_F4 : deltaStrafeX = -1; 01209 // fogAct = false; 01210 break; 01211 case GLUT_KEY_F5 : 01212 isSphere = !isSphere; 01213 break; 01214 case GLUT_KEY_F6 : 01215 start = !start; 01216 manager.addSubComponent(ct); 01217 manager.start(); 01218 01219 break; 01220 case GLUT_KEY_F7 : storeImage("up", ima1); break; 01221 case GLUT_KEY_F8 : storeImage("front", ima2); break; 01222 case GLUT_KEY_F9 : storeImage("down",ima3); break; 01223 case GLUT_KEY_PAGE_UP : deltaPitch = 0.05f; break; 01224 case GLUT_KEY_PAGE_DOWN : deltaPitch = -0.05f; break; 01225 } 01226 } 01227 01228 void releaseKey(int key, int x, int y) { 01229 01230 switch (key) { 01231 case GLUT_KEY_LEFT : 01232 SimSub->advanceRel(5); 01233 break; 01234 /* case GLUT_KEY_RIGHT : 01235 if (deltaHeading > 0.0f) 01236 deltaHeading = 0.0f; 01237 break; 01238 case GLUT_KEY_UP : 01239 if (deltaMove > 0) 01240 deltaMove = 0; 01241 break; 01242 case GLUT_KEY_DOWN : 01243 if (deltaMove < 0) 01244 deltaMove = 0; 01245 break; 01246 case GLUT_KEY_F1: 01247 if(deltaDepth > 0) 01248 deltaDepth =0; 01249 break; 01250 case GLUT_KEY_F2: 01251 if(deltaDepth < 0) 01252 deltaDepth =0; 01253 break; 01254 case GLUT_KEY_F3: 01255 if(deltaStrafeX < 0) 01256 deltaStrafeX = 0; 01257 break; 01258 case GLUT_KEY_F4: 01259 if(deltaStrafeX > 0) 01260 deltaStrafeX = 0; 01261 break; 01262 case GLUT_KEY_F5: 01263 if(deltaStrafeZ < 0) 01264 deltaStrafeY 01265 case GLUT_KEY_F4 : 01266 fogAct = true; 01267 break; 01268 case GLUT_KEY_PAGE_UP : 01269 if(deltaPitch > 0) 01270 deltaPitch = 0; 01271 break; 01272 case GLUT_KEY_PAGE_DOWN : 01273 if(deltaPitch < 0) 01274 deltaPitch = 0; 01275 break;*/ 01276 } 01277 } 01278 01279 // assume RGB RGB RGB ... 01280 // 3 bytes per pixel 01281 // data is 'w' pixels wide by 'h' pixels high 01282 // hence data should have at least w*h*3 bytes of memory allocated 01283 Image< PixRGB<byte> > 01284 simulateCamera(const byte *data, const int aw, const int ah) 01285 { 01286 // build an image from the raw data: 01287 Image< PixRGB<byte> > im((PixRGB<byte> *)data, aw, ah); 01288 01289 // add some small amount of speckle noise to the image: 01290 //inplaceColorSpeckleNoise(im, 1000); 01291 01292 // blur the image: 01293 //im = lowPass9(lowPass9(lowPass9(im))); 01294 //im = lowPass9(im); 01295 01296 // open a window if not open yet: 01297 // if (xw == NULL) xw = new XWindow(Dims(w, h)); 01298 01299 // show image in the window: 01300 // xw->drawImage(im); 01301 01302 return im; 01303 } 01304 /* 01305 void *control(bool dummy) 01306 { 01307 printf("\n\ngot in\n\n"); 01308 01309 //=============================== 01310 // define the action for task a 01311 01312 //TASK A 01313 01314 } 01315 */ 01316 01317 #endif // HAVE_GL_GLUT_H 01318 01319 int main(int argc, char **argv) 01320 { 01321 #ifndef HAVE_GL_GLUT_H 01322 01323 LFATAL("<GL/glut.h> must be installed to use this program"); 01324 01325 #else 01326 01327 glutInit(&argc, argv); 01328 glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH); 01329 glutInitWindowPosition(0,0); 01330 glutInitWindowSize(w,h); 01331 mainWindow = glutCreateWindow("Sub"); 01332 glutIgnoreKeyRepeat(1); 01333 glutKeyboardFunc(processNormalKeys); 01334 glutSpecialFunc(pressKey); 01335 glutSpecialUpFunc(releaseKey); 01336 glutMouseFunc(mouseButton); 01337 glutReshapeFunc(changeSize); 01338 glutDisplayFunc(renderScene); 01339 glutIdleFunc(renderSceneAll); 01340 01341 glEnable(GL_NORMALIZE); 01342 glEnable(GL_LIGHTING); 01343 glEnable(GL_COLOR_MATERIAL); 01344 glEnable(GL_DEPTH_TEST); 01345 glEnable(GL_LIGHT0); 01346 glEnable(GL_TEXTURE_2D); 01347 glMatrixMode(GL_PROJECTION); 01348 01349 glLoadIdentity(); 01350 gluPerspective(60.0f,1.0,1.0,400.0); 01351 01352 glMatrixMode(GL_MODELVIEW); 01353 glLoadIdentity(); 01354 glTranslatef(0.0,0.0,-150.0); 01355 01356 01357 //setup windows 01358 subWindow1 = glutCreateSubWindow(mainWindow, border,border, 01359 w-border, (h-4*border)/3); 01360 glutDisplayFunc(renderScene); 01361 initScene(); 01362 01363 subWindow2 = glutCreateSubWindow(mainWindow, border,2*border+(h-4*border)/3, 01364 w-border, (h-4*border)/3); 01365 glutDisplayFunc(renderScenesw2); 01366 initScene(); 01367 01368 subWindow3 = glutCreateSubWindow(mainWindow, border,3*border+2*(h-4*border)/3, 01369 w-border, (h-4*border)/3); 01370 glutDisplayFunc(renderScenesw3); 01371 initScene(); 01372 01373 //subWindow4 = glutCreateSubWindow(mainWindow,w-border-mainx, border, 01374 // w-3*border-mainx, (h-4*border)/3); 01375 //glutDisplayFunc(display4); 01376 //initWindow2(); 01377 01378 //subWindow5 = glutCreateSubWindow(mainWindow,w-border-mainx,2*border+(h-4*border)/3, 01379 // w-3*border-mainx, (h-4*border)/3); 01380 //glutDisplayFunc(display5); 01381 //initWindow2(); 01382 //subWindow6 = glutCreateSubWindow(mainWindow,w-border-mainx,3*border+2*(h-4*border)/3, 01383 // w-3*border-mainx, (h-4*border)/3); 01384 //glutDisplayFunc(display6); 01385 //initWindow2(); 01386 01387 glutMainLoop(); 01388 return 1; 01389 01390 #endif // HAVE_GL_GLUT_H 01391 01392 } 01393