From: Philip L. <phi...@us...> - 2004-11-22 03:39:14
|
Update of /cvsroot/artoolkit/artoolkit/examples/collide In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25429 Added Files: object.c object.h collideTest.dsp collideTest.c Makefile.in collideTest.vcproj Log Message: Added new examples. --- NEW FILE: object.c --- /* ** ARToolKit object parsing function ** - reads in object data from object file in Data/object_data ** ** Format: ** <obj_num> ** ** <obj_name> ** <obj_pattern filename> ** <marker_width> ** <centerX centerY> ** ** ... ** ** eg ** ** #pattern 1 ** Hiro ** Data/hiroPatt ** 80.0 ** 0.0 0.0 ** */ #include <stdio.h> #include <malloc.h> #include <stdlib.h> #include <string.h> #include <AR/ar.h> #include "object.h" static char *get_buff( char *buf, int n, FILE *fp ); ObjectData_T *read_ObjData( char *name, int *objectnum ) { FILE *fp; ObjectData_T *object; char buf[256], buf1[256]; int i; printf("Opening Data File %s\n",name); if( (fp=fopen(name, "r")) == NULL ) { printf("Can't find the file - quitting \n"); return(0); } get_buff(buf, 256, fp); if( sscanf(buf, "%d", objectnum) != 1 ) {fclose(fp); return(0);} printf("About to load %d Models\n",*objectnum); object = (ObjectData_T *)malloc( sizeof(ObjectData_T) * *objectnum ); if( object == NULL ) return(0); for( i = 0; i < *objectnum; i++ ) { object[i].visible = 0; get_buff(buf, 256, fp); if( sscanf(buf, "%s", buf1, object[i].name) != 1 ) { fclose(fp); free(object); return(0); } printf("Read in No.%d \n", i+1); get_buff(buf, 256, fp); if( sscanf(buf, "%s", buf1) != 1 ) { fclose(fp); free(object); return(0);} if( (object[i].id = arLoadPatt(buf1)) < 0 ) {fclose(fp); free(object); return(0);} get_buff(buf, 256, fp); if( sscanf(buf, "%lf", &object[i].marker_width) != 1 ) { fclose(fp); free(object); return(0); } get_buff(buf, 256, fp); if( sscanf(buf, "%lf %lf", &object[i].marker_center[0], &object[i].marker_center[1]) != 2 ) { fclose(fp); free(object); return(0); } } fclose(fp); return( object ); } static char *get_buff( char *buf, int n, FILE *fp ) { char *ret; for(;;) { ret = fgets( buf, n, fp ); if( ret == NULL ) return(NULL); if( buf[0] != '\n' && buf[0] != '#' ) return(ret); } } --- NEW FILE: collideTest.c --- #ifdef _WIN32 #include <windows.h> #endif #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <GL/gl.h> #include <GL/glut.h> #include <AR/gsub.h> #include <AR/param.h> #include <AR/ar.h> #include <AR/video.h> #include "object.h" #define COLLIDE_DIST 30000.0 /* Object Data */ char *model_name = "Data/object_data2"; ObjectData_T *object; int objectnum; int xsize, ysize; int thresh = 100; int count = 0; /* set video capture configuration */ #ifdef _WIN32 char *vconf = "flipV,showDlg"; // see video.h for a list of supported parameters #else char *vconf = ""; #endif char *cparam_name = "Data/camera_para.dat"; ARParam cparam; static void init(void); static void cleanup(void); static void keyEvent( unsigned char key, int x, int y); static void mainLoop(void); static int checkCollisions( ObjectData_T object0, ObjectData_T object1, float collide_dist); static int draw( ObjectData_T *object, int objectnum ); static int draw_object( int obj_id, double gl_para[16], int collide_flag ); int main() { //initialize applications init(); //start video capture arVideoCapStart(); //start the main event loop argMainLoop( NULL, keyEvent, mainLoop ); return 0; } static void keyEvent( unsigned char key, int x, int y) { /* quit if the ESC key is pressed */ if( key == 0x1b ) { printf("*** %f (frame/sec)\n", (double)count/arUtilTimer()); cleanup(); exit(0); } } /* main loop */ static void mainLoop(void) { ARUint8 *dataPtr; ARMarkerInfo *marker_info; int marker_num; int i,j,k; /* grab a video frame */ if( (dataPtr = (ARUint8 *)arVideoGetImage()) == NULL ) { arUtilSleep(2); return; } if( count == 0 ) arUtilTimerReset(); count++; /*draw the video*/ argDrawMode2D(); argDispImage( dataPtr, 0,0 ); /* capture the next video frame */ arVideoCapNext(); glColor3f( 1.0, 0.0, 0.0 ); glLineWidth(6.0); /* detect the markers in the video frame */ if(arDetectMarker(dataPtr, thresh, &marker_info, &marker_num) < 0 ) { cleanup(); exit(0); } for( i = 0; i < marker_num; i++ ) { argDrawSquare(marker_info[i].vertex,0,0); } /* check for known patterns */ for( i = 0; i < objectnum; i++ ) { k = -1; for( j = 0; j < marker_num; j++ ) { if( object[i].id == marker_info[j].id) { /* you've found a pattern */ //printf("Found pattern: %d ",patt_id); glColor3f( 0.0, 1.0, 0.0 ); argDrawSquare(marker_info[j].vertex,0,0); if( k == -1 ) k = j; else /* make sure you have the best pattern (highest confidence factor) */ if( marker_info[k].cf < marker_info[j].cf ) k = j; } } if( k == -1 ) { object[i].visible = 0; continue; } /* calculate the transform for each marker */ if( object[i].visible == 0 ) { arGetTransMat(&marker_info[k], object[i].marker_center, object[i].marker_width, object[i].trans); } else { arGetTransMatCont(&marker_info[k], object[i].trans, object[i].marker_center, object[i].marker_width, object[i].trans); } object[i].visible = 1; } /*check for object collisions between marker 0 and 1 */ if(object[0].visible && object[1].visible){ if(checkCollisions(object[0],object[1],COLLIDE_DIST)){ object[0].collide = 1; object[1].collide = 1; } else{ object[0].collide = 0; object[1].collide = 0; } } /* draw the AR graphics */ draw( object, objectnum ); /*swap the graphics buffers*/ argSwapBuffers(); } /* check collision between two markers */ static int checkCollisions( ObjectData_T object0, ObjectData_T object1, float collide_dist) { float x1,y1,z1; float x2,y2,z2; float dist; x1 = object0.trans[0][3]; y1 = object0.trans[1][3]; z1 = object0.trans[2][3]; x2 = object1.trans[0][3]; y2 = object1.trans[1][3]; z2 = object1.trans[2][3]; dist = (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2); printf("Dist = %3.2f\n",dist); if(dist < collide_dist) return 1; else return 0; } static void init( void ) { ARParam wparam; /* open the video path */ if( arVideoOpen( vconf ) < 0 ) exit(0); /* find the size of the window */ if( arVideoInqSize(&xsize, &ysize) < 0 ) exit(0); printf("Image size (x,y) = (%d,%d)\n", xsize, ysize); /* set the initial camera parameters */ if( arParamLoad(cparam_name, 1, &wparam) < 0 ) { printf("Camera parameter load error !!\n"); exit(0); } arParamChangeSize( &wparam, xsize, ysize, &cparam ); arInitCparam( &cparam ); printf("*** Camera Parameter ***\n"); arParamDisp( &cparam ); /* load in the object data - trained markers and associated bitmap files */ if( (object=read_ObjData(model_name, &objectnum)) == NULL ) exit(0); printf("Objectfile num = %d\n", objectnum); /* open the graphics window */ argInit( &cparam, 2.0, 0, 0, 0, 0 ); } /* cleanup function called when program exits */ static void cleanup(void) { arVideoCapStop(); arVideoClose(); argCleanup(); } /* draw the the AR objects */ static int draw( ObjectData_T *object, int objectnum ) { int i; double gl_para[16]; glClearDepth( 1.0 ); glClear(GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glEnable(GL_LIGHTING); /* calculate the viewing parameters - gl_para */ for( i = 0; i < objectnum; i++ ) { if( object[i].visible == 0 ) continue; argConvGlpara(object[i].trans, gl_para); draw_object( object[i].id, gl_para, object[i].collide ); } glDisable( GL_LIGHTING ); glDisable( GL_DEPTH_TEST ); return(0); } /* draw the user object */ static int draw_object( int obj_id, double gl_para[16], int collide_flag ) { GLfloat mat_ambient[] = {0.0, 0.0, 1.0, 1.0}; GLfloat mat_ambient_collide[] = {1.0, 0.0, 0.0, 1.0}; GLfloat mat_flash[] = {0.0, 0.0, 1.0, 1.0}; GLfloat mat_flash_collide[] = {1.0, 0.0, 0.0, 1.0}; GLfloat mat_flash_shiny[] = {50.0}; GLfloat light_position[] = {100.0,-200.0,200.0,0.0}; GLfloat ambi[] = {0.1, 0.1, 0.1, 0.1}; GLfloat lightZeroColor[] = {0.9, 0.9, 0.9, 0.1}; argDrawMode3D(); argDraw3dCamera( 0, 0 ); glMatrixMode(GL_MODELVIEW); glLoadMatrixd( gl_para ); /* set the material */ glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glLightfv(GL_LIGHT0, GL_AMBIENT, ambi); glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor); glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny); if(collide_flag){ glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash_collide); glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_collide); /* draw a cube */ glTranslatef( 0.0, 0.0, 30.0 ); glutSolidSphere(30,12,6); } else { glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash); glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); /* draw a cube */ glTranslatef( 0.0, 0.0, 30.0 ); glutSolidCube(60); } argDrawMode2D(); return 0; } --- NEW FILE: Makefile.in --- INC_DIR= ../../include LIB_DIR= ../../lib BIN_DIR= ../../bin LDFLAG=@LDFLAG@ -L$(LIB_DIR) LIBS= -lARgsub -lARvideo -lAR @LIBS@ CFLAG= @CFLAG@ -I$(INC_DIR) OBJS = object.o HEADDERS = object.h all: $(BIN_DIR)/collideTest $(BIN_DIR)/collideTest: collideTest.o $(OBJS) cc -o $(BIN_DIR)/collideTest collideTest.o $(OBJS) $(LDFLAG) $(LIBS) collideTest.o: collideTest.c $(HEADDERS) cc -c $(CFLAG) collideTest.c object.o: object.c $(HEADDERS) cc -c $(CFLAG) object.c clean: rm -f *.o rm -f $(BIN_DIR)/collideTest allclean: rm -f *.o rm -f $(BIN_DIR)/collideTest rm -f Makefile --- NEW FILE: collideTest.vcproj --- <?xml version="1.0" encoding="Windows-1252"?> <VisualStudioProject ProjectType="Visual C++" Version="7.10" Name="collideTest" ProjectGUID="{5FA7A8DD-5430-4644-91A9-51626083BB97}"> <Platforms> <Platform Name="Win32"/> </Platforms> <Configurations> <Configuration Name="Debug|Win32" OutputDirectory="Debug" IntermediateDirectory="Debug" ConfigurationType="1"> <Tool Name="VCCLCompilerTool" Optimization="0" AdditionalIncludeDirectories="$(ProjectDir)/../../include" PreprocessorDefinitions="WIN32;_DEBUG" BasicRuntimeChecks="3" RuntimeLibrary="1" BrowseInformation="1" WarningLevel="3" DebugInformationFormat="3"/> <Tool Name="VCCustomBuildTool"/> <Tool Name="VCLinkerTool" AdditionalOptions="/DEBUG" AdditionalDependencies="libARd.lib libARgsubd.lib libARvideod.lib" OutputFile="$(ProjectDir)/../../bin/$(ProjectName)d.exe" AdditionalLibraryDirectories=""$(ProjectDir)/../../lib"" GenerateDebugInformation="TRUE"/> <Tool Name="VCMIDLTool" PreprocessorDefinitions="_DEBUG" MkTypLibCompatible="TRUE" SuppressStartupBanner="TRUE" TargetEnvironment="1"/> <Tool Name="VCPostBuildEventTool"/> <Tool Name="VCPreBuildEventTool"/> <Tool Name="VCPreLinkEventTool"/> <Tool Name="VCResourceCompilerTool"/> <Tool Name="VCWebServiceProxyGeneratorTool"/> <Tool Name="VCXMLDataGeneratorTool"/> <Tool Name="VCWebDeploymentTool"/> <Tool Name="VCManagedWrapperGeneratorTool"/> <Tool Name="VCAuxiliaryManagedWrapperGeneratorTool"/> </Configuration> <Configuration Name="Release|Win32" OutputDirectory="Release" IntermediateDirectory="Release" ConfigurationType="1" WholeProgramOptimization="TRUE"> <Tool Name="VCCLCompilerTool" GlobalOptimizations="TRUE" InlineFunctionExpansion="1" FavorSizeOrSpeed="1" AdditionalIncludeDirectories="$(ProjectDir)/../../include" PreprocessorDefinitions="WIN32;NDEBUG" RuntimeLibrary="0"/> <Tool Name="VCCustomBuildTool"/> <Tool Name="VCLinkerTool" AdditionalDependencies="libAR.lib libARgsub.lib libARvideo.lib" OutputFile="$(ProjectDir)/../../bin/$(ProjectName).exe" AdditionalLibraryDirectories=""$(ProjectDir)/../../lib""/> <Tool Name="VCMIDLTool" PreprocessorDefinitions="NDEBUG" MkTypLibCompatible="TRUE" SuppressStartupBanner="TRUE" TargetEnvironment="1"/> <Tool Name="VCPostBuildEventTool"/> <Tool Name="VCPreBuildEventTool"/> <Tool Name="VCPreLinkEventTool"/> <Tool Name="VCResourceCompilerTool"/> <Tool Name="VCWebServiceProxyGeneratorTool"/> <Tool Name="VCXMLDataGeneratorTool"/> <Tool Name="VCWebDeploymentTool"/> <Tool Name="VCManagedWrapperGeneratorTool"/> <Tool Name="VCAuxiliaryManagedWrapperGeneratorTool"/> </Configuration> </Configurations> <References> </References> <Files> <File RelativePath="collideTest.c"> </File> <File RelativePath="object.c"> </File> <File RelativePath="object.h"> </File> </Files> <Globals> </Globals> </VisualStudioProject> --- NEW FILE: object.h --- #ifndef OBJECT_H #define OBJECT_H #define OBJECT_MAX 30 typedef struct { char name[256]; int id; int visible; int collide; double marker_coord[4][2]; double trans[3][4]; double marker_width; double marker_center[2]; } ObjectData_T; ObjectData_T *read_ObjData( char *name, int *objectnum ); #endif --- NEW FILE: collideTest.dsp --- # Microsoft Developer Studio Project File - Name="collideTest" - Package Owner=<4> # Microsoft Developer Studio Generated Build File, Format Version 6.00 # ** DO NOT EDIT ** # TARGTYPE "Win32 (x86) Console Application" 0x0103 CFG=collideTest - Win32 Debug !MESSAGE This is not a valid makefile. To build this project using NMAKE, !MESSAGE use the Export Makefile command and run !MESSAGE !MESSAGE NMAKE /f "collideTest.mak". !MESSAGE !MESSAGE You can specify a configuration when running NMAKE !MESSAGE by defining the macro CFG on the command line. For example: !MESSAGE !MESSAGE NMAKE /f "collideTest.mak" CFG="collideTest - Win32 Debug" !MESSAGE !MESSAGE Possible choices for configuration are: !MESSAGE !MESSAGE "collideTest - Win32 Release" (based on "Win32 (x86) Console Application") !MESSAGE "collideTest - Win32 Debug" (based on "Win32 (x86) Console Application") !MESSAGE # Begin Project # PROP AllowPerConfigDependencies 0 # PROP Scc_ProjName "" # PROP Scc_LocalPath "" CPP=cl.exe RSC=rc.exe !IF "$(CFG)" == "collideTest - Win32 Release" # PROP BASE Use_MFC 0 # PROP BASE Use_Debug_Libraries 0 # PROP BASE Output_Dir "Release" # PROP BASE Intermediate_Dir "Release" # PROP BASE Target_Dir "" # PROP Use_MFC 0 # PROP Use_Debug_Libraries 0 # PROP Output_Dir "Release" # PROP Intermediate_Dir "Release" # PROP Ignore_Export_Lib 0 # PROP Target_Dir "" # ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c # ADD CPP /nologo /W3 /GX /O2 /I "..\..\include" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /FD /c # SUBTRACT CPP /YX # ADD BASE RSC /l 0x1409 /d "NDEBUG" # ADD RSC /l 0x1409 /d "NDEBUG" BSC32=bscmake.exe # ADD BASE BSC32 /nologo # ADD BSC32 /nologo LINK32=link.exe # ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 # ADD LINK32 libAR.lib libARvideo.lib libARgsub.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 /nodefaultlib:"libc.lib" /out:"../../bin/collideTest.exe" /libpath:"..\..\lib" # SUBTRACT LINK32 /nodefaultlib !ELSEIF "$(CFG)" == "collideTest - Win32 Debug" # PROP BASE Use_MFC 0 # PROP BASE Use_Debug_Libraries 1 # PROP BASE Output_Dir "Debug" # PROP BASE Intermediate_Dir "Debug" # PROP BASE Target_Dir "" # PROP Use_MFC 0 # PROP Use_Debug_Libraries 1 # PROP Output_Dir "Debug" # PROP Intermediate_Dir "Debug" # PROP Ignore_Export_Lib 0 # PROP Target_Dir "" # ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c # ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "..\..\include" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /FD /GZ /c # SUBTRACT CPP /YX # ADD BASE RSC /l 0x1409 /d "_DEBUG" # ADD RSC /l 0x1409 /d "_DEBUG" BSC32=bscmake.exe # ADD BASE BSC32 /nologo # ADD BSC32 /nologo LINK32=link.exe # ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept # ADD LINK32 libARd.lib libARvideod.lib libARgsubd.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /nodefaultlib:"libcd.lib" /out:"../../bin/collideTestd.exe" /pdbtype:sept /libpath:"..\..\lib" # SUBTRACT LINK32 /nodefaultlib !ENDIF # Begin Target # Name "collideTest - Win32 Release" # Name "collideTest - Win32 Debug" # Begin Group "Source Files" # PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" # Begin Source File SOURCE=.\collideTest.c # End Source File # Begin Source File SOURCE=.\object.c # End Source File # End Group # Begin Group "Header Files" # PROP Default_Filter "h;hpp;hxx;hm;inl" # Begin Source File SOURCE=.\object.h # End Source File # End Group # Begin Group "Resource Files" # PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe" # End Group # End Target # End Project |