-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.cpp
336 lines (277 loc) · 10.7 KB
/
app.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
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
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <imgui/imgui.h>
#include <imgui/imgui_impl_glfw.h>
#include <imgui/imgui_impl_opengl3.h>
#include "shader.h"
#include "model.h"
#include "mesh.h"
#include "renderer.h"
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb_image_write.h>
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <filesystem>
// Window + Controls
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
// Rendering
void writeFrame(GLuint FBO, const std::string& filename);
void renderSpin(const int numFrames, GLuint FBO, const std::string& filename);
// Camera initialisation
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
float pitch = 0.0f, yaw = -90.0f, fov = 45.0f;
// Mouse initialisation
float lastX = 400, lastY = 300;
bool firstMouse = true;
bool mouseActive = false;
// UI settings
bool spinning = false;
std::string filename = "output/test.png";
// Frame timing
float deltaTime = 0.0f;
float lastFrame = 0.0f;
// Render dimensions
unsigned int RENDER_SIZE_X = 360, RENDER_SIZE_Y = 270;
unsigned int WINDOW_SIZE_X = 800, WINDOW_SIZE_Y = 600;
// Quad object that fills whole screen, for use with render textures
float fullscreenQuad[] = {
// positions // texCoords
1.0f, -1.0f, 1.0f, 0.0f,
-1.0f, -1.0f, 0.0f, 0.0f,
-1.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 0.0f, 1.0f
};
// Main window
int main()
{
// Initialise GLFW and hint settings
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Enable this for Mac OS X compatibility
#endif
// Create OpenGL window
GLFWwindow* window = glfwCreateWindow(WINDOW_SIZE_X/2, WINDOW_SIZE_Y/2, "Campfire", NULL, NULL);
if(window == NULL)
{
std::cout << "Failed to create a window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// Attach callbacks
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetKeyCallback(window, key_callback);
glfwSetCursorPosCallback(window, mouse_callback);
// Initialise GLAD
if(!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// Load shaders
Shader shader1("assets/shaders/shader.vert", "assets/shaders/shader.frag");
Shader screenShader("assets/shaders/screenshader.vert", "assets/shaders/screenshader.frag");
// Load models
Model testModel(filesystem::path("assets/models/mario-kart/F2_Item_Kart_Mario_S.dae"));
// Create and bind frame buffer object
unsigned int FBO;
glGenFramebuffers(1, &FBO); // We will render to this and then use it as a texture for our fullscreen quad
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
// Create frame buffer texture and attach to FBO
unsigned int framebufferTexture;
glGenTextures(1, &framebufferTexture);
glBindTexture(GL_TEXTURE_2D, framebufferTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, RENDER_SIZE_X, RENDER_SIZE_Y, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, framebufferTexture, 0);
// Create render buffer
unsigned int RBO;
glGenRenderbuffers(1, &RBO);
glBindRenderbuffer(GL_RENDERBUFFER, RBO);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, RENDER_SIZE_X, RENDER_SIZE_Y);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, RBO);
auto fboStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (fboStatus != GL_FRAMEBUFFER_COMPLETE)
std::cout << "ERROR::FRAMEBUFFER::" << fboStatus << std::endl;
// Screen quad
unsigned int quadVAO, quadVBO;
glGenVertexArrays(1, &quadVAO);
glGenBuffers(1, &quadVBO);
glBindVertexArray(quadVAO);
glBindBuffer(GL_ARRAY_BUFFER, quadVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(fullscreenQuad), &fullscreenQuad, GL_STATIC_DRAW);
glEnableVertexAttribArray(0); // Set position
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1); // Set UV coords
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float)));
screenShader.use();
glUniform1i(glGetUniformLocation(screenShader.ID, "screenTexture"), 0);
// Setup imgui version
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
ImGui::StyleColorsDark();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 330");
Renderer renderer(shader1, testModel, FBO, filename, RENDER_SIZE_X, RENDER_SIZE_Y);
// Render loop
while(!glfwWindowShouldClose(window))
{
// Input
processInput(window);
// Check deltaTime
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// Enable mouse
if (!mouseActive)
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
else
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// Low-res buffer
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
glViewport(0, 0, RENDER_SIZE_X, RENDER_SIZE_Y);
// Render commands
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
shader1.use();
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
// Camera view
glm::mat4 view;
view = glm::lookAt(cameraPos,
cameraPos+cameraFront,
cameraUp);
// Create transforms
glm::mat4 model = glm::mat4(1.0f);
glm::mat4 projection = glm::mat4(1.0f);
if (spinning) {
model = glm::rotate(model, (float)glfwGetTime(), glm::vec3(0.0f, -1.0f, 0.0f));
}
projection = glm::perspective(glm::radians(fov), (float)WINDOW_SIZE_X/(float)WINDOW_SIZE_Y, 0.1f, 100.0f);
// Send transforms to shader
int modelLoc = glGetUniformLocation(shader1.ID, "model");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
int viewLoc = glGetUniformLocation(shader1.ID, "view");
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
int projectionLoc = glGetUniformLocation(shader1.ID, "projection");
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
testModel.Draw(shader1);
// Render framebuffer to screen
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, WINDOW_SIZE_X, WINDOW_SIZE_Y);
screenShader.use();
glBindVertexArray(quadVAO);
glDisable(GL_DEPTH_TEST);
glBindTexture(GL_TEXTURE_2D, framebufferTexture);
glDrawArrays(GL_TRIANGLES, 0, 6);
ImGui::Begin("Export");
ImGui::Checkbox("Spin", &spinning);
if (ImGui::Button("Render")) {
renderer.renderSpin(48, filename);
}
ImGui::End();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
// Swap buffers and call events
glfwSwapBuffers(window);
glfwPollEvents();
}
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
// Clear GLFW and end program
glfwTerminate();
return 0;
}
// Setup window resizing
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
WINDOW_SIZE_X = width;
WINDOW_SIZE_Y = height;
}
// Setup inputs
void processInput(GLFWwindow *window)
{
// Close program
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
// Disable movement if mouse not active
if (!mouseActive)
return;
// Movement
const float cameraSpeed = 2.5f * deltaTime;
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) // Forwards
cameraPos += cameraSpeed * cameraFront;
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) // Backwards
cameraPos -= cameraSpeed * cameraFront;
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) // Right
cameraPos += cameraSpeed * glm::normalize(glm::cross(cameraFront, cameraUp));
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) // Left
cameraPos -= cameraSpeed * glm::normalize(glm::cross(cameraFront, cameraUp));
if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS) // Up
cameraPos += cameraSpeed * cameraUp;
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) // Down
cameraPos -= cameraSpeed * cameraUp;
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_SPACE && action == GLFW_PRESS)
{
mouseActive = !mouseActive;
firstMouse = true;
}
}
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
if (!mouseActive)
return;
if (firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
float xoffset = xpos - lastX;
float yoffset = lastY - ypos;
lastX = xpos;
lastY = ypos;
const float sensitivity = 0.15f;
xoffset *= sensitivity;
yoffset *= sensitivity;
yaw += xoffset;
pitch += yoffset;
if (pitch > 89.0f)
pitch = 89.0f;
else if (pitch < -89.0f)
pitch = -89.0f;
glm::vec3 direction;
direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
direction.y = sin(glm::radians(pitch));
direction.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
cameraFront = glm::normalize(direction);
}