-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindow.cpp
More file actions
executable file
·472 lines (371 loc) · 14.6 KB
/
Window.cpp
File metadata and controls
executable file
·472 lines (371 loc) · 14.6 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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#include <iostream>
#include <GL/glew.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include "Window.h"
#include "Cube.h"
#include "Matrix4.h"
#include "Globals.h"
#include "Sphere.h"
#include "Group.h"
#include "Frustum.h"
#include "SkyBox.h"
#include "Game.h"
int Window::width = 900; //Set window width in pixels here
int Window::height = 900; //Set window height in pixels here
float Window::spinDelta = .005;
enum Control {
OBJ = 0, POINT = 1, SPOT = 2, DIRECT = 3, CAMERA = 4
};
static Control keyState = CAMERA;
static Game* game = new Game();
static Shader* phongShader= NULL;
static Shader* geometryPassShader = NULL;
static Shader* deferredPassShader = NULL;
GLuint positionID;
GLuint normalID;
GLuint specID;
GLuint gPosition;
GLuint gNormal;
GLuint gSpec;
GLuint depthBuffer;
GLuint gBuffer;
void Window::initialize(void) {
glewInit();
//Setup the light
Globals::point.setPosition(Vector4(-66, -100, -66.5, 500));
Globals::point.quadraticAttenuation = 1.15;
Globals::point.constantAttenuation = 5.85;
Globals::point.linearAttenuation = 5.85;
Globals::spotLight.setPosition(Vector4(-2.5, -2.5, 7.5, 0));
Globals::directional.setPosition(Vector4(0, 0, 5555, 0));
Globals::directional.scaleIntensity(.25f);
phongShader = new Shader("VShader.c", "LShader.c");
geometryPassShader = new Shader("gPass.vert", "gPass.frag");
deferredPassShader = new Shader("deferredPass.vert", "deferredPass.frag");
glEnable(GL_NORMALIZE);
positionID = glGetUniformLocationARB(deferredPassShader->getPid(),"gPosition");
normalID = glGetUniformLocationARB(deferredPassShader->getPid(),"gNormal");
specID = glGetUniformLocationARB(deferredPassShader->getPid(),"gSpec");
cameraID = glGetUniformLocationARB(deferredPassShader->getPid(),"cameraPosition");
worldMatrixID = glGetUniformLocationARB(geometryPassShader->getPid(),"WorldMatrix");
setupGBuffer();
}
void Window::idleCallback() {
static int frameCount = 0;
static int currentTime = 0, previousTime = 0;
static int lastFrame = 0;
++frameCount;
// Get the number of milliseconds since glutInit called
// (or first call to glutGet(GLUT ELAPSED TIME)).
currentTime = glutGet(GLUT_ELAPSED_TIME);
// Calculate time passed
int timeInterval = currentTime - previousTime;
if (timeInterval > 1000) {
// calculate the number of frames per second
float fps = frameCount / (timeInterval / 1000.0f);
std::cout << "FPS: " << fps << std::endl;
// Set time
previousTime = currentTime;
// Reset frame count
frameCount = 0;
}
//Set up a static time delta for update calls
Globals::updateData.dt = (currentTime - lastFrame) / 1000.0; // 60 fps
lastFrame = currentTime;
static int tick = 0;
game->update(++tick);
displayCallback();
}
void Window::reshapeCallback(int w, int h) {
width = w; //Set the window width
height = h; //Set the window height
glViewport(0, 0, w, h); //Set new viewport size
glMatrixMode(GL_PROJECTION); //Set the OpenGL matrix mode to Projection
glLoadIdentity(); //Clear the projection matrix by loading the identity
gluPerspective(60.0, double(width) / (double) height, .5, 256.0); //Set perspective projection viewing frustum
Frustum::update(width, height);
}
void Window::displayCallback() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(Globals::camera.getInverseMatrix().ptr());
glPushMatrix();
//Replace the current top of the matrix stack with the inverse camera matrix
//This will convert all world coordiantes into camera coordiantes
glLoadMatrixf(Globals::camera.getInverseMatrix().ptr());
//Globals::directional.bind();
/* Globals::spotLight.bind();
Globals::point.setPosition(game->ball->getTranslation());
Globals::point.bind();
game->ball->ballLight.setPosition(game->ball->getTranslation());
game->ball->ballLight.bind();
*/
MatrixStack stack(Matrix4::identity());
// MatrixStack s2 = stack;
//phongShader->bind();
//Write to the gBuffer
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, gBuffer);
glPushAttrib(GL_VIEWPORT_BIT); //Saves viewport info easier
glViewport(0,0,width, height); //Makes sure we render a size matching the framebuffer
GLenum buffers[] = { GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT };
glDrawBuffers(3, buffers);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
geometryPassShader->bind(); //Write to the gBuffer using the geometryPassShader
game->draw(stack); //Render image
geometryPassShader->unbind();
glPopAttrib();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
//render using 2nd pass shader
////////////////////////////////////////////////////////////////
//////Code taken directly from source code//////////////////////
//Projection setup
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
deferredPassShader->bind();
glActiveTextureARB(GL_TEXTURE0_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, gPosition);
glUniform1iARB ( positionID, 0 );
glActiveTextureARB(GL_TEXTURE1_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, gNormal);
glUniform1iARB ( normalID, 1 );
glActiveTextureARB(GL_TEXTURE2_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, gSpec);
glUniform1iARB ( specID, 2 );
glUniform3fv(cameraID, 3, Globals::camera.e);
renderQuad();
// Reset OpenGL state
glActiveTextureARB(GL_TEXTURE0_ARB);
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTextureARB(GL_TEXTURE1_ARB);
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTextureARB(GL_TEXTURE2_ARB);
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
deferredPassShader->unbind();
/*
//Reset to the matrices
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
/*
//phongShader->unbind();
//Globals::point.draw(s2);
// Globals::point.draw(Globals::drawData);
// Globals::spotLight.draw(Globals::drawData);
//Pop off the changes we made to the matrix stack this frame
*/
glPopMatrix();
// glFlush();
glutSwapBuffers();
}
/*
* Helper Function
* Sets up the GBuffer
*/
void Window::setupGBuffer() {
//Setting up G-buffer
//Buffer is necessary so that shader can access data from all pixels when evaluating a certain pixel
glGenFramebuffersEXT(1, &gBuffer);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, gBuffer);
//Set up the 3 textures that go in the buffer
// Generate and bind the OGL texture for positions
glGenTextures(1, &gPosition);
glBindTexture(GL_TEXTURE_2D, gPosition);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, Window::width, Window::height, 0, GL_RGBA, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, gPosition, 0);
// Generate and bind the OGL texture for normals
glGenTextures(1, &gNormal);
glBindTexture(GL_TEXTURE_2D, gNormal);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, Window::width, Window::height, 0, GL_RGBA, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_2D, gNormal, 0);
// Generate and bind the OGL texture for diffuse
glGenTextures(1, &gSpec);
glBindTexture(GL_TEXTURE_2D, gSpec);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Window::width, Window::height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_TEXTURE_2D, gSpec, 0);
/*
GLenum buffers[] = { GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT };
glDrawBuffers(3, buffers);
*/
GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if( status != GL_FRAMEBUFFER_COMPLETE_EXT)
std::cout << "Error with framebuffer or something" << std::endl;
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
}
GLuint quadVAO = 0;
GLuint quadVBO = 0;
void Window::renderQuad() {
//Projection setup
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0,Window::width,0,Window::height,0.1f,2);
//Model setup
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
// Render the quad
glLoadIdentity();
glColor3f(1,1,1);
glTranslatef(0,0,-1.0);
glBegin(GL_QUADS);
glTexCoord2f( 0, 0 );
glVertex3f( 0.0f, 0.0f, 0.0f);
glTexCoord2f( 1, 0 );
glVertex3f( (float) Window::width, 0.0f, 0.0f);
glTexCoord2f( 1, 1 );
glVertex3f( (float) Window::width, (float) Window::height, 0.0f);
glTexCoord2f( 0, 1 );
glVertex3f( 0.0f, (float) Window::height, 0.0f);
glEnd();
//Reset to the matrices
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}
const float scaleFactor = .95;
const float orbitDelta = 3.14159265 / 180 * 5; // 3.141592 / 180 == 1 degree;
const float moveDelta = .1;
const Matrix4 xt = Matrix4().makeTranslate(moveDelta, 0, 0);
const Matrix4 nxt = Matrix4().makeTranslate(-moveDelta, 0, 0);
const Matrix4 yt = Matrix4().makeTranslate(0, moveDelta, 0);
const Matrix4 nyt = Matrix4().makeTranslate(0, -moveDelta, 0);
const Matrix4 zt = Matrix4().makeTranslate(0, 0, moveDelta);
const Matrix4 nzt = Matrix4().makeTranslate(0, 0, -moveDelta);
const Matrix4 scalaUp = Matrix4().makeScale(1 / scaleFactor, 1 / scaleFactor, 1 / scaleFactor);
const Matrix4 scalaDown = Matrix4().makeScale(scaleFactor, scaleFactor, scaleFactor);
const Matrix4 orbit = Matrix4().makeRotateZ(orbitDelta);
const Matrix4 reverseOrbit = Matrix4().makeRotateZ(-orbitDelta);
bool Window::handleKeypress(unsigned char key) {
if (key >= '0' && key <= '4'){
keyState = (Control) (key - '0');
return true;
}
switch (key) {
case 'b': Globals::isBoundsOn = !Globals::isBoundsOn;
break;
case 'A': Globals::isAnimated = !Globals::isAnimated;
break;
default: return false;
}
return true;
}
void Window::specialKeys(int key, int x, int y) {
if (keyState == CAMERA) {
switch (key) {
case GLUT_KEY_UP:
Globals::camera.rotateX(10 * spinDelta);
return;
case GLUT_KEY_DOWN:
Globals::camera.rotateX(10 * -spinDelta);
return;
case GLUT_KEY_LEFT:
Globals::camera.rotateY(10 * spinDelta);
return;
case GLUT_KEY_RIGHT:
Globals::camera.rotateY(-10 * spinDelta);
return;
}
}
}
class Listener : public Mouse {
Vector3 lastTrack;
public:
Listener() {
lastTrack = Vector3(0, 0, 0);
}
Vector3 trackBallMapping(Point point) {
Vector3 v((2.0 * point.x - Window::width) / Window::width, (Window::height - 2.0 * point.y) / Window::height, 0);
double d = v.magnitude(); // this is the distance from the trackball's origin to the mouse location, without considering depth (=in the plane of the trackball's origin)
d = (d < 1.0) ? d : 1.0; // this limits d to values of 1.0 or less to avoid square roots of negative values in the following line
v.set(2, sqrt(1.001 - d * d));
return v.normalize();
}
void onLeftClick() override {
}
void onRightClick() override {
}
void onMiddleClick() override {
}
void onLeftDrag(Point prev, Point next) override {
Vector3 mapped = trackBallMapping(next);
Vector3 cross = lastTrack.cross(mapped);
cross = cross.normalize();
lastTrack = mapped;
Matrix4 r = Matrix4().makeRotateArbitrary(cross, .05);
switch (keyState) {
case OBJ:
break;
case POINT:
Globals::point.toWorld.applyOther(r);
break;
case SPOT:
Globals::spotLight.toWorld.applyOther(r);
break;
case DIRECT:
Globals::directional.toWorld.applyOther(r);
break;
case CAMERA:
Globals::camera.translate((prev.toV() - next.toV()).multiply(Vector3(.1, -.1, 1)));
}
}
void onRightDrag(Point prev, Point next) override {
Point diff = next.minus(prev);
Vector3 dp = diff.scale(1.0 / 25);
if (keyState == SPOT) {
if (abs(diff.x) > abs(diff.y)) {
Globals::spotLight.exp += (diff.x > 0) ? 1 : -1;
} else {
Globals::spotLight.angle += (diff.y > 0) ? 1 : -1;
}
}
}
void onMouseScroll(State direction) override {
if (direction == UP) {
if (keyState == POINT) {
Vector3 dist = Globals::point.toWorld.getColumn(3).toVector3();
Globals::point.setPosition(dist.scale(1.05).toVector4(1) + Vector4(0, 0, .1, 0));
} else if (keyState == SPOT) {
Vector3 dist = Globals::spotLight.toWorld.getColumn(3).toVector3();
Globals::spotLight.setPosition(dist.scale(1.05).toVector4(1) + Vector4(0, 0, .1, 0));
} else if (keyState == CAMERA) {
Globals::camera.translate(Vector3(0, 0, -1));
}
} else {
if (keyState == POINT) {
Vector3 dist = Globals::point.toWorld.getColumn(3).toVector3();
Globals::point.setPosition(dist.scale(.95).toVector4(1)+ Vector4(0, 0, -.1, 0));
} else if (keyState == SPOT) {
Vector3 dist = Globals::spotLight.toWorld.getColumn(3).toVector3();
Globals::spotLight.setPosition(dist.scale(.95).toVector4(1)+ Vector4(0, 0, -.1, 0));
} else if (keyState == CAMERA) {
Globals::camera.translate(Vector3(0, 0, 1));
}
}
}
};
static Mouse* mouseListener = new Listener();
//TODO: Mouse callbacks!
//TODO: Mouse Motion callbacks!