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/matrix.h>
00023 #include "draw_object.h"
00024 
00025 
00026 
00027 static void setup_light( void );
00028 static void draw_camera( double trans[3][4] );
00029 static int  draw_object( char *name, double gl_para[16], int xwin, int ywin );
00030 static void get_trans( double a, double b, double r, double trans[3][4] );
00031 static void draw_axis( void );
00032 
00033 
00034 void print_string( char *string )
00035 {
00036   int     i;
00037 
00038   glMatrixMode(GL_MODELVIEW);
00039   glLoadIdentity();
00040   glMatrixMode(GL_PROJECTION);
00041   glLoadIdentity();
00042 
00043   /* display the position data */
00044   glTranslatef(-0.95, -0.20, 0.0);
00045 
00046   /* draw a white polygon */
00047   glColor3f(1.0, 1.0, 1.0);
00048   glBegin(GL_POLYGON);
00049     glVertex2f(1.50, 0.10);
00050     glVertex2f(1.50, -0.12);
00051     glVertex2f(0.001, -0.12);
00052     glVertex2f(0.001, 0.10);
00053   glEnd();
00054 
00055   /* draw red text on the polygon */
00056   glColor3f(0.75, 0.0, 0.0);
00057   glRasterPos2i(0.0, 0.0);
00058   for (i=0; i<(int)strlen(string); i++) {
00059       if(string[i] != '\n' ) {
00060           glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, string[i]);
00061       }
00062       else {
00063           glTranslatef(0.0, -0.07, 0.0);
00064           glRasterPos2i(0.0, 0.0);
00065       }
00066   }
00067 
00068   return;
00069 }
00070 
00071 int draw_exview( double a, double b, double r, double trans[3][4], int xwin, int ywin )
00072 {
00073     double      vtrans[3][4];
00074     double      gl_para[16];
00075     int         i, j;
00076 
00077     argDrawMode3D();
00078     argDraw3dCamera( xwin, ywin );
00079     glDepthFunc(GL_LEQUAL);
00080     glEnable(GL_DEPTH_TEST);
00081     glEnable(GL_NORMALIZE);
00082 
00083     get_trans( a, b, r, vtrans );
00084     argConvGlpara(vtrans, gl_para);
00085     glMatrixMode(GL_PROJECTION);
00086     glMultMatrixd( gl_para );
00087 
00088     glMatrixMode(GL_MODELVIEW);
00089     glLoadIdentity();
00090     setup_light();
00091 
00092     glPushMatrix();
00093     glEnable(GL_LIGHTING);
00094     glEnable(GL_LIGHT0);
00095           
00096     for( j = -300; j <= 200; j+= 100 ) {
00097         for( i = -300; i <= 200; i+= 100 ) {
00098             glBegin(GL_QUADS);
00099             glNormal3f( 0.0, 0.0, 1.0 );
00100             if( (j/100+i/100)%2 ) glColor4f( 0.6, 0.6, 0.6, 1.0 );
00101              else                 glColor4f( 0.0, 0.3, 0.0, 1.0 );
00102             glVertex3f( i,     j,     0.0 );
00103             glVertex3f( i,     j+100, 0.0 );
00104             glVertex3f( i+100, j+100, 0.0 );
00105             glVertex3f( i+100, j,     0.0 );
00106             glEnd();
00107         }
00108     }
00109     draw_axis();
00110 
00111     glColor4f( 0.0, 0.0, 0.5, 1.0 );
00112     glTranslatef( 0.0, 0.0, 25.0 );
00113     glutSolidCube(50.0);
00114 
00115     glDisable( GL_LIGHTING );
00116     glPopMatrix();
00117 
00118     draw_camera( trans );
00119 
00120     glDisable(GL_NORMALIZE);
00121     glDisable( GL_DEPTH_TEST );
00122     argDrawMode2D();
00123 
00124     return 0;
00125 }
00126 
00127 
00128 static void draw_camera( double trans[3][4] )
00129 {
00130 /*
00131     double      gl_para[16];
00132 */
00133     double      btrans[3][4];
00134     double      quat[4], pos[3], angle;
00135 
00136     arUtilMatInv( trans, btrans );
00137 
00138     glMatrixMode(GL_MODELVIEW);
00139     glPushMatrix();
00140       arUtilMat2QuatPos( btrans, quat, pos );
00141       angle = -acos(quat[3])*360.0/3.141592;
00142       glTranslatef( pos[0], pos[1], pos[2] );
00143       glRotated( angle, quat[0], quat[1], quat[2] );
00144 /*
00145       argConvGlpara(btrans, gl_para);
00146       glMultMatrixd( gl_para );
00147 */
00148 
00149       glEnable(GL_LIGHTING);
00150       glEnable(GL_LIGHT0);
00151 
00152       glPushMatrix();
00153         glColor4f( 0.9, 0.9, 0.0, 1.0 );
00154         glTranslatef( 0.0, 0.0, -10.0 );
00155         glScalef( 10.0, 10.0, 20.0 );
00156         glutSolidCube(1.0);
00157       glPopMatrix();
00158 
00159       glColor4f( 0.9, 0.0, 0.9, 1.0 );
00160       glPushMatrix();
00161         glTranslatef( 0.0, 0.0, -40.0 );
00162         glScalef( 30.0, 30.0, 50.0 );
00163         glutSolidCube(1.0);
00164       glPopMatrix();
00165 
00166       glDisable( GL_LIGHTING );
00167     glPopMatrix();
00168 
00169     return;
00170 }
00171 
00172 
00173 int draw( char *name, double trans[4][4], int xwin, int ywin )
00174 {
00175     double      gl_para[16];
00176     
00177     argConvGlpara(trans, gl_para);
00178     draw_object( name, gl_para, xwin, ywin );
00179     
00180     return(0);
00181 }
00182 
00183 /* draw the user object */
00184 static int  draw_object( char *name, double gl_para[16], int xwin, int ywin )
00185 {
00186     argDrawMode3D();
00187     argDraw3dCamera( xwin, ywin );
00188 
00189     glDepthFunc(GL_LEQUAL);
00190     glEnable(GL_DEPTH_TEST);
00191     glEnable(GL_NORMALIZE);
00192     /* load the camera transformation matrix */
00193     glMatrixMode(GL_PROJECTION);
00194     glMultMatrixd( gl_para );
00195 
00196     glMatrixMode(GL_MODELVIEW);
00197     glLoadIdentity();
00198     setup_light();
00199     glEnable(GL_LIGHTING);
00200     glEnable(GL_LIGHT0);
00201 
00202     if( strcmp(name, "target") == 0 ) {
00203         draw_axis();
00204     }
00205     else {
00206         printf("unknown object type!!\n");
00207     }
00208 
00209     glDisable( GL_LIGHTING );
00210     glDisable( GL_NORMALIZE );
00211     glDisable( GL_DEPTH_TEST );
00212     argDrawMode2D();
00213 
00214     return 0;
00215 }
00216 
00217 static void setup_light()
00218 {
00219     static int  mat_f = 1;
00220     GLfloat     mat_amb_diff[]  = {0.9, 0.9, 0.0, 1.0};
00221     GLfloat     mat_specular[]  = {0.5, 0.5, 0.5, 1.0};
00222     GLfloat     mat_shininess[] = {10.0};
00223     GLfloat     light_ambient[] = { 0.01, 0.01, 0.01, 1.0 };
00224     GLfloat     light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
00225     GLfloat     light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
00226     GLfloat     light_position[] = { 100.0, 300.0, 700.0, 1.0 };
00227 
00228     if( mat_f ) {
00229       glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_amb_diff);
00230       glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
00231       glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);      
00232       glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
00233       glEnable(GL_COLOR_MATERIAL);
00234       mat_f = 0;
00235     }
00236 
00237     glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
00238     glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
00239     glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
00240     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
00241 }
00242 
00243 static void get_trans( double a, double b, double r, double trans[3][4] )
00244 {
00245     ARMat   *mat;
00246     double  sa, ca, sb, cb;
00247     double  x, y, z;
00248     int     i, j;
00249 
00250     sa = sin(a*3.141592/180.0);
00251     ca = cos(a*3.141592/180.0);
00252     sb = sin(b*3.141592/180.0);
00253     cb = cos(b*3.141592/180.0);
00254 
00255     x = 0.0;
00256     y = -r * cb;
00257     z = -r * sb;
00258 
00259     mat = arMatrixAlloc( 4, 4 );
00260 
00261     mat->m[0*4+0] =  ca;
00262     mat->m[0*4+1] =  sa*sb;
00263     mat->m[0*4+2] =  sa*cb;
00264     mat->m[1*4+0] = -sa;
00265     mat->m[1*4+1] =  ca*sb;
00266     mat->m[1*4+2] =  ca*cb;
00267     mat->m[2*4+0] =  0;
00268     mat->m[2*4+1] = -cb;
00269     mat->m[2*4+2] =  sb;
00270     mat->m[0*4+3] =  x*ca + y*sa;
00271     mat->m[1*4+3] = -x*sa + y*ca;
00272     mat->m[2*4+3] = z;
00273     mat->m[3*4+0] = 0;
00274     mat->m[3*4+1] = 0;
00275     mat->m[3*4+2] = 0;
00276     mat->m[3*4+3] = 1;
00277 
00278     arMatrixSelfInv( mat );
00279     for( j = 0; j < 3; j++ ) {
00280         for( i = 0; i < 4; i++ ) {
00281             trans[j][i] = mat->m[j*4+i];
00282         }
00283     }
00284     arMatrixFree( mat );
00285 
00286     return;
00287 }
00288 
00289 static void draw_axis( void )
00290 {
00291     glPushMatrix();
00292         glRotatef( 90.0, 0.0, 1.0, 0.0 );
00293         glColor4f( 1.0, 0.0, 0.0, 1.0 );
00294         glutSolidCone(5.0, 100.0, 20, 24);
00295     glPopMatrix();
00296 
00297     glPushMatrix();
00298         glRotatef( -90.0, 1.0, 0.0, 0.0 );
00299         glColor4f( 0.0, 1.0, 0.0, 1.0 );
00300         glutSolidCone(5.0, 100.0, 20, 24);
00301     glPopMatrix();
00302 
00303     glPushMatrix();
00304         glRotatef( 00.0, 0.0, 0.0, 1.0 );
00305         glColor4f( 0.0, 0.0, 1.0, 1.0 );
00306         glutSolidCone(5.0, 100.0, 20, 24);
00307     glPopMatrix();
00308 
00309     return;
00310 }
 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