/
Window.cpp
184 lines (145 loc) · 5.91 KB
/
Window.cpp
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
#include <iostream>
#include "GLee.h"
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
//#include <GL/freeglut.h>
#include "Window.h"
#include "sphere.h"
#include "OBJObject.h"
#include "Matrix4.h"
#include "Globals.h"
#include "Texture.h"
#include "Shader.h"
int Window::width = 512; //Set window width in pixels here
int Window::height = 512; //Set window height in pixels here
bool shading = true;
Shader * shader;
void Window::initialize(void)
{
//Initialize skybox textures
Globals::skybox = *new Skybox();
Globals::skybox.top = new Texture("skybox_top.ppm");
Globals::skybox.base = new Texture("skybox_base.ppm");
Globals::skybox.front = new Texture("skybox_front.ppm");
Globals::skybox.back = new Texture("skybox_back.ppm");
Globals::skybox.right = new Texture("skybox_right.ppm");
Globals::skybox.left = new Texture("skybox_left.ppm");
Globals::bezier.flag = new Texture("Ucsd-logo.ppm");
Globals::bezier2.flag = new Texture("Ucsd-logo.ppm");
//Setup the light
Vector4 lightPos(0.0, 10.0, 15.0, 1.0);
Globals::light.position = lightPos;
Globals::light.quadraticAttenuation = 0.0002;
//Globals::camera.set(Vector3(0, 0, 1), Vector3(0, 0, 0), Vector3(0, 1, 0));
//Patch 1
Vector3 a = Vector3(-.5, -.25, .3); Vector3 b = Vector3(-.5, 0, .1); Vector3 c = Vector3(-.5, .25, .3); Vector3 d = Vector3(-.5, .5, .2);
Vector3 e = Vector3(-.25, -.25, .4); Vector3 f = Vector3(-.25, 0, .2); Vector3 g = Vector3(-.25, .25, .2); Vector3 h = Vector3(-.25, .5, .4);
Vector3 i = Vector3(0, -.25, .5); Vector3 j = Vector3(0, 0, .4); Vector3 k = Vector3(0, .25, .1); Vector3 l = Vector3(0, .5, .4);
Vector3 m = Vector3(.25, -.25, .2); Vector3 n = Vector3(.25, 0, .2); Vector3 o = Vector3(.25, .25, .5); Vector3 p = Vector3(.25, .5, .4);
//Patch 2
Vector3 q = Vector3(.5, -.25, 2 * m[2] - i[2]); Vector3 r = Vector3(.5, 0, 2*n[2] - j[2]); Vector3 s = Vector3(.5, .25, 2*o[2] - k[2]); Vector3 t = Vector3(.5, .5, 2*p[2] - l[2]);
Vector3 u = Vector3(.75, -.25, .3); Vector3 v = Vector3(.75, 0, .1); Vector3 w = Vector3(.75, .25, .7); Vector3 x = Vector3(.75, .5, .2);
Vector3 y = Vector3(1, -.25, .8); Vector3 z = Vector3(1, 0, .9); Vector3 aa = Vector3(1, .25, .3); Vector3 bb = Vector3(1, .5, .4);
Globals::bezier.setCurve(0, a, b, c, d);
Globals::bezier.setCurve(1, e, f, g, h);
Globals::bezier.setCurve(2, i, j, k, l);
Globals::bezier.setCurve(3, m, n, o, p);
Globals::bezier2.setCurve(0, m, n, o, p);
Globals::bezier2.setCurve(1, q, r, s, t);
Globals::bezier2.setCurve(2, u, v, w, x);
Globals::bezier2.setCurve(3, y, z, aa, bb);
shader = new Shader("shader.vert", "shader.frag", true);
}
//----------------------------------------------------------------------------
// Callback method called when system is idle.
// This is called at the start of every new "frame" (qualitatively)
void Window::idleCallback()
{
//Set up a static time delta for update calls
Globals::updateData.dt = 1.0/60.0;// 60 fps
//Call the display routine to draw the sphere
displayCallback();
}
//----------------------------------------------------------------------------
// Callback method called by GLUT when graphics window is resized by the user
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, 1.0, 1000.0); //Set perspective projection viewing frustum
}
float t = 0;
//----------------------------------------------------------------------------
// Callback method called by GLUT when window readraw is necessary or when glutPostRedisplay() was called.
void Window::displayCallback()
{
//rasterizeVertex
//Clear color and depth buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Set the OpenGL matrix mode to ModelView
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(Globals::camera.getInverseMatrix().ptr());
glPushMatrix();
Globals::light.bind(0);
Globals::bezier.update(t);
Globals::bezier2.update2(t, &Globals::bezier);
GLint loc = glGetUniformLocation(shader->getPid(), "cameraPosition");
if(loc != -1)
glUniform3f(loc, Globals::camera.e[0], Globals::camera.e[1], Globals::camera.e[2]);
shader->unbind();
Globals::skybox.draw();
if (shading == true) {
shader->bind();
}
Globals::bezier.drawPatches();
Globals::bezier2.drawPatches();
glPopMatrix();
glFlush();
glutSwapBuffers();
t += .1;
}
//TODO: Keyboard callbacks!
void Window::processNormalKeys(unsigned char key, int x, int y) {
//The d key is pressed
if (key == 100) {
Matrix4 rotate;
rotate.identity();
rotate = rotate.makeRotateY(-.05);
//Rotate camera right
Globals::camera.set(rotate * Globals::camera.e, Globals::camera.d, Globals::camera.up);
}
//The a key is pressed
if (key == 97) {
Matrix4 rotate;
rotate.identity();
rotate = rotate.makeRotateY(.05);
//Rotate camera left
Globals::camera.set(rotate * Globals::camera.e, Globals::camera.d, Globals::camera.up);
}
//The w key is pressed
if (key == 119) {
Matrix4 rotate;
rotate.identity();
rotate = rotate.makeRotateX(-.05);
//Rotate camera left
Globals::camera.set(rotate * Globals::camera.e, Globals::camera.d, Globals::camera.up);
}
//The letter 's' is pressed, zoom in
if (key == 115) {
Matrix4 rotate;
rotate.identity();
rotate = rotate.makeRotateX(.05);
//Rotate camera left
Globals::camera.set(rotate * Globals::camera.e, Globals::camera.d, Globals::camera.up);
}
//If the letter t is pressed, turn off animation
if (key == 116) {
shading = !shading;
}
}