-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplication.cpp
245 lines (191 loc) · 6.61 KB
/
Application.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
#define GLEW_STATIC
#include "Application.h"
#include <iostream>
#include <thread>
#include <Windows.h>
#include "imgui/imgui.h"
#include "imgui/imgui_impl_glfw_gl3.h"
#pragma comment(lib, "glew32s.lib")
#pragma comment(lib, "glfw3.lib")
#pragma comment(lib, "opengl32.lib")
static const glm::vec2 g_initialSize(1920, 1080);
static const char* g_windowTitle = "BRDF Example";
static bool g_showUI = true;
static bool g_hasFocus = true;
static const char* debugModes[] =
{ "Final", "Position", "Normal", "Diffuse", "Metallic", "Roughness", "Irradiance", "Shadowmap" };
static const char* brdfMethods[] =
{ "Blinn-Phong", "Cook-Torrance" };
Application::Application() :
m_LastFrameTime(0),
m_pCamera(nullptr),
m_pModelManager(nullptr),
m_pRenderer(nullptr),
m_pWindow(nullptr)
{
}
Application::~Application()
{
}
bool Application::Initialize()
{
/*--------------------------------------
Initialize GLFW and create a window
---------------------------------------*/
glfwSetErrorCallback(errorCallback);
if (!glfwInit())
{
std::cerr << "Failed to initialize GLFW" << std::endl;
abort();
return false;
}
m_pWindow = glfwCreateWindow(g_initialSize.x, g_initialSize.y, g_windowTitle, NULL, NULL);
if (!m_pWindow)
{
std::cerr << "Failed to create a window" << std::endl;
abort();
return false;
}
glfwMakeContextCurrent(m_pWindow);
glfwSetWindowFocusCallback(m_pWindow, focusCallback);
glfwSetWindowSizeCallback(m_pWindow, sizeCallback);
glfwSwapInterval(0);
/*------------------
Initialize GLEW
-------------------*/
GLenum err = glewInit();
if (err != GLEW_OK)
{
std::cerr << "Failed to initialize GLEW" << std::endl;
printf("Error: %s\n", glewGetErrorString(err));
abort();
return false;
}
/*------------------
Initialize ImGui
-------------------*/
ImGui::CreateContext();
ImGui_ImplGlfwGL3_Init(m_pWindow, true);
ImGui::StyleColorsDark();
/*-----------------------------------------*/
m_pCamera = std::make_unique<Camera>();
m_pLightManager = std::make_unique<LightManager>();
m_pModelManager = std::make_unique<ModelManager>();
m_pRenderer = std::make_unique<Renderer>();
m_pRenderer->Initialize(g_initialSize);
m_pModelManager->LoadObj("./Resources/Scene.obj");
m_pModelManager->CreateSphereGrid();
return true;
}
void Application::ThreadingTest()
{
while (true)
{
if (g_hasFocus)
{
m_pCamera->Update(0.03);
m_pRenderer->UpdateMatrices(m_pCamera->GetTransform(), m_pCamera->GetFov());
}
Sleep(30);
}
}
void Application::Run()
{
// For some reason key callback refuses to work if it's set in Initialize()
glfwSetKeyCallback(m_pWindow, keyCallback);
//std::thread th1(&Application::ThreadingTest, this);
//th1.detach();
while (!glfwWindowShouldClose(m_pWindow))
{
glfwPollEvents();
m_CurrentFrameTime = glfwGetTime();
m_dtFrameTime = m_CurrentFrameTime - m_LastFrameTime;
m_LastFrameTime = m_CurrentFrameTime;
if (g_hasFocus)
{
// Update camera and render view accordingly
m_pCamera->Update(m_dtFrameTime);
m_pRenderer->UpdateMatrices(m_pCamera->GetTransform(), m_pCamera->GetFov());
}
// Update randomly moving point lights
m_pLightManager->Update(m_dtFrameTime);
// Draw stuff
m_pRenderer->NewFrame();
m_pRenderer->DrawGeometry(m_pModelManager->GetModels(), m_pLightManager->GetLights());
// UI drawing
if (g_showUI)
{
ImGui_ImplGlfwGL3_NewFrame();
ImGui::Begin("Test");
{
ImGui::Text("Hello world");
DebugUniformBlock& debug = m_pRenderer->GetDebugStruct();
RenderOptions& options = m_pRenderer->GetOptions();
ImGui::Text("Debug mode");
ImGui::Combo("##debug", &debug.debugMode, debugModes, 8);
ImGui::Text("BRDF Methods");
ImGui::Combo("##BRDFMethod", &debug.brdfMethod, brdfMethods, 2);
ImGui::Text("Albeido");
ImGui::InputFloat("##Albeido", &debug.AlbeidoMultiplier, 0.01f, 0.01f, 2);
ImGui::Text("Metallic");
ImGui::InputFloat("##Metallic", &debug.MetallicMultiplier, 0.01f, 0.01f, 2);
ImGui::Text("Roughness");
ImGui::InputFloat("##Roughness", &debug.RoughnessMultiplier, 0.01f, 0.01f, 2);
ImGui::Text("Specular shininess");
ImGui::InputFloat("##Shininess", &debug.HardcodedSpecular, 1.f, 2.f, 2);
debug.AlbeidoMultiplier = max(0, debug.AlbeidoMultiplier);
debug.MetallicMultiplier = max(0, debug.MetallicMultiplier);
debug.RoughnessMultiplier = max(0, debug.RoughnessMultiplier);
ImGui::Checkbox("Draw sunlight", &options.DrawSun);
ImGui::Checkbox("Draw skybox", &options.DrawSkybox);
ImGui::Checkbox("Draw lights", &options.DrawLights);
ImGui::Checkbox("Draw irradiance", &options.DrawIndirect);
if (ImGui::Button("Recompile shaders"))
m_pRenderer->RecompileShaders();
if (ImGui::Button("Create Lights"))
m_pLightManager->CreateLights();
ImGui::SameLine();
if (ImGui::Button("Destroy Lights"))
m_pLightManager->DestroyLights();
ImGui::Dummy(ImVec2(10, 10));
ImGui::Text("FPS: %f", ImGui::GetIO().Framerate);
ImGui::PlotLines("##FPS2", m_FPSBuffer, 99, 0, "Frame time (ms)", 0, 5, ImVec2(250, 100));
ImGui::Dummy(ImVec2(10, 10));
m_FPSBuffer[99] += (m_dtFrameTime) / 120;
m_FPSSamples += 1;
if (m_FPSSamples >= 120)
{
m_FPSSamples = 0;
m_FPSBuffer[99] *= 1000;
for (int i = 0; i < 99; ++i)
m_FPSBuffer[i] = m_FPSBuffer[i + 1];
m_FPSBuffer[99] = 0;
}
}ImGui::End();
ImGui::Render();
ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData());
}
glfwSwapBuffers(m_pWindow);
}
}
/*--------------------
GLFW Callbacks
---------------------*/
void Application::errorCallback(int error, const char* description)
{
printf("ErrorCallback Error %d, description: %s\n", error, description);
}
void Application::focusCallback(GLFWwindow* pWindow, int focused)
{
printf("FocusCallback Window 0x%X\tFocused: %d\n", pWindow, focused);
g_hasFocus = focused;
}
void Application::sizeCallback(GLFWwindow* pWindow, int width, int height)
{
printf("SizeCallback Window 0x%X\tSize %dx%d\n", pWindow, width, height);
}
void Application::keyCallback(GLFWwindow* pWindow, int key, int scancode, int action, int mods)
{
printf("KeyCallback Window 0x%X\tKey %d, Scancode %d, Action %d, Mods %d\n", pWindow, key, scancode, action, mods);
}
/*---------------------------------------------------------------------------*/