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/gsub.h>
00015 #include <AR/video.h>
00016 #include <AR/param.h>
00017 #include <AR/ar.h>
00018 #include "draw_object.h"
00019
00020
00021
00022
00023
00024 #ifdef _WIN32
00025 char *vconf = "Data\\WDM_camera_flipV.xml";
00026 #else
00027 char *vconf = "";
00028 #endif
00029
00030 int xsize;
00031 int ysize;
00032 int thresh = 100;
00033 ARParam cparam;
00034 int outputMode = 0;
00035
00036 int mouse_ox;
00037 int mouse_oy;
00038 int mouse_st = 0;
00039 int disp_mode = 1;
00040 double a = 0.0;
00041 double b = -45.0;
00042 double r = 500.0;
00043
00044 int target_id;
00045 double target_center[2] = {0.0, 0.0};
00046 double target_width = 80.0;
00047
00048
00049
00050 static int init(void);
00051 static void cleanup(void);
00052 static void keyEvent( unsigned char key, int x, int y);
00053 static void mouseEvent(int button, int state, int x, int y);
00054 static void motionEvent( int x, int y );
00055 static void mainLoop(void);
00056
00057 static void getResultRaw( ARMarkerInfo *marker_info );
00058 static void getResultQuat( ARMarkerInfo *marker_info );
00059
00060 int main(int argc, char **argv)
00061 {
00062 glutInit(&argc, argv);
00063 if( init() < 0 ) exit(0);
00064
00065 arVideoCapStart();
00066 glutMotionFunc( motionEvent );
00067 argMainLoop( mouseEvent, keyEvent, mainLoop );
00068 return (0);
00069 }
00070
00071 static void mainLoop(void)
00072 {
00073 ARUint8 *dataPtr;
00074 ARMarkerInfo *marker_info;
00075 int marker_num;
00076 int j, k;
00077
00078
00079 if( (dataPtr = (ARUint8 *)arVideoGetImage()) == NULL ) {
00080 arUtilSleep(2);
00081 return;
00082 }
00083
00084 glClearColor( 0.0, 0.0, 0.0, 0.0 );
00085 glClearDepth( 1.0 );
00086 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00087 argDrawMode2D();
00088 if( disp_mode ) {
00089 argDispImage( dataPtr, 0, 0 );
00090 }
00091 else {
00092 argDispImage( dataPtr, 1, 1 );
00093 }
00094
00095
00096 if( arDetectMarker(dataPtr, thresh, &marker_info, &marker_num) < 0 ) {
00097 cleanup();
00098 exit(0);
00099 }
00100 arVideoCapNext();
00101
00102
00103
00104 if( arDebug ) {
00105 if( arImageProcMode == AR_IMAGE_PROC_IN_HALF )
00106 argDispHalfImage( arImage, 2, 1 );
00107 else
00108 argDispImage( arImage, 2, 1);
00109 }
00110
00111
00112 k = -1;
00113 for( j = 0; j < marker_num; j++ ) {
00114 if( marker_info[j].id == target_id ) {
00115 if( k == -1 ) k = j;
00116 else {
00117 if( marker_info[k].cf < marker_info[j].cf ) k = j;
00118 }
00119 }
00120 }
00121 if( k != -1 ) {
00122 glDisable(GL_DEPTH_TEST);
00123 switch( outputMode ) {
00124 case 0:
00125 getResultRaw( &marker_info[k] );
00126 break;
00127 case 1:
00128 getResultQuat( &marker_info[k] );
00129 break;
00130 }
00131 }
00132
00133 argSwapBuffers();
00134 }
00135
00136 static void getResultRaw( ARMarkerInfo *marker_info )
00137 {
00138 double target_trans[3][4];
00139 double cam_trans[3][4];
00140 char string[256];
00141
00142 if( arGetTransMat(marker_info, target_center, target_width, target_trans) < 0 ) return;
00143 if( arUtilMatInv(target_trans, cam_trans) < 0 ) return;
00144
00145 sprintf(string," RAW: Cam Pos x: %3.1f y: %3.1f z: %3.1f",
00146 cam_trans[0][3], cam_trans[1][3], cam_trans[2][3]);
00147
00148 if( disp_mode ) {
00149 draw( "target", target_trans, 0, 0 );
00150 draw_exview( a, b, r, target_trans, 1, 1 );
00151 }
00152 else {
00153 draw( "target", target_trans, 1, 1 );
00154 draw_exview( a, b, r, target_trans, 0, 0 );
00155 }
00156 print_string( string );
00157
00158 return;
00159 }
00160
00161 static void getResultQuat( ARMarkerInfo *marker_info )
00162 {
00163 double target_trans[3][4];
00164 double cam_trans[3][4];
00165 double quat[4], pos[3];
00166 char string1[256];
00167 char string2[256];
00168
00169 if( arGetTransMat(marker_info, target_center, target_width, target_trans) < 0 ) return;
00170 if( arUtilMatInv(target_trans, cam_trans) < 0 ) return;
00171 if( arUtilMat2QuatPos(cam_trans, quat, pos) < 0 ) return;
00172
00173 sprintf(string1," QUAT: Pos x: %3.1f y: %3.1f z: %3.1f\n",
00174 pos[0], pos[1], pos[2]);
00175 sprintf(string2, " Quat qx: %3.2f qy: %3.2f qz: %3.2f qw: %3.2f ",
00176 quat[0], quat[1], quat[2], quat[3]);
00177 strcat( string1, string2 );
00178
00179 if( disp_mode ) {
00180 draw( "target", target_trans, 0, 0 );
00181 draw_exview( a, b, r, target_trans, 1, 1 );
00182 }
00183 else {
00184 draw( "target", target_trans, 1, 1 );
00185 draw_exview( a, b, r, target_trans, 0, 0 );
00186 }
00187 print_string( string1 );
00188
00189 return;
00190 }
00191
00192
00193 static int init(void)
00194 {
00195 char cparaname[256];
00196 char pattname[256];
00197 ARParam wparam;
00198
00199 strcpy( cparaname, "Data/camera_para.dat" );
00200 strcpy( pattname, "Data/patt.hiro" );
00201
00202
00203 if( arVideoOpen( vconf ) < 0 ) exit(0);
00204
00205 if( arVideoInqSize(&xsize, &ysize) < 0 ) exit(0);
00206 printf("Image size (x,y) = (%d,%d)\n", xsize, ysize);
00207
00208
00209 if( arParamLoad(cparaname, 1, &wparam) < 0 ) {
00210 printf("Camera parameter load error !!\n");
00211 exit(0);
00212 }
00213 arParamChangeSize( &wparam, xsize, ysize, &cparam );
00214 arInitCparam( &cparam );
00215 printf("*** Camera Parameter ***\n");
00216 arParamDisp( &cparam );
00217
00218
00219 argInit( &cparam, 1.0, 0, 2, 1, 0 );
00220
00221 if( (target_id = arLoadPatt(pattname)) < 0 ) {
00222 printf("Target pattern load error!!\n");
00223 exit(0);
00224 }
00225
00226 arDebug = 0;
00227
00228 return 0;
00229 }
00230
00231
00232 static void cleanup(void)
00233 {
00234 arVideoCapStop();
00235 arVideoClose();
00236 argCleanup();
00237 }
00238
00239 static void keyEvent( unsigned char key, int x, int y)
00240 {
00241
00242 if( key == 0x1b ) {
00243 cleanup();
00244 exit(0);
00245 }
00246
00247
00248 if( key == 't' ) {
00249 printf("Enter new threshold value (default = 100): ");
00250 scanf("%d",&thresh); while( getchar()!='\n' );
00251 printf("\n");
00252 }
00253
00254
00255 if( key == 'd' ) {
00256 arDebug = 1 - arDebug;
00257 if( arDebug == 0 ) {
00258 glClearColor( 0.0, 0.0, 0.0, 0.0 );
00259 glClear(GL_COLOR_BUFFER_BIT);
00260 argSwapBuffers();
00261 glClear(GL_COLOR_BUFFER_BIT);
00262 argSwapBuffers();
00263 }
00264 }
00265
00266 if(key == 'o') {
00267 outputMode = (outputMode + 1) % 2;
00268 }
00269
00270 if(key == 'c' ) {
00271 disp_mode = 1 - disp_mode;
00272 }
00273
00274 }
00275
00276 static void motionEvent( int x, int y )
00277 {
00278 if( mouse_st == 1 ) {
00279 a += ((double)x - mouse_ox) / 2.0;
00280 b -= ((double)y - mouse_oy) / 2.0;
00281 if( a < 0.0 ) a += 360.0;
00282 if( a > 360.0 ) a -= 360.0;
00283 if( b < -90.0 ) b = -90.0;
00284 if( b > 0.0 ) b = 0.0;
00285 }
00286 else if( mouse_st == 2 ) {
00287 r *= (1.0 + ((double)y - mouse_oy)*0.01);
00288 if( r < 10.0 ) r = 10.0;
00289 }
00290
00291 mouse_ox = x;
00292 mouse_oy = y;
00293 }
00294
00295 static void mouseEvent(int button, int state, int x, int y)
00296 {
00297 if( state == GLUT_UP ) {
00298 mouse_st = 0;
00299 }
00300 else if( state == GLUT_DOWN ) {
00301 if( button == GLUT_LEFT_BUTTON ) {
00302 mouse_st = 1;
00303 mouse_ox = x;
00304 mouse_oy = y;
00305 }
00306 else if( button == GLUT_MIDDLE_BUTTON ) {
00307 disp_mode = 1 - disp_mode;
00308 }
00309 else if( button == GLUT_RIGHT_BUTTON ) {
00310 mouse_st = 2;
00311 mouse_ox = x;
00312 mouse_oy = y;
00313 }
00314 }
00315 }