Subversion Repositories AndroidProjects

Rev

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
#include <string.h>
7
#include <math.h>
8
 
9
#ifndef __APPLE__
10
#  include <GL/glut.h>
11
#else
12
#  include <GLUT/glut.h>
13
#endif
14
#include <AR/gsub.h>
15
#include <AR/param.h>
16
#include <AR/ar.h>
17
#include <AR/video.h>
18
 
19
#include "object.h"
20
 
21
 
22
#define COLLIDE_DIST 30000.0
23
 
24
/* Object Data */
25
char            *model_name = "Data/object_data2";
26
ObjectData_T    *object;
27
int             objectnum;
28
 
29
int             xsize, ysize;
30
int                             thresh = 100;
31
int             count = 0;
32
 
33
/* set up the video format globals */
34
 
35
#ifdef _WIN32
36
char                    *vconf = "Data\\WDM_camera_flipV.xml";
37
#else
38
char                    *vconf = "";
39
#endif
40
 
41
char           *cparam_name    = "Data/camera_para.dat";
42
ARParam         cparam;
43
 
44
static void   init(void);
45
static void   cleanup(void);
46
static void   keyEvent( unsigned char key, int x, int y);
47
static void   mainLoop(void);
48
static int checkCollisions( ObjectData_T object0, ObjectData_T object1, float collide_dist);
49
static int draw( ObjectData_T *object, int objectnum );
50
static int  draw_object( int obj_id, double gl_para[16], int collide_flag );
51
 
52
int main(int argc, char **argv)
53
{
54
        //initialize applications
55
        glutInit(&argc, argv);
56
    init();
57
 
58
        //start video capture
59
        arVideoCapStart();
60
 
61
        //start the main event loop
62
    argMainLoop( NULL, keyEvent, mainLoop );
63
 
64
        return 0;
65
}
66
 
67
static void   keyEvent( unsigned char key, int x, int y)  
68
{
69
    /* quit if the ESC key is pressed */
70
    if( key == 0x1b ) {
71
        printf("*** %f (frame/sec)\n", (double)count/arUtilTimer());
72
        cleanup();
73
        exit(0);
74
    }
75
}
76
 
77
/* main loop */
78
static void mainLoop(void)
79
{
80
    ARUint8         *dataPtr;
81
    ARMarkerInfo    *marker_info;
82
    int             marker_num;
83
    int             i,j,k;
84
 
85
    /* grab a video frame */
86
    if( (dataPtr = (ARUint8 *)arVideoGetImage()) == NULL ) {
87
        arUtilSleep(2);
88
        return;
89
    }
90
 
91
    if( count == 0 ) arUtilTimerReset();  
92
    count++;
93
 
94
        /*draw the video*/
95
    argDrawMode2D();
96
    argDispImage( dataPtr, 0,0 );
97
 
98
        /* capture the next video frame */
99
        arVideoCapNext();
100
 
101
        glColor3f( 1.0, 0.0, 0.0 );
102
        glLineWidth(6.0);
103
 
104
        /* detect the markers in the video frame */
105
        if(arDetectMarker(dataPtr, thresh,
106
                &marker_info, &marker_num) < 0 ) {
107
                cleanup();
108
                exit(0);
109
        }
110
        for( i = 0; i < marker_num; i++ ) {
111
                argDrawSquare(marker_info[i].vertex,0,0);
112
        }
113
 
114
        /* check for known patterns */
115
    for( i = 0; i < objectnum; i++ ) {
116
                k = -1;
117
                for( j = 0; j < marker_num; j++ ) {
118
                if( object[i].id == marker_info[j].id) {
119
 
120
                                /* you've found a pattern */
121
                                //printf("Found pattern: %d ",patt_id);
122
                                glColor3f( 0.0, 1.0, 0.0 );
123
                                argDrawSquare(marker_info[j].vertex,0,0);
124
 
125
                                if( k == -1 ) k = j;
126
                        else /* make sure you have the best pattern (highest confidence factor) */
127
                                        if( marker_info[k].cf < marker_info[j].cf ) k = j;
128
                        }
129
                }
130
                if( k == -1 ) {
131
                        object[i].visible = 0;
132
                        continue;
133
                }
134
 
135
                /* calculate the transform for each marker */
136
                if( object[i].visible == 0 ) {
137
            arGetTransMat(&marker_info[k],
138
                          object[i].marker_center, object[i].marker_width,
139
                          object[i].trans);
140
        }
141
        else {
142
            arGetTransMatCont(&marker_info[k], object[i].trans,
143
                          object[i].marker_center, object[i].marker_width,
144
                          object[i].trans);
145
        }
146
        object[i].visible = 1;
147
        }
148
 
149
        /*check for object collisions between marker 0 and 1 */
150
        if(object[0].visible && object[1].visible){
151
                if(checkCollisions(object[0],object[1],COLLIDE_DIST)){
152
                        object[0].collide = 1;
153
                        object[1].collide = 1;
154
                }
155
                else{
156
                        object[0].collide = 0;
157
                        object[1].collide = 0;
158
                }
159
        }
160
 
161
        /* draw the AR graphics */
162
    draw( object, objectnum );
163
 
164
        /*swap the graphics buffers*/
165
        argSwapBuffers();
166
}
167
 
