-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplication.cpp
More file actions
214 lines (166 loc) · 5.44 KB
/
Application.cpp
File metadata and controls
214 lines (166 loc) · 5.44 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
#include <memory>
#include <functional>
// GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtx/matrix_operation.hpp>
#include "glm/ext.hpp"
#include "Application.h"
int Application::SCREEN_WIDTH = 0;
int Application::SCREEN_HEIGHT = 0;
// Camera
Camera Application::camera = Camera::Camera(glm::vec3(0.0f, 0.0f, 0.0f));;
double Application::lastX = WIDTH / 2.0;
double Application::lastY = HEIGHT / 2.0;
bool Application::firstMouse = true;
bool Application::keys[1024];
bool Application::pauseSimulation = false;
Application::Application()
{
m_shader = Shader();// "resources/shaders/core.vert", "resources/shaders/core.frag");
}
Application::~Application()
{
}
// Moves/alters the camera positions based on user input
void Application::doMovement(GLfloat deltaTime)
{
glfwPollEvents();
// Camera controls
if (keys[GLFW_KEY_W] || keys[GLFW_KEY_UP])
{
camera.ProcessKeyboard(FORWARD, deltaTime);
}
if (keys[GLFW_KEY_S] || keys[GLFW_KEY_DOWN])
{
camera.ProcessKeyboard(BACKWARD, deltaTime);
}
if (keys[GLFW_KEY_A] || keys[GLFW_KEY_LEFT])
{
camera.ProcessKeyboard(LEFT, deltaTime);
}
if (keys[GLFW_KEY_D] || keys[GLFW_KEY_RIGHT])
{
camera.ProcessKeyboard(RIGHT, deltaTime);
}
}
// Is called whenever a key is pressed/released via GLFW
static void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mode)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, GL_TRUE);
}
if (key == GLFW_KEY_P && action == GLFW_PRESS)
{
Application::pauseSimulation = !Application::pauseSimulation;
}
if (key >= 0 && key < 1024)
{
if (action == GLFW_PRESS)
{
Application::keys[key] = true;
}
else if (action == GLFW_RELEASE)
{
Application::keys[key] = false;
}
}
}
static void mouseCallback(GLFWwindow *window, double xPos, double yPos)
{
if (Application::firstMouse)
{
Application::lastX = xPos;
Application::lastY = yPos;
Application::firstMouse = false;
}
double xOffset = xPos - Application::lastX;
double yOffset = Application::lastY - yPos;
Application::lastX = xPos;
Application::lastY = yPos;
Application::camera.ProcessMouseMovement((GLfloat)xOffset, (GLfloat)yOffset);
}
static void scrollCallback(GLFWwindow *window, double xOffset, double yOffset)
{
Application::camera.ProcessMouseScroll((GLfloat)yOffset);
}
// Renderer initialisation
int Application::initRender() {
// Init GLFW
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
m_window = glfwCreateWindow(WIDTH, HEIGHT, "Physics-Based Animation", nullptr, nullptr);
if (nullptr == m_window)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return EXIT_FAILURE;
}
glfwMakeContextCurrent(m_window);
glfwGetFramebufferSize(m_window, &SCREEN_WIDTH, &SCREEN_HEIGHT);
// Set the required callback functions
glfwSetKeyCallback(m_window, keyCallback);
glfwSetCursorPosCallback(m_window, mouseCallback);
glfwSetScrollCallback(m_window, scrollCallback);
// remove the mouse cursor
glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// moder GLEW approach
glewExperimental = GL_TRUE;
// Initialize GLEW to setup the OpenGL Function pointers
if (GLEW_OK != glewInit())
{
std::cout << "Failed to initialize GLEW" << std::endl;
return EXIT_FAILURE;
}
// Define the viewport dimensions
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
// Setup some OpenGL options
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
return 1;
}
// clear buffer
void Application::clear() {
// Clear the colorbuffer
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
// draw mesh
void Application::draw(const Mesh &mesh)
{
mesh.getShader().Use();
// view and projection matrices
m_projection = glm::perspective(camera.GetZoom(), (GLfloat)SCREEN_WIDTH / (GLfloat)SCREEN_HEIGHT, 0.1f, 1000.0f);
m_view = camera.GetViewMatrix();
// Get the uniform locations for MVP
GLint modelLoc = glGetUniformLocation(mesh.getShader().Program, "model");
GLint viewLoc = glGetUniformLocation(mesh.getShader().Program, "view");
GLint projLoc = glGetUniformLocation(mesh.getShader().Program, "projection");
GLint rotateLoc = glGetUniformLocation(mesh.getShader().Program, "rotate");
// get the uniform locations for lighing
GLint ambientLoc = glGetUniformLocation(mesh.getShader().Program, "ambient");
GLint eyePositionLoc = glGetUniformLocation(mesh.getShader().Program, "eyePosition");
// Pass the matrices to the shader
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(mesh.getModel()));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(m_view));
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(m_projection));
glUniformMatrix4fv(rotateLoc, 1, GL_FALSE, glm::value_ptr(mesh.getRotate()));
// pass lighting data to shader
glUniform4fv(ambientLoc, 1, glm::value_ptr(glm::vec4(0.0, 1.0, 1.0, 1.0)));
glUniform3fv(eyePositionLoc, 1, glm::value_ptr(Application::camera.getPosition()));
glBindVertexArray(mesh.getVertexArrayObject());
glDrawArrays(GL_TRIANGLES, 0, mesh.getNumIndices());
glBindVertexArray(0);
}
void Application::display() {
glBindVertexArray(0);
// Swap the buffers
glfwSwapBuffers(m_window);
}