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/gsub.h>
15
#include <AR/video.h>
16
#include <AR/param.h>
17
#include <AR/ar.h>
18
#include "draw_object.h"
19
 
20
 
21
 
22
/* set up the video format globals */
23
 
24
#ifdef _WIN32
25
char    *vconf = "Data\\WDM_camera_flipV.xml";
26
#else
27
char    *vconf = "";
28
#endif
29
 
30
int             xsize;
31
int             ysize;
32
int             thresh = 100;
33
ARParam         cparam;
34
int             outputMode = 0;
35
 
36
int             mouse_ox;
37
int             mouse_oy;
38
int             mouse_st = 0;
39
int             disp_mode = 1;
40
double          a =   0.0;
41
double          b = -45.0;
42
double          r = 500.0;
43
 
44
int             target_id;
45
double          target_center[2] = {0.0, 0.0};
46
double          target_width = 80.0;
47
 
48
 
49
/* function definitions */
50
static int    init(void);
51
static void   cleanup(void);
52
static void   keyEvent( unsigned char key, int x, int y);
53
static void   mouseEvent(int button, int state, int x, int y);
54
static void   motionEvent( int x, int y );
55
static void   mainLoop(void);
56
 
57
static void   getResultRaw( ARMarkerInfo *marker_info );
58
static void   getResultQuat( ARMarkerInfo *marker_info );
59
 
60
int main(int argc, char **argv)
61
{
62
        glutInit(&argc, argv);
63
    if( init() < 0 ) exit(0);
64
 
65
    arVideoCapStart();
66
    glutMotionFunc( motionEvent );
67
    argMainLoop( mouseEvent, keyEvent, mainLoop );
68
        return (0);
69
}
70
 
71
static void mainLoop(void)
72
{
73
    ARUint8         *dataPtr;
74
    ARMarkerInfo    *marker_info;
75
    int             marker_num;
76
    int             j, k;
77
 
78
    /* grab a vide frame */
79
    if( (dataPtr = (ARUint8 *)arVideoGetImage()) == NULL ) {
80
        arUtilSleep(2);
81
        return;
82
    }
83
 
84
    glClearColor( 0.0, 0.0, 0.0, 0.0 );
85
    glClearDepth( 1.0 );
86
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
87
    argDrawMode2D();
88
    if( disp_mode ) {
89
        argDispImage( dataPtr, 0, 0 );
90
    }
91
    else {
92
        argDispImage( dataPtr, 1, 1 );
93
    }
94
 
95
    /* detect the markers in the video frame */
96
    if( arDetectMarker(dataPtr, thresh, &marker_info, &marker_num) < 0 ) {
97
        cleanup();
98
        exit(0);
99
    }
100
    arVideoCapNext();
101
 
102
    /* if the debug mode is on draw squares
103
       around the detected squares in the video image */
104
    if( arDebug ) {
105
        if( arImageProcMode == AR_IMAGE_PROC_IN_HALF )
106
            argDispHalfImage( arImage, 2, 1 );
107
        else
108
            argDispImage( arImage, 2, 1);
109
    }
110
 
111
    /* check for object visibility */
112
    k = -1;
113
    for( j = 0; j < marker_num; j++ ) {
114
        if( marker_info[j].id == target_id ) {
115
            if( k == -1 ) k = j;
116
            else {
117
                if( marker_info[k].cf < marker_info[j].cf ) k = j;
118
            }
119
        }
120
    }
121
    if( k != -1 ) {
122
        glDisable(GL_DEPTH_TEST);
123
        switch( outputMode ) {
124
            case 0:
125
                getResultRaw( &marker_info[k] );
126
                break;
127
            case 1:
128
                getResultQuat( &marker_info[k] );
129
                break;
130
        }
131
    }
132
 
133
    argSwapBuffers();
134
}
135
 
136
static void getResultRaw( ARMarkerInfo *marker_info )
137
{
138
    double      target_trans[3][4];
139
    double      cam_trans[3][4];
140
    char        string[256];
141
 
142
    if( arGetTransMat(marker_info, target_center, target_width, target_trans) < 0 ) return;
143
    if( arUtilMatInv(target_trans, cam_trans) < 0 ) return;
144
 
145
    sprintf(string," RAW: Cam Pos x: %3.1f  y: %3.1f  z: %3.1f",
146
            cam_trans[0][3], cam_trans[1][3], cam_trans[2][3]);
147
 
148
    if( disp_mode ) {
149
        draw( "target", target_trans, 0, 0 );
150
        draw_exview( a, b, r, target_trans, 1, 1 );
151
    }
152
    else {
153
        draw( "target", target_trans, 1, 1 );
154
        draw_exview( a, b, r, target_trans, 0, 0 );
155
    }
156
    print_string( string );
157
 
158
    return;
159
}
160
 