168
/* check collision between two markers */
169
static int checkCollisions( ObjectData_T object0, ObjectData_T object1, float collide_dist)
170
{
171
        float x1,y1,z1;
172
        float x2,y2,z2;
173
        float dist;
174
 
175
        x1 = object0.trans[0][3];
176
        y1 = object0.trans[1][3];
177
        z1 = object0.trans[2][3];
178
 
179
        x2 = object1.trans[0][3];
180
        y2 = object1.trans[1][3];
181
        z2 = object1.trans[2][3];
182
 
183
        dist = (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2);
184
 
185
        printf("Dist = %3.2f\n",dist);
186
 
187
        if(dist < collide_dist)
188
                return 1;
189
        else
190
                return 0;
191
}
192
static void init( void )
193
{
194
        ARParam  wparam;
195
 
196
    /* open the video path */
197
    if( arVideoOpen( vconf ) < 0 ) exit(0);
198
    /* find the size of the window */
199
    if( arVideoInqSize(&xsize, &ysize) < 0 ) exit(0);
200
    printf("Image size (x,y) = (%d,%d)\n", xsize, ysize);
201
 
202
    /* set the initial camera parameters */
203
    if( arParamLoad(cparam_name, 1, &wparam) < 0 ) {
204
        printf("Camera parameter load error !!\n");
205
        exit(0);
206
    }
207
    arParamChangeSize( &wparam, xsize, ysize, &cparam );
208
    arInitCparam( &cparam );
209
    printf("*** Camera Parameter ***\n");
210
    arParamDisp( &cparam );
211
 
212
        /* load in the object data - trained markers and associated bitmap files */
213
    if( (object=read_ObjData(model_name, &objectnum)) == NULL ) exit(0);
214
    printf("Objectfile num = %d\n", objectnum);
215
 
216
    /* open the graphics window */
217
    argInit( &cparam, 2.0, 0, 0, 0, 0 );
218
}
219
 
220
/* cleanup function called when program exits */
221
static void cleanup(void)
222
{
223
        arVideoCapStop();
224
    arVideoClose();
225
    argCleanup();
226
}
227
 
228
/* draw the the AR objects */
229
static int draw( ObjectData_T *object, int objectnum )
230
{
231
    int     i;
232
    double  gl_para[16];
233
 
234
        glClearDepth( 1.0 );
235
    glClear(GL_DEPTH_BUFFER_BIT);
236
    glEnable(GL_DEPTH_TEST);
237
    glDepthFunc(GL_LEQUAL);
238
    glEnable(GL_LIGHTING);
239
 
240
    /* calculate the viewing parameters - gl_para */
241
    for( i = 0; i < objectnum; i++ ) {
242
        if( object[i].visible == 0 ) continue;
243
        argConvGlpara(object[i].trans, gl_para);
244
        draw_object( object[i].id, gl_para, object[i].collide );
245
    }
246
 
247
        glDisable( GL_LIGHTING );
248
    glDisable( GL_DEPTH_TEST );
249
 
250
    return(0);
251
}
252
 
253
/* draw the user object */
254
static int  draw_object( int obj_id, double gl_para[16], int collide_flag )
255
{
256
    GLfloat   mat_ambient[]                             = {0.0, 0.0, 1.0, 1.0};
257
        GLfloat   mat_ambient_collide[]     = {1.0, 0.0, 0.0, 1.0};
258
    GLfloat   mat_flash[]                               = {0.0, 0.0, 1.0, 1.0};
259
        GLfloat   mat_flash_collide[]       = {1.0, 0.0, 0.0, 1.0};
260
    GLfloat   mat_flash_shiny[] = {50.0};
261
    GLfloat   light_position[]  = {100.0,-200.0,200.0,0.0};
262
    GLfloat   ambi[]            = {0.1, 0.1, 0.1, 0.1};
263
    GLfloat   lightZeroColor[]  = {0.9, 0.9, 0.9, 0.1};
264
 
265
    argDrawMode3D();
266
    argDraw3dCamera( 0, 0 );
267
    glMatrixMode(GL_MODELVIEW);
268
    glLoadMatrixd( gl_para );
269
 
270
        /* set the material */
271
    glEnable(GL_LIGHTING);
272
    glEnable(GL_LIGHT0);
273
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
274
    glLightfv(GL_LIGHT0, GL_AMBIENT, ambi);
275
    glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
276
 
277
    glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny);     
278
 
279
        if(collide_flag){
280
                glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash_collide);
281
                glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_collide);
282
                /* draw a cube */
283
                glTranslatef( 0.0, 0.0, 30.0 );
284
                glutSolidSphere(30,12,6);
285
        }
286
        else {
287
                glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash);
288
                glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
289
                /* draw a cube */
290
                glTranslatef( 0.0, 0.0, 30.0 );
291
                glutSolidCube(60);
292
        }
293
 
294
    argDrawMode2D();
295
 
296
    return 0;
297
}