00001 #ifdef _WIN32
00002 #include <windows.h>
00003 #endif
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <string.h>
00007 #ifndef __APPLE__
00008 #include <GL/gl.h>
00009 #include <GL/glut.h>
00010 #else
00011 #include <OpenGL/gl.h>
00012 #include <GLUT/glut.h>
00013 #endif
00014 #include <AR/gsubUtil.h>
00015 #include <AR/gsub.h>
00016 #include <AR/video.h>
00017 #include <AR/param.h>
00018 #include <AR/ar.h>
00019 #include "object.h"
00020 #include "draw_object.h"
00021
00022 #define TARGET_PATT_FILE "Data/patt.calib"
00023
00024
00025
00026 #ifdef _WIN32
00027 char *vconf = "Data\\WDM_camera_flipV.xml";
00028 #else
00029 char *vconf = "";
00030 #endif
00031
00032 int xsize;
00033 int ysize;
00034 int thresh = 100;
00035 int opticalFlag = 0;
00036 int saveFittingMode;
00037
00038
00039 ARParam cparam;
00040 int hmd_param_flag;
00041 int target_id;
00042 ObjectData_T *object;
00043 int objectnum;
00044 double object_center[2] = {0.0, 0.0};
00045
00046 static int count = 0;
00047
00048
00049 static void usage( char *com );
00050 static int init( int argc, char *argv[] );
00051 static void cleanup(void);
00052
00053 static void keyEvent( unsigned char key, int x, int y);
00054 static void mouseEvent(int button, int state, int x, int y);
00055 static void mainLoop(void);
00056 static void calibPostFunc(ARParam *lpara, ARParam *rpara);
00057
00058 static void usage( char *com )
00059 {
00060 printf("Usage: %s [options]\n", com);
00061 printf(" Options:\n");
00062 printf(" -c <camera parameter filename>\n");
00063 printf(" -o <object data filename>\n");
00064 printf("\n");
00065 exit(0);
00066 }
00067
00068 int main(int argc, char **argv)
00069 {
00070 glutInit(&argc, argv);
00071 if( init( argc, argv ) < 0 ) exit(0);
00072
00073 arVideoCapStart();
00074 argMainLoop( mouseEvent, keyEvent, mainLoop );
00075 return (0);
00076 }
00077
00078 static void keyEvent( unsigned char key, int x, int y)
00079 {
00080
00081 if( key == 0x1b ) {
00082
00083 cleanup();
00084 printf("*** %f (frame/sec)\n", (double)count/arUtilTimer());
00085 exit(0);
00086 }
00087
00088
00089 if( key == 't' ) {
00090 printf("Enter new threshold value (default = 100): ");
00091 scanf("%d",&thresh); while( getchar()!='\n' );
00092 printf("\n");
00093 count = 0;
00094 }
00095
00096
00097 if( key == 'd' ) {
00098 arDebug = 1 - arDebug;
00099 if( arDebug == 0 ) {
00100 glClearColor( 0.0, 0.0, 0.0, 0.0 );
00101 glClear(GL_COLOR_BUFFER_BIT);
00102 argSwapBuffers();
00103 glClear(GL_COLOR_BUFFER_BIT);
00104 argSwapBuffers();
00105 }
00106 }
00107 }
00108
00109
00110 static void mouseEvent(int button, int state, int x, int y)
00111 {
00112
00113
00114 if( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN ) {
00115 if( hmd_param_flag ) {
00116 opticalFlag = 1 - opticalFlag;
00117 if( opticalFlag == 0 ) {
00118 arFittingMode = saveFittingMode;
00119 }
00120 else {
00121 arFittingMode = AR_FITTING_TO_IDEAL;
00122 }
00123 }
00124 }
00125
00126
00127 if( button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN ) {
00128 argUtilCalibHMD( target_id, thresh, calibPostFunc );
00129 }
00130 }
00131
00132 static void calibPostFunc(ARParam *lpara, ARParam *rpara)
00133 {
00134 count = 0;
00135
00136 if( lpara == NULL || rpara == NULL ) {
00137 printf("Calibration error!!\n");
00138 return;
00139 }
00140
00141 hmd_param_flag = 1;
00142 opticalFlag = 1;
00143 arFittingMode = AR_FITTING_TO_IDEAL;
00144
00145 printf("*** HMD Parameter ***\n");
00146 printf("LEFT\n");
00147 arParamDisp( lpara );
00148 printf("Right\n");
00149 arParamDisp( rpara );
00150 printf("\n");
00151 }
00152
00153
00154 static void mainLoop(void)
00155 {
00156 ARUint8 *dataPtr;
00157 ARMarkerInfo *marker_info;
00158 int marker_num;
00159 int i, j, k;
00160
00161
00162 if( (dataPtr = (ARUint8 *)arVideoGetImage()) == NULL ) {
00163 arUtilSleep(2);
00164 return;
00165 }
00166 if( count == 0 ) arUtilTimerReset();
00167 count++;
00168
00169 if( opticalFlag == 0 ) {
00170 argDispImage( dataPtr, 0, 0 );
00171 if( !arDebug ) arVideoCapNext();
00172 glClearDepth( 1.0 );
00173 glClear(GL_DEPTH_BUFFER_BIT);
00174 }
00175 else {
00176 if( !arDebug ) arVideoCapNext();
00177 glClearDepth( 1.0 );
00178 glClearColor( 0.0, 0.0, 0.0, 0.0 );
00179 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00180 }
00181
00182
00183 if( arDetectMarker(dataPtr, thresh, &marker_info, &marker_num) < 0 ) {
00184 cleanup();
00185 exit(0);
00186 }
00187
00188
00189
00190 if( arDebug ) {
00191 argDispImage( dataPtr, 1, 1 );
00192 arVideoCapNext();
00193 if( arImageProcMode == AR_IMAGE_PROC_IN_HALF )
00194 argDispHalfImage( arImage, 2, 1 );
00195 else
00196 argDispImage( arImage, 2, 1);
00197
00198 glColor3f( 1.0, 0.0, 0.0 );
00199 glLineWidth( 3.0 );
00200 for( i = 0; i < marker_num; i++ ) {
00201 if( marker_info[i].id < 0 ) continue;
00202 argDrawSquare( marker_info[i].vertex, 2, 1 );
00203 }
00204 glLineWidth( 1.0 );
00205 }
00206
00207
00208 for( i = 0; i < objectnum; i++ ) {
00209 k = -1;
00210 for( j = 0; j < marker_num; j++ ) {
00211 if( object[i].id == marker_info[j].id ) {
00212 if( k == -1 ) k = j;
00213 else {
00214 if( marker_info[k].cf < marker_info[j].cf ) k = j;
00215 }
00216 }
00217 }
00218 if( k == -1 ) {
00219 object[i].visible = 0;
00220 continue;
00221 }
00222
00223
00224 if( arGetTransMat(&marker_info[k],
00225 object_center, object[i].marker_width, object[i].trans) < 0 ) {
00226 object[i].visible = 0;
00227 }
00228 else {
00229 object[i].visible = 1;
00230 }
00231 }
00232
00233
00234 draw( object, objectnum, opticalFlag );
00235
00236 argSwapBuffers();
00237 }
00238
00239
00240 static int init( int argc, char *argv[] )
00241 {
00242 char cparaname[256];
00243 char odataname[256];
00244 ARParam wparam;
00245
00246
00247
00248 int i;
00249
00250
00251
00252 strcpy( cparaname, "Data/camera_para.dat" );
00253 strcpy( odataname, "Data/object_data" );
00254
00255
00256 for( i = 1; i < argc; i++ ) {
00257 if( strcmp(argv[i],"-c") == 0 ) {
00258 if( i < argc-1 && argv[i+1][0] != '-' ) {
00259 strcpy( cparaname, argv[i+1] );
00260 i++;
00261 }
00262 else usage( argv[0] );
00263 }
00264 else if( strcmp(argv[i],"-o") == 0 ) {
00265 if( i < argc-1 && argv[i+1][0] != '-' ) {
00266 strcpy( odataname, argv[i+1] );
00267 i++;
00268 }
00269 else usage( argv[0] );
00270 }
00271 else usage( argv[0] );
00272 }
00273
00274
00275 if( (object=read_objectdata(odataname,&objectnum)) == NULL ) exit(0);
00276 if( (target_id = arLoadPatt(TARGET_PATT_FILE)) < 0 ) {
00277 printf("Target pattern load error!!\n");
00278 exit(0);
00279 }
00280
00281
00282 if( arVideoOpen( vconf ) < 0 ) exit(0);
00283
00284 if( arVideoInqSize(&xsize, &ysize) < 0 ) exit(0);
00285 printf("Image size (x,y) = (%d,%d)\n", xsize, ysize);
00286
00287
00288 if( arParamLoad(cparaname, 1, &wparam) < 0 ) {
00289 printf("Camera parameter load error !!\n");
00290 exit(0);
00291 }
00292 arParamChangeSize( &wparam, xsize, ysize, &cparam );
00293 arInitCparam( &cparam );
00294 printf("*** Camera Parameter ***\n");
00295 arParamDisp( &cparam );
00296
00297
00298
00299
00300
00301
00302 hmd_param_flag = 0;
00303
00304
00305 argInit( &cparam, 1.0, 0, 2, 1, 1 );
00306
00307
00308 init_lights();
00309
00310 saveFittingMode = arFittingMode;
00311 arDebug = 0;
00312
00313 return 0;
00314 }
00315
00316
00317 static void cleanup(void)
00318 {
00319 arVideoCapStop();
00320 arVideoClose();
00321 argCleanup();
00322 }