Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 204 | chris | 1 | #ifdef _WIN32 |
| 2 | #include <windows.h> |
||
| 3 | #endif |
||
| 4 | #include <stdio.h> |
||
| 5 | #include <stdlib.h> |
||
| 6 | #ifndef __APPLE__ |
||
| 7 | #include <GL/gl.h> |
||
| 8 | #include <GL/glut.h> |
||
| 9 | #else |
||
| 10 | #include <OpenGL/gl.h> |
||
| 11 | #include <GLUT/glut.h> |
||
| 12 | #endif |
||
| 13 | #include <AR/gsub.h> |
||
| 14 | #include <AR/video.h> |
||
| 15 | #include <AR/param.h> |
||
| 16 | #include <AR/ar.h> |
||
| 17 | |||
| 18 | /* set up the video format globals */ |
||
| 19 | |||
| 20 | #ifdef _WIN32 |
||
| 21 | char *vconf = "Data\\WDM_camera_flipV.xml"; |
||
| 22 | #else |
||
| 23 | char *vconf = ""; |
||
| 24 | #endif |
||
| 25 | |||
| 26 | int xsize, ysize; |
||
| 27 | int thresh = 100; |
||
| 28 | int count = 0; |
||
| 29 | |||
| 30 | int mode = 1; |
||
| 31 | |||
| 32 | char *cparam_name = "Data/camera_para.dat"; |
||
| 33 | ARParam cparam; |
||
| 34 | |||
| 35 | char *patt_name = "Data/patt.hiro"; |
||
| 36 | int patt_id; |
||
| 37 | int patt_width = 80.0; |
||
| 38 | double patt_center[2] = {0.0, 0.0}; |
||
| 39 | double patt_trans[3][4]; |
||
| 40 | |||
| 41 | static void init(void); |
||
| 42 | static void cleanup(void); |
||
| 43 | static void keyEvent( unsigned char key, int x, int y); |
||
| 44 | static void mainLoop(void); |
||
| 45 | static void draw( double trans[3][4] ); |
||
| 46 | |||
| 47 | int main(int argc, char **argv) |
||
| 48 | { |
||
| 49 | glutInit(&argc, argv); |
||
| 50 | init(); |
||
| 51 | |||
| 52 | arVideoCapStart(); |
||
| 53 | argMainLoop( NULL, keyEvent, mainLoop ); |
||
| 54 | return (0); |
||
| 55 | } |
||
| 56 | |||
| 57 | static void keyEvent( unsigned char key, int x, int y) |
||
| 58 | { |
||
| 59 | /* quit if the ESC key is pressed */ |
||
| 60 | if( key == 0x1b ) { |
||
| 61 | printf("*** %f (frame/sec)\n", (double)count/arUtilTimer()); |
||
| 62 | cleanup(); |
||
| 63 | exit(0); |
||
| 64 | } |
||
| 65 | |||
| 66 | if( key == 'c' ) { |
||
| 67 | printf("*** %f (frame/sec)\n", (double)count/arUtilTimer()); |
||
| 68 | count = 0; |
||
| 69 | |||
| 70 | mode = 1 - mode; |
||
| 71 | if( mode ) printf("Continuous mode: Using arGetTransMatCont.\n"); |
||
| 72 | else printf("One shot mode: Using arGetTransMat.\n"); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | /* main loop */ |
||
| 77 | static void mainLoop(void) |
||
| 78 | { |
||
| 79 | static int contF = 0; |
||
| 80 | ARUint8 *dataPtr; |
||
| 81 | ARMarkerInfo *marker_info; |
||
| 82 | int marker_num; |
||
| 83 | int j, k; |
||
| 84 | |||
| 85 | /* grab a vide frame */ |
||
| 86 | if( (dataPtr = (ARUint8 *)arVideoGetImage()) == NULL ) { |
||
| 87 | arUtilSleep(2); |
||
| 88 | return; |
||
| 89 | } |
||
| 90 | if( count == 0 ) arUtilTimerReset(); |
||
| 91 | count++; |
||
| 92 | |||
| 93 | argDrawMode2D(); |
||
| 94 | argDispImage( dataPtr, 0,0 ); |
||
| 95 | |||
| 96 | /* detect the markers in the video frame */ |
||
| 97 | if( arDetectMarker(dataPtr, thresh, &marker_info, &marker_num) < 0 ) { |
||
| 98 | cleanup(); |
||
| 99 | exit(0); |
||
| 100 | } |
||
| 101 | |||
| 102 | arVideoCapNext(); |
||
| 103 | |||
| 104 | /* check for object visibility */ |
||
| 105 | k = -1; |
||
| 106 | for( j = 0; j < marker_num; j++ ) { |
||
| 107 | if( patt_id == marker_info[j].id ) { |
||
| 108 | if( k == -1 ) k = j; |
||
| 109 | else if( marker_info[k].cf < marker_info[j].cf ) k = j; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | if( k == -1 ) { |
||
| 113 | contF = 0; |
||
| 114 | argSwapBuffers(); |
||
| 115 | return; |
||
| 116 | } |
||
| 117 | |||
| 118 | /* get the transformation between the marker and the real camera */ |
||
| 119 | if( mode == 0 || contF == 0 ) { |
||
| 120 | arGetTransMat(&marker_info[k], patt_center, patt_width, patt_trans); |
||
| 121 | } |
||
| 122 | else { |
||
| 123 | arGetTransMatCont(&marker_info[k], patt_trans, patt_center, patt_width, patt_trans); |
||
| 124 | } |
||
| 125 | contF = 1; |
||
| 126 | |||
| 127 | draw( patt_trans ); |
||
| 128 | |||
| 129 | argSwapBuffers(); |
||
| 130 | } |
||
| 131 | |||
| 132 | static void init( void ) |
||
| 133 | { |
||
| 134 | ARParam wparam; |
||
| 135 | |||
| 136 | /* open the video path */ |
||
| 137 | if( arVideoOpen( vconf ) < 0 ) exit(0); |
||
| 138 | /* find the size of the window */ |
||
| 139 | if( arVideoInqSize(&xsize, &ysize) < 0 ) exit(0); |
||
| 140 | printf("Image size (x,y) = (%d,%d)\n", xsize, ysize); |
||
| 141 | |||
| 142 | /* set the initial camera parameters */ |
||
| 143 | if( arParamLoad(cparam_name, 1, &wparam) < 0 ) { |
||
| 144 | printf("Camera parameter load error !!\n"); |
||
| 145 | exit(0); |
||
| 146 | } |
||
| 147 | arParamChangeSize( &wparam, xsize, ysize, &cparam ); |
||
| 148 | arInitCparam( &cparam ); |
||
| 149 | printf("*** Camera Parameter ***\n"); |
||
| 150 | arParamDisp( &cparam ); |
||
| 151 | |||
| 152 | if( (patt_id=arLoadPatt(patt_name)) < 0 ) { |
||
| 153 | printf("pattern load error !!\n"); |
||
| 154 | exit(0); |
||
| 155 | } |
||
| 156 | |||
| 157 | /* open the graphics window */ |
||
| 158 | argInit( &cparam, 1.0, 0, 0, 0, 0 ); |
||
| 159 | } |
||
| 160 | |||
| 161 | /* cleanup function called when program exits */ |
||
| 162 | static void cleanup(void) |
||
| 163 | { |
||
| 164 | arVideoCapStop(); |
||
| 165 | arVideoClose(); |
||
| 166 | argCleanup(); |
||
| 167 | } |
||
| 168 | |||
| 169 | static void draw( double trans[3][4] ) |
||
| 170 | { |
||
| 171 | double gl_para[16]; |
||
| 172 | GLfloat mat_ambient[] = {0.0, 0.0, 1.0, 1.0}; |
||
| 173 | GLfloat mat_flash[] = {0.0, 0.0, 1.0, 1.0}; |
||
| 174 | GLfloat mat_flash_shiny[] = {50.0}; |
||
| 175 | GLfloat light_position[] = {100.0,-200.0,200.0,0.0}; |
||
| 176 | GLfloat ambi[] = {0.1, 0.1, 0.1, 0.1}; |
||
| 177 | GLfloat lightZeroColor[] = {0.9, 0.9, 0.9, 0.1}; |
||
| 178 | |||
| 179 | argDrawMode3D(); |
||
| 180 | argDraw3dCamera( 0, 0 ); |
||
| 181 | glClearDepth( 1.0 ); |
||
| 182 | glClear(GL_DEPTH_BUFFER_BIT); |
||
| 183 | glEnable(GL_DEPTH_TEST); |
||
| 184 | glDepthFunc(GL_LEQUAL); |
||
| 185 | |||
| 186 | /* load the camera transformation matrix */ |
||
| 187 | argConvGlpara(trans, gl_para); |
||
| 188 | glMatrixMode(GL_MODELVIEW); |
||
| 189 | glLoadMatrixd( gl_para ); |
||
| 190 | |||
| 191 | glEnable(GL_LIGHTING); |
||
| 192 | glEnable(GL_LIGHT0); |
||
| 193 | glLightfv(GL_LIGHT0, GL_POSITION, light_position); |
||
| 194 | glLightfv(GL_LIGHT0, GL_AMBIENT, ambi); |
||
| 195 | glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor); |
||
| 196 | glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash); |
||
| 197 | glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny); |
||
| 198 | glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); |
||
| 199 | glMatrixMode(GL_MODELVIEW); |
||
| 200 | glTranslatef( 0.0, 0.0, 25.0 ); |
||
| 201 | glutSolidCube(50.0); |
||
| 202 | glDisable( GL_LIGHTING ); |
||
| 203 | |||
| 204 | glDisable( GL_DEPTH_TEST ); |
||
| 205 | } |