Skip to content

Commit dad80c3

Browse files
committed
Finishing up a5
1 parent b2f42a3 commit dad80c3

File tree

3 files changed

+70
-29
lines changed

3 files changed

+70
-29
lines changed

CS637/a5/.teapot.cpp.swp

-16 KB
Binary file not shown.

CS637/a5/teapot.cpp

Lines changed: 66 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,37 @@ typedef Angel::vec4 point4;
1515

1616
const int NumVertices = 6320*3;
1717
point4 vertices[3644];
18-
vec3 normals[6320];
1918
point4 points[NumVertices];
19+
vec3 normals[NumVertices];
2020
//color4 colors[NumVertices];
2121

2222
enum Projection {PARALLEL, PERSPECTIVE};
2323
int currentProjection;
24+
mat4 projection;
2425
GLuint program;
2526

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+
2631
GLfloat delta;
2732
GLfloat r_x, r_y, r_z;
2833
GLfloat s_x, s_y, s_z;
2934
GLfloat t_x, t_y, t_z;
3035

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+
3145
// OpenGL initialization
3246
void
3347
init()
3448
{
35-
//colorcube();
36-
3749
// Create a vertex array object
3850
GLuint vao;
3951
glGenVertexArrays( 1, &vao );
@@ -57,7 +69,7 @@ init()
5769
glVertexAttribPointer( vPosition, 4, GL_FLOAT, GL_FALSE, 0,
5870
BUFFER_OFFSET(0) );
5971

60-
GLuint vNormal = glGetAttribLocation( program, "vNormal" );
72+
GLuint vNormal = glGetAttribLocation( program, "vNormal" );
6173
glEnableVertexAttribArray( vNormal );
6274
glVertexAttribPointer( vNormal, 3, GL_FLOAT, GL_FALSE, 0,
6375
BUFFER_OFFSET(sizeof(points)) );
@@ -66,16 +78,18 @@ init()
6678
glClearColor( 1.0, 1.0, 1.0, 1.0 );
6779

6880
t_x = 0.0;
69-
t_y = -1.0;
81+
t_y = 0.0;
7082
t_z = 0.0;
7183
r_x = 0.0;
7284
r_y = 0.0;
7385
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;
7789
delta = 0.01;
7890

91+
projection = Ortho( o_left, o_right, bottom, top, zNear, zFar );
92+
7993
}
8094

8195
//----------------------------------------------------------------------------
@@ -95,6 +109,17 @@ display( void )
95109
glUniform1f(glGetUniformLocation( program, "s_y" ), s_y);
96110
glUniform1f(glGetUniformLocation( program, "s_z" ), s_z);
97111

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+
98123
glDrawArrays( GL_TRIANGLES, 0, NumVertices );
99124

100125
glutSwapBuffers();
@@ -105,29 +130,44 @@ display( void )
105130
void
106131
keyboard( unsigned char key, int x, int y )
107132
{
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+
117142
}
118143
glutPostRedisplay();
119144
}
120145

121146
//----------------------------------------------------------------------------
122147

123-
void processMenuEvents(int option)
148+
void myidle()
124149
{
125-
currentProjection = option;
150+
if (animate)
151+
{
152+
theta += 3.14159/camspeed;
153+
theta = fmodf(theta, (2*3.14159));
154+
glutPostRedisplay();
155+
}
126156
}
127157

128-
void CalculateSurfaceNormal(){
158+
//----------------------------------------------------------------------------
129159

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();
131171
}
132172

133173
int
@@ -147,7 +187,7 @@ main( int argc, char **argv )
147187
{
148188
switch (t) {
149189
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);
151191
v_i++;
152192
break;
153193
case 'f' :
@@ -171,11 +211,9 @@ main( int argc, char **argv )
171211
n_y = Normal_y / NormalisationFactor;
172212
n_z = Normal_z / NormalisationFactor;
173213

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 );
179217

180218
f_i += 3;
181219
break;
@@ -196,6 +234,7 @@ main( int argc, char **argv )
196234
init();
197235

198236
glutDisplayFunc( display );
237+
glutIdleFunc(myidle);
199238
glutKeyboardFunc( keyboard );
200239

201240
glutMainLoop();

CS637/a5/vshader36.glsl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ attribute vec3 vNormal;
33
attribute vec4 vColor;
44
uniform float r_x, r_y, r_z, s_x, s_y, s_z, t_x, t_y, t_z;
55
varying vec4 color;
6+
uniform mat4 model_view;
7+
uniform mat4 projection;
68

79
mat4 scale() {
810
return mat4(s_x, 0, 0, 0,
@@ -42,10 +44,10 @@ mat4 trans() {
4244

4345
void main()
4446
{
45-
gl_Position = scale() * rotate() * trans() * vPosition;
47+
gl_Position = model_view * projection * scale() * rotate() * trans() * vPosition;
4648

4749
vec3 lightDir;
48-
lightDir = normalize(vec3(0.0, 1.0, 2.0));
50+
lightDir = normalize(vec3(3.0, 3.0, -3.0));
4951

5052
float NdotL;
5153
NdotL = max(dot(vNormal, lightDir), 0.0);

0 commit comments

Comments
 (0)