-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWindow.cpp
190 lines (150 loc) · 4.26 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
#include "Window.h"
// Window Properties
int Window::width;
int Window::height;
const char* Window::windowTitle = "GLFW Starter Project";
// Objects to Render
Cube * Window::cube;
PointCloud * Window::cubePoints;
Object* currObj;
// Camera Matrices
// Projection matrix:
glm::mat4 Window::projection;
// View Matrix:
glm::vec3 Window::eyePos(0, 0, 20); // Camera position.
glm::vec3 Window::lookAtPoint(0, 0, 0); // The point we are looking at.
glm::vec3 Window::upVector(0, 1, 0); // The up direction of the camera.
glm::mat4 Window::view = glm::lookAt(Window::eyePos, Window::lookAtPoint, Window::upVector);
// Shader Program ID
GLuint Window::shaderProgram;
bool Window::initializeProgram() {
// Create a shader program with a vertex shader and a fragment shader.
shaderProgram = LoadShaders("shaders/shader.vert", "shaders/shader.frag");
// Check the shader program.
if (!shaderProgram)
{
std::cerr << "Failed to initialize shader program" << std::endl;
return false;
}
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);
// Set cube to be the first to display
currObj = cube;
return true;
}
void Window::cleanUp()
{
// Deallcoate the objects.
delete cube;
delete cubePoints;
// Delete the shader program.
glDeleteProgram(shaderProgram);
}
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 necessary updates here
currObj->update();
}
void Window::displayCallback(GLFWwindow* window)
{
// Clear the color and depth buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Render the objects
currObj->draw(view, projection, shaderProgram);
// 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;
// switch between the cube and the cube pointCloud
case GLFW_KEY_1:
currObj = cube;
break;
case GLFW_KEY_2:
currObj = cubePoints;
break;
default:
break;
}
}
}