161
static void getResultQuat( ARMarkerInfo *marker_info )
162
{
163
    double      target_trans[3][4];
164
    double      cam_trans[3][4];
165
    double      quat[4], pos[3];
166
    char        string1[256];
167
    char        string2[256];
168
 
169
    if( arGetTransMat(marker_info, target_center, target_width, target_trans) < 0 ) return;
170
    if( arUtilMatInv(target_trans, cam_trans) < 0 ) return;
171
    if( arUtilMat2QuatPos(cam_trans, quat, pos) < 0 ) return;
172
 
173
    sprintf(string1," QUAT: Pos x: %3.1f  y: %3.1f  z: %3.1f\n",
174
            pos[0], pos[1], pos[2]);
175
    sprintf(string2, "      Quat qx: %3.2f qy: %3.2f qz: %3.2f qw: %3.2f ",
176
            quat[0], quat[1], quat[2], quat[3]);
177
    strcat( string1, string2 );
178
 
179
    if( disp_mode ) {
180
        draw( "target", target_trans, 0, 0 );
181
        draw_exview( a, b, r, target_trans, 1, 1 );
182
    }
183
    else {
184
        draw( "target", target_trans, 1, 1 );
185
        draw_exview( a, b, r, target_trans, 0, 0 );
186
    }
187
    print_string( string1 );
188
 
189
    return;
190
}
191
 
192
/* set up the application parameters - read in from command line*/
193
static int init(void)
194
{
195
    char     cparaname[256];
196
    char     pattname[256];
197
    ARParam  wparam;
198
 
199
    strcpy( cparaname, "Data/camera_para.dat" );
200
    strcpy( pattname,  "Data/patt.hiro" );
201
 
202
    /* open the video path */
203
    if( arVideoOpen( vconf ) < 0 ) exit(0);
204
    /* find the size of the window */
205
    if( arVideoInqSize(&xsize, &ysize) < 0 ) exit(0);
206
    printf("Image size (x,y) = (%d,%d)\n", xsize, ysize);
207
 
208
    /* set the initial camera parameters */
209
    if( arParamLoad(cparaname, 1, &wparam) < 0 ) {
210
       printf("Camera parameter load error !!\n");
211
        exit(0);
212
    }
213
    arParamChangeSize( &wparam, xsize, ysize, &cparam );
214
    arInitCparam( &cparam );
215
    printf("*** Camera Parameter ***\n");
216
    arParamDisp( &cparam );
217
 
218
    /* open the graphics window */
219
    argInit( &cparam, 1.0, 0, 2, 1, 0 );
220
 
221
    if( (target_id = arLoadPatt(pattname)) < 0 ) {
222
        printf("Target pattern load error!!\n");
223
        exit(0);
224
    }
225
 
226
    arDebug = 0;
227
 
228
    return 0;
229
}
230
 
231
/* cleanup function called when program exits */
232
static void cleanup(void)
233
{
234
    arVideoCapStop();
235
    arVideoClose();
236
    argCleanup();
237
}
238
 
239
static void   keyEvent( unsigned char key, int x, int y)
240
{
241
    /* quit if the ESC key is pressed */
242
    if( key == 0x1b ) {
243
        cleanup();
244
        exit(0);
245
    }
246
 
247
    /* change the threshold value when 't' key pressed */
248
    if( key == 't' ) {
249
        printf("Enter new threshold value (default = 100): ");
250
        scanf("%d",&thresh); while( getchar()!='\n' );
251
        printf("\n");
252
    }
253
 
254
    /* turn on and off the debug mode with right mouse */
255
    if( key == 'd' ) {
256
        arDebug = 1 - arDebug;
257
        if( arDebug == 0 ) {
258
            glClearColor( 0.0, 0.0, 0.0, 0.0 );
259
            glClear(GL_COLOR_BUFFER_BIT);
260
            argSwapBuffers();
261
            glClear(GL_COLOR_BUFFER_BIT);
262
            argSwapBuffers();
263
        }
264
    }
265
 
266
    if(key == 'o') {
267
        outputMode = (outputMode + 1) % 2;
268
    }
269
 
270
    if(key == 'c' ) {
271
        disp_mode = 1 - disp_mode;
272
    }
273
 
274
}
275
 
276
static void motionEvent( int x, int y )
277
{
278
    if( mouse_st == 1 ) {
279
        a += ((double)x - mouse_ox) / 2.0;
280
        b -= ((double)y - mouse_oy) / 2.0;
281
        if( a <   0.0 ) a += 360.0;
282
        if( a > 360.0 ) a -= 360.0;
283
        if( b < -90.0 ) b =  -90.0;
284
        if( b >   0.0 ) b =    0.0;
285
    }
286
    else if( mouse_st == 2 ) {
287
        r *= (1.0 + ((double)y - mouse_oy)*0.01);
288
        if( r < 10.0 ) r = 10.0;
289
    }
290
 
291
    mouse_ox = x;
292
    mouse_oy = y;
293
}
294
 
295
static void mouseEvent(int button, int state, int x, int y)
296
{
297
    if( state == GLUT_UP ) {
298
        mouse_st = 0;
299
    }
300
    else if( state == GLUT_DOWN ) {
301
        if( button == GLUT_LEFT_BUTTON ) {
302
            mouse_st = 1;
303
            mouse_ox = x;
304
            mouse_oy = y;
305
        }
306
        else if( button == GLUT_MIDDLE_BUTTON ) {
307
            disp_mode = 1 - disp_mode;
308
        }
309
        else if( button == GLUT_RIGHT_BUTTON ) {
310
            mouse_st = 2;
311
            mouse_ox = x;
312
            mouse_oy = y;
313
        }
314
    }
315
}