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
#ifndef __APPLE__
8
#include <GL/gl.h>
9
#include <GL/glut.h>
10
#else
11
#include <OpenGL/gl.h>
12
#include <GLUT/glut.h>
13
#endif
14
#include <AR/gsubUtil.h>
15
#include <AR/gsub.h>
16
#include <AR/video.h>
17
#include <AR/param.h>
18
#include <AR/ar.h>
19
#include "object.h"
20
#include "draw_object.h"
21
 
22
#define  TARGET_PATT_FILE  "Data/patt.calib"
23
 
24
/* set up the video format globals */
25
 
26
#ifdef _WIN32
27
char                    *vconf = "Data\\WDM_camera_flipV.xml";
28
#else
29
char                    *vconf = "";
30
#endif
31
 
32
int             xsize;
33
int             ysize;
34
int             thresh = 100;
35
int             opticalFlag = 0;
36
int             saveFittingMode;
37
 
38
/* set up the matrix arrays for the camera and view transforms */
39
ARParam         cparam;
40
int             hmd_param_flag;
41
int             target_id;
42
ObjectData_T    *object;
43
int             objectnum;
44
double          object_center[2] = {0.0, 0.0};
45
 
46
static int      count = 0;
47
 
48
/* function definitions */
49
static void   usage( char *com );
50
static int    init( int argc, char *argv[] );
51
static void   cleanup(void);
52
 
53
static void   keyEvent( unsigned char key, int x, int y);
54
static void   mouseEvent(int button, int state, int x, int y);
55
static void   mainLoop(void);
56
static void   calibPostFunc(ARParam *lpara, ARParam *rpara);
57
 
58
static void usage( char *com )
59
{
60
    printf("Usage: %s [options]\n", com);
61
    printf("   Options:\n");
62
    printf("      -c <camera parameter filename>\n");
63
    printf("      -o <object data filename>\n");
64
    printf("\n");
65
    exit(0);
66
}
67
 
68
int main(int argc, char **argv)
69
{
70
        glutInit(&argc, argv);
71
    if( init( argc, argv ) < 0 ) exit(0);
72
 
73
    arVideoCapStart();
74
    argMainLoop( mouseEvent, keyEvent, mainLoop );
75
        return (0);
76
}
77
 
78
static void   keyEvent( unsigned char key, int x, int y)
79
{
80
    /* quit if the ESC key is pressed */
81
    if( key == 0x1b ) {
82
        /* print out frame/sec and shut everything down */
83
        cleanup();
84
        printf("*** %f (frame/sec)\n", (double)count/arUtilTimer());
85
        exit(0);
86
    }
87
 
88
    /* change the threshold value when 't' key pressed */
89
    if( key == 't' ) {
90
        printf("Enter new threshold value (default = 100): ");
91
        scanf("%d",&thresh); while( getchar()!='\n' );
92
        printf("\n");
93
        count = 0;
94
    }
95
 
96
    /* turn on and off the debug mode with right mouse */
97
    if( key == 'd' ) {
98
        arDebug = 1 - arDebug;
99
        if( arDebug == 0 ) {
100
            glClearColor( 0.0, 0.0, 0.0, 0.0 );
101
            glClear(GL_COLOR_BUFFER_BIT);
102
            argSwapBuffers();
103
            glClear(GL_COLOR_BUFFER_BIT);
104
            argSwapBuffers();
105
        }
106
    }
107
}
108
 
109
/* mouse event handling function */
110
static void mouseEvent(int button, int state, int x, int y)
111
{
112
    /* change display modes on left mouse button
113
        - between video see-through and optical see-through */
114
    if( button == GLUT_LEFT_BUTTON  && state == GLUT_DOWN ) {
115
        if( hmd_param_flag ) {
116
                        opticalFlag = 1 - opticalFlag;
117
                        if( opticalFlag == 0 ) {
118
                arFittingMode = saveFittingMode;
119
                        }
120
                        else {
121
                arFittingMode = AR_FITTING_TO_IDEAL;
122
                        }
123
                }
124
    }
125
 
126
    /* turn on and off the debug mode with middle mouse */
127
    if( button == GLUT_RIGHT_BUTTON  && state == GLUT_DOWN ) {
128
        argUtilCalibHMD( target_id, thresh, calibPostFunc );
129
    }
130
}
131
 
132
static void calibPostFunc(ARParam *lpara, ARParam *rpara)
133
{
134
    count = 0;
135
 
136
    if( lpara == NULL || rpara == NULL ) {
137
        printf("Calibration error!!\n");
138
        return;
139
    }
140
 
141
    hmd_param_flag = 1;
142
    opticalFlag  = 1;
143
    arFittingMode = AR_FITTING_TO_IDEAL;
144
 
145
    printf("*** HMD Parameter ***\n");
146
    printf("LEFT\n");
147
    arParamDisp( lpara );
148
    printf("Right\n");
149
    arParamDisp( rpara );
150
    printf("\n");
151
}
152
 
