Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 204 | chris | 1 | /* |
| 2 | ** draw_object function for AR tracking sample code |
||
| 3 | ** uses glut functions to draw simple objects |
||
| 4 | ** |
||
| 5 | */ |
||
| 6 | #include <stdio.h> |
||
| 7 | #include <string.h> |
||
| 8 | #include <math.h> |
||
| 9 | #if defined(_WIN32) |
||
| 10 | #include <windows.h> |
||
| 11 | #else |
||
| 12 | #include <strings.h> |
||
| 13 | #endif |
||
| 14 | #ifndef __APPLE__ |
||
| 15 | #include <GL/gl.h> |
||
| 16 | #include <GL/glut.h> |
||
| 17 | #else |
||
| 18 | #include <OpenGL/gl.h> |
||
| 19 | #include <GLUT/glut.h> |
||
| 20 | #endif |
||
| 21 | #include <AR/gsub.h> |
||
| 22 | #include <AR/matrix.h> |
||
| 23 | #include "draw_object.h" |
||
| 24 | |||
| 25 | |||
| 26 | |||
| 27 | static void setup_light( void ); |
||
| 28 | static void draw_camera( double trans[3][4] ); |
||
| 29 | static int draw_object( char *name, double gl_para[16], int xwin, int ywin ); |
||
| 30 | static void get_trans( double a, double b, double r, double trans[3][4] ); |
||
| 31 | static void draw_axis( void ); |
||
| 32 | |||
| 33 | |||
| 34 | void print_string( char *string ) |
||
| 35 | { |
||
| 36 | int i; |
||
| 37 | |||
| 38 | glMatrixMode(GL_MODELVIEW); |
||
| 39 | glLoadIdentity(); |
||
| 40 | glMatrixMode(GL_PROJECTION); |
||
| 41 | glLoadIdentity(); |
||
| 42 | |||
| 43 | /* display the position data */ |
||
| 44 | glTranslatef(-0.95, -0.20, 0.0); |
||
| 45 | |||
| 46 | /* draw a white polygon */ |
||
| 47 | glColor3f(1.0, 1.0, 1.0); |
||
| 48 | glBegin(GL_POLYGON); |
||
| 49 | glVertex2f(1.50, 0.10); |
||
| 50 | glVertex2f(1.50, -0.12); |
||
| 51 | glVertex2f(0.001, -0.12); |
||
| 52 | glVertex2f(0.001, 0.10); |
||
| 53 | glEnd(); |
||
| 54 | |||
| 55 | /* draw red text on the polygon */ |
||
| 56 | glColor3f(0.75, 0.0, 0.0); |
||
| 57 | glRasterPos2i(0.0, 0.0); |
||
| 58 | for (i=0; i<(int)strlen(string); i++) { |
||
| 59 | if(string[i] != '\n' ) { |
||
| 60 | glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, string[i]); |
||
| 61 | } |
||
| 62 | else { |
||
| 63 | glTranslatef(0.0, -0.07, 0.0); |
||
| 64 | glRasterPos2i(0.0, 0.0); |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | return; |
||
| 69 | } |
||
| 70 | |||
| 71 | int draw_exview( double a, double b, double r, double trans[3][4], int xwin, int ywin ) |
||
| 72 | { |
||
| 73 | double vtrans[3][4]; |
||
| 74 | double gl_para[16]; |
||
| 75 | int i, j; |
||
| 76 | |||
| 77 | argDrawMode3D(); |
||
| 78 | argDraw3dCamera( xwin, ywin ); |
||
| 79 | glDepthFunc(GL_LEQUAL); |
||
| 80 | glEnable(GL_DEPTH_TEST); |
||
| 81 | glEnable(GL_NORMALIZE); |
||
| 82 | |||
| 83 | get_trans( a, b, r, vtrans ); |
||
| 84 | argConvGlpara(vtrans, gl_para); |
||
| 85 | glMatrixMode(GL_PROJECTION); |
||
| 86 | glMultMatrixd( gl_para ); |
||
| 87 | |||
| 88 | glMatrixMode(GL_MODELVIEW); |
||
| 89 | glLoadIdentity(); |
||
| 90 | setup_light(); |
||
| 91 | |||
| 92 | glPushMatrix(); |
||
| 93 | glEnable(GL_LIGHTING); |
||
| 94 | glEnable(GL_LIGHT0); |
||
| 95 | |||
| 96 | for( j = -300; j <= 200; j+= 100 ) { |
||
| 97 | for( i = -300; i <= 200; i+= 100 ) { |
||
| 98 | glBegin(GL_QUADS); |
||
| 99 | glNormal3f( 0.0, 0.0, 1.0 ); |
||
| 100 | if( (j/100+i/100)%2 ) glColor4f( 0.6, 0.6, 0.6, 1.0 ); |
||
| 101 | else glColor4f( 0.0, 0.3, 0.0, 1.0 ); |
||
| 102 | glVertex3f( i, j, 0.0 ); |
||
| 103 | glVertex3f( i, j+100, 0.0 ); |
||
| 104 | glVertex3f( i+100, j+100, 0.0 ); |
||
| 105 | glVertex3f( i+100, j, 0.0 ); |
||
| 106 | glEnd(); |
||
| 107 | } |
||
| 108 | } |
||
| 109 | draw_axis(); |
||
| 110 | |||
| 111 | glColor4f( 0.0, 0.0, 0.5, 1.0 ); |
||
| 112 | glTranslatef( 0.0, 0.0, 25.0 ); |
||
| 113 | glutSolidCube(50.0); |
||
| 114 | |||
| 115 | glDisable( GL_LIGHTING ); |
||
| 116 | glPopMatrix(); |
||
| 117 | |||
| 118 | draw_camera( trans ); |
||
| 119 | |||
| 120 | glDisable(GL_NORMALIZE); |
||
| 121 | glDisable( GL_DEPTH_TEST ); |
||
| 122 | argDrawMode2D(); |
||
| 123 | |||
| 124 | return 0; |
||
| 125 | } |
||
| 126 | |||
| 127 | |||
| 128 | static void draw_camera( double trans[3][4] ) |
||
| 129 | { |
||
| 130 | /* |
||
| 131 | double gl_para[16]; |
||
| 132 | */ |
||
| 133 | double btrans[3][4]; |
||
| 134 | double quat[4], pos[3], angle; |
||
| 135 | |||
| 136 | arUtilMatInv( trans, btrans ); |
||
| 137 | |||
| 138 | glMatrixMode(GL_MODELVIEW); |
||
| 139 | glPushMatrix(); |
||
| 140 | arUtilMat2QuatPos( btrans, quat, pos ); |
||
| 141 | angle = -acos(quat[3])*360.0/3.141592; |
||
| 142 | glTranslatef( pos[0], pos[1], pos[2] ); |
||
| 143 | glRotated( angle, quat[0], quat[1], quat[2] ); |
||
| 144 | /* |
||
| 145 | argConvGlpara(btrans, gl_para); |
||
| 146 | glMultMatrixd( gl_para ); |
||
| 147 | */ |
||
| 148 | |||
| 149 | glEnable(GL_LIGHTING); |
||
| 150 | glEnable(GL_LIGHT0); |
||
| 151 | |||
| 152 | glPushMatrix(); |
||
| 153 | glColor4f( 0.9, 0.9, 0.0, 1.0 ); |
||
| 154 | glTranslatef( 0.0, 0.0, -10.0 ); |
||
| 155 | glScalef( 10.0, 10.0, 20.0 ); |
||
| 156 | glutSolidCube(1.0); |
||
| 157 | glPopMatrix(); |
||
| 158 | |||
| 159 | glColor4f( 0.9, 0.0, 0.9, 1.0 ); |
||
| 160 | glPushMatrix(); |
||
| 161 | glTranslatef( 0.0, 0.0, -40.0 ); |
||
| 162 | glScalef( 30.0, 30.0, 50.0 ); |
||
| 163 | glutSolidCube(1.0); |
||
| 164 | glPopMatrix(); |
||
| 165 | |||
| 166 | glDisable( GL_LIGHTING ); |
||
| 167 | glPopMatrix(); |
||
| 168 | |||
| 169 | return; |
||
| 170 | } |
||
| 171 | |||
| 172 | |||
| 173 | int draw( char *name, double trans[4][4], int xwin, int ywin ) |
||
| 174 | { |
||
| 175 | double gl_para[16]; |
||
| 176 | |||
| 177 | argConvGlpara(trans, gl_para); |
||
| 178 | draw_object( name, gl_para, xwin, ywin ); |
||
| 179 | |||
| 180 | return(0); |
||
| 181 | } |
||
| 182 | |||
| 183 | /* draw the user object */ |
||
| 184 | static int draw_object( char *name, double gl_para[16], int xwin, int ywin ) |
||
| 185 | { |
||
| 186 | argDrawMode3D(); |
||
| 187 | argDraw3dCamera( xwin, ywin ); |
||
| 188 | |||
| 189 | glDepthFunc(GL_LEQUAL); |
||
| 190 | glEnable(GL_DEPTH_TEST); |
||
| 191 | glEnable(GL_NORMALIZE); |
||
| 192 | /* load the camera transformation matrix */ |
||
| 193 | glMatrixMode(GL_PROJECTION); |
||
| 194 | glMultMatrixd( gl_para ); |
||
| 195 | |||
| 196 | glMatrixMode(GL_MODELVIEW); |
||
| 197 | glLoadIdentity(); |
||
| 198 | setup_light(); |
||
| 199 | glEnable(GL_LIGHTING); |
||
| 200 | glEnable(GL_LIGHT0); |
||
| 201 | |||
| 202 | if( strcmp(name, "target") == 0 ) { |
||
| 203 | draw_axis(); |
||
| 204 | } |
||
| 205 | else { |
||
| 206 | printf("unknown object type!!\n"); |
||
| 207 | } |
||
| 208 | |||
| 209 | glDisable( GL_LIGHTING ); |
||
| 210 | glDisable( GL_NORMALIZE ); |
||
| 211 | glDisable( GL_DEPTH_TEST ); |
||
| 212 | argDrawMode2D(); |
||
| 213 | |||
| 214 | return 0; |
||
| 215 | } |
||
| 216 | |||
| 217 | static void setup_light() |
||
| 218 | { |
||
| 219 | static int mat_f = 1; |
||
| 220 | GLfloat mat_amb_diff[] = {0.9, 0.9, 0.0, 1.0}; |
||
| 221 | GLfloat mat_specular[] = {0.5, 0.5, 0.5, 1.0}; |
||
| 222 | GLfloat mat_shininess[] = {10.0}; |
||
| 223 | GLfloat light_ambient[] = { 0.01, 0.01, 0.01, 1.0 }; |
||
| 224 | GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; |
||
| 225 | GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 }; |
||
| 226 | GLfloat light_position[] = { 100.0, 300.0, 700.0, 1.0 }; |
||
| 227 | |||
| 228 | if( mat_f ) { |
||
| 229 | glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_amb_diff); |
||
| 230 | glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); |
||
| 231 | glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); |
||
| 232 | glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); |
||
| 233 | glEnable(GL_COLOR_MATERIAL); |
||
| 234 | mat_f = 0; |
||
| 235 | } |
||
| 236 | |||
| 237 | glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); |
||
| 238 | glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); |
||
| 239 | glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); |
||
| 240 | glLightfv(GL_LIGHT0, GL_POSITION, light_position); |
||
| 241 | } |
||
| 242 | |||
| 243 | static void get_trans( double a, double b, double r, double trans[3][4] ) |
||
| 244 | { |
||
| 245 | ARMat *mat; |
||
| 246 | double sa, ca, sb, cb; |
||
| 247 | double x, y, z; |
||
| 248 | int i, j; |
||
| 249 | |||
| 250 | sa = sin(a*3.141592/180.0); |
||
| 251 | ca = cos(a*3.141592/180.0); |
||
| 252 | sb = sin(b*3.141592/180.0); |
||
| 253 | cb = cos(b*3.141592/180.0); |
||
| 254 | |||
| 255 | x = 0.0; |
||
| 256 | y = -r * cb; |
||
| 257 | z = -r * sb; |
||
| 258 | |||
| 259 | mat = arMatrixAlloc( 4, 4 ); |
||
| 260 | |||
| 261 | mat->m[0*4+0] = ca; |
||
| 262 | mat->m[0*4+1] = sa*sb; |
||
| 263 | mat->m[0*4+2] = sa*cb; |
||
| 264 | mat->m[1*4+0] = -sa; |
||
| 265 | mat->m[1*4+1] = ca*sb; |
||
| 266 | mat->m[1*4+2] = ca*cb; |
||
| 267 | mat->m[2*4+0] = 0; |
||
| 268 | mat->m[2*4+1] = -cb; |
||
| 269 | mat->m[2*4+2] = sb; |
||
| 270 | mat->m[0*4+3] = x*ca + y*sa; |
||
| 271 | mat->m[1*4+3] = -x*sa + y*ca; |
||
| 272 | mat->m[2*4+3] = z; |
||
| 273 | mat->m[3*4+0] = 0; |
||
| 274 | mat->m[3*4+1] = 0; |
||
| 275 | mat->m[3*4+2] = 0; |
||
| 276 | mat->m[3*4+3] = 1; |
||
| 277 | |||
| 278 | arMatrixSelfInv( mat ); |
||
| 279 | for( j = 0; j < 3; j++ ) { |
||
| 280 | for( i = 0; i < 4; i++ ) { |
||
| 281 | trans[j][i] = mat->m[j*4+i]; |
||
| 282 | } |
||
| 283 | } |
||
| 284 | arMatrixFree( mat ); |
||
| 285 | |||
| 286 | return; |
||
| 287 | } |
||
| 288 | |||
| 289 | static void draw_axis( void ) |
||
| 290 | { |
||
| 291 | glPushMatrix(); |
||
| 292 | glRotatef( 90.0, 0.0, 1.0, 0.0 ); |
||
| 293 | glColor4f( 1.0, 0.0, 0.0, 1.0 ); |
||
| 294 | glutSolidCone(5.0, 100.0, 20, 24); |
||
| 295 | glPopMatrix(); |
||
| 296 | |||
| 297 | glPushMatrix(); |
||
| 298 | glRotatef( -90.0, 1.0, 0.0, 0.0 ); |
||
| 299 | glColor4f( 0.0, 1.0, 0.0, 1.0 ); |
||
| 300 | glutSolidCone(5.0, 100.0, 20, 24); |
||
| 301 | glPopMatrix(); |
||
| 302 | |||
| 303 | glPushMatrix(); |
||
| 304 | glRotatef( 00.0, 0.0, 0.0, 1.0 ); |
||
| 305 | glColor4f( 0.0, 0.0, 1.0, 1.0 ); |
||
| 306 | glutSolidCone(5.0, 100.0, 20, 24); |
||
| 307 | glPopMatrix(); |
||
| 308 | |||
| 309 | return; |
||
| 310 | } |