00001 #ifdef _WIN32
00002 #include <windows.h>
00003 #endif
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #ifndef __APPLE__
00007 #include <GL/gl.h>
00008 #include <GL/glut.h>
00009 #else
00010 #include <OpenGL/gl.h>
00011 #include <GLUT/glut.h>
00012 #endif
00013 #include <AR/gsub.h>
00014 #include <AR/video.h>
00015 #include <AR/param.h>
00016 #include <AR/ar.h>
00017
00018
00019
00020 #ifdef _WIN32
00021 char *vconf = "Data\\WDM_camera_flipV.xml";
00022 #else
00023 char *vconf = "";
00024 #endif
00025
00026 int xsize, ysize;
00027 int thresh = 100;
00028 int count = 0;
00029
00030 int mode = 1;
00031
00032 char *cparam_name = "Data/camera_para.dat";
00033 ARParam cparam;
00034
00035 char *patt_name = "Data/patt.hiro";
00036 int patt_id;
00037 int patt_width = 80.0;
00038 double patt_center[2] = {0.0, 0.0};
00039 double patt_trans[3][4];
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 trans[3][4] );
00046
00047 int main(int argc, char **argv)
00048 {
00049 glutInit(&argc, argv);
00050 init();
00051
00052 arVideoCapStart();
00053 argMainLoop( NULL, keyEvent, mainLoop );
00054 return (0);
00055 }
00056
00057 static void keyEvent( unsigned char key, int x, int y)
00058 {
00059
00060 if( key == 0x1b ) {
00061 printf("*** %f (frame/sec)\n", (double)count/arUtilTimer());
00062 cleanup();
00063 exit(0);
00064 }
00065
00066 if( key == 'c' ) {
00067 printf("*** %f (frame/sec)\n", (double)count/arUtilTimer());
00068 count = 0;
00069
00070 mode = 1 - mode;
00071 if( mode ) printf("Continuous mode: Using arGetTransMatCont.\n");
00072 else printf("One shot mode: Using arGetTransMat.\n");
00073 }
00074 }
00075
00076
00077 static void mainLoop(void)
00078 {
00079 static int contF = 0;
00080 ARUint8 *dataPtr;
00081 ARMarkerInfo *marker_info;
00082 int marker_num;
00083 int j, k;
00084
00085
00086 if( (dataPtr = (ARUint8 *)arVideoGetImage()) == NULL ) {
00087 arUtilSleep(2);
00088 return;
00089 }
00090 if( count == 0 ) arUtilTimerReset();
00091 count++;
00092
00093 argDrawMode2D();
00094 argDispImage( dataPtr, 0,0 );
00095
00096
00097 if( arDetectMarker(dataPtr, thresh, &marker_info, &marker_num) < 0 ) {
00098 cleanup();
00099 exit(0);
00100 }
00101
00102 arVideoCapNext();
00103
00104
00105 k = -1;
00106 for( j = 0; j < marker_num; j++ ) {
00107 if( patt_id == marker_info[j].id ) {
00108 if( k == -1 ) k = j;
00109 else if( marker_info[k].cf < marker_info[j].cf ) k = j;
00110 }
00111 }
00112 if( k == -1 ) {
00113 contF = 0;
00114 argSwapBuffers();
00115 return;
00116 }
00117
00118
00119 if( mode == 0 || contF == 0 ) {
00120 arGetTransMat(&marker_info[k], patt_center, patt_width, patt_trans);
00121 }
00122 else {
00123 arGetTransMatCont(&marker_info[k], patt_trans, patt_center, patt_width, patt_trans);
00124 }
00125 contF = 1;
00126
00127 draw( patt_trans );
00128
00129 argSwapBuffers();
00130 }
00131
00132 static void init( void )
00133 {
00134 ARParam wparam;
00135
00136
00137 if( arVideoOpen( vconf ) < 0 ) exit(0);
00138
00139 if( arVideoInqSize(&xsize, &ysize) < 0 ) exit(0);
00140 printf("Image size (x,y) = (%d,%d)\n", xsize, ysize);
00141
00142
00143 if( arParamLoad(cparam_name, 1, &wparam) < 0 ) {
00144 printf("Camera parameter load error !!\n");
00145 exit(0);
00146 }
00147 arParamChangeSize( &wparam, xsize, ysize, &cparam );
00148 arInitCparam( &cparam );
00149 printf("*** Camera Parameter ***\n");
00150 arParamDisp( &cparam );
00151
00152 if( (patt_id=arLoadPatt(patt_name)) < 0 ) {
00153 printf("pattern load error !!\n");
00154 exit(0);
00155 }
00156
00157
00158 argInit( &cparam, 1.0, 0, 0, 0, 0 );
00159 }
00160
00161
00162 static void cleanup(void)
00163 {
00164 arVideoCapStop();
00165 arVideoClose();
00166 argCleanup();
00167 }
00168
00169 static void draw( double trans[3][4] )
00170 {
00171 double gl_para[16];
00172 GLfloat mat_ambient[] = {0.0, 0.0, 1.0, 1.0};
00173 GLfloat mat_flash[] = {0.0, 0.0, 1.0, 1.0};
00174 GLfloat mat_flash_shiny[] = {50.0};
00175 GLfloat light_position[] = {100.0,-200.0,200.0,0.0};
00176 GLfloat ambi[] = {0.1, 0.1, 0.1, 0.1};
00177 GLfloat lightZeroColor[] = {0.9, 0.9, 0.9, 0.1};
00178
00179 argDrawMode3D();
00180 argDraw3dCamera( 0, 0 );
00181 glClearDepth( 1.0 );
00182 glClear(GL_DEPTH_BUFFER_BIT);
00183 glEnable(GL_DEPTH_TEST);
00184 glDepthFunc(GL_LEQUAL);
00185
00186
00187 argConvGlpara(trans, gl_para);
00188 glMatrixMode(GL_MODELVIEW);
00189 glLoadMatrixd( gl_para );
00190
00191 glEnable(GL_LIGHTING);
00192 glEnable(GL_LIGHT0);
00193 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
00194 glLightfv(GL_LIGHT0, GL_AMBIENT, ambi);
00195 glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
00196 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash);
00197 glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny);
00198 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00199 glMatrixMode(GL_MODELVIEW);
00200 glTranslatef( 0.0, 0.0, 25.0 );
00201 glutSolidCube(50.0);
00202 glDisable( GL_LIGHTING );
00203
00204 glDisable( GL_DEPTH_TEST );
00205 }