00001 #ifdef _WIN32
00002 # include <windows.h>
00003 #endif
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <string.h>
00007 #include <math.h>
00008
00009 #ifndef __APPLE__
00010 # include <GL/glut.h>
00011 #else
00012 # include <GLUT/glut.h>
00013 #endif
00014 #include <AR/gsub.h>
00015 #include <AR/param.h>
00016 #include <AR/ar.h>
00017 #include <AR/video.h>
00018
00019 #include "object.h"
00020
00021
00022 #define COLLIDE_DIST 30000.0
00023
00024
00025 char *model_name = "Data/object_data2";
00026 ObjectData_T *object;
00027 int objectnum;
00028
00029 int xsize, ysize;
00030 int thresh = 100;
00031 int count = 0;
00032
00033
00034
00035 #ifdef _WIN32
00036 char *vconf = "Data\\WDM_camera_flipV.xml";
00037 #else
00038 char *vconf = "";
00039 #endif
00040
00041 char *cparam_name = "Data/camera_para.dat";
00042 ARParam cparam;
00043
00044 static void init(void);
00045 static void cleanup(void);
00046 static void keyEvent( unsigned char key, int x, int y);
00047 static void mainLoop(void);
00048 static int checkCollisions( ObjectData_T object0, ObjectData_T object1, float collide_dist);
00049 static int draw( ObjectData_T *object, int objectnum );
00050 static int draw_object( int obj_id, double gl_para[16], int collide_flag );
00051
00052 int main(int argc, char **argv)
00053 {
00054
00055 glutInit(&argc, argv);
00056 init();
00057
00058
00059 arVideoCapStart();
00060
00061
00062 argMainLoop( NULL, keyEvent, mainLoop );
00063
00064 return 0;
00065 }
00066
00067 static void keyEvent( unsigned char key, int x, int y)
00068 {
00069
00070 if( key == 0x1b ) {
00071 printf("*** %f (frame/sec)\n", (double)count/arUtilTimer());
00072 cleanup();
00073 exit(0);
00074 }
00075 }
00076
00077
00078 static void mainLoop(void)
00079 {
00080 ARUint8 *dataPtr;
00081 ARMarkerInfo *marker_info;
00082 int marker_num;
00083 int i,j,k;
00084
00085
00086 if( (dataPtr = (ARUint8 *)arVideoGetImage()) == NULL ) {
00087 arUtilSleep(2);
00088 return;
00089 }
00090
00091 if( count == 0 ) arUtilTimerReset();
00092 count++;
00093
00094
00095 argDrawMode2D();
00096 argDispImage( dataPtr, 0,0 );
00097
00098
00099 arVideoCapNext();
00100
00101 glColor3f( 1.0, 0.0, 0.0 );
00102 glLineWidth(6.0);
00103
00104
00105 if(arDetectMarker(dataPtr, thresh,
00106 &marker_info, &marker_num) < 0 ) {
00107 cleanup();
00108 exit(0);
00109 }
00110 for( i = 0; i < marker_num; i++ ) {
00111 argDrawSquare(marker_info[i].vertex,0,0);
00112 }
00113
00114
00115 for( i = 0; i < objectnum; i++ ) {
00116 k = -1;
00117 for( j = 0; j < marker_num; j++ ) {
00118 if( object[i].id == marker_info[j].id) {
00119
00120
00121
00122 glColor3f( 0.0, 1.0, 0.0 );
00123 argDrawSquare(marker_info[j].vertex,0,0);
00124
00125 if( k == -1 ) k = j;
00126 else
00127 if( marker_info[k].cf < marker_info[j].cf ) k = j;
00128 }
00129 }
00130 if( k == -1 ) {
00131 object[i].visible = 0;
00132 continue;
00133 }
00134
00135
00136 if( object[i].visible == 0 ) {
00137 arGetTransMat(&marker_info[k],
00138 object[i].marker_center, object[i].marker_width,
00139 object[i].trans);
00140 }
00141 else {
00142 arGetTransMatCont(&marker_info[k], object[i].trans,
00143 object[i].marker_center, object[i].marker_width,
00144 object[i].trans);
00145 }
00146 object[i].visible = 1;
00147 }
00148
00149
00150 if(object[0].visible && object[1].visible){
00151 if(checkCollisions(object[0],object[1],COLLIDE_DIST)){
00152 object[0].collide = 1;
00153 object[1].collide = 1;
00154 }
00155 else{
00156 object[0].collide = 0;
00157 object[1].collide = 0;
00158 }
00159 }
00160
00161
00162 draw( object, objectnum );
00163
00164
00165 argSwapBuffers();
00166 }
00167
00168
00169 static int checkCollisions( ObjectData_T object0, ObjectData_T object1, float collide_dist)
00170 {
00171 float x1,y1,z1;
00172 float x2,y2,z2;
00173 float dist;
00174
00175 x1 = object0.trans[0][3];
00176 y1 = object0.trans[1][3];
00177 z1 = object0.trans[2][3];
00178
00179 x2 = object1.trans[0][3];
00180 y2 = object1.trans[1][3];
00181 z2 = object1.trans[2][3];
00182
00183 dist = (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2);
00184
00185 printf("Dist = %3.2f\n",dist);
00186
00187 if(dist < collide_dist)
00188 return 1;
00189 else
00190 return 0;
00191 }
00192 static void init( void )
00193 {
00194 ARParam wparam;
00195
00196
00197 if( arVideoOpen( vconf ) < 0 ) exit(0);
00198
00199 if( arVideoInqSize(&xsize, &ysize) < 0 ) exit(0);
00200 printf("Image size (x,y) = (%d,%d)\n", xsize, ysize);
00201
00202
00203 if( arParamLoad(cparam_name, 1, &wparam) < 0 ) {
00204 printf("Camera parameter load error !!\n");
00205 exit(0);
00206 }
00207 arParamChangeSize( &wparam, xsize, ysize, &cparam );
00208 arInitCparam( &cparam );
00209 printf("*** Camera Parameter ***\n");
00210 arParamDisp( &cparam );
00211
00212
00213 if( (object=read_ObjData(model_name, &objectnum)) == NULL ) exit(0);
00214 printf("Objectfile num = %d\n", objectnum);
00215
00216
00217 argInit( &cparam, 2.0, 0, 0, 0, 0 );
00218 }
00219
00220
00221 static void cleanup(void)
00222 {
00223 arVideoCapStop();
00224 arVideoClose();
00225 argCleanup();
00226 }
00227
00228
00229 static int draw( ObjectData_T *object, int objectnum )
00230 {
00231 int i;
00232 double gl_para[16];
00233
00234 glClearDepth( 1.0 );
00235 glClear(GL_DEPTH_BUFFER_BIT);
00236 glEnable(GL_DEPTH_TEST);
00237 glDepthFunc(GL_LEQUAL);
00238 glEnable(GL_LIGHTING);
00239
00240
00241 for( i = 0; i < objectnum; i++ ) {
00242 if( object[i].visible == 0 ) continue;
00243 argConvGlpara(object[i].trans, gl_para);
00244 draw_object( object[i].id, gl_para, object[i].collide );
00245 }
00246
00247 glDisable( GL_LIGHTING );
00248 glDisable( GL_DEPTH_TEST );
00249
00250 return(0);
00251 }
00252
00253
00254 static int draw_object( int obj_id, double gl_para[16], int collide_flag )
00255 {
00256 GLfloat mat_ambient[] = {0.0, 0.0, 1.0, 1.0};
00257 GLfloat mat_ambient_collide[] = {1.0, 0.0, 0.0, 1.0};
00258 GLfloat mat_flash[] = {0.0, 0.0, 1.0, 1.0};
00259 GLfloat mat_flash_collide[] = {1.0, 0.0, 0.0, 1.0};
00260 GLfloat mat_flash_shiny[] = {50.0};
00261 GLfloat light_position[] = {100.0,-200.0,200.0,0.0};
00262 GLfloat ambi[] = {0.1, 0.1, 0.1, 0.1};
00263 GLfloat lightZeroColor[] = {0.9, 0.9, 0.9, 0.1};
00264
00265 argDrawMode3D();
00266 argDraw3dCamera( 0, 0 );
00267 glMatrixMode(GL_MODELVIEW);
00268 glLoadMatrixd( gl_para );
00269
00270
00271 glEnable(GL_LIGHTING);
00272 glEnable(GL_LIGHT0);
00273 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
00274 glLightfv(GL_LIGHT0, GL_AMBIENT, ambi);
00275 glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
00276
00277 glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny);
00278
00279 if(collide_flag){
00280 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash_collide);
00281 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_collide);
00282
00283 glTranslatef( 0.0, 0.0, 30.0 );
00284 glutSolidSphere(30,12,6);
00285 }
00286 else {
00287 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash);
00288 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00289
00290 glTranslatef( 0.0, 0.0, 30.0 );
00291 glutSolidCube(60);
00292 }
00293
00294 argDrawMode2D();
00295
00296 return 0;
00297 }