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/glut.h> |
||
| 8 | #else |
||
| 9 | # include <GLUT/glut.h> |
||
| 10 | #endif |
||
| 11 | #include <AR/gsub.h> |
||
| 12 | #include <AR/video.h> |
||
| 13 | #include <AR/param.h> |
||
| 14 | #include <AR/ar.h> |
||
| 15 | |||
| 16 | #define OBJ1_PATT_NAME "Data/patt.hiro" |
||
| 17 | #define OBJ2_PATT_NAME "Data/patt.kanji" |
||
| 18 | #define OBJ1_SIZE 80.0 |
||
| 19 | #define OBJ2_SIZE 80.0 |
||
| 20 | |||
| 21 | #define OBJ1_MODEL_ID 1 |
||
| 22 | #define OBJ2_MODEL_ID 2 |
||
| 23 | |||
| 24 | typedef struct { |
||
| 25 | char *patt_name; |
||
| 26 | int patt_id; |
||
| 27 | int model_id; |
||
| 28 | int visible; |
||
| 29 | double width; |
||
| 30 | double center[2]; |
||
| 31 | double trans[3][4]; |
||
| 32 | } OBJECT_T; |
||
| 33 | |||
| 34 | OBJECT_T object[2] = { |
||
| 35 | {OBJ1_PATT_NAME, -1, OBJ1_MODEL_ID, 0, OBJ1_SIZE, {0.0,0.0}}, |
||
| 36 | {OBJ2_PATT_NAME, -1, OBJ2_MODEL_ID, 0, OBJ2_SIZE, {0.0,0.0}} |
||
| 37 | }; |
||
| 38 | |||
| 39 | /* set up the video format globals */ |
||
| 40 | |||
| 41 | #ifdef _WIN32 |
||
| 42 | char *vconf = "Data\\WDM_camera_flipV.xml"; |
||
| 43 | #else |
||
| 44 | char *vconf = ""; |
||
| 45 | #endif |
||
| 46 | |||
| 47 | int xsize, ysize; |
||
| 48 | int thresh = 100; |
||
| 49 | int count = 0; |
||
| 50 | |||
| 51 | char *cparam_name = "Data/camera_para.dat"; |
||
| 52 | ARParam cparam; |
||
| 53 | |||
| 54 | static void init(void); |
||
| 55 | static void cleanup(void); |
||
| 56 | static void keyEvent( unsigned char key, int x, int y); |
||
| 57 | static void mainLoop(void); |
||
| 58 | static void draw( int object, double trans[3][4] ); |
||
| 59 | |||
| 60 | |||
| 61 | int main(int argc, char *argv[]) |
||
| 62 | { |
||
| 63 | glutInit(&argc, argv); |
||
| 64 | init(); |
||
| 65 | |||
| 66 | arVideoCapStart(); |
||
| 67 | argMainLoop( NULL, keyEvent, mainLoop ); |
||
| 68 | return (0); |
||
| 69 | } |
||
| 70 | |||
| 71 | static void keyEvent( unsigned char key, int x, int y) |
||
| 72 | { |
||
| 73 | /* quit if the ESC key is pressed */ |
||
| 74 | if( key == 0x1b ) { |
||
| 75 | printf("*** %f (frame/sec)\n", (double)count/arUtilTimer()); |
||
| 76 | cleanup(); |
||
| 77 | exit(0); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | /* main loop */ |
||
| 82 | static void mainLoop(void) |
||
| 83 | { |
||
| 84 | ARUint8 *dataPtr; |
||
| 85 | ARMarkerInfo *marker_info; |
||
| 86 | int marker_num; |
||
| 87 | int i, j, k; |
||
| 88 | |||
| 89 | /* grab a vide frame */ |
||
| 90 | if( (dataPtr = (ARUint8 *)arVideoGetImage()) == NULL ) { |
||
| 91 | arUtilSleep(2); |
||
| 92 | return; |
||
| 93 | } |
||
| 94 | if( count == 0 ) arUtilTimerReset(); |
||
| 95 | count++; |
||
| 96 | |||
| 97 | argDrawMode2D(); |
||
| 98 | argDispImage( dataPtr, 0,0 ); |
||
| 99 | |||
| 100 | /* detect the markers in the video frame */ |
||
| 101 | if( arDetectMarker(dataPtr, thresh, &marker_info, &marker_num) < 0 ) { |
||
| 102 | cleanup(); |
||
| 103 | exit(0); |
||
| 104 | } |
||
| 105 | arVideoCapNext(); |
||
| 106 | |||
| 107 | argDrawMode3D(); |
||
| 108 | argDraw3dCamera( 0, 0 ); |
||
| 109 | glClearDepth( 1.0 ); |
||
| 110 | glClear(GL_DEPTH_BUFFER_BIT); |
||
| 111 | |||
| 112 | /* check for object visibility */ |
||
| 113 | for( i = 0; i < 2; i++ ) { |
||
| 114 | k = -1; |
||
| 115 | for( j = 0; j < marker_num; j++ ) { |
||
| 116 | if( object[i].patt_id == marker_info[j].id ) { |
||
| 117 | if( k == -1 ) k = j; |
||
| 118 | else if( marker_info[k].cf < marker_info[j].cf ) k = j; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | object[i].visible = k; |
||
| 122 | |||
| 123 | if( k >= 0 ) { |
||
| 124 | arGetTransMat(&marker_info[k], |
||
| 125 | object[i].center, object[i].width, |
||
| 126 | object[i].trans); |
||
| 127 | draw( object[i].model_id, object[i].trans ); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | argSwapBuffers(); |
||
| 131 | |||
| 132 | if( object[0].visible >= 0 |
||
| 133 | && object[1].visible >= 0 ) { |
||
| 134 | double wmat1[3][4], wmat2[3][4]; |
||
| 135 | |||
| 136 | arUtilMatInv(object[0].trans, wmat1); |
||
| 137 | arUtilMatMul(wmat1, object[1].trans, wmat2); |
||
| 138 | |||
| 139 | for( j = 0; j < 3; j++ ) { |
||
| 140 | for( i = 0; i < 4; i++ ) printf("%8.4f ", wmat2[j][i]); |
||
| 141 | printf("\n"); |
||
| 142 | } |
||
| 143 | printf("\n\n"); |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | static void init( void ) |
||
| 148 | { |
||
| 149 | ARParam wparam; |
||
| 150 | int i; |
||
| 151 | |||
| 152 | /* open the video path */ |
||
| 153 | if( arVideoOpen( vconf ) < 0 ) exit(0); |
||
| 154 | /* find the size of the window */ |
||
| 155 | if( arVideoInqSize(&xsize, &ysize) < 0 ) exit(0); |
||
| 156 | printf("Image size (x,y) = (%d,%d)\n", xsize, ysize); |
||
| 157 | |||
| 158 | /* set the initial camera parameters */ |
||
| 159 | if( arParamLoad(cparam_name, 1, &wparam) < 0 ) { |
||
| 160 | printf("Camera parameter load error !!\n"); |
||
| 161 | exit(0); |
||
| 162 | } |
||
| 163 | arParamChangeSize( &wparam, xsize, ysize, &cparam ); |
||
| 164 | arInitCparam( &cparam ); |
||
| 165 | printf("*** Camera Parameter ***\n"); |
||
| 166 | arParamDisp( &cparam ); |
||
| 167 | |||
| 168 | for( i = 0; i < 2; i++ ) { |
||
| 169 | if( (object[i].patt_id=arLoadPatt(object[i].patt_name)) < 0 ) { |
||
| 170 | printf("pattern load error: %s\n", object[i].patt_name); |
||
| 171 | exit(0); |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | /* open the graphics window */ |
||
| 176 | argInit( &cparam, 1.0, 0, 0, 0, 0 ); |
||
| 177 | } |
||
| 178 | |||
| 179 | /* cleanup function called when program exits */ |
||
| 180 | static void cleanup(void) |
||
| 181 | { |
||
| 182 | arVideoCapStop(); |
||
| 183 | arVideoClose(); |
||
| 184 | argCleanup(); |
||
| 185 | } |
||
| 186 | |||
| 187 | static void draw( int object, double trans[3][4] ) |
||
| 188 | { |
||
| 189 | double gl_para[16]; |
||
| 190 | GLfloat mat_ambient[] = {0.0, 0.0, 1.0, 1.0}; |
||
| 191 | GLfloat mat_flash[] = {0.0, 0.0, 1.0, 1.0}; |
||
| 192 | GLfloat mat_flash_shiny[] = {50.0}; |
||
| 193 | GLfloat light_position[] = {100.0,-200.0,200.0,0.0}; |
||
| 194 | GLfloat ambi[] = {0.1, 0.1, 0.1, 0.1}; |
||
| 195 | GLfloat lightZeroColor[] = {0.9, 0.9, 0.9, 0.1}; |
||
| 196 | |||
| 197 | argDrawMode3D(); |
||
| 198 | argDraw3dCamera( 0, 0 ); |
||
| 199 | glEnable(GL_DEPTH_TEST); |
||
| 200 | glDepthFunc(GL_LEQUAL); |
||
| 201 | |||
| 202 | /* load the camera transformation matrix */ |
||
| 203 | argConvGlpara(trans, gl_para); |
||
| 204 | glMatrixMode(GL_MODELVIEW); |
||
| 205 | glLoadMatrixd( gl_para ); |
||
| 206 | |||
| 207 | glEnable(GL_LIGHTING); |
||
| 208 | glEnable(GL_LIGHT0); |
||
| 209 | glLightfv(GL_LIGHT0, GL_POSITION, light_position); |
||
| 210 | glLightfv(GL_LIGHT0, GL_AMBIENT, ambi); |
||
| 211 | glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor); |
||
| 212 | glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash); |
||
| 213 | glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny); |
||
| 214 | glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); |
||
| 215 | glMatrixMode(GL_MODELVIEW); |
||
| 216 | |||
| 217 | switch( object ) { |
||
| 218 | case 0: |
||
| 219 | glTranslatef( 0.0, 0.0, 25.0 ); |
||
| 220 | glutSolidCube(50.0); |
||
| 221 | break; |
||
| 222 | case 1: |
||
| 223 | glTranslatef( 0.0, 0.0, 40.0 ); |
||
| 224 | glutSolidSphere(40.0, 24, 24); |
||
| 225 | break; |
||
| 226 | case 2: |
||
| 227 | glutSolidCone(25.0, 100.0, 20, 24); |
||
| 228 | break; |
||
| 229 | default: |
||
| 230 | glTranslatef( 0.0, 0.0, 10.0 ); |
||
| 231 | glutSolidTorus(10.0, 40.0, 24, 24); |
||
| 232 | break; |
||
| 233 | } |
||
| 234 | |||
| 235 | glDisable( GL_LIGHTING ); |
||
| 236 | glDisable( GL_DEPTH_TEST ); |
||
| 237 | } |