153
/* main loop */
154
static void mainLoop(void)
155
{
156
    ARUint8         *dataPtr;
157
    ARMarkerInfo    *marker_info;
158
    int             marker_num;
159
    int             i, j, k;
160
 
161
    /* grab a vide frame */
162
    if( (dataPtr = (ARUint8 *)arVideoGetImage()) == NULL ) {
163
        arUtilSleep(2);
164
        return;
165
    }
166
    if( count == 0 ) arUtilTimerReset();
167
    count++;
168
 
169
    if( opticalFlag == 0 ) {
170
        argDispImage( dataPtr, 0, 0 );
171
        if( !arDebug ) arVideoCapNext();
172
        glClearDepth( 1.0 );
173
        glClear(GL_DEPTH_BUFFER_BIT);
174
    }
175
    else {
176
        if( !arDebug ) arVideoCapNext();
177
        glClearDepth( 1.0 );
178
        glClearColor( 0.0, 0.0, 0.0, 0.0 );
179
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
180
    }
181
 
182
    /* detect the markers in the video frame */
183
    if( arDetectMarker(dataPtr, thresh, &marker_info, &marker_num) < 0 ) {
184
        cleanup();
185
        exit(0);
186
    }
187
 
188
    /* if the debug mode is on draw squares
189
       around the detected squares in the video image */
190
    if( arDebug ) {
191
        argDispImage( dataPtr, 1, 1 );
192
        arVideoCapNext();
193
        if( arImageProcMode == AR_IMAGE_PROC_IN_HALF )
194
            argDispHalfImage( arImage, 2, 1 );
195
        else
196
            argDispImage( arImage, 2, 1);
197
 
198
        glColor3f( 1.0, 0.0, 0.0 );
199
        glLineWidth( 3.0 );
200
        for( i = 0; i < marker_num; i++ ) {
201
            if( marker_info[i].id < 0 ) continue;
202
            argDrawSquare( marker_info[i].vertex, 2, 1 );
203
        }
204
        glLineWidth( 1.0 );
205
    }
206
 
207
    /* check for object visibility */
208
    for( i = 0; i < objectnum; i++ ) {
209
        k = -1;
210
        for( j = 0; j < marker_num; j++ ) {
211
            if( object[i].id == marker_info[j].id ) {
212
                if( k == -1 ) k = j;
213
                else {
214
                    if( marker_info[k].cf < marker_info[j].cf ) k = j;
215
                }
216
            }
217
        }
218
        if( k == -1 ) {
219
            object[i].visible = 0;
220
            continue;
221
        }
222
 
223
        /* get the transformation between the marker and the real camera */
224
        if( arGetTransMat(&marker_info[k],
225
                          object_center, object[i].marker_width, object[i].trans) < 0 ) {
226
            object[i].visible = 0;
227
        }
228
        else {
229
            object[i].visible = 1;
230
        }
231
    }
232
 
233
    /* draw the virtual objects attached to the cards */
234
    draw( object, objectnum, opticalFlag );
235
 
236
    argSwapBuffers();
237
}
238
 
239
/* set up the application parameters - read in from command line*/
240
static int init( int argc, char *argv[] )
241
{
242
    char     cparaname[256];
243
    char     odataname[256];
244
    ARParam  wparam;
245
/*
246
    ARParam  wlpara, wrpara;
247
*/
248
    int      i;
249
 
250
    /* copy in name of the camera parameter file, the hmd parameter file
251
       and the object data file */
252
    strcpy( cparaname, "Data/camera_para.dat" );
253
    strcpy( odataname, "Data/object_data" );
254
 
255
    /* read in the parameters from the various files */
256
    for( i = 1; i < argc; i++ ) {
257
        if( strcmp(argv[i],"-c") == 0 ) {
258
            if( i < argc-1 && argv[i+1][0] != '-' ) {
259
                strcpy( cparaname, argv[i+1] );
260
                i++;
261
            }
262
            else usage( argv[0] );
263
        }
264
        else if( strcmp(argv[i],"-o") == 0 ) {
265
            if( i < argc-1 && argv[i+1][0] != '-' ) {
266
                strcpy( odataname, argv[i+1] );
267
                i++;
268
            }
269
            else usage( argv[0] );
270
        }
271
        else usage( argv[0] );
272
    }
273
 
274
    /* load in the object data - trained markers and associated bitmap files */
275
    if( (object=read_objectdata(odataname,&objectnum)) == NULL ) exit(0);
276
    if( (target_id = arLoadPatt(TARGET_PATT_FILE)) < 0 ) {
277
        printf("Target pattern load error!!\n");
278
        exit(0);
279
    }
280
 
281
    /* open the video path */
282
    if( arVideoOpen( vconf ) < 0 ) exit(0);
283
    /* find the size of the window */
284
    if( arVideoInqSize(&xsize, &ysize) < 0 ) exit(0);
285
    printf("Image size (x,y) = (%d,%d)\n", xsize, ysize);
286
 
287
    /* set the initial camera parameters */
288
    if( arParamLoad(cparaname, 1, &wparam) < 0 ) {
289
       printf("Camera parameter load error !!\n");
290
        exit(0);
291
    }
292
    arParamChangeSize( &wparam, xsize, ysize, &cparam );
293
    arInitCparam( &cparam );
294
    printf("*** Camera Parameter ***\n");
295
    arParamDisp( &cparam );
296
 
297
/*
298
    arParamLoad( "Data/hmd_para", 2, &wlpara, &wrpara);
299
    argLoadHMDparam( &wrpara, &wlpara );
300
    hmd_param_flag = 1;
301
*/
302
    hmd_param_flag = 0;
303
 
304
    /* open the graphics window */
305
    argInit( &cparam, 1.0, 0, 2, 1, 1 );
306
 
307
    /* initialize lights and material properties */
308
    init_lights();
309
 
310
    saveFittingMode = arFittingMode;
311
    arDebug       = 0;
312
 
313
    return 0;
314
}
315
 
316
/* cleanup function called when program exits */
317
static void cleanup(void)
318
{
319
    arVideoCapStop();
320
    arVideoClose();
321
    argCleanup();
322
}