forked from IVCenter/CSE167StarterCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindow.cpp
234 lines (194 loc) · 6.22 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
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
#include "Window.h"
int Window::width;
int Window::height;
const char* Window::windowTitle = "GLFW Starter Project";
// Objects to display.
Cube * Window::cube;
PointCloud * Window::cubePoints;
// The object currently displaying.
Object * Window::currentObj;
PointCloud * Window::bunny;
PointCloud * Window::bear;
PointCloud * Window::dragon;
PointCloud * Window::transitionPoints;
glm::mat4 Window::projection; // Projection matrix.
glm::vec3 Window::eye(0, 0, 20); // Camera position.
glm::vec3 Window::center(0, 0, 0); // The point we are looking at.
glm::vec3 Window::up(0, 1, 0); // The up direction of the camera.
// View matrix, defined by eye, center and up.
glm::mat4 Window::view = glm::lookAt(Window::eye, Window::center, Window::up);
GLuint Window::program; // The shader program id.
GLuint Window::projectionLoc; // Location of projection in shader.
GLuint Window::viewLoc; // Location of view in shader.
GLuint Window::modelLoc; // Location of model in shader.
GLuint Window::colorLoc; // Location of color in shader.
bool Window::initializeProgram() {
// Create a shader program with a vertex shader and a fragment shader.
program = LoadShaders("shaders/shader.vert", "shaders/shader.frag");
// Check the shader program.
if (!program)
{
std::cerr << "Failed to initialize shader program" << std::endl;
return false;
}
// Activate the shader program.
glUseProgram(program);
// Get the locations of uniform variables.
projectionLoc = glGetUniformLocation(program, "projection");
viewLoc = glGetUniformLocation(program, "view");
modelLoc = glGetUniformLocation(program, "model");
colorLoc = glGetUniformLocation(program, "color");
return true;
}
bool Window::initializeObjects()
{
// Create a cube of size 5.
cube = new Cube(5.0f);
// Create a point cloud consisting of cube vertices.
cubePoints = new PointCloud("foo", 100);
// Creates pointclouds of the OBJ files
bunny = new PointCloud("res/bunny.obj", 10);
bear = new PointCloud("res/bear.obj", 10);
dragon = new PointCloud("res/dragon.obj", 10);
// Set cube to be the first to display
currentObj = cube;
return true;
}
void Window::cleanUp()
{
// Deallcoate the objects.
delete cube;
delete bunny;
delete dragon;
delete bear;
delete cubePoints;
// Delete the shader program.
glDeleteProgram(program);
}
GLFWwindow* Window::createWindow(int width, int height)
{
// Initialize GLFW.
if (!glfwInit())
{
std::cerr << "Failed to initialize GLFW" << std::endl;
return NULL;
}
// 4x antialiasing.
glfwWindowHint(GLFW_SAMPLES, 4);
#ifdef __APPLE__
// Apple implements its own version of OpenGL and requires special treatments
// to make it uses modern OpenGL.
// Ensure that minimum OpenGL version is 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// Enable forward compatibility and allow a modern OpenGL context
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// Create the GLFW window.
GLFWwindow* window = glfwCreateWindow(width, height, windowTitle, NULL, NULL);
// Check if the window could not be created.
if (!window)
{
std::cerr << "Failed to open GLFW window." << std::endl;
glfwTerminate();
return NULL;
}
// Make the context of the window.
glfwMakeContextCurrent(window);
#ifndef __APPLE__
// On Windows and Linux, we need GLEW to provide modern OpenGL functionality.
// Initialize GLEW.
if (glewInit())
{
std::cerr << "Failed to initialize GLEW" << std::endl;
return NULL;
}
#endif
// Set swap interval to 1.
glfwSwapInterval(0);
// Call the resize callback to make sure things get drawn immediately.
Window::resizeCallback(window, width, height);
return window;
}
void Window::resizeCallback(GLFWwindow* window, int width, int height)
{
#ifdef __APPLE__
// In case your Mac has a retina display.
glfwGetFramebufferSize(window, &width, &height);
#endif
Window::width = width;
Window::height = height;
// Set the viewport size.
glViewport(0, 0, width, height);
// Set the projection matrix.
Window::projection = glm::perspective(glm::radians(60.0),
double(width) / (double)height, 1.0, 1000.0);
}
void Window::idleCallback()
{
// Perform any updates as necessary.
currentObj->update();
}
void Window::displayCallback(GLFWwindow* window)
{
// Clear the color and depth buffers.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Specify the values of the uniform variables we are going to use.
glm::mat4 model = currentObj->getModel();
glm::vec3 color = currentObj->getColor();
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniform3fv(colorLoc, 1, glm::value_ptr(color));
// Render the object.
currentObj->draw();
// Gets events, including input such as keyboard and mouse or window resizing.
glfwPollEvents();
// Swap buffers.
glfwSwapBuffers(window);
}
void Window::keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
/*
* TODO: Modify below to add your key callbacks.
*/
// Check for a key press.
if (action == GLFW_PRESS)
{
switch (key)
{
case GLFW_KEY_ESCAPE:
// Close the window. This causes the program to also terminate.
glfwSetWindowShouldClose(window, GL_TRUE);
break;
case GLFW_KEY_1:
// Set currentObj to cube
currentObj = cube;
break;
case GLFW_KEY_2:
// Set currentObj to cubePoints
currentObj = cubePoints;
break;
case GLFW_KEY_F1:
currentObj = bunny;
break;
case GLFW_KEY_F2:
currentObj = dragon;
break;
case GLFW_KEY_F3:
currentObj = bear;
break;
case GLFW_KEY_P:
if(mods == GLFW_MOD_SHIFT){
currentObj->updatePointSize(1);
}
else {
currentObj->updatePointSize(-1);
}
break;
default:
break;
}
}
}