@@ -15,25 +15,37 @@ typedef Angel::vec4 point4;
15
15
16
16
const int NumVertices = 6320 *3 ;
17
17
point4 vertices[3644 ];
18
- vec3 normals[6320 ];
19
18
point4 points[NumVertices];
19
+ vec3 normals[NumVertices];
20
20
// color4 colors[NumVertices];
21
21
22
22
enum Projection {PARALLEL, PERSPECTIVE};
23
23
int currentProjection;
24
+ mat4 projection;
24
25
GLuint program;
25
26
27
+ GLfloat o_left = -1.0 , o_right = 1.0 ;
28
+ GLfloat bottom = -1.0 , top = 1.0 ;
29
+ GLfloat zNear = 0.5 , zFar = 3.0 ;
30
+
26
31
GLfloat delta;
27
32
GLfloat r_x, r_y, r_z;
28
33
GLfloat s_x, s_y, s_z;
29
34
GLfloat t_x, t_y, t_z;
30
35
36
+ GLfloat radius = 1.0 ;
37
+ GLfloat theta = 0.0 ;
38
+ GLfloat phi = 0.0 ;
39
+ GLfloat height = 0.5 ;
40
+
41
+ int camspeed = 60 ;
42
+
43
+ bool animate = true ;
44
+
31
45
// OpenGL initialization
32
46
void
33
47
init ()
34
48
{
35
- // colorcube();
36
-
37
49
// Create a vertex array object
38
50
GLuint vao;
39
51
glGenVertexArrays ( 1 , &vao );
57
69
glVertexAttribPointer ( vPosition, 4 , GL_FLOAT, GL_FALSE, 0 ,
58
70
BUFFER_OFFSET (0 ) );
59
71
60
- GLuint vNormal = glGetAttribLocation ( program, " vNormal" );
72
+ GLuint vNormal = glGetAttribLocation ( program, " vNormal" );
61
73
glEnableVertexAttribArray ( vNormal );
62
74
glVertexAttribPointer ( vNormal, 3 , GL_FLOAT, GL_FALSE, 0 ,
63
75
BUFFER_OFFSET (sizeof (points)) );
@@ -66,16 +78,18 @@ init()
66
78
glClearColor ( 1.0 , 1.0 , 1.0 , 1.0 );
67
79
68
80
t_x = 0.0 ;
69
- t_y = - 1 .0 ;
81
+ t_y = 0 .0 ;
70
82
t_z = 0.0 ;
71
83
r_x = 0.0 ;
72
84
r_y = 0.0 ;
73
85
r_z = 0.0 ;
74
- s_x = 0.3 ;
75
- s_y = 0.3 ;
76
- s_z = 0.3 ;
86
+ s_x = 0.2 ;
87
+ s_y = 0.2 ;
88
+ s_z = 0.2 ;
77
89
delta = 0.01 ;
78
90
91
+ projection = Ortho ( o_left, o_right, bottom, top, zNear, zFar );
92
+
79
93
}
80
94
81
95
// ----------------------------------------------------------------------------
@@ -95,6 +109,17 @@ display( void )
95
109
glUniform1f (glGetUniformLocation ( program, " s_y" ), s_y);
96
110
glUniform1f (glGetUniformLocation ( program, " s_z" ), s_z);
97
111
112
+ glUniformMatrix4fv (glGetUniformLocation ( program, " projection" ), 1 , GL_TRUE, projection );
113
+
114
+ point4 eye ( radius*sin (theta)*cos (phi),
115
+ radius*sin (theta)*sin (phi),
116
+ radius*cos (theta),
117
+ 1.0 );
118
+ point4 at ( 0.0 , height, 0.0 , 0.0 );
119
+ vec4 up ( 0.0 , 1.0 , 0.0 , 0.0 );
120
+ mat4 mv = LookAt ( eye, at, up );
121
+ glUniformMatrix4fv ( glGetUniformLocation ( program, " model_view" ), 1 , GL_TRUE, mv );
122
+
98
123
glDrawArrays ( GL_TRIANGLES, 0 , NumVertices );
99
124
100
125
glutSwapBuffers ();
@@ -105,29 +130,44 @@ display( void )
105
130
void
106
131
keyboard ( unsigned char key, int x, int y )
107
132
{
108
- switch ( currentProjection ) {
109
- case PARALLEL:
110
- switch ( key ) {
111
- }
112
- break ;
113
- case PERSPECTIVE:
114
- switch ( key ) {
115
- }
116
- break ;
133
+ switch ( key ) {
134
+ case ' s ' : camspeed += 1 ; break ;
135
+ case ' S ' : camspeed -= 1 ; if (camspeed < 1 ) { camspeed = 1 ; } break ;
136
+ case ' r ' : radius *= 2.0 ; break ;
137
+ case ' R ' : radius *= 0.5 ; break ;
138
+ case ' h ' : height += 0.1 ; break ;
139
+ case ' H ' : height *= 0.1 ; break ;
140
+ case ' ' : animate = !animate; break ;
141
+
117
142
}
118
143
glutPostRedisplay ();
119
144
}
120
145
121
146
// ----------------------------------------------------------------------------
122
147
123
- void processMenuEvents ( int option )
148
+ void myidle ( )
124
149
{
125
- currentProjection = option;
150
+ if (animate)
151
+ {
152
+ theta += 3.14159 /camspeed;
153
+ theta = fmodf (theta, (2 *3.14159 ));
154
+ glutPostRedisplay ();
155
+ }
126
156
}
127
157
128
- void CalculateSurfaceNormal (){
158
+ // ----------------------------------------------------------------------------
129
159
130
- return ;
160
+ void processMenuEvents (int option)
161
+ {
162
+ switch ( option ) {
163
+ case PARALLEL:
164
+ projection = Ortho ( o_left, o_right, bottom, top, zNear, zFar);
165
+ break ;
166
+ case PERSPECTIVE:
167
+ projection = Perspective (90.0 , 1.0 , 0.5 , 3.0 );
168
+ break ;
169
+ }
170
+ glutPostRedisplay ();
131
171
}
132
172
133
173
int
@@ -147,7 +187,7 @@ main( int argc, char **argv )
147
187
{
148
188
switch (t) {
149
189
case ' v' :
150
- vertices[v_i] = point4 ( atof (x.c_str ()), atof (y.c_str ()), atof (z.c_str ()), 1.0 );
190
+ vertices[v_i]= point4 (atof (x.c_str ()),atof (y.c_str ()),atof (z.c_str ())- 10.0 , 1.0 );
151
191
v_i++;
152
192
break ;
153
193
case ' f' :
@@ -171,11 +211,9 @@ main( int argc, char **argv )
171
211
n_y = Normal_y / NormalisationFactor;
172
212
n_z = Normal_z / NormalisationFactor;
173
213
174
- normals[f_i/3 ] = vec3 ( n_x, n_y, n_z );
175
-
176
- // colors[f_i] = color4( 1.0, 0.0, 0.0, 1.0 );
177
- // colors[f_i+1] = color4( 1.0, 0.0, 0.0, 1.0 );
178
- // colors[f_i+2] = color4( 1.0, 0.0, 0.0, 1.0 );
214
+ normals[f_i] = vec3 ( n_x, n_y, n_z );
215
+ normals[f_i+1 ] = vec3 ( n_x, n_y, n_z );
216
+ normals[f_i+2 ] = vec3 ( n_x, n_y, n_z );
179
217
180
218
f_i += 3 ;
181
219
break ;
@@ -196,6 +234,7 @@ main( int argc, char **argv )
196
234
init ();
197
235
198
236
glutDisplayFunc ( display );
237
+ glutIdleFunc (myidle);
199
238
glutKeyboardFunc ( keyboard );
200
239
201
240
glutMainLoop ();
0 commit comments