00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifdef _WIN32
00026 # include <windows.h>
00027 #endif
00028 #include <math.h>
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <string.h>
00032 #ifndef __APPLE__
00033 # include <GL/glut.h>
00034 # ifdef GL_VERSION_1_2
00035 # include <GL/glext.h>
00036 # endif
00037 #else
00038 # include <GLUT/glut.h>
00039 # include <OpenGL/glext.h>
00040 #endif
00041 #include <AR/config.h>
00042 #include <AR/video.h>
00043 #include <AR/param.h>
00044 #include <AR/ar.h>
00045 #include <AR/gsub_lite.h>
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 #if defined(__sgi)
00058 char *vconf = "-size=FULL";
00059 #elif defined(__linux)
00060 # if defined(AR_INPUT_GSTREAMER)
00061 char *vconf = "videotestsrc";
00062 # elif defined(AR_INPUT_V4L)
00063 char *vconf = "-width=640 -height=480";
00064 # elif defined(AR_INPUT_1394CAM)
00065 char *vconf = "-mode=640x480_YUV411";
00066 # elif defined(AR_INPUT_DV)
00067 char *vconf = "";
00068 # endif
00069 #elif defined(_WIN32)
00070 char *vconf = "Data\\WDM_camera_flipV.xml";
00071 #elif defined(__APPLE__)
00072 char *vconf = "-width=640 -height=480";
00073 #else
00074 char *vconf = "";
00075 #endif
00076
00077
00078 static ARUint8 *gARTImage = NULL;
00079 static ARUint8 *gARTsaveImage = NULL;
00080
00081
00082 static int gARTThreshhold = 100;
00083 static ARMarkerInfo* gTarget = NULL;
00084
00085
00086
00087 static ARParam gARTCparam;
00088 static ARGL_CONTEXT_SETTINGS_REF gArglSettings = NULL;
00089
00090
00091
00092
00093
00094 void lineSeg(double x1, double y1, double x2, double y2, ARGL_CONTEXT_SETTINGS_REF contextSettings, ARParam cparam, double zoom)
00095 {
00096 int enable;
00097 float ox, oy;
00098 double xx1, yy1, xx2, yy2;
00099
00100 if (!contextSettings) return;
00101 arglDistortionCompensationGet(contextSettings, &enable);
00102 if (arglDrawModeGet(contextSettings) == AR_DRAW_BY_TEXTURE_MAPPING && enable) {
00103 xx1 = x1; yy1 = y1;
00104 xx2 = x2; yy2 = y2;
00105 } else {
00106 arParamIdeal2Observ(cparam.dist_factor, x1, y1, &xx1, &yy1);
00107 arParamIdeal2Observ(cparam.dist_factor, x2, y2, &xx2, &yy2);
00108 }
00109
00110 xx1 *= zoom; yy1 *= zoom;
00111 xx2 *= zoom; yy2 *= zoom;
00112
00113 ox = 0;
00114 oy = cparam.ysize - 1;
00115 glBegin(GL_LINES);
00116 glVertex2f(ox + xx1, oy - yy1);
00117 glVertex2f(ox + xx2, oy - yy2);
00118 glEnd();
00119 glFlush();
00120 }
00121
00122 static int setupCamera(ARParam *cparam)
00123 {
00124 ARParam wparam;
00125 char name1[256], name2[256];
00126 int xsize, ysize;
00127
00128 printf("Enter camera parameter filename");
00129 printf("(Data/camera_para.dat): ");
00130 if (fgets(name1, 256, stdin) == NULL) exit(0);
00131 if (sscanf(name1, "%s", name2) != 1) {
00132 strcpy(name2, "Data/camera_para.dat");
00133 }
00134
00135
00136 if (arParamLoad(name2, 1, &wparam) < 0 ) {
00137 printf("Parameter load error !!\n");
00138 return (FALSE);
00139 }
00140
00141
00142 if (arVideoOpen(vconf) < 0) {
00143 fprintf(stderr, "setupCamera(): Unable to open connection to camera.\n");
00144 return (FALSE);
00145 }
00146
00147
00148 if (arVideoInqSize(&xsize, &ysize) < 0) return (FALSE);
00149 fprintf(stdout, "Camera image size (x,y) = (%d,%d)\n", xsize, ysize);
00150
00151
00152 arParamChangeSize(&wparam, xsize, ysize, cparam);
00153 fprintf(stdout, "*** Camera Parameter ***\n");
00154 arParamDisp(cparam);
00155
00156 arInitCparam(cparam);
00157
00158 if (arVideoCapStart() != 0) {
00159 fprintf(stderr, "setupCamera(): Unable to begin camera data capture.\n");
00160 return (FALSE);
00161 }
00162
00163 return (TRUE);
00164 }
00165
00166 static void Quit(void)
00167 {
00168 free(gARTsaveImage); gARTsaveImage = NULL;
00169 arglCleanup(gArglSettings);
00170 arVideoCapStop();
00171 arVideoClose();
00172 exit(0);
00173 }
00174
00175 static void Keyboard(unsigned char key, int x, int y)
00176 {
00177 switch (key) {
00178 case 0x1B:
00179 case 'Q':
00180 case 'q':
00181 Quit();
00182 break;
00183 case 'T':
00184 case 't':
00185 printf("Enter new threshold value (default = 100): ");
00186 scanf("%d", &gARTThreshhold); while (getchar()!='\n');
00187 printf("\n");
00188 break;
00189 case '?':
00190 case '/':
00191 printf("Keys:\n");
00192 printf(" q or [esc] Quit demo.\n");
00193 printf(" t Enter new binarization threshold value.\n");
00194 printf(" ? or / Show this help.\n");
00195 printf("\nAdditionally, the ARVideo library supplied the following help text:\n");
00196 arVideoDispOption();
00197 break;
00198 default:
00199 break;
00200 }
00201 }
00202
00203 static void Mouse(int button, int state, int x, int y)
00204 {
00205 char name1[256], name2[256];
00206
00207 if (state == GLUT_DOWN) {
00208 if (button == GLUT_RIGHT_BUTTON) {
00209 Quit();
00210 } else if (button == GLUT_MIDDLE_BUTTON) {
00211 printf("Enter new threshold value (default = 100): ");
00212 scanf("%d", &gARTThreshhold); while (getchar() != '\n');
00213 printf("\n");
00214 } else if (button == GLUT_LEFT_BUTTON && gARTsaveImage && gTarget) {
00215 printf("Enter filename: ");
00216 if (fgets(name1, 256, stdin) == NULL) return;
00217 if (sscanf(name1, "%s", name2) != 1 ) return;
00218 if (arSavePatt(gARTsaveImage, gTarget, name2) < 0) {
00219 printf("ERROR!!\n");
00220 } else {
00221 printf(" Saved\n");
00222 }
00223 }
00224 }
00225 }
00226
00227 static void Idle(void)
00228 {
00229 static int ms_prev;
00230 int ms;
00231 float s_elapsed;
00232 ARUint8 *image;
00233 int areamax;
00234 ARMarkerInfo *marker_info;
00235 int marker_num;
00236 int i;
00237
00238
00239 ms = glutGet(GLUT_ELAPSED_TIME);
00240 s_elapsed = (float)(ms - ms_prev) * 0.001;
00241 if (s_elapsed < 0.01f) return;
00242 ms_prev = ms;
00243
00244
00245 if ((image = arVideoGetImage()) != NULL) {
00246 gARTImage = image;
00247
00248 if (arDetectMarker(gARTImage, gARTThreshhold, &marker_info, &marker_num) < 0) {
00249 Quit();
00250 }
00251
00252 areamax = 0;
00253 gTarget = NULL;
00254 for (i = 0; i < marker_num; i++) {
00255 if (marker_info[i].area > areamax) {
00256 areamax = marker_info[i].area;
00257 gTarget = &(marker_info[i]);
00258 }
00259 }
00260 memcpy(gARTsaveImage, gARTImage, gARTCparam.xsize * gARTCparam.ysize * AR_PIX_SIZE_DEFAULT);
00261
00262
00263 glutPostRedisplay();
00264 }
00265 }
00266
00267
00268
00269
00270
00271 static void Visibility(int visible)
00272 {
00273 if (visible == GLUT_VISIBLE) {
00274 glutIdleFunc(Idle);
00275 } else {
00276 glutIdleFunc(NULL);
00277 }
00278 }
00279
00280
00281
00282
00283
00284 static void Reshape(int w, int h)
00285 {
00286 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00287 glViewport(0, 0, (GLsizei) w, (GLsizei) h);
00288
00289 glMatrixMode(GL_PROJECTION);
00290 glLoadIdentity();
00291 glMatrixMode(GL_MODELVIEW);
00292 glLoadIdentity();
00293
00294
00295 }
00296
00297 static void beginOrtho2D(int xsize, int ysize) {
00298 glMatrixMode(GL_PROJECTION);
00299 glPushMatrix();
00300 glLoadIdentity();
00301 gluOrtho2D(0.0, xsize, 0.0, ysize);
00302 glMatrixMode(GL_MODELVIEW);
00303 glPushMatrix();
00304 glLoadIdentity();
00305 }
00306
00307 static void endOrtho2D(void) {
00308 glMatrixMode(GL_PROJECTION);
00309 glPopMatrix();
00310 glMatrixMode(GL_MODELVIEW);
00311 glPopMatrix();
00312 }
00313
00314
00315
00316
00317 static void Display(void)
00318 {
00319
00320 glDrawBuffer(GL_BACK);
00321 glClear(GL_COLOR_BUFFER_BIT);
00322
00323 arglDispImage(gARTImage, &gARTCparam, 1.0, gArglSettings);
00324 arVideoCapNext();
00325 gARTImage = NULL;
00326
00327 if (gTarget != NULL) {
00328 glDisable(GL_DEPTH_TEST);
00329 glDisable(GL_LIGHTING);
00330 glDisable(GL_TEXTURE_2D);
00331 beginOrtho2D(gARTCparam.xsize, gARTCparam.ysize);
00332 glLineWidth(2.0f);
00333 glColor3d(0.0, 1.0, 0.0);
00334 lineSeg(gTarget->vertex[0][0], gTarget->vertex[0][1],
00335 gTarget->vertex[1][0], gTarget->vertex[1][1], gArglSettings, gARTCparam, 1.0);
00336 lineSeg(gTarget->vertex[3][0], gTarget->vertex[3][1],
00337 gTarget->vertex[0][0], gTarget->vertex[0][1], gArglSettings, gARTCparam, 1.0);
00338 glColor3d(1.0, 0.0, 0.0);
00339 lineSeg(gTarget->vertex[1][0], gTarget->vertex[1][1],
00340 gTarget->vertex[2][0], gTarget->vertex[2][1], gArglSettings, gARTCparam, 1.0);
00341 lineSeg(gTarget->vertex[2][0], gTarget->vertex[2][1],
00342 gTarget->vertex[3][0], gTarget->vertex[3][1], gArglSettings, gARTCparam, 1.0);
00343 endOrtho2D();
00344 }
00345
00346 glutSwapBuffers();
00347 }
00348
00349 int main(int argc, char *argv[])
00350 {
00351
00352
00353
00354
00355 glutInit(&argc, argv);
00356
00357
00358
00359
00360
00361 if (!setupCamera(&gARTCparam)) {
00362 fprintf(stderr, "main(): Unable to set up AR camera.\n");
00363 exit(-1);
00364 }
00365
00366
00367
00368
00369
00370
00371 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
00372 glutInitWindowSize(gARTCparam.xsize, gARTCparam.ysize);
00373 glutCreateWindow(argv[0]);
00374
00375
00376 if ((gArglSettings = arglSetupForCurrentContext()) == NULL) {
00377 fprintf(stderr, "main(): arglSetupForCurrentContext() returned error.\n");
00378 exit(-1);
00379 }
00380
00381 arMalloc(gARTsaveImage, ARUint8, gARTCparam.xsize * gARTCparam.ysize * AR_PIX_SIZE_DEFAULT);
00382
00383
00384
00385 glutDisplayFunc(Display);
00386 glutReshapeFunc(Reshape);
00387 glutVisibilityFunc(Visibility);
00388 glutKeyboardFunc(Keyboard);
00389 glutMouseFunc(Mouse);
00390
00391 glutMainLoop();
00392
00393 return (0);
00394 }
00395