00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include "Image/Image.H"
00040 #include "Component/ModelManager.H"
00041 #include "plugins/SceneUnderstanding/TensorVoting.H"
00042 #include "plugins/SceneUnderstanding/V2.H"
00043 #include "plugins/SceneUnderstanding/SFS.H"
00044 #include "Raster/Raster.H"
00045 #include "GUI/DebugWin.H"
00046 #include "GUI/ViewPort.H"
00047 #include <GL/glut.h>
00048
00049 #include <signal.h>
00050 #include <sys/types.h>
00051
00052 typedef unsigned char BYTE;
00053
00054 typedef GLXContext (*glXCreateContextAttribsARBProc)(Display*, GLXFBConfig, GLXContext, Bool, const int*);
00055
00056
00057 typedef struct
00058 {
00059 int type;
00060 unsigned int maxX,
00061 maxY;
00062 unsigned char *image;
00063 } PIC;
00064
00065 void drawQube()
00066 {
00067 glBegin(GL_QUADS);
00068 glColor4f(1, 1, 1, 1);
00069
00070
00071 glNormal3f(0,0,1);
00072 glTexCoord2f(1, 1); glVertex3f(1,1,1);
00073 glTexCoord2f(0, 1); glVertex3f(-1,1,1);
00074 glTexCoord2f(0, 0); glVertex3f(-1,-1,1);
00075 glTexCoord2f(1, 0); glVertex3f(1,-1,1);
00076
00077
00078 glNormal3f(1,0,0);
00079 glTexCoord2f(0, 1); glVertex3f(1,1,1);
00080 glTexCoord2f(0, 0); glVertex3f(1,-1,1);
00081 glTexCoord2f(1, 0); glVertex3f(1,-1,-1);
00082 glTexCoord2f(1, 1); glVertex3f(1,1,-1);
00083
00084
00085 glNormal3f(0,1,0);
00086 glTexCoord2f(1, 0); glVertex3f(1,1,1);
00087 glTexCoord2f(1, 1); glVertex3f(1,1,-1);
00088 glTexCoord2f(0, 1); glVertex3f(-1,1,-1);
00089 glTexCoord2f(0, 0); glVertex3f(-1,1,1);
00090
00091
00092 glNormal3f(-1,0,0);
00093 glTexCoord2f(1, 1); glVertex3f(-1,1,1);
00094 glTexCoord2f(0, 1); glVertex3f(-1,1,-1);
00095 glTexCoord2f(0, 0); glVertex3f(-1,-1,-1);
00096 glTexCoord2f(1, 0); glVertex3f(-1,-1,1);
00097
00098
00099 glNormal3f(0,-1,0);
00100 glTexCoord2f(0, 0); glVertex3f(-1,-1,-1);
00101 glTexCoord2f(1, 0); glVertex3f(1,-1,-1);
00102 glTexCoord2f(1, 1); glVertex3f(1,-1,1);
00103 glTexCoord2f(0, 1); glVertex3f(-1,-1,1);
00104
00105
00106 glNormal3f(0,0,-1);
00107 glTexCoord2f(0, 0); glVertex3f(1,-1,-1);
00108 glTexCoord2f(1, 0); glVertex3f(-1,-1,-1);
00109 glTexCoord2f(1, 1); glVertex3f(-1,1,-1);
00110 glTexCoord2f(0, 1); glVertex3f(1,1,-1);
00111 glEnd();
00112 }
00113
00114 void displayCB()
00115 {
00116
00117 int screenWidth = 320;
00118 int screenHeight = 240;
00119
00120 LINFO("Draw");
00121
00122 glViewport(0, 0, screenWidth, screenHeight);
00123 glMatrixMode(GL_PROJECTION);
00124 glLoadIdentity();
00125 gluPerspective(60.0f, (float)(screenWidth)/screenHeight, 1.0f, 100.0f);
00126 glMatrixMode(GL_MODELVIEW);
00127
00128
00129 glClearColor(0, 0, 0, 0);
00130 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00131
00132
00133
00134 glPushAttrib(GL_COLOR_BUFFER_BIT | GL_PIXEL_MODE_BIT);
00135 glDrawBuffer(GL_BACK);
00136 glReadBuffer(GL_BACK);
00137
00138
00139
00140 glPushMatrix();
00141 glTranslatef(0, 0, 1.5);
00142 glRotatef(45, 1, 0, 0);
00143 glRotatef(45, 0, 1, 0);
00144
00145 drawQube();
00146 glPopMatrix();
00147
00148
00149
00150 glPopAttrib();
00151
00152
00153
00154
00155 glFlush();
00156
00157
00158 }
00159
00160 void setCamera(float posX, float posY, float posZ, float targetX, float targetY, float targetZ)
00161 {
00162 glMatrixMode(GL_MODELVIEW);
00163 glLoadIdentity();
00164 gluLookAt(posX, posY, posZ, targetX, targetY, targetZ, 0, 1, 0);
00165 }
00166
00167 void initLights()
00168 {
00169
00170 GLfloat lightKa[] = {.2f, .2f, .2f, 1.0f};
00171 GLfloat lightKd[] = {.7f, .7f, .7f, 1.0f};
00172 GLfloat lightKs[] = {1, 1, 1, 1};
00173 glLightfv(GL_LIGHT0, GL_AMBIENT, lightKa);
00174 glLightfv(GL_LIGHT0, GL_DIFFUSE, lightKd);
00175 glLightfv(GL_LIGHT0, GL_SPECULAR, lightKs);
00176
00177
00178 float lightPos[4] = {0, 0, 20, 1};
00179 glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
00180
00181 glEnable(GL_LIGHT0);
00182 }
00183
00184
00185 PIC UCFReadPic(const char* filename)
00186 {
00187 FILE* infile = fopen(filename,"r");
00188 PIC temp;
00189 int ret;
00190
00191
00192 ret=fread(&temp.type,sizeof(temp.type),1,infile);
00193 switch (temp.type)
00194 {
00195 case 0xF10F:
00196 case 0xF200:
00197 case 0xF201:
00198 case 0xF204:
00199 case 0x0000:
00200 {
00201
00202
00203 unsigned char byte1,byte2,byte3,byte4;
00204 ret=fread(&byte1,sizeof(BYTE),1,infile);
00205 ret=fread(&byte2,sizeof(BYTE),1,infile);
00206 ret=fread(&byte3,sizeof(BYTE),1,infile);
00207 ret=fread(&byte4,sizeof(BYTE),1,infile);
00208 temp.maxX= byte1*16777216+byte2*65536+byte3*256+byte4;
00209 ret=fread(&byte1,sizeof(BYTE),1,infile);
00210 ret=fread(&byte2,sizeof(BYTE),1,infile);
00211 ret=fread(&byte3,sizeof(BYTE),1,infile);
00212 ret=fread(&byte4,sizeof(BYTE),1,infile);
00213 temp.maxY= byte1*16777216+byte2*65536+byte3*256+byte4;
00214 printf("Max: %i %i\n", temp.maxX, temp.maxY);
00215 break;
00216 }
00217 case 0x8000:
00218 case 0x8001:
00219 case 0xB003:
00220 default :
00221 {
00222 ret=fread(&temp.maxX,sizeof(temp.maxX),1,infile);
00223 ret=fread(&temp.maxY,sizeof(temp.maxY),1,infile);
00224 break;
00225 }
00226 }
00227 if((temp.image=(BYTE*)calloc(temp.maxX*temp.maxY,sizeof(BYTE)))==NULL)
00228 {
00229 temp.maxX = temp.maxY = 0;
00230 temp.image = NULL;
00231 return(temp);
00232 }
00233
00234 ret=fread(temp.image,sizeof(BYTE),temp.maxX * temp.maxY,infile);
00235 fclose(infile);
00236 return(temp);
00237 }
00238
00239
00240
00241 int main(const int argc, const char **argv)
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
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291 Display *display = XOpenDisplay(0);
00292
00293 if ( !display )
00294 {
00295 printf( "Failed to open X display\n" );
00296 exit(1);
00297 }
00298
00299
00300 static int visual_attribs[] =
00301 {
00302 GLX_X_RENDERABLE , True,
00303 GLX_DRAWABLE_TYPE , GLX_WINDOW_BIT,
00304 GLX_RENDER_TYPE , GLX_RGBA_BIT,
00305 GLX_X_VISUAL_TYPE , GLX_TRUE_COLOR,
00306 GLX_RED_SIZE , 8,
00307 GLX_GREEN_SIZE , 8,
00308 GLX_BLUE_SIZE , 8,
00309 GLX_ALPHA_SIZE , 8,
00310 GLX_DEPTH_SIZE , 24,
00311 GLX_STENCIL_SIZE , 8,
00312 GLX_DOUBLEBUFFER , True,
00313
00314
00315 None
00316 };
00317
00318 printf( "Getting matching framebuffer configs\n" );
00319 int fbcount;
00320 GLXFBConfig *fbc = glXChooseFBConfig( display, DefaultScreen( display ),
00321 visual_attribs, &fbcount );
00322 if ( !fbc )
00323 {
00324 printf( "Failed to retrieve a framebuffer config\n" );
00325 exit(1);
00326 }
00327 printf( "Found %d matching FB configs.\n", fbcount );
00328
00329
00330 printf( "Getting XVisualInfos\n" );
00331 int best_fbc = -1, worst_fbc = -1, best_num_samp = -1, worst_num_samp = 999;
00332
00333 int i;
00334 for ( i = 0; i < fbcount; i++ )
00335 {
00336 XVisualInfo *vi = glXGetVisualFromFBConfig( display, fbc[i] );
00337 if ( vi )
00338 {
00339 int samp_buf, samples;
00340 glXGetFBConfigAttrib( display, fbc[i], GLX_SAMPLE_BUFFERS, &samp_buf );
00341 glXGetFBConfigAttrib( display, fbc[i], GLX_SAMPLES , &samples );
00342
00343 printf( " Matching fbconfig %d, visual ID 0x%2x: SAMPLE_BUFFERS = %d,"
00344 " SAMPLES = %d\n",
00345 i, (uint)vi -> visualid, samp_buf, samples );
00346
00347 if ( (best_fbc < 0 || samp_buf ) && (samples > best_num_samp) )
00348 best_fbc = i, best_num_samp = samples;
00349 if ( worst_fbc < 0 || !samp_buf || samples < worst_num_samp )
00350 worst_fbc = i, worst_num_samp = samples;
00351 }
00352 XFree( vi );
00353 }
00354
00355
00356 int fbc_id = best_fbc;
00357
00358
00359 XVisualInfo *vi = glXGetVisualFromFBConfig( display, fbc[ fbc_id ] );
00360 printf( "Chosen visual ID = 0x%x\n", (uint)vi->visualid );
00361
00362 printf( "Creating colormap\n" );
00363 XSetWindowAttributes swa;
00364 swa.colormap = XCreateColormap( display, RootWindow( display, vi->screen ),
00365 vi->visual, AllocNone );
00366 swa.background_pixmap = None ;
00367 swa.border_pixel = 0;
00368 swa.event_mask = StructureNotifyMask;
00369
00370 printf( "Creating window\n" );
00371 Window win = XCreateWindow( display, RootWindow( display, vi->screen ),
00372 0, 0, 320, 240, 0, vi->depth, InputOutput,
00373 vi->visual,
00374 CWBorderPixel|CWColormap|CWEventMask, &swa );
00375 if ( !win )
00376 {
00377 printf( "Failed to create window.\n" );
00378 exit(1);
00379 }
00380
00381 XStoreName( display, win, "GL 3.0 Window");
00382
00383 printf( "Mapping window\n" );
00384 XMapWindow( display, win );
00385
00386
00387
00388 glXCreateContextAttribsARBProc glXCreateContextAttribsARB = 0;
00389
00390 GLXContext ctx_old = glXCreateContext( display, vi, 0, True );
00391 glXCreateContextAttribsARB = (glXCreateContextAttribsARBProc)
00392 glXGetProcAddress( (const GLubyte *) "glXCreateContextAttribsARB" );
00393
00394 GLXContext ctx = ctx_old;
00395 XFree( fbc );
00396
00397
00398 printf( "Verifying that context is direct\n" );
00399 if ( ! glXIsDirect ( display, ctx ) )
00400 {
00401 printf( "Indirect GLX rendering context obtained" );
00402 exit(1);
00403 }
00404
00405 printf( "Making context current\n" );
00406 glXMakeCurrent( display, win, ctx );
00407
00408
00409
00410 for(uint i=0; i<10; i++)
00411 {
00412 displayCB();
00413 glXSwapBuffers ( display, win );
00414
00415 Image<PixRGB<byte> > img(320,240, ZEROS);
00416
00417
00418
00419 glReadPixels (0, 0, 320, 240,
00420 GL_RGB, GL_UNSIGNED_BYTE, (unsigned char*)img.getArrayPtr());
00421 SHOWIMG(img);
00422 }
00423
00424
00425
00426 return 0;
00427 }
00428