Subversion Repositories AndroidProjects

Rev

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/ar.h>
23
#include "draw_object.h"
24
#include "object.h"
25
 
26
/* material properties */
27
GLfloat mat_specular1[] = {0.2, 0.0, 0.0, 1.0};
28
GLfloat mat_shininess1[] = {50.0};
29
GLfloat mat_specular2[] = {0.0, 0.0, 0.2, 1.0};
30
GLfloat mat_shininess2[] = {25.0};
31
 
32
GLfloat mat_ambient1[] = {1.0, 0.0, 0.0, 1.0};
33
GLfloat mat_ambient2[] = {0.0, 0.0, 1.0, 1.0};
34
GLfloat mat_flash_ambient1[] = {0.0, 1.0, 0.0, 1.0};
35
 
36
GLfloat mat_flash1[] = {1.0, 0.0, 0.0, 1.0};
37
GLfloat mat_flash_shiny1[] = {25.0};
38
GLfloat mat_flash2[] = {0.0, 0.0, 1.0, 1.0};
39
GLfloat mat_flash_shiny2[] = {50.0};
40
 
41
static int  draw_object( char *name, double gl_para[16], int dispmode);
42
 
43
/* draw the the AR objects */
44
int draw( ObjectData_T *object, int objectnum, int dispmode )
45
{
46
    int     i;
47
    double  gl_para[16];
48
 
49
    /* calculate the viewing parameters - gl_para */
50
    for( i = 0; i < objectnum; i++ ) {
51
        if( object[i].visible == 0 ) continue;
52
 
53
        argConvGlpara(object[i].trans, gl_para);
54
 
55
        /* draw the object */
56
        draw_object( object[i].name, gl_para, dispmode);
57
    }
58
 
59
    return(0);
60
}
61
 
62
/* draw the user object */
63
static int  draw_object( char *name, double gl_para[16], int dispmode)
64
{
65
    int    i;
66
 
67
    argDrawMode3D();
68
    glDepthFunc(GL_LEQUAL);
69
    glEnable(GL_DEPTH_TEST);
70
 
71
    /* if in 3D display mode render views
72
       from both eye - otherwise from one eye */
73
    for( i = 0; i < 3; i++ ) {
74
        if( dispmode == 1 ) {
75
            switch(i) {
76
                case 0:  argDraw3dLeft();  break;
77
                case 1:  argDraw3dRight(); break;
78
                case 2:  argDraw3dCamera( 1, 1 ); break;
79
            }
80
        }
81
        else argDraw3dCamera( 0, 0 );
82
 
83
        /* load the camera transformation matrix */
84
        glMatrixMode(GL_MODELVIEW);
85
        glLoadMatrixd( gl_para );
86
        init_lights();
87
 
88
 
89
        /* draw the user object here
90
           - using the object name to select the object */
91
        if( strcmp(name, "torus") == 0 ) {
92
          /* set object color */
93
          glEnable(GL_LIGHTING);
94
          glEnable(GL_LIGHT0);
95
          glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash1);
96
          glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny1);
97
          glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient1);
98
 
99
          /* draw a simple torus */
100
          glMatrixMode(GL_MODELVIEW);
101
          glTranslatef( 0.0, 0.0, 10.0 );
102
          glutSolidTorus(10.0, 40.0, 24, 24);
103
          glDisable( GL_LIGHTING );
104
        }
105
        else if( strcmp(name, "sphere") == 0 ) {
106
          glEnable(GL_LIGHTING);
107
          glEnable(GL_LIGHT0);
108
          glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash1);
109
          glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny1);
110
          glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient1);
111
 
112
          /* draw a sphere */
113
          glMatrixMode(GL_MODELVIEW);
114
          glTranslatef( 0.0, 0.0, 40.0 );
115
          glutSolidSphere(40.0, 24, 24);
116
          glDisable( GL_LIGHTING );
117
        }
118
        else if( strcmp(name, "cube") == 0 ) {
119
          glEnable(GL_LIGHTING);
120
          glEnable(GL_LIGHT0);
121
          glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash2);
122
          glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny2);
123
          glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient2);
124
 
125
          glMatrixMode(GL_MODELVIEW);
126
          glTranslatef( 0.0, 0.0, 25.0 );
127
          glutSolidCube(50.0);
128
          glDisable( GL_LIGHTING );
129
        }
130
        else if( strcmp(name, "cone") == 0 ) {
131
          glEnable(GL_LIGHTING);
132
          glEnable(GL_LIGHT0);
133
          glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash1);
134
          glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny1);
135
          glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient1);
136
 
137
          glMatrixMode(GL_MODELVIEW);
138
          glutSolidCone(25.0, 100.0, 20, 24);
139
          glDisable( GL_LIGHTING );
140
        }
141
        else {
142
          printf("unknown object type!!\n");
143
        }
144
 
145
        if( dispmode == 0 ) break;
146
        if( arDebug == 0 && i == 1 ) break;
147
    }
148
 
149
    glDisable( GL_DEPTH_TEST );
150
    argDrawMode2D();
151
 
152
    return 0;
153
}
154
 
155
/* initialize the lights in the scene */
156
void init_lights()
157
{
158
    GLfloat light_position[] = {0.0,-200.0,0.0,0.0};
159
    GLfloat ambi[] = {0.1, 0.1, 0.1, 0.1};
160
    GLfloat lightZeroColor[]    = {0.9, 0.9, 0.9, 0.1};
161
 
162
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
163
    glLightfv(GL_LIGHT0, GL_AMBIENT, ambi);
164
    glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
165
}