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
#if defined(_WIN32)
9
#include <windows.h>
10
#else
11
#include <strings.h>
12
#endif
13
#ifndef __APPLE__
14
#include <GL/gl.h>
15
#include <GL/glut.h>
16
#else
17
#include <OpenGL/gl.h>
18
#include <GLUT/glut.h>
19
#endif
20
#include <AR/gsub.h>
21
#include "draw_object.h"
22
#include "object.h"
23
 
24
/* material properties */
25
GLfloat mat_specular1[] = {0.2, 0.0, 0.0, 1.0};
26
GLfloat mat_shininess1[] = {50.0};
27
GLfloat mat_specular2[] = {0.0, 0.0, 0.2, 1.0};
28
GLfloat mat_shininess2[] = {25.0};
29
 
30
GLfloat mat_ambient1[] = {1.0, 0.0, 0.0, 1.0};
31
GLfloat mat_ambient2[] = {0.0, 0.0, 1.0, 1.0};
32
GLfloat mat_flash_ambient1[] = {0.0, 1.0, 0.0, 1.0};
33
 
34
GLfloat mat_flash1[] = {1.0, 0.0, 0.0, 1.0};
35
GLfloat mat_flash_shiny1[] = {25.0};
36
GLfloat mat_flash2[] = {0.0, 0.0, 1.0, 1.0};
37
GLfloat mat_flash_shiny2[] = {50.0};
38
 
39
static int  draw_object( char *name, double gl_para[16] );
40
static void init_lights( void );
41
 
42
/* draw the the AR objects */
43
int draw( ObjectData_T *object, int objectnum )
44
{
45
    int     i;
46
    double  gl_para[16];
47
 
48
    /* calculate the viewing parameters - gl_para */
49
    for( i = 0; i < objectnum; i++ ) {
50
        if( object[i].visible == 0 ) continue;
51
 
52
        argConvGlpara(object[i].trans, gl_para);
53
 
54
        /* draw the object */
55
        draw_object( object[i].name, gl_para );
56
    }
57
 
58
    return(0);
59
}
60
 
61
/* draw the user object */
62
static int  draw_object( char *name, double gl_para[16] )
63
{
64
    argDrawMode3D();
65
    argDraw3dCamera( 0, 0 );
66
    glDepthFunc(GL_LEQUAL);
67
    glEnable(GL_DEPTH_TEST);
68
 
69
    /* load the camera transformation matrix */
70
    glMatrixMode(GL_MODELVIEW);
71
    glLoadMatrixd( gl_para );
72
    init_lights();
73
 
74
 
75
    /* draw the user object here
76
       - using the object name to select the object */
77
    if( strcmp(name, "torus") == 0 ) {
78
      /* set object color */
79
      glEnable(GL_LIGHTING);
80
      glEnable(GL_LIGHT0);
81
      glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash1);
82
      glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny1);  
83
      glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient1);
84
 
85
      /* draw a simple torus */
86
      glMatrixMode(GL_MODELVIEW);
87
      glTranslatef( 0.0, 0.0, 10.0 );
88
      glutSolidTorus(10.0, 40.0, 24, 24);
89
      glDisable( GL_LIGHTING );
90
    }
91
    else if( strcmp(name, "sphere") == 0 ) {
92
      glEnable(GL_LIGHTING);
93
      glEnable(GL_LIGHT0);
94
      glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash1);
95
      glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny1);  
96
      glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient1);
97
 
98
      /* draw a sphere */
99
      glMatrixMode(GL_MODELVIEW);
100
      glTranslatef( 0.0, 0.0, 40.0 );
101
      glutSolidSphere(40.0, 24, 24);
102
      glDisable( GL_LIGHTING );
103
    }
104
    else if( strcmp(name, "cube") == 0 ) {
105
      glEnable(GL_LIGHTING);
106
      glEnable(GL_LIGHT0);
107
      glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash2);
108
      glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny2);  
109
      glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient2);
110
 
111
      glMatrixMode(GL_MODELVIEW);
112
      glTranslatef( 0.0, 0.0, 25.0 );
113
      glutSolidCube(50.0);
114
      glDisable( GL_LIGHTING );
115
    }
116
    else if( strcmp(name, "cone") == 0 ) {
117
      glEnable(GL_LIGHTING);
118
      glEnable(GL_LIGHT0);
119
      glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash1);
120
      glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny1);  
121
      glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient1);
122
 
123
      glMatrixMode(GL_MODELVIEW);
124
      glutSolidCone(25.0, 50.0, 20, 24);
125
      glDisable( GL_LIGHTING );
126
    }
127
    else {
128
      printf("unknown object type!!\n");
129
    }
130
 
131
    glDisable( GL_DEPTH_TEST );
132
 
133
    return 0;
134
}
135
 
136
/* initialize the lights in the scene */
137
static void init_lights( void )
138
{
139
    GLfloat light_position[] = {0.0,-200.0,0.0,0.0};
140
    GLfloat ambi[] = {0.1, 0.1, 0.1, 0.1};
141
    GLfloat lightZeroColor[]    = {0.9, 0.9, 0.9, 0.1};
142
 
143
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
144
    glLightfv(GL_LIGHT0, GL_AMBIENT, ambi);
145
    glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
146
}