paddleDemo.c
Go to the documentation of this file.
00001 #ifdef _WIN32
00002 #  include <windows.h>
00003 #endif
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <string.h>
00007 #include <math.h>
00008 
00009 #ifndef __APPLE__
00010 #  include <GL/glut.h>
00011 #else
00012 #  include <GLUT/glut.h>
00013 #endif
00014 #include <AR/gsub.h>
00015 #include <AR/video.h>
00016 #include <AR/param.h>
00017 #include <AR/matrix.h>
00018 #include <AR/ar.h>
00019 #include <AR/arMulti.h>
00020 
00021 /* set up the video format globals */
00022 
00023 #ifdef _WIN32
00024 char    *vconf = "Data\\WDM_camera_flipV.xml";
00025 #else
00026 char    *vconf = "";
00027 #endif
00028 
00029 #define TARGET_NUM 5
00030 
00031 #include "paddle.h"
00032 #include "command_sub.h"
00033 
00034 PaddleItemInfo myPaddleItem;
00035 ItemList myListItem;
00036 
00037 int             xsize, ysize;
00038 int             thresh = 100;
00039 int             count = 0;
00040 
00041 char           *cparam_name    = "Data/camera_para.dat";
00042 ARParam         cparam;
00043 
00044 char                *config_name = "Data/multi/marker.dat";
00045 ARMultiMarkerInfoT  *config;
00046 
00047 /* paddle information  */
00048 int              marker_flag[AR_SQUARE_MAX];
00049 ARPaddleInfo   *paddleInfo;
00050 char           *paddle_name    = "Data/paddle_data";  
00051 
00052 GLfloat   light_position[]  = {100.0,-200.0,200.0,0.0};
00053 GLfloat   ambi[]            = {0.1, 0.1, 0.1, 0.1};
00054 GLfloat   lightZeroColor[]  = {0.9, 0.9, 0.9, 0.1};
00055 
00056 
00057 PaddleItemInfo myPaddleItem;
00058 ItemList myListItem;
00059 
00060 static int    draw_paddle( ARPaddleInfo *paddleInfo, PaddleItemInfo *myPaddleItem);
00061 static void   init(void);
00062 static void   cleanup(void);
00063 static void   keyEvent( unsigned char key, int x, int y);
00064 static void   mainLoop(void);
00065 static int    draw_paddle( ARPaddleInfo *paddleInfo, PaddleItemInfo *myPaddleItem);
00066 static int    drawGroundGrid( double trans[3][4], int divisions, float x, float y, float height);
00067 static void   findPaddlePosition(float curPaddlePos[], double card_trans[3][4],double base_trans[3][4]);
00068 static void drawItems(double trans[3][4],ItemList* list);
00069 
00070 int main(int argc, char **argv)
00071 {
00072         //initialize applications
00073         glutInit(&argc, argv);
00074     init();
00075 
00076     arVideoCapStart();
00077         //start the main event loop
00078     argMainLoop( NULL, keyEvent, mainLoop );
00079 
00080         return 0;
00081 }
00082 
00083 /* keyboard events */
00084 static void   keyEvent( unsigned char key, int x, int y)   
00085 {
00086     /* quit if the ESC key is pressed */
00087     if( key == 0x1b ) {
00088         printf("*** %f (frame/sec)\n", (double)count/arUtilTimer());
00089         cleanup();
00090         exit(0);
00091     }
00092 
00093         /* change the threshold value when 't' key pressed */
00094     if( key == 't' ) {
00095         printf("Enter new threshold value (default = 100): ");
00096         scanf("%d",&thresh); while( getchar()!='\n' );
00097         printf("\n");
00098     }
00099 
00100         /* turn on and off the debug mode with d key */
00101     if( key == 'd' ) {
00102         printf("*** %f (frame/sec)\n", (double)count/arUtilTimer());
00103         arDebug = 1 - arDebug;
00104         if( arDebug == 0 ) {
00105             glClearColor( 0.0, 0.0, 0.0, 0.0 );
00106             glClear(GL_COLOR_BUFFER_BIT);
00107             argSwapBuffers();
00108             glClear(GL_COLOR_BUFFER_BIT);
00109             argSwapBuffers();
00110         }
00111         count = 0;
00112     }
00113 }
00114 
00115 /* main loop */
00116 static void mainLoop(void)
00117 {
00118     ARUint8         *dataPtr;
00119     ARMarkerInfo    *marker_info;
00120     int             marker_num;
00121         float curPaddlePos[3];
00122     int             i;
00123     double          err;
00124     double                      angle;
00125 
00126         err=0.;
00127     /* grab a video frame */
00128     if( (dataPtr = (ARUint8 *)arVideoGetImage()) == NULL ) {
00129         arUtilSleep(2); 
00130         return;
00131     }
00132         
00133     if( count == 0 ) arUtilTimerReset();  
00134     count++;
00135    
00136         /* detect the markers in the video frame */
00137     if( arDetectMarkerLite(dataPtr, thresh, &marker_info, &marker_num) < 0 ) {
00138         cleanup();
00139         exit(0);
00140     }
00141 
00142     argDrawMode2D();
00143     if( !arDebug ) {
00144         argDispImage( dataPtr, 0,0 );
00145     }
00146     else {
00147         argDispImage( dataPtr, 1, 1 );
00148         if( arImageProcMode == AR_IMAGE_PROC_IN_HALF )
00149             argDispHalfImage( arImage, 0, 0 );
00150         else
00151             argDispImage( arImage, 0, 0);
00152 
00153         glColor3f( 1.0, 0.0, 0.0 );
00154         glLineWidth( 1.0 );
00155         for( i = 0; i < marker_num; i++ ) {
00156             argDrawSquare( marker_info[i].vertex, 0, 0 );
00157         }
00158         glLineWidth( 1.0 );
00159     }
00160 
00161     arVideoCapNext();
00162 
00163         for( i = 0; i < marker_num; i++ ) 
00164                 marker_flag[i] = 0;
00165   
00166         /* get the paddle position */
00167         paddleGetTrans(paddleInfo, marker_info, marker_flag, 
00168                                 marker_num, &cparam);
00169         /* draw the 3D models */
00170         glClearDepth( 1.0 );
00171         glClear(GL_DEPTH_BUFFER_BIT);
00172 
00173 
00174         /* get the translation from the multimarker pattern */
00175         if( (err=arMultiGetTransMat(marker_info, marker_num, config)) < 0 ) {
00176         argSwapBuffers();
00177         return;
00178     }   
00179         
00180         //    printf("err = %f\n", err);
00181     if(err > 100.0 ) {
00182         argSwapBuffers();
00183         return;
00184     }
00185         
00186         //draw a red ground grid
00187         drawGroundGrid( config->trans, 15, 150.0, 110.0, 0.0);
00188 
00189         /* find the paddle position relative to the base */
00190         if (paddleInfo->active)
00191                 findPaddlePosition(curPaddlePos,paddleInfo->trans,config->trans);
00192 
00193         /* checking for paddle gesture */
00194         if( paddleInfo->active) 
00195           {
00196             int findItem=-1;
00197             if (myPaddleItem.item!=-1)
00198               {
00199 
00200                   if( check_incline(paddleInfo->trans, config->trans, &angle) ) {
00201                       myPaddleItem.x += 2.0 * cos(angle);
00202                       myPaddleItem.y += 2.0 * sin(angle);
00203                       if( myPaddleItem.x*myPaddleItem.x + 
00204                           myPaddleItem.y*myPaddleItem.y > 900.0 ) {
00205                           myPaddleItem.x -= 2.0 * cos(angle);
00206                           myPaddleItem.y -= 2.0 * sin(angle);
00207                           myListItem.item[myPaddleItem.item].onpaddle=0;                     
00208                           myListItem.item[myPaddleItem.item].pos[0]=curPaddlePos[0]; 
00209                           myListItem.item[myPaddleItem.item].pos[1]=curPaddlePos[1];  
00210                           myPaddleItem.item = -1;
00211                         }
00212                   }
00213               }
00214             else
00215               {
00216                 if ((findItem=check_pickup(paddleInfo->trans, config->trans,&myListItem, &angle))!=-1)  {
00217                     
00218                     myPaddleItem.item=findItem;
00219                     myPaddleItem.x =0.0;
00220                     myPaddleItem.y =0.0;
00221                     myPaddleItem.angle = 0.0;
00222                     myListItem.item[myPaddleItem.item].onpaddle=1;
00223                   }
00224               }
00225           }
00226 
00227         /* draw the item */
00228         drawItems(config->trans,&myListItem);
00229 
00230         /* draw the paddle */
00231         if( paddleInfo->active ){ 
00232           draw_paddle(paddleInfo,&myPaddleItem);
00233         }
00234         
00235         argSwapBuffers();
00236 }
00237 
00238 static void init( void )
00239 {
00240   ARParam  wparam;
00241         
00242     /* open the video path */
00243     if( arVideoOpen( vconf ) < 0 ) exit(0);
00244     /* find the size of the window */
00245     if( arVideoInqSize(&xsize, &ysize) < 0 ) exit(0);
00246     printf("Image size (x,y) = (%d,%d)\n", xsize, ysize);
00247 
00248     /* set the initial camera parameters */
00249     if( arParamLoad(cparam_name, 1, &wparam) < 0 ) {
00250         printf("Camera parameter load error !!\n");
00251         exit(0);
00252     }
00253     arParamChangeSize( &wparam, xsize, ysize, &cparam );
00254     arInitCparam( &cparam );
00255     printf("*** Camera Parameter ***\n");
00256     arParamDisp( &cparam );
00257 
00258         /* load the paddle marker file */
00259         if( (paddleInfo = paddleInit(paddle_name)) == NULL ) {
00260                 printf("paddleInit error!!\n");
00261                 exit(0);
00262         }
00263         printf("Loaded Paddle File\n");
00264 
00265         if( (config = arMultiReadConfigFile(config_name)) == NULL ) {
00266         printf("config data load error !!\n");
00267         exit(0);
00268     }
00269         printf("Loaded Multi Marker File\n");
00270 
00271         /* init items */
00272         myListItem.itemnum=4;
00273         myListItem.item[0].pos[0]=0.;myListItem.item[0].pos[1]=0.;myListItem.item[0].onpaddle=0;
00274         myListItem.item[1].pos[0]=100.;myListItem.item[1].pos[1]=-100.;myListItem.item[1].onpaddle=0;
00275         myListItem.item[2].pos[0]=200.;myListItem.item[2].pos[1]=0.;myListItem.item[2].onpaddle=0;
00276         myListItem.item[3].pos[0]=0.;myListItem.item[3].pos[1]=0.;myListItem.item[3].onpaddle=1;        
00277 
00278         /* set up the initial paddle contents */
00279         myPaddleItem.item = 3;
00280         myPaddleItem.angle = 0.0;
00281         myPaddleItem.x = 0.0;
00282         myPaddleItem.y = 0.0;
00283 
00284     /* open the graphics window */
00285         argInit( &cparam, 1.0, 0, 0, 0, 0 );
00286 }
00287 
00288 /* cleanup function called when program exits */
00289 static void cleanup(void)
00290 {
00291   arVideoCapStop();
00292   arVideoClose();
00293   argCleanup();
00294 }
00295 
00296 /* find the position of the paddle card relative to the base and set the dropped blob position to this */
00297 static void       findPaddlePosition(float curPaddlePos[], double card_trans[3][4],double base_trans[3][4])
00298 {
00299         int i,j;
00300 
00301         ARMat   *mat_a,  *mat_b, *mat_c;
00302     double  x, y, z;
00303 
00304         //get card position relative to base pattern 
00305     mat_a = arMatrixAlloc( 4, 4 );
00306     mat_b = arMatrixAlloc( 4, 4 );
00307     mat_c = arMatrixAlloc( 4, 4 );
00308     for( j = 0; j < 3; j++ ) {
00309         for( i = 0; i < 4; i++ ) {
00310             mat_b->m[j*4+i] = base_trans[j][i];
00311         }
00312     }
00313     mat_b->m[3*4+0] = 0.0;
00314     mat_b->m[3*4+1] = 0.0;
00315     mat_b->m[3*4+2] = 0.0;
00316     mat_b->m[3*4+3] = 1.0;
00317     for( j = 0; j < 3; j++ ) {
00318         for( i = 0; i < 4; i++ ) {
00319             mat_a->m[j*4+i] = card_trans[j][i];
00320         }
00321     }
00322     mat_a->m[3*4+0] = 0.0;
00323     mat_a->m[3*4+1] = 0.0;
00324     mat_a->m[3*4+2] = 0.0;
00325     mat_a->m[3*4+3] = 1.0;
00326     arMatrixSelfInv( mat_b );
00327     arMatrixMul( mat_c, mat_b, mat_a );
00328 
00329         //x,y,z is card position relative to base pattern
00330     x = mat_c->m[0*4+3];
00331     y = mat_c->m[1*4+3];
00332     z = mat_c->m[2*4+3];
00333     
00334         curPaddlePos[0] = x;
00335         curPaddlePos[1] = y;
00336         curPaddlePos[2] = z;
00337 
00338         //      printf("Position: %3.2f %3.2f %3.2f\n",x,y,z);
00339 
00340         arMatrixFree( mat_a );
00341     arMatrixFree( mat_b );
00342     arMatrixFree( mat_c );
00343         
00344 }
00345 
00346 
00347 /* draw the paddle */
00348 int  draw_paddle( ARPaddleInfo *paddleInfo, PaddleItemInfo *paddleItemInfo )
00349 {
00350     double  gl_para[16];
00351     int     i;
00352 
00353     argDrawMode3D();
00354     glEnable(GL_DEPTH_TEST);
00355     glDepthFunc(GL_LEQUAL);
00356     
00357     argDraw3dCamera( 0, 0 );
00358     argConvGlpara(paddleInfo->trans, gl_para);
00359       
00360     glMatrixMode(GL_MODELVIEW);
00361     glLoadMatrixd( gl_para );
00362 
00363     glColor3f( 1.0, 0.0, 0.0 );
00364     glLineWidth(4.0);
00365     glBegin(GL_LINE_LOOP);
00366         glVertex2f( -25.0, -25.0 );
00367         glVertex2f(  25.0, -25.0 );
00368         glVertex2f(  25.0,  25.0 );
00369         glVertex2f( -25.0,  25.0 );
00370     glEnd();
00371 
00372     glColor3f( 0.0, 0.0, 1.0);
00373     glBegin(GL_LINE_LOOP);
00374     for( i = 0; i < 16; i++ ) {
00375         double  x, y;
00376         x = PADDLE_RADIUS * cos(i*3.141592*2/16);
00377         y = PADDLE_RADIUS * sin(i*3.141592*2/16);
00378         glVertex2d( x, y );
00379     }
00380     glEnd();
00381     glBegin(GL_LINE_LOOP);
00382         glVertex2f( -7.5,    0.0 );
00383         glVertex2f(  7.5,    0.0 );
00384         glVertex2f(  7.5, -105.0 );
00385         glVertex2f( -7.5, -105.0 );
00386     glEnd();
00387 
00388     glEnable(GL_BLEND);
00389     glBlendFunc(GL_ZERO,GL_ONE);
00390     
00391     glColor4f(1,1,1,0);    
00392         glBegin(GL_POLYGON);
00393     for( i = 0; i < 16; i++ ) {
00394         double  x, y;
00395         x = 40.0 * cos(i*3.141592*2/16);
00396         y = 40.0 * sin(i*3.141592*2/16);
00397         glVertex2d( x, y );    
00398         }
00399     glEnd();
00400     glBegin(GL_POLYGON);
00401         glVertex2f( -7.5,    0.0 );
00402         glVertex2f(  7.5,    0.0 );
00403         glVertex2f(  7.5, -105.0 );
00404         glVertex2f( -7.5, -105.0 );
00405     glEnd();
00406     glDisable(GL_BLEND);
00407 
00408         /* draw any objects on the paddle */
00409         if( (i=paddleItemInfo->item) !=-1) {
00410 
00411         glPushMatrix();
00412         glTranslatef( paddleItemInfo->x, paddleItemInfo->y, 10.0 );
00413         glRotatef( paddleItemInfo->angle * 180.0/3.141592, 0.0, 0.0, 1.0 );
00414         glColor3f(0.0,1.0,0.0);
00415         glutSolidSphere(10,10,10);
00416         //      glutSolidTeapot(10.);
00417         glPopMatrix();
00418         }
00419 
00420     glDisable(GL_DEPTH_TEST);
00421         argDrawMode2D();
00422     return 0;
00423 }
00424 
00425 /* draw the items on the ground */
00426 void drawItems(double trans[3][4], ItemList* itlist)
00427 { 
00428   int i;
00429   double        gl_para[16];
00430   GLfloat   light_position[]  = {100.0,-200.0,200.0,0.0};
00431   GLfloat   ambi[]            = {0.1, 0.1, 0.1, 0.1};
00432   GLfloat   lightZeroColor[]  = {0.9, 0.9, 0.9, 0.1};
00433   GLfloat   mat_ambient[]    = {0.0, 1.0, 0.0, 1.0};
00434   GLfloat   mat_flash2[]      = {0.0, 1.0, 1.0, 1.0};
00435   GLfloat   mat_flash_shiny2[]= {50.0};
00436 
00437   argDrawMode3D();
00438   argDraw3dCamera( 0, 0 );
00439   glEnable(GL_DEPTH_TEST);
00440   glDepthFunc(GL_LEQUAL);
00441   
00442   glEnable(GL_LIGHTING);
00443     glEnable(GL_LIGHT0);
00444     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
00445     glLightfv(GL_LIGHT0, GL_AMBIENT, ambi);
00446     glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
00447     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash2);
00448     glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny2);
00449     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00450 
00451     /* load the camera transformation matrix */
00452     glMatrixMode(GL_MODELVIEW);
00453     argConvGlpara(trans, gl_para);
00454     glLoadMatrixd( gl_para );
00455     for(i = 0; i < itlist->itemnum; i ++ )
00456       {
00457         if (!itlist->item[i].onpaddle)
00458           {
00459             glPushMatrix();
00460             glTranslatef(itlist->item[i].pos[0],itlist->item[i].pos[1], 10.0 );
00461              glColor3f(0.0,1.0,0.0);
00462              glutSolidSphere(10,10,10);
00463             glPopMatrix();
00464           }
00465       }
00466     glDisable( GL_LIGHTING );
00467     glDisable( GL_DEPTH_TEST );    
00468     argDrawMode2D();
00469 }
00470 
00471 /*************************************************************************************
00472 **
00473 ** drawGroundGrid - draws a ground plane
00474 **
00475 ***************************************************************************************/
00476 int drawGroundGrid( double trans[3][4], int divisions, float x, float y, float height)
00477 {
00478         double        gl_para[16];
00479     int           i;
00480         float x0,x1,y0,y1;
00481         float deltaX, deltaY;
00482 
00483         argDrawMode3D();
00484     argDraw3dCamera( 0, 0 );
00485     glEnable(GL_DEPTH_TEST);
00486     glDepthFunc(GL_LEQUAL);
00487     
00488     /* load the camera transformation matrix */
00489     glMatrixMode(GL_MODELVIEW);
00490     argConvGlpara(trans, gl_para);
00491     glLoadMatrixd( gl_para );
00492         
00493     glTranslatef(x/2.,-y/2.,0.);
00494         //draw the grid
00495     glColor3f(1,0,0);
00496         glLineWidth(6.0);
00497         glBegin(GL_LINE_LOOP);
00498         glVertex3f( -x, y, height );
00499         glVertex3f(  x, y, height );  
00500         glVertex3f(  x, -y, height );
00501         glVertex3f( -x, -y, height );
00502         glEnd();
00503         glLineWidth(3.0);
00504         
00505         //draw a grid of lines
00506         //X direction
00507         x0 = -x; x1 = -x;
00508         y0 = -y; y1 = y;
00509         deltaX = (2*x)/divisions;
00510 
00511         for(i=0;i<divisions;i++){
00512                 x0 = x0 + deltaX;
00513                 glBegin(GL_LINES);
00514                 glVertex3f(x0,y0,height);
00515                 glVertex3f(x0,y1,height);
00516                 glEnd();
00517         }
00518 
00519         x0 = -x; x1 = x;
00520         deltaY = (2*y)/divisions;
00521 
00522         for(i=0;i<divisions;i++){
00523                 y0 = y0 + deltaY;
00524                 glBegin(GL_LINES);
00525                 glVertex3f(x0,y0,height);
00526                 glVertex3f(x1,y0,height);
00527                 glEnd();
00528         }
00529 
00530         glLineWidth(1.0);
00531 
00532         glEnable(GL_LIGHTING);
00533     glEnable(GL_LIGHT0);
00534     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
00535     glLightfv(GL_LIGHT0, GL_AMBIENT, ambi);
00536     glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
00537 
00538         glDisable( GL_LIGHTING );
00539     glDisable( GL_DEPTH_TEST );    
00540     argDrawMode2D();
00541     return 0;
00542 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


ar_recog
Author(s): Graylin Trevor Jay and Christopher Crick
autogenerated on Fri Jan 25 2013 12:15:00