00001
00002
00003
00004
00005
00006
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;
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
00076 LINFO("CREATING MANAGER");
00077 ModelManager manager("BeoSubCanny Tester");
00078 nub::soft_ref<ColorTracker> ct(new ColorTracker(manager));
00079
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
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
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
00126
00127 int ImageLoad(char *filename, Texture *image) {
00128 FILE *file;
00129 unsigned long size;
00130 unsigned long i;
00131 unsigned short int planes;
00132 unsigned short int bpp;
00133 char temp;
00134
00135
00136 if ((file = fopen(filename, "rb"))==NULL)
00137 {
00138 printf("File Not Found : %s\n",filename);
00139 return 0;
00140 }
00141
00142
00143 fseek(file, 18, SEEK_CUR);
00144
00145
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
00152 if ((i = fread(&image->sizeY, 4, 1, file)) != 1) {
00153 printf("Error reading height from %s.\n", filename);
00154 return 0;
00155 }
00156
00157
00158
00159 size = image->sizeX * image->sizeY * 3;
00160
00161
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
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
00183 fseek(file, 24, SEEK_CUR);
00184
00185
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) {
00198 temp = image->data[i];
00199 image->data[i] = image->data[i+2];
00200 image->data[i+2] = temp;
00201 }
00202
00203
00204 return 1;
00205 }
00206
00207
00208 void LoadGLTextures(char *filename, int i) {
00209
00210 Texture *texture;
00211
00212
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
00224 glGenTextures(1, (GLuint *)&t[i]);
00225 glBindTexture(GL_TEXTURE_2D, t[i]);
00226
00227 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
00228 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
00229
00230
00231
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
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267 void changeSize2(int w1, int h1)
00268 {
00269
00270
00271
00272 ratio = 1.0f * w1 / h1;
00273
00274
00275 glViewport(0, 0, w1, h1);
00276
00277
00278 glMatrixMode(GL_PROJECTION);
00279 glLoadIdentity();
00280
00281
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
00293
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
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324 }
00325
00326
00327 void drawSnowMan() {}
00328
00329
00330 void drawRecRotate(GLfloat de, GLfloat x, GLfloat y, GLfloat z, GLfloat w, GLfloat d, GLfloat h)
00331 {
00332
00333
00334
00335
00336
00337
00338
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
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
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
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
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
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
00411 glMatrixMode(GL_PROJECTION);
00412
00413
00414 glPushMatrix();
00415
00416 glLoadIdentity();
00417
00418 gluOrtho2D(0, w, 0, h/2);
00419
00420 glScalef(1, -1, 1);
00421
00422
00423 glTranslatef(0, -h/2, 0);
00424 glMatrixMode(GL_MODELVIEW);
00425 }
00426
00427 void resetPerspectiveProjection() {
00428
00429 glMatrixMode(GL_PROJECTION);
00430
00431 glPopMatrix();
00432
00433 glMatrixMode(GL_MODELVIEW);
00434 }
00435
00436 void renderBitmapString(float x, float y, void *font,char *string)
00437 {
00438 char *c;
00439
00440 glRasterPos2f(x, y);
00441
00442 for (c=string; *c != '\0'; c++) {
00443 glutBitmapCharacter(font, *c);
00444 }
00445 }
00446 void initScene() {
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500 glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
00501 glClearDepth(1.0);
00502 glDepthFunc(GL_LESS);
00503 glEnable(GL_DEPTH_TEST);
00504 glShadeModel(GL_SMOOTH);
00505
00506 glEnable(GL_CULL_FACE);
00507
00508
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();
00522
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
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
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
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
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
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
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
00690
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
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
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
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);
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
00745 glEnable(GL_TEXTURE_2D);
00746 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
00747
00748
00749
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);
00754 glTexCoord2f(1.0f, 0.0f); glVertex3f(-100.0f+(80), -1.5f,-20.0f);
00755 glTexCoord2f(1.0f, 1.0f); glVertex3f(-80.0f+(80), -1.5f,-20.0f);
00756 glTexCoord2f(0.0f, 1.0f); glVertex3f(-80.0f+(80), -1.5f,-40.0f);
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782 if(fogAct)
00783 glEnable(GL_FOG);
00784 else
00785 glDisable(GL_FOG);
00786
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
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
00819 glPushMatrix();
00820
00821 draw(false);
00822 glPopMatrix();
00823
00824
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
00834
00835 if (currentWindow == subWindow1)
00836 {
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857 frame++;
00858 frametime = glutGet(GLUT_ELAPSED_TIME);
00859 if(frametime - timebase > 1000)
00860 {
00861 printf("FPS:%4.2f\n", frame * 1000.0 / (frametime- timebase));
00862 timebase = frametime;
00863 frame = 0;
00864 }
00865
00866 }
00867
00868
00869
00870
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)
00877 {
00878 glReadPixels(0,0,sw,sh,GL_RGB,GL_UNSIGNED_BYTE,image2);
00879 ima2 = simulateCamera(image2, sw, sh);
00880
00881 if(!start)
00882 {
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896 }
00897
00898 }
00899 if(currentWindow==subWindow3)
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
00942
00943
00944
00945
00946
00947
00948
00949
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
00964 renderScenesw1();
00965 renderScenesw2();
00966 renderScenesw3();
00967
00968
00969
00970 }while(atime - atimebase < steps*1000);
00971 atimebase = atime;
00972
00973 }
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017
01018
01019
01020
01021
01022
01023
01024
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053
01054
01055
01056
01057 void renderSceneAll() {
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070
01071
01072
01073
01074
01075
01076
01077
01078 if(!setForward) {
01079
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
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
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
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
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
01172
01173
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
01191
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
01205
01206
01207
01208 case GLUT_KEY_F4 : deltaStrafeX = -1;
01209
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
01235
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245
01246
01247
01248
01249
01250
01251
01252
01253
01254
01255
01256
01257
01258
01259
01260
01261
01262
01263
01264
01265
01266
01267
01268
01269
01270
01271
01272
01273
01274
01275
01276 }
01277 }
01278
01279
01280
01281
01282
01283 Image< PixRGB<byte> >
01284 simulateCamera(const byte *data, const int aw, const int ah)
01285 {
01286
01287 Image< PixRGB<byte> > im((PixRGB<byte> *)data, aw, ah);
01288
01289
01290
01291
01292
01293
01294
01295
01296
01297
01298
01299
01300
01301
01302 return im;
01303 }
01304
01305
01306
01307
01308
01309
01310
01311
01312
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
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
01374
01375
01376
01377
01378
01379
01380
01381
01382
01383
01384
01385
01386
01387 glutMainLoop();
01388 return 1;
01389
01390 #endif // HAVE_GL_GLUT_H
01391
01392 }
01393