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