draw_object.c
Go to the documentation of this file.
00001 /*
00002 ** draw_object function for AR tracking sample code 
00003 ** uses glut functions to draw simple objects
00004 **
00005 */
00006 #include <stdio.h>
00007 #include <string.h>
00008 #include <math.h>
00009 #if defined(_WIN32)
00010 #include <windows.h>
00011 #else
00012 #include <strings.h>
00013 #endif
00014 #ifndef __APPLE__
00015 #include <GL/gl.h>
00016 #include <GL/glut.h>
00017 #else
00018 #include <OpenGL/gl.h>
00019 #include <GLUT/glut.h>
00020 #endif
00021 #include <AR/gsub.h>
00022 #include <AR/ar.h>
00023 #include "draw_object.h"
00024 #include "object.h"
00025 
00026 /* material properties */
00027 GLfloat mat_specular1[] = {0.2, 0.0, 0.0, 1.0};
00028 GLfloat mat_shininess1[] = {50.0};
00029 GLfloat mat_specular2[] = {0.0, 0.0, 0.2, 1.0};
00030 GLfloat mat_shininess2[] = {25.0};
00031 
00032 GLfloat mat_ambient1[] = {1.0, 0.0, 0.0, 1.0};
00033 GLfloat mat_ambient2[] = {0.0, 0.0, 1.0, 1.0};
00034 GLfloat mat_flash_ambient1[] = {0.0, 1.0, 0.0, 1.0};
00035 
00036 GLfloat mat_flash1[] = {1.0, 0.0, 0.0, 1.0};
00037 GLfloat mat_flash_shiny1[] = {25.0};
00038 GLfloat mat_flash2[] = {0.0, 0.0, 1.0, 1.0};
00039 GLfloat mat_flash_shiny2[] = {50.0};
00040 
00041 static int  draw_object( char *name, double gl_para[16], int dispmode);
00042 
00043 /* draw the the AR objects */
00044 int draw( ObjectData_T *object, int objectnum, int dispmode )
00045 {
00046     int     i;
00047     double  gl_para[16];
00048     
00049     /* calculate the viewing parameters - gl_para */
00050     for( i = 0; i < objectnum; i++ ) {
00051         if( object[i].visible == 0 ) continue;
00052 
00053         argConvGlpara(object[i].trans, gl_para);
00054       
00055         /* draw the object */
00056         draw_object( object[i].name, gl_para, dispmode);
00057     }
00058     
00059     return(0);
00060 }
00061 
00062 /* draw the user object */
00063 static int  draw_object( char *name, double gl_para[16], int dispmode)
00064 {
00065     int    i;
00066 
00067     argDrawMode3D();
00068     glDepthFunc(GL_LEQUAL);
00069     glEnable(GL_DEPTH_TEST);
00070     
00071     /* if in 3D display mode render views
00072        from both eye - otherwise from one eye */
00073     for( i = 0; i < 3; i++ ) {
00074         if( dispmode == 1 ) {
00075             switch(i) {
00076                 case 0:  argDraw3dLeft();  break;
00077                 case 1:  argDraw3dRight(); break;
00078                 case 2:  argDraw3dCamera( 1, 1 ); break;
00079             }
00080         }
00081         else argDraw3dCamera( 0, 0 );
00082 
00083         /* load the camera transformation matrix */
00084         glMatrixMode(GL_MODELVIEW);
00085         glLoadMatrixd( gl_para );
00086         init_lights();
00087 
00088 
00089         /* draw the user object here 
00090            - using the object name to select the object */
00091         if( strcmp(name, "torus") == 0 ) {
00092           /* set object color */
00093           glEnable(GL_LIGHTING);
00094           glEnable(GL_LIGHT0);
00095           glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash1);
00096           glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny1);
00097           glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient1);
00098 
00099           /* draw a simple torus */
00100           glMatrixMode(GL_MODELVIEW);
00101           glTranslatef( 0.0, 0.0, 10.0 );
00102           glutSolidTorus(10.0, 40.0, 24, 24);
00103           glDisable( GL_LIGHTING );
00104         }
00105         else if( strcmp(name, "sphere") == 0 ) {
00106           glEnable(GL_LIGHTING);
00107           glEnable(GL_LIGHT0);
00108           glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash1);
00109           glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny1);
00110           glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient1);
00111     
00112           /* draw a sphere */
00113           glMatrixMode(GL_MODELVIEW);
00114           glTranslatef( 0.0, 0.0, 40.0 );
00115           glutSolidSphere(40.0, 24, 24);
00116           glDisable( GL_LIGHTING );
00117         }
00118         else if( strcmp(name, "cube") == 0 ) {
00119           glEnable(GL_LIGHTING);
00120           glEnable(GL_LIGHT0);
00121           glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash2);
00122           glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny2);
00123           glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient2);
00124 
00125           glMatrixMode(GL_MODELVIEW);
00126           glTranslatef( 0.0, 0.0, 25.0 );
00127           glutSolidCube(50.0);
00128           glDisable( GL_LIGHTING );
00129         }
00130         else if( strcmp(name, "cone") == 0 ) {
00131           glEnable(GL_LIGHTING);
00132           glEnable(GL_LIGHT0);
00133           glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash1);
00134           glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny1);
00135           glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient1);
00136 
00137           glMatrixMode(GL_MODELVIEW);
00138           glutSolidCone(25.0, 100.0, 20, 24);
00139           glDisable( GL_LIGHTING );
00140         }
00141         else {
00142           printf("unknown object type!!\n");
00143         }
00144 
00145         if( dispmode == 0 ) break;
00146         if( arDebug == 0 && i == 1 ) break;
00147     }
00148 
00149     glDisable( GL_DEPTH_TEST );
00150     argDrawMode2D();
00151 
00152     return 0;
00153 }
00154 
00155 /* initialize the lights in the scene */
00156 void init_lights()
00157 {
00158     GLfloat light_position[] = {0.0,-200.0,0.0,0.0};
00159     GLfloat ambi[] = {0.1, 0.1, 0.1, 0.1};
00160     GLfloat lightZeroColor[]    = {0.9, 0.9, 0.9, 0.1};
00161 
00162     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
00163     glLightfv(GL_LIGHT0, GL_AMBIENT, ambi);
00164     glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
00165 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


ar_recog
Author(s): Graylin Trevor Jay and Christopher Crick
autogenerated on Fri Jan 25 2013 12:15:00