paddleTest.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/param.h>
00016 #include <AR/ar.h>
00017 #include <AR/video.h>
00018 #include <AR/arMulti.h>
00019 
00020 #include "paddle.h"
00021 int  draw_paddle( ARPaddleInfo *paddleInfo );
00022 
00023 int             xsize, ysize;
00024 int             thresh = 100;
00025 int             count = 0;
00026 
00027 char           *cparam_name    = "Data/camera_para.dat";
00028 ARParam         cparam;
00029 
00030 /* set up the video format globals */
00031 
00032 #ifdef _WIN32
00033 char                    *vconf = "Data\\WDM_camera_flipV.xml";
00034 #else
00035 char                    *vconf = "";
00036 #endif
00037 
00038 char                *config_name = "Data/multi/marker.dat";
00039 ARMultiMarkerInfoT  *config;
00040 
00041 //MB - paddle information 
00042 int              marker_flag[AR_SQUARE_MAX];
00043 ARPaddleInfo   *paddleInfo;
00044 char           *paddle_name    = "Data/paddle_data";  
00045 
00046 static void   init(void);
00047 static void   cleanup(void);
00048 static void   keyEvent( unsigned char key, int x, int y);
00049 static void   mainLoop(void);
00050 static void   draw( double trans1[3][4], double trans2[3][4], int mode );
00051 
00052 int main(int argc, char **argv)
00053 {
00054         //initialize application
00055         glutInit(&argc, argv);
00056     init();
00057 
00058         arVideoCapStart();
00059 
00060         //start the main event loop
00061     argMainLoop( NULL, keyEvent, mainLoop );
00062 
00063         return 0;
00064 }
00065 
00066 static void   keyEvent( unsigned char key, int x, int y)   
00067 {
00068     /* quit if the ESC key is pressed */
00069     if( key == 0x1b ) {
00070         printf("*** %f (frame/sec)\n", (double)count/arUtilTimer());
00071         cleanup();
00072         exit(0);
00073     }
00074 
00075         /* turn on and off the debug mode with d key */
00076     if( key == 'd' ) {
00077         printf("*** %f (frame/sec)\n", (double)count/arUtilTimer());
00078         arDebug = 1 - arDebug;
00079         if( arDebug == 0 ) {
00080             glClearColor( 0.0, 0.0, 0.0, 0.0 );
00081             glClear(GL_COLOR_BUFFER_BIT);
00082             argSwapBuffers();
00083             glClear(GL_COLOR_BUFFER_BIT);
00084             argSwapBuffers();
00085         }
00086         count = 0;
00087     }
00088 }
00089 
00090 /* main loop */
00091 static void mainLoop(void)
00092 {
00093     ARUint8         *dataPtr;
00094     ARMarkerInfo    *marker_info;
00095     int             marker_num;
00096     int             i;
00097     double          err;
00098     
00099     /* grab a video frame */
00100     if( (dataPtr = (ARUint8 *)arVideoGetImage()) == NULL ) {
00101         arUtilSleep(2);
00102         return;
00103     }
00104         
00105     if( count == 0 ) arUtilTimerReset();  
00106     count++;
00107    
00108         /* detect the markers in the video frame */
00109     if( arDetectMarkerLite(dataPtr, thresh, &marker_info, &marker_num) < 0 ) {
00110         cleanup();
00111         exit(0);
00112     }
00113 
00114     argDrawMode2D();
00115     if( !arDebug ) {
00116         argDispImage( dataPtr, 0,0 );
00117     }
00118     else {
00119         argDispImage( dataPtr, 1, 1 );
00120         if( arImageProcMode == AR_IMAGE_PROC_IN_HALF )
00121             argDispHalfImage( arImage, 0, 0 );
00122         else
00123             argDispImage( arImage, 0, 0);
00124 
00125         glColor3f( 1.0, 0.0, 0.0 );
00126         glLineWidth( 1.0 );
00127         for( i = 0; i < marker_num; i++ ) {
00128             argDrawSquare( marker_info[i].vertex, 0, 0 );
00129         }
00130         glLineWidth( 1.0 );
00131     }
00132 
00133         arVideoCapNext();
00134 
00135         //MB - multiple marker tracking
00136         for( i = 0; i < marker_num; i++ ) marker_flag[i] = 0;
00137   
00138         /* get the paddle position */
00139         paddleGetTrans(paddleInfo, marker_info, marker_flag, 
00140                                 marker_num, &cparam);
00141         
00142         /* draw the 3D models */
00143         glClearDepth( 1.0 );
00144     glClear(GL_DEPTH_BUFFER_BIT);
00145 
00146         /* draw the paddle */
00147         if( paddleInfo->active ){ 
00148                 draw_paddle( paddleInfo);
00149         }
00150 
00151         /* get the translation from the multimarker pattern */
00152         if( (err=arMultiGetTransMat(marker_info, marker_num, config)) < 0 ) {
00153         argSwapBuffers();
00154         return;
00155     }   
00156         
00157     //printf("err = %f\n", err);
00158     if(err > 100.0 ) {
00159         argSwapBuffers();
00160         return;
00161     }
00162 
00163         /* draw the multimarker pattern */
00164     for( i = 0; i < config->marker_num; i++ ) {
00165         if( config->marker[i].visible >= 0 ) 
00166                                 draw( config->trans, config->marker[i].trans, 0 );
00167         else                                 
00168                                 draw( config->trans, config->marker[i].trans, 1 );
00169     }
00170 
00171         argSwapBuffers();
00172 }
00173 
00174 static void init( void )
00175 {
00176         ARParam  wparam;
00177         
00178     /* open the video path */
00179     if( arVideoOpen( vconf ) < 0 ) exit(0);
00180     /* find the size of the window */
00181     if( arVideoInqSize(&xsize, &ysize) < 0 ) exit(0);
00182     printf("Image size (x,y) = (%d,%d)\n", xsize, ysize);
00183 
00184     /* set the initial camera parameters */
00185     if( arParamLoad(cparam_name, 1, &wparam) < 0 ) {
00186         printf("Camera parameter load error !!\n");
00187         exit(0);
00188     }
00189     arParamChangeSize( &wparam, xsize, ysize, &cparam );
00190     arInitCparam( &cparam );
00191     printf("*** Camera Parameter ***\n");
00192     arParamDisp( &cparam );
00193 
00194         /* load the paddle marker file */
00195         if( (paddleInfo = paddleInit(paddle_name)) == NULL ) {
00196                 printf("paddleInit error!!\n");
00197                 exit(0);
00198         }
00199         printf("Loaded Paddle File\n");
00200 
00201         if( (config = arMultiReadConfigFile(config_name)) == NULL ) {
00202         printf("config data load error !!\n");
00203         exit(0);
00204     }
00205         printf("Loaded Multi Marker File\n");
00206 
00207     /* open the graphics window */
00208         //   argInit( &cparam, 2.0, 0, 2, 1, 0 );
00209 argInit( &cparam, 1.0, 0, 0, 0, 0 );
00210 
00211 }
00212 
00213 /* cleanup function called when program exits */
00214 static void cleanup(void)
00215 {
00216         arVideoCapStop();
00217     arVideoClose();
00218     argCleanup();
00219 }
00220 
00221 static void draw( double trans1[3][4], double trans2[3][4], int mode )
00222 {
00223     double    gl_para[16];
00224     GLfloat   mat_ambient[]     = {0.0, 0.0, 1.0, 1.0};
00225     GLfloat   mat_ambient1[]    = {1.0, 0.0, 0.0, 1.0};
00226     GLfloat   mat_flash[]       = {0.0, 0.0, 1.0, 1.0};
00227     GLfloat   mat_flash1[]      = {1.0, 0.0, 0.0, 1.0};
00228     GLfloat   mat_flash_shiny[] = {50.0};
00229     GLfloat   mat_flash_shiny1[]= {50.0};
00230     GLfloat   light_position[]  = {100.0,-200.0,200.0,0.0};
00231     GLfloat   ambi[]            = {0.1, 0.1, 0.1, 0.1};
00232     GLfloat   lightZeroColor[]  = {0.9, 0.9, 0.9, 0.1};
00233     
00234     argDrawMode3D();
00235     argDraw3dCamera( 0, 0 );
00236     glEnable(GL_DEPTH_TEST);
00237     glDepthFunc(GL_LEQUAL);
00238     
00239     /* load the camera transformation matrix */
00240     glMatrixMode(GL_MODELVIEW);
00241     argConvGlpara(trans1, gl_para);
00242     glLoadMatrixd( gl_para );
00243     argConvGlpara(trans2, gl_para);
00244     glMultMatrixd( gl_para );
00245 
00246     if( mode == 0 ) {
00247         glEnable(GL_LIGHTING);
00248         glEnable(GL_LIGHT0);
00249         glLightfv(GL_LIGHT0, GL_POSITION, light_position);
00250         glLightfv(GL_LIGHT0, GL_AMBIENT, ambi);
00251         glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
00252         glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash);
00253         glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny);  
00254         glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00255     }
00256     else {
00257         glEnable(GL_LIGHTING);
00258         glEnable(GL_LIGHT0);
00259         glLightfv(GL_LIGHT0, GL_POSITION, light_position);
00260         glLightfv(GL_LIGHT0, GL_AMBIENT, ambi);
00261         glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
00262         glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash1);
00263         glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny1); 
00264         glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient1);
00265     }
00266     glMatrixMode(GL_MODELVIEW);
00267     glTranslatef( 0.0, 0.0, 25.0 );
00268     if( !arDebug ) glutSolidCube(50.0);
00269      else          glutWireCube(50.0);
00270     glDisable( GL_LIGHTING );
00271     glDisable( GL_DEPTH_TEST );    
00272     argDrawMode2D();
00273 }
00274 
00275 /* draw the paddle - using a stencil buffer */
00276 int  draw_paddle( ARPaddleInfo *paddleInfo )
00277 {
00278     double  gl_para[16];
00279     int     i;
00280 
00281     argDrawMode3D();
00282     glEnable(GL_DEPTH_TEST);
00283     glDepthFunc(GL_LEQUAL);
00284     
00285     argDraw3dCamera( 0, 0 );
00286     argConvGlpara(paddleInfo->trans, gl_para);
00287       
00288     glMatrixMode(GL_MODELVIEW);
00289     glLoadMatrixd( gl_para );
00290 
00291         /* draw the paddle graphics */
00292     glColor3f( 1.0, 0.0, 0.0 );
00293     glLineWidth(4.0);
00294     glBegin(GL_LINE_LOOP);
00295         glVertex2f( -25.0, -25.0 );
00296         glVertex2f(  25.0, -25.0 );
00297         glVertex2f(  25.0,  25.0 );
00298         glVertex2f( -25.0,  25.0 );
00299     glEnd();
00300 
00301     glColor3f( 0.0, 0.0, 1.0);
00302     glBegin(GL_LINE_LOOP);
00303     for( i = 0; i < 16; i++ ) {
00304         double  x, y;
00305         x = PADDLE_RADIUS * cos(i*3.141592*2/16);
00306         y = PADDLE_RADIUS * sin(i*3.141592*2/16);
00307         glVertex2d( x, y );
00308     }
00309     glEnd();
00310     glBegin(GL_LINE_LOOP);
00311         glVertex2f( -7.5,    0.0 );
00312         glVertex2f(  7.5,    0.0 );
00313         glVertex2f(  7.5, -105.0 );
00314         glVertex2f( -7.5, -105.0 );
00315     glEnd();
00316 
00317         /* start drawing the stencil */
00318     glEnable(GL_BLEND);
00319     glBlendFunc(GL_ZERO,GL_ONE);
00320     
00321     glColor4f(1,1,1,0);    
00322         glBegin(GL_POLYGON);
00323     for( i = 0; i < 16; i++ ) {
00324         double  x, y;
00325         x = 40.0 * cos(i*3.141592*2/16);
00326         y = 40.0 * sin(i*3.141592*2/16);
00327         glVertex2d( x, y );    
00328         }
00329     glEnd();
00330     glBegin(GL_POLYGON);
00331         glVertex2f( -7.5,    0.0 );
00332         glVertex2f(  7.5,    0.0 );
00333         glVertex2f(  7.5, -105.0 );
00334         glVertex2f( -7.5, -105.0 );
00335     glEnd();
00336     glDisable(GL_BLEND);
00337 
00338     glDisable(GL_DEPTH_TEST);
00339         argDrawMode2D();
00340     return 0;
00341 }
 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