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