rangeTest.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 
00019 char           *patt_name      = "Data/patt.hiro";
00020 int             patt_id;
00021 
00022 int             marker_width     = 80.0;
00023 double          marker_center[2] = {0.0, 0.0};
00024 double          marker_trans[3][4];
00025 
00026 int             xsize, ysize;
00027 int                             thresh = 100;
00028 int             count = 0;
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           *cparam_name    = "Data/camera_para.dat";
00039 ARParam         cparam;
00040 
00041 static void   init(void);
00042 static void   cleanup(void);
00043 static void   keyEvent( unsigned char key, int x, int y);
00044 static void   mainLoop(void);
00045 static void   draw(double marker_trans[3][4],double range);
00046 
00047 int main(int argc, char **argv)
00048 {
00049         //initialize applications
00050         glutInit(&argc, argv);
00051     init();
00052     
00053         arVideoCapStart();
00054 
00055         //start the main event loop
00056     argMainLoop( NULL, keyEvent, mainLoop );
00057 
00058         return 0;
00059 }
00060 
00061 static void   keyEvent( unsigned char key, int x, int y)   
00062 {
00063     /* quit if the ESC key is pressed */
00064     if( key == 0x1b ) {
00065         printf("*** %f (frame/sec)\n", (double)count/arUtilTimer());
00066         cleanup();
00067         exit(0);
00068     }
00069 }
00070 
00071 /* main loop */
00072 static void mainLoop(void)
00073 {
00074     ARUint8         *dataPtr;
00075     ARMarkerInfo    *marker_info;
00076     int             marker_num;
00077     int             i,k;
00078         float                   Xpos, Ypos, Zpos;
00079         double                  range;
00080 
00081     /* grab a video frame */
00082     if( (dataPtr = (ARUint8 *)arVideoGetImage()) == NULL ) {
00083         arUtilSleep(2);
00084         return;
00085     }
00086         
00087     if( count == 0 ) arUtilTimerReset();  
00088     count++;
00089 
00090         /*draw the video*/
00091     argDrawMode2D();
00092     argDispImage( dataPtr, 0,0 );
00093 
00094         glColor3f( 1.0, 0.0, 0.0 );
00095         glLineWidth(6.0);
00096 
00097         /* detect the markers in the video frame */ 
00098         if(arDetectMarker(dataPtr, thresh, 
00099                 &marker_info, &marker_num) < 0 ) {
00100                 cleanup(); 
00101                 exit(0);
00102         }
00103 
00104         arVideoCapNext();
00105 
00106         for( i = 0; i < marker_num; i++ ) {
00107                 argDrawSquare(marker_info[i].vertex,0,0);
00108         }
00109 
00110         /* check for known patterns */
00111     k = -1;
00112     for( i = 0; i < marker_num; i++ ) {
00113         if( marker_info[i].id  == patt_id) {
00114 
00115                         /* you've found a pattern */
00116                         printf("Found pattern: %d ",patt_id);
00117                         glColor3f( 0.0, 1.0, 0.0 );
00118             argDrawSquare(marker_info[i].vertex,0,0);
00119 
00120                         if( k == -1 ) k = i;
00121             else /* make sure you have the best pattern (highest confidence factor) */
00122                                 if( marker_info[k].cf < marker_info[i].cf ) k = i;
00123         }
00124     }
00125         if( k == -1 ) {
00126         argSwapBuffers();
00127         return;
00128     }
00129 
00130     /* get the transformation between the marker and the real camera */
00131     arGetTransMat(&marker_info[k], marker_center, marker_width, marker_trans);
00132 
00133         /* find the range */
00134         Xpos = marker_trans[0][3];
00135         Ypos = marker_trans[1][3];
00136         Zpos = marker_trans[2][3];
00137         range = sqrt(Xpos*Xpos+Ypos*Ypos+Zpos*Zpos);
00138 
00139         printf(" X: %3.2f Y: %3.2f Z: %3.2f Range: %3.2f \n",Xpos,Ypos,Zpos,range);
00140 
00141         /* draw the AR graphics */
00142     draw(marker_trans,range);
00143 
00144         /*swap the graphics buffers*/
00145         argSwapBuffers();
00146 }
00147 
00148 static void init( void )
00149 {
00150         ARParam  wparam;
00151 
00152     /* open the video path */
00153     if( arVideoOpen( vconf ) < 0 ) exit(0);
00154     /* find the size of the window */
00155     if( arVideoInqSize(&xsize, &ysize) < 0 ) exit(0);
00156     printf("Image size (x,y) = (%d,%d)\n", xsize, ysize);
00157 
00158     /* set the initial camera parameters */
00159     if( arParamLoad(cparam_name, 1, &wparam) < 0 ) {
00160         printf("Camera parameter load error !!\n");
00161         exit(0);
00162     }
00163     arParamChangeSize( &wparam, xsize, ysize, &cparam );
00164     arInitCparam( &cparam );
00165     printf("*** Camera Parameter ***\n");
00166     arParamDisp( &cparam );
00167 
00168         /* load pattern file */
00169         if(patt_id=arLoadPatt (patt_name) < 0)
00170         {
00171                 printf ("Pattern file load error !! \n"); 
00172                 exit(0); 
00173         } 
00174 
00175     /* open the graphics window */
00176     argInit( &cparam, 2.0, 0, 0, 0, 0 );
00177 }
00178 
00179 /* cleanup function called when program exits */
00180 static void cleanup(void)
00181 {
00182         arVideoCapStop();
00183     arVideoClose();
00184     argCleanup();
00185 }
00186 
00187 static void draw(double marker_trans[3][4], double range)
00188 {
00189     double    gl_para[16];
00190         double    myScale;
00191 
00192     GLfloat   mat_ambient[]     = {0.0, 0.0, 1.0, 1.0};
00193     GLfloat   mat_flash[]       = {0.0, 0.0, 1.0, 1.0};
00194     GLfloat   mat_flash_shiny[] = {50.0};
00195     GLfloat   light_position[]  = {100.0,-200.0,200.0,0.0};
00196     GLfloat   ambi[]            = {0.1, 0.1, 0.1, 0.1};
00197     GLfloat   lightZeroColor[]  = {0.9, 0.9, 0.9, 0.1};
00198  
00199         /* set the openGL projection mode */
00200     argDrawMode3D();
00201     argDraw3dCamera( 0, 0 );
00202     glClearDepth( 1.0 );
00203     glClear(GL_DEPTH_BUFFER_BIT);
00204     glEnable(GL_DEPTH_TEST);
00205     glDepthFunc(GL_LEQUAL);
00206     
00207     /* load the camera transformation matrix */
00208     argConvGlpara(marker_trans, gl_para);
00209     glMatrixMode(GL_MODELVIEW);
00210     glLoadMatrixd( gl_para );
00211 
00212         /* set the material */
00213     glEnable(GL_LIGHTING);
00214     glEnable(GL_LIGHT0);
00215     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
00216     glLightfv(GL_LIGHT0, GL_AMBIENT, ambi);
00217     glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
00218     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash);
00219     glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny);      
00220     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00221         
00222         /* draw the object */   
00223         myScale = range/200.0;
00224 
00225         glScalef(myScale,myScale,myScale);
00226         glRotatef( 90, 1.0, 0.0, 0.0 );
00227         glTranslatef( 0.0, 17.5, 0.0 );
00228 
00229         glutSolidTeapot(30);
00230     
00231     glDisable( GL_LIGHTING );
00232     glDisable( GL_DEPTH_TEST );
00233 }
 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