-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathteapot_vision.cpp
More file actions
367 lines (311 loc) · 12.4 KB
/
teapot_vision.cpp
File metadata and controls
367 lines (311 loc) · 12.4 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//
// Michael Shafae
// mshafae at fullerton.edu
//
// A toy program which renders a teapot and two light sources.
//
//
#include <tuple>
#include <cstdlib>
#include <cstdio>
#include <sys/time.h>
#include <glm/vec3.hpp>
#include <glm/vec4.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/matrix_inverse.hpp>
#include "GLFWApp.h"
#include "GLSLShader.h"
#include "glut_teapot.h"
#include "SpinningLight.h"
#include "Camera.h"
#include "UtahTeapot.h"
void msglVersion(void){
fprintf(stderr, "OpenGL Version Information:\n");
fprintf(stderr, "\tVendor: %s\n", glGetString(GL_VENDOR));
fprintf(stderr, "\tRenderer: %s\n", glGetString(GL_RENDERER));
fprintf(stderr, "\tOpenGL Version: %s\n", glGetString(GL_VERSION));
fprintf(stderr, "\tGLSL Version: %s\n",
glGetString(GL_SHADING_LANGUAGE_VERSION));
}
class TeapotVisionApp : public GLFWApp{
private:
float rotationDelta;
glm::vec3 centerPosition;
Camera* currentCamera;
// main point of view camera
Camera mainCamera;
// Bird's eye view camera
Camera bevCamera;
glm::mat4 modelViewMatrix;
glm::mat4 projectionMatrix;
glm::mat4 normalMatrix;
GLSLProgram shaderProgram;
SpinningLight light0;
SpinningLight light1;
UtahTeapot* teapots[20];
const int teapotCount = 20;
bool debugMaterialFlag;
// Variables to set uniform params for lighting fragment shader
unsigned int uModelViewMatrix;
unsigned int uProjectionMatrix;
unsigned int uNormalMatrix;
unsigned int uLight0_position;
unsigned int uLight0_color;
unsigned int uLight1_position;
unsigned int uLight1_color;
unsigned int uAmbient;
unsigned int uDiffuse;
unsigned int uSpecular;
unsigned int uShininess;
public:
TeapotVisionApp(int argc, char* argv[]) :
GLFWApp(argc, argv, std::string("Teapot Vision").c_str( ),
600, 600){ }
void initCenterPosition( ){
centerPosition = glm::vec3(0.0, 0.0, 0.0);
}
void initTeapots( ){
std::srand(time(NULL));
for(int i = 0; i < teapotCount; i++){
glm::vec3 _diffuseColor = glm::linearRand(glm::vec3(0.2), glm::vec3(1.0));
//std::cerr << glm::to_string(_diffuseColor) << std::endl;
glm::vec4 diffuseColor = glm::vec4(_diffuseColor, 1.0);
Material* m = new Material(glm::vec4(0.2, 0.2, 0.2, 1.0), diffuseColor, glm::vec4(1.0, 1.0, 1.0, 1.0), 100.0);
glm::vec2 xy = glm::diskRand(30.0);
glm::vec3 position = glm::vec3(xy, 0.0);
teapots[i] = new UtahTeapot(position, 1.0, m);
}
}
void initCamera( ){
// Main point of view camera
mainCamera = Camera(glm::vec3(0.0, 0.0, 20.0), glm::vec3(0.0, 1.0, 0.0), glm::vec3(0.0, 0.0, 0.0), 45.0, 0.2, 50.0);
// Bird's eye view camera
bevCamera = Camera(glm::vec3(0.0, 0.0, 70.0), glm::vec3(0.0, 1.0, 0.0), glm::vec3(0.0, 0.0, 0.0), 45.0, 10.0, 150.0);
currentCamera = &mainCamera;
}
void initRotationDelta( ){
rotationDelta = 0.05;
}
void initLights( ){
glm::vec3 color0(1.0, 1.0, 1.0);
glm::vec3 position0(0.0, 20.0, 20.0);
glm::vec3 color1(1.0, 1.0, 1.0);
glm::vec3 position1(0.0, 20.0, -20.0);
light0 = SpinningLight(color0, position0, centerPosition);
light1 = SpinningLight(color1, position1, centerPosition);
}
bool begin( ){
msglError( );
initCenterPosition( );
initTeapots( );
initCamera( );
initRotationDelta( );
initLights( );
debugMaterialFlag = false;
// Load shader programs
const char* vertexShaderSource = "blinn_phong.vert.glsl";
const char* fragmentShaderSource = "blinn_phong.frag.glsl";
FragmentShader fragmentShader(fragmentShaderSource);
VertexShader vertexShader(vertexShaderSource);
shaderProgram.attach(vertexShader);
shaderProgram.attach(fragmentShader);
shaderProgram.link( );
shaderProgram.activate( );
printf("Shader program built from %s and %s.\n",
vertexShaderSource, fragmentShaderSource);
if( shaderProgram.isActive( ) ){
printf("Shader program is loaded and active with id %d.\n", shaderProgram.id( ) );
}else{
printf("Shader program is not active, id: %d\n.", shaderProgram.id( ));
}
// Set up uniform variables for the shader program
uModelViewMatrix = glGetUniformLocation(shaderProgram.id( ), "modelViewMatrix");
uProjectionMatrix = glGetUniformLocation(shaderProgram.id( ), "projectionMatrix");
uNormalMatrix = glGetUniformLocation(shaderProgram.id( ), "normalMatrix");
uLight0_position = glGetUniformLocation(shaderProgram.id( ), "light0_position");
uLight0_color = glGetUniformLocation(shaderProgram.id( ), "light0_color");
uLight1_position = glGetUniformLocation(shaderProgram.id( ), "light1_position");
uLight1_color = glGetUniformLocation(shaderProgram.id( ), "light1_color");
uAmbient = glGetUniformLocation(shaderProgram.id( ), "ambient");
uDiffuse = glGetUniformLocation(shaderProgram.id( ), "diffuse");
uSpecular = glGetUniformLocation(shaderProgram.id( ), "specular");
uShininess = glGetUniformLocation(shaderProgram.id( ), "shininess");
glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
msglVersion( );
return !msglError( );
}
bool end( ){
windowShouldClose( );
return true;
}
void activateUniforms(glm::vec4& _light0, glm::vec4& _light1, Material* m){
glUniformMatrix4fv(uModelViewMatrix, 1, false, glm::value_ptr(modelViewMatrix));
glUniformMatrix4fv(uProjectionMatrix, 1, false, glm::value_ptr(projectionMatrix));
glUniformMatrix4fv(uNormalMatrix, 1, false, glm::value_ptr(normalMatrix));
glUniform4fv(uLight0_position, 1, glm::value_ptr(_light0));
glUniform4fv(uLight0_color, 1, glm::value_ptr(light0.color( )));
glUniform4fv(uLight1_position, 1, glm::value_ptr(_light1));
glUniform4fv(uLight1_color, 1, glm::value_ptr(light1.color( )));
glUniform4fv(uAmbient, 1, glm::value_ptr(m->ambient));
glUniform4fv(uDiffuse, 1, glm::value_ptr(m->diffuse));
glUniform4fv(uSpecular, 1, glm::value_ptr(m->specular));
glUniform1f(uShininess, m->shininess);
}
void checkVisibility(glm::mat4 clipPlaneMatrix){
// For the sake of this assignment, a teapot is defined to be
// inside the view frustum if and only if the position data
// member is within the volume defined by the view frustum.
glm::mat4 lookAtMatrix;
mainCamera.lookAtMatrix(lookAtMatrix); //camera.h line 272
for(int i = 0; i < teapotCount; i++){
UtahTeapot* ut = teapots[i]; //utah tepot datatype
//calc look at matrix (bring teapot into cam space)
// teapot position * lookatmatrix ()
glm::vec4 cameraSpaceTeapot = lookAtMatrix*glm::vec4(ut->position, 1.0);
glm::vec4 pv = clipPlaneMatrix*cameraSpaceTeapot;
// do something here to check to see if the teapots
// are in or out of the view frustum. Set the visibility
if(-pv.w < pv.x && pv.x < pv.w && -pv.w < pv.y && pv.y < pv.w && -pv.w < pv.z && pv.z < pv.w)
teapots[i]->visible = true;// flag as needed.
else
teapots[i]->visible = false;
}
}
bool render( ){
glm::vec4 _light0;
glm::vec4 _light1;
glm::mat4 lookAtMatrix;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
std::tuple<int, int> w = windowSize( );
double ratio = double(std::get<0>(w)) / double(std::get<1>(w));
glm::mat4 clipPlaneMatrix;
mainCamera.perspectiveMatrix(clipPlaneMatrix, ratio);
checkVisibility(clipPlaneMatrix);
currentCamera->perspectiveMatrix(projectionMatrix, ratio);
currentCamera->lookAtMatrix(lookAtMatrix);
// Set light & material properties for the teapot;
// lights are transformed by current modelview matrix
// such that they are positioned correctly in the scene.
_light0 = lookAtMatrix * light0.position4( );
_light1 = lookAtMatrix * light1.position4( );
if(currentCamera == &mainCamera){
for(int i = 0; i < teapotCount; i++){
if(teapots[i]->visible){
// If the teapot is visible and it's in the main camera mode
// then draw the teapot; otherwise don't
modelViewMatrix = glm::translate(lookAtMatrix, teapots[i]->position);
//modelViewMatrix = lookAtMatrix;
normalMatrix = glm::inverseTranspose(modelViewMatrix);
shaderProgram.activate( );
activateUniforms(_light0, _light1, teapots[i]->material);
//no_lightShaderProgram.activate( );
teapots[i]->draw( );
}
}
}else{
// If this is the bird's eye view then draw everything
// but with different materials
Material redMaterial = Material(glm::vec4(0.2, 0.2, 0.2, 1.0), glm::vec4(1.0, 0.0, 0.0, 1.0), glm::vec4(1.0, 1.0, 1.0, 1.0), 100.0);
Material whiteMaterial = Material(glm::vec4(0.2, 0.2, 0.2, 1.0), glm::vec4(1.0, 1.0, 1.0, 1.0), glm::vec4(1.0, 1.0, 1.0, 1.0), 100.0);
Material yellowMaterial = Material(glm::vec4(0.2, 0.2, 0.2, 1.0), glm::vec4(1.0, 1.0, 0.0, 1.0), glm::vec4(1.0, 1.0, 1.0, 1.0), 100.0);
Material blueMaterial = Material(glm::vec4(0.2, 0.2, 0.2, 1.0), glm::vec4(0.0, 0.0, 1.0, 1.0), glm::vec4(1.0, 1.0, 1.0, 1.0), 100.0);
Material* currentMaterial = &redMaterial;
shaderProgram.activate( );
for(int i = 0; i < teapotCount; i++){
// multiply the lookAtMatrix with the teapot's translation
// to position the teapot in the right spot.
modelViewMatrix = glm::translate(lookAtMatrix, teapots[i]->position);
normalMatrix = glm::inverseTranspose(modelViewMatrix);
if(teapots[i]->visible){
currentMaterial = &redMaterial;
}else{
currentMaterial = &whiteMaterial;
}
if(debugMaterialFlag){
activateUniforms(_light0, _light1, teapots[i]->material);
}else{
activateUniforms(_light0, _light1, currentMaterial);
}
teapots[i]->draw( );
}
modelViewMatrix = glm::translate(lookAtMatrix, mainCamera.eyePosition);
normalMatrix = glm::inverseTranspose(modelViewMatrix);
activateUniforms(_light0, _light1, &yellowMaterial);
mainCamera.draw( );
mainCamera.drawViewFrustum(ratio);
modelViewMatrix = glm::translate(lookAtMatrix, light0.position);
normalMatrix = glm::inverseTranspose(modelViewMatrix);
activateUniforms(_light0, _light1, &blueMaterial);
light0.draw( );
modelViewMatrix = glm::translate(lookAtMatrix, light1.position);
normalMatrix = glm::inverseTranspose(modelViewMatrix);
activateUniforms(_light0, _light1, &blueMaterial);
light1.draw( );
}
if(isKeyPressed('Q')){
end( );
}else if(isKeyPressed(GLFW_KEY_EQUAL)){
}else if(isKeyPressed(GLFW_KEY_MINUS)){
}else if(isKeyPressed('R')){
/*initEyePosition( );
initUpVector( );*/
initCamera( );
initRotationDelta( );
initLights( );
printf("Eye position, up vector and rotation delta reset.\n");
}else if(isKeyPressed(GLFW_KEY_LEFT)){
mainCamera.rotateCameraRight( );
}else if(isKeyPressed(GLFW_KEY_RIGHT)){
mainCamera.rotateCameraLeft( );
}else if(isKeyPressed(GLFW_KEY_UP)){
mainCamera.rotateCameraDown( );
}else if(isKeyPressed(GLFW_KEY_DOWN)){
mainCamera.rotateCameraUp( );
}else if(isKeyPressed('W')){
light0.rotateUp( );
}else if(isKeyPressed('S')){
light0.rotateUp( );
}else if(isKeyPressed('A')){
light0.rotateLeft( );
}else if(isKeyPressed('D')){
light0.rotateLeft( );
}else if(isKeyPressed('X')){
light0.roll( );
}else if(isKeyPressed('Y')){
light1.rotateUp( );
}else if(isKeyPressed('H')){
light1.rotateUp( );
}else if(isKeyPressed('G')){
light1.rotateLeft( );
}else if(isKeyPressed('J')){
light1.rotateLeft( );
}else if(isKeyPressed('N')){
light1.roll( );
}else if(isKeyPressed('O')){
mainCamera.forward( );
}else if(isKeyPressed('L')){
mainCamera.backward( );
}else if(isKeyPressed('K')){
mainCamera.panLeft( );
}else if(isKeyPressed(';')){
mainCamera.panRight( );
}else if(isKeyPressed('1')){
light0.toggle( );
}else if(isKeyPressed('2')){
light1.toggle( );
}else if(isKeyPressed('3')){
debugMaterialFlag = !debugMaterialFlag;
}else if(isKeyPressed('P')){
currentCamera = &mainCamera;
}else if(isKeyPressed('B')){
currentCamera = &bevCamera;
}
return !msglError( );
}
};
int main(int argc, char* argv[]){
TeapotVisionApp app(argc, argv);
return app();
}