relationTest.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 #ifndef __APPLE__
00007 #  include <GL/glut.h>
00008 #else
00009 #  include <GLUT/glut.h>
00010 #endif
00011 #include <AR/gsub.h>
00012 #include <AR/video.h>
00013 #include <AR/param.h>
00014 #include <AR/ar.h>
00015 
00016 #define  OBJ1_PATT_NAME    "Data/patt.hiro"
00017 #define  OBJ2_PATT_NAME    "Data/patt.kanji"
00018 #define  OBJ1_SIZE         80.0
00019 #define  OBJ2_SIZE         80.0
00020 
00021 #define  OBJ1_MODEL_ID      1
00022 #define  OBJ2_MODEL_ID      2
00023 
00024 typedef struct {
00025   char    *patt_name;
00026   int     patt_id;
00027   int     model_id;
00028   int     visible;
00029   double  width;
00030   double  center[2];
00031   double  trans[3][4];
00032 } OBJECT_T;
00033 
00034 OBJECT_T   object[2] = {
00035              {OBJ1_PATT_NAME, -1, OBJ1_MODEL_ID, 0, OBJ1_SIZE, {0.0,0.0}},
00036              {OBJ2_PATT_NAME, -1, OBJ2_MODEL_ID, 0, OBJ2_SIZE, {0.0,0.0}}
00037            };
00038 
00039 /* set up the video format globals */
00040 
00041 #ifdef _WIN32
00042 char                    *vconf = "Data\\WDM_camera_flipV.xml";
00043 #else
00044 char                    *vconf = "";
00045 #endif
00046 
00047 int             xsize, ysize;
00048 int             thresh = 100;
00049 int             count = 0;
00050 
00051 char           *cparam_name    = "Data/camera_para.dat";
00052 ARParam         cparam;
00053 
00054 static void   init(void);
00055 static void   cleanup(void);
00056 static void   keyEvent( unsigned char key, int x, int y);
00057 static void   mainLoop(void);
00058 static void   draw( int object, double trans[3][4] );
00059 
00060 
00061 int main(int argc, char *argv[])
00062 {
00063         glutInit(&argc, argv);
00064     init();
00065 
00066     arVideoCapStart();
00067     argMainLoop( NULL, keyEvent, mainLoop );
00068         return (0);
00069 }
00070 
00071 static void   keyEvent( unsigned char key, int x, int y)
00072 {
00073     /* quit if the ESC key is pressed */
00074     if( key == 0x1b ) {
00075         printf("*** %f (frame/sec)\n", (double)count/arUtilTimer());
00076         cleanup();
00077         exit(0);
00078     }
00079 }
00080 
00081 /* main loop */
00082 static void mainLoop(void)
00083 {
00084     ARUint8         *dataPtr;
00085     ARMarkerInfo    *marker_info;
00086     int             marker_num;
00087     int             i, j, k;
00088 
00089     /* grab a vide frame */
00090     if( (dataPtr = (ARUint8 *)arVideoGetImage()) == NULL ) {
00091         arUtilSleep(2);
00092         return;
00093     }
00094     if( count == 0 ) arUtilTimerReset();
00095     count++;
00096 
00097     argDrawMode2D();
00098     argDispImage( dataPtr, 0,0 );
00099 
00100     /* detect the markers in the video frame */
00101     if( arDetectMarker(dataPtr, thresh, &marker_info, &marker_num) < 0 ) {
00102         cleanup();
00103         exit(0);
00104     }
00105     arVideoCapNext();
00106 
00107     argDrawMode3D();
00108     argDraw3dCamera( 0, 0 );
00109     glClearDepth( 1.0 );
00110     glClear(GL_DEPTH_BUFFER_BIT);
00111 
00112     /* check for object visibility */
00113     for( i = 0; i < 2; i++ ) {
00114         k = -1;
00115         for( j = 0; j < marker_num; j++ ) {
00116             if( object[i].patt_id == marker_info[j].id ) {
00117                 if( k == -1 ) k = j;
00118                 else if( marker_info[k].cf < marker_info[j].cf ) k = j;
00119             }
00120         }
00121         object[i].visible = k;
00122 
00123         if( k >= 0 ) {
00124             arGetTransMat(&marker_info[k],
00125                           object[i].center, object[i].width,
00126                           object[i].trans);
00127             draw( object[i].model_id, object[i].trans );
00128         }
00129     }
00130     argSwapBuffers();
00131 
00132     if( object[0].visible >= 0
00133      && object[1].visible >= 0 ) {
00134         double  wmat1[3][4], wmat2[3][4];
00135 
00136         arUtilMatInv(object[0].trans, wmat1);
00137         arUtilMatMul(wmat1, object[1].trans, wmat2);
00138 
00139         for( j = 0; j < 3; j++ ) {
00140             for( i = 0; i < 4; i++ ) printf("%8.4f ", wmat2[j][i]);
00141             printf("\n");
00142         }
00143         printf("\n\n");
00144     }
00145 }
00146 
00147 static void init( void )
00148 {
00149     ARParam  wparam;
00150     int      i;
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     for( i = 0; i < 2; i++ ) {
00169         if( (object[i].patt_id=arLoadPatt(object[i].patt_name)) < 0 ) {
00170             printf("pattern load error: %s\n", object[i].patt_name);
00171             exit(0);
00172         }
00173     }
00174 
00175     /* open the graphics window */
00176     argInit( &cparam, 1.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( int object, double trans[3][4] )
00188 {
00189     double    gl_para[16];
00190     GLfloat   mat_ambient[]     = {0.0, 0.0, 1.0, 1.0};
00191     GLfloat   mat_flash[]       = {0.0, 0.0, 1.0, 1.0};
00192     GLfloat   mat_flash_shiny[] = {50.0};
00193     GLfloat   light_position[]  = {100.0,-200.0,200.0,0.0};
00194     GLfloat   ambi[]            = {0.1, 0.1, 0.1, 0.1};
00195     GLfloat   lightZeroColor[]  = {0.9, 0.9, 0.9, 0.1};
00196     
00197     argDrawMode3D();
00198     argDraw3dCamera( 0, 0 );
00199     glEnable(GL_DEPTH_TEST);
00200     glDepthFunc(GL_LEQUAL);
00201     
00202     /* load the camera transformation matrix */
00203     argConvGlpara(trans, gl_para);
00204     glMatrixMode(GL_MODELVIEW);
00205     glLoadMatrixd( gl_para );
00206 
00207     glEnable(GL_LIGHTING);
00208     glEnable(GL_LIGHT0);
00209     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
00210     glLightfv(GL_LIGHT0, GL_AMBIENT, ambi);
00211     glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
00212     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash);
00213     glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny);      
00214     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00215     glMatrixMode(GL_MODELVIEW);
00216 
00217     switch( object ) {
00218       case 0:
00219         glTranslatef( 0.0, 0.0, 25.0 );
00220         glutSolidCube(50.0);
00221         break;
00222       case 1:
00223         glTranslatef( 0.0, 0.0, 40.0 );
00224         glutSolidSphere(40.0, 24, 24);
00225         break;
00226       case 2:
00227         glutSolidCone(25.0, 100.0, 20, 24);
00228         break;
00229       default:
00230         glTranslatef( 0.0, 0.0, 10.0 );
00231         glutSolidTorus(10.0, 40.0, 24, 24);
00232         break;
00233     }
00234 
00235     glDisable( GL_LIGHTING );
00236     glDisable( GL_DEPTH_TEST );
00237 }
 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