-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathlab4.cpp
More file actions
241 lines (191 loc) · 6.84 KB
/
Copy pathlab4.cpp
File metadata and controls
241 lines (191 loc) · 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// fragment shading of sphere model
#include "Angel.h"
#include <iostream>
typedef Angel::vec4 point4;
typedef Angel::vec4 color4;
// Model-view and projection matrices uniform location
GLuint ModelView, Projection;
// Create camera view variables
point4 at( 0.0, 0.0, 0.0, 1.0 );
point4 eye( 0.0, 0.0, 0.0, 1.0 );
vec4 up( 0.0, 10.0, 0.0, 0.0 );
// Sperical Coordinate vector (r, theta, phi)
vec3 sphericaleye( 4.0, M_PI/4.0, M_PI/4.0 ) ;
GLfloat size=1;
GLfloat norm=1/sqrt(3.0); // 1 / sqrt(1^2 + 1^2 + 1^2)
GLfloat vertexarray[]={
size,size,-size,
size,-size,-size,
-size,-size,-size,
-size,size,-size,
size,size,size,
size,-size,size,
-size,-size,size,
-size,size,size
};
GLfloat normalarray[]={
norm,norm,-norm,
norm,-norm,-norm,
-norm,-norm,-norm,
-norm,norm,-norm,
norm,norm,norm,
norm,-norm,norm,
-norm,-norm,norm,
-norm,norm,norm
};
GLubyte elems[]={
7,3,4,0,1,3,2,
7,6,4,5,1,6,2,1
};
//----------------------------------------------------------------------------
// OpenGL initialization
void
init()
{
// Create a vertex array object
GLuint vao, vbo, ebo;
glGenVertexArrays( 1,&vao);
glBindVertexArray( vao );
// Create Vertex and Normal buffer
glGenBuffers(1,&vbo);
glBindBuffer(GL_ARRAY_BUFFER,vbo);
glBufferData(GL_ARRAY_BUFFER,sizeof(vertexarray) + sizeof(normalarray),NULL,GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER,0,sizeof(vertexarray),vertexarray);
glBufferSubData(GL_ARRAY_BUFFER,sizeof(vertexarray),sizeof(normalarray),normalarray);
// Load shaders and use the resulting shader program
GLuint program = InitShader( "vshader56.glsl", "fshader56.glsl" );
glUseProgram( program );
// set up vertex arrays
GLuint vPosition = glGetAttribLocation(program, "vPosition" );
glVertexAttribPointer(vPosition,3,GL_FLOAT,GL_FALSE,0,BUFFER_OFFSET(0));
glEnableVertexAttribArray( vPosition );
GLuint vNormal = glGetAttribLocation(program, "vNormal" );
glVertexAttribPointer(vNormal,3,GL_FLOAT,GL_FALSE,0,BUFFER_OFFSET(sizeof(vertexarray)));
glEnableVertexAttribArray( vNormal );
// Create Element Array Buffer
glGenBuffers(1,&ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(elems),elems,GL_STATIC_DRAW);
// Initialize shader lighting parameters for light 1
point4 light_position1( -0.5, -1.0, 2.0, 0.0 );
color4 light_ambient1( 0.3, 0.2, 0.2, 1.0 );
color4 light_diffuse1( 0.9, 0.6, 0.6, 1.0 );
color4 light_specular1( 1.0, 0.6, 0.6, 1.0 );
// Initialize shader lighting parameters for light 2
point4 light_position2( 0.5, 1.0, -1.0, 0.0 );
color4 light_ambient2( 0.2, 0.2, 0.3, 1.0 );
color4 light_diffuse2( 0.6, 0.6, 0.9, 1.0 );
color4 light_specular2( 0.6, 0.6, 1.0, 1.0 );
// Initialize shader material parameters for cube
color4 material_ambient( 0.4, 0.25, 0.1, 1.0 );
color4 material_diffuse( 0.8, 0.5, 0.2, 1.0 );
color4 material_specular( 0.8, 0.8, 0.8, 1.0 );
float material_shininess = 16.0;
color4 ambient_product1 = (light_ambient1) * material_ambient;
color4 diffuse_product1 = (light_diffuse1) * material_diffuse;
color4 specular_product1 = (light_specular1) * material_specular;
color4 ambient_product2 = (light_ambient2) * material_ambient;
color4 diffuse_product2 = (light_diffuse2) * material_diffuse;
color4 specular_product2 = (light_specular2) * material_specular;
glUniform4fv( glGetUniformLocation(program, "AmbientProduct1"),
1, ambient_product1 );
glUniform4fv( glGetUniformLocation(program, "AmbientProduct2"),
1, ambient_product2 );
glUniform4fv( glGetUniformLocation(program, "DiffuseProduct1"),
1, diffuse_product1 );
glUniform4fv( glGetUniformLocation(program, "DiffuseProduct2"),
1, diffuse_product2 );
glUniform4fv( glGetUniformLocation(program, "SpecularProduct1"),
1, specular_product1 );
glUniform4fv( glGetUniformLocation(program, "SpecularProduct2"),
1, specular_product2 );
glUniform4fv( glGetUniformLocation(program, "LightPosition1"),
1, light_position1 );
glUniform4fv( glGetUniformLocation(program, "LightPosition2"),
1, light_position2 );
glUniform1f( glGetUniformLocation(program, "Shininess"),
material_shininess );
// Retrieve transformation uniform variable locations
ModelView = glGetUniformLocation( program, "ModelView" );
Projection = glGetUniformLocation( program, "Projection" );
glEnable( GL_DEPTH_TEST );
glClearColor( 1.0, 1.0, 1.0, 1.0 ); /* white background */
}
//----------------------------------------------------------------------------
void
display( void )
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
// Create new eye vector from sphericaleye vector
eye.z = sphericaleye.x * cos(sphericaleye.y) * sin(sphericaleye.z);
eye.x = sphericaleye.x * sin(sphericaleye.y) * sin(sphericaleye.z);
eye.y = sphericaleye.x * cos(sphericaleye.z);
std::cout << "Eye: (" << eye.x << "," << eye.y << "," << eye.z << ") \t";
std::cout << "Spherical Eye: (" << sphericaleye.x << "," << sphericaleye.y << "," << sphericaleye.z << ")" << std::endl;
mat4 model_view = LookAt( eye, at, up );
glUniformMatrix4fv( ModelView, 1, GL_TRUE, model_view );
glDrawElements( GL_TRIANGLE_STRIP,sizeof(elems),GL_UNSIGNED_BYTE,NULL);
glutSwapBuffers();
}
//----------------------------------------------------------------------------
void
keyboard( unsigned char key, int x, int y )
{
switch( key ) {
case 033: // Escape Key
case 'q': case 'Q':
exit( EXIT_SUCCESS );
break;
case 'w': case 'W':
sphericaleye.z+=M_PI/64.0;
break;
case 'a': case 'A':
sphericaleye.y-=M_PI/64.0;
break;
case 's': case 'S':
sphericaleye.z-=M_PI/64.0;
break;
case 'd': case 'D':
sphericaleye.y+=M_PI/64.0;
break;
}
glutPostRedisplay();
}
//----------------------------------------------------------------------------
void
reshape( int width, int height )
{
glViewport( 0, 0, width, height );
GLfloat left = -2.0, right = 2.0;
GLfloat top = 2.0, bottom = -2.0;
GLfloat zNear = -20.0, zFar = 20.0;
GLfloat aspect = GLfloat(width)/height;
if ( aspect > 1.0 ) {
left *= aspect;
right *= aspect;
}
else {
top /= aspect;
bottom /= aspect;
}
mat4 projection = Ortho( left, right, bottom, top, zNear, zFar );
glUniformMatrix4fv( Projection, 1, GL_TRUE, projection );
}
//----------------------------------------------------------------------------
int
main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH );
glutInitWindowSize( 512, 512 );
glutInitContextVersion( 2, 1 );
glutInitContextProfile( GLUT_CORE_PROFILE );
glutCreateWindow( "Schwarz - lab4" );
glewInit();
init();
glutDisplayFunc( display );
glutReshapeFunc( reshape );
glutKeyboardFunc( keyboard );
glutMainLoop();
return 0;
}