-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplication.cpp
More file actions
207 lines (168 loc) · 7.53 KB
/
Copy pathApplication.cpp
File metadata and controls
207 lines (168 loc) · 7.53 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
#include "Application.h"
#include "ImGuiNfd.h"
#include "Window.h"
#include <glm/gtc/type_ptr.hpp>
#include <imgui.h>
#include <glad/glad.h>
#include <iostream>
#include <string>
Application::Application(int width, int height, Window* window)
: m_camera()
, m_previousCursorPosition()
, m_physicalSky()
, m_window(window)
, m_exposure(-2.0f)
, m_max_white(1e6f)
, m_displayMode(DisplayMode::DAY)
, m_blueTint(0.1f, 0.1f, 0.5f)
, m_noiseScale(50.0f)
, m_noiseStrength(0.005f)
, m_noiseSpeed(10.0f)
, m_mesopicRangeStart(0.0f)
, m_mesopicRangeEnd(0.01f)
{
std::cout << "Creating application" << std::endl;
glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_BLEND);
glBlendEquation(GL_FUNC_ADD);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
OnFramebufferSize(width, height);
// FRAMEBUFFER STUFF
glGenFramebuffers(1, &m_hdrFramebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, m_hdrFramebuffer);
glGenTextures(1, &m_hdrTexture);
glBindTexture(GL_TEXTURE_2D, m_hdrTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, 1920, 1080, 0, GL_RGBA, GL_FLOAT, nullptr); // TODO: Correct width and height parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_hdrTexture, 0);
glGenRenderbuffers(1, &m_depthRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, m_depthRenderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 1920, 1080);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER , m_depthRenderbuffer);
// POSTPROCESS STUFF
ShaderStage vertexShader = ShaderStage();
vertexShader.Create(ShaderType::VERTEX);
vertexShader.Compile("./resources/shaders/postprocess.vert", "./resources/shaders");
ShaderStage fragmentShader = ShaderStage();
fragmentShader.Create(ShaderType::FRAGMENT);
fragmentShader.Compile("./resources/shaders/postprocess.frag", "./resources/shaders");
m_postprocessShader.Create();
m_postprocessShader.AttachShader(vertexShader.m_id);
m_postprocessShader.AttachShader(fragmentShader.m_id);
m_postprocessShader.Build();
// BUFFERS STUFF
glGenVertexArrays(1, &m_fullScreenQuadVao);
glBindVertexArray(m_fullScreenQuadVao);
float vertices[] = {
-1.0, -1.0, 0.0,
+1.0, -1.0, 0.0,
-1.0, +1.0, 0.0,
+1.0, +1.0, 0.0
};
glGenBuffers(1, &m_fullScreenQuadVbo);
glBindBuffer(GL_ARRAY_BUFFER, m_fullScreenQuadVbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
glEnableVertexAttribArray(0);
}
// TODO: Delete opengl resources
Application::~Application()
{
// glDeleteFramebuffers(1, &m_hdrFramebuffer);
// glDeleteTextures();
std::cout << "Destroying Application" << std::endl;
}
void Application::OnCursorPos(double xpos, double ypos)
{
glm::vec2 currentCursorPosition = glm::vec2(xpos, ypos);
glm::vec2 cursorMovement = currentCursorPosition - m_previousCursorPosition;
bool captured = false;
if (!captured) captured = m_camera.OnCursorMovement(cursorMovement);
m_previousCursorPosition = currentCursorPosition;
}
void Application::OnMouseButton(int button, int action, int mods)
{
m_camera.OnMouseButton(button, action, mods);
}
void Application::OnFramebufferSize(int width, int height)
{
m_resolution = glm::vec3(width, height, 0.0);
glViewport(0, 0, width, height);
m_camera.SetAspectRatio(static_cast<float>(width) / static_cast<float>(height));
GLenum errorCode = glGetError();
if (errorCode != GL_NO_ERROR) std::cerr << "GL error after resize" << std::endl;
}
void Application::OnScroll(double xoffset, double yoffset)
{
m_camera.OnScroll(static_cast<int>(yoffset));
}
void Application::OnUpdate()
{
ImGui::ShowDemoWindow();
ImGui::ShowMetricsWindow();
m_camera.OnUpdate();
m_physicalSky.Update();
if (ImGui::Begin("Postprocess"))
{
ImGui::SliderFloat("Exposure", &m_exposure, -5.0f, 5.0f);
ImGui::SliderFloat("White Point", &m_max_white, 0.0f, 1e6f, "%.3f", ImGuiSliderFlags_Logarithmic);
ImGui::RadioButton("Day", reinterpret_cast<int*>(&m_displayMode), static_cast<int>(DisplayMode::DAY)); ImGui::SameLine();
ImGui::RadioButton("Night", reinterpret_cast<int*>(&m_displayMode), static_cast<int>(DisplayMode::NIGHT)); ImGui::SameLine();
ImGui::RadioButton("Photopic Luminance", reinterpret_cast<int*>(&m_displayMode), static_cast<int>(DisplayMode::PHOTOPIC_LUMINANCE)); ImGui::SameLine();
ImGui::RadioButton("Scotopic Luminance", reinterpret_cast<int*>(&m_displayMode), static_cast<int>(DisplayMode::SCOTOPIC_LUMINANCE));
if (m_displayMode == DisplayMode::NIGHT)
{
ImGui::ColorEdit3("Tint Color", glm::value_ptr(m_blueTint), ImGuiColorEditFlags_Float);
ImGui::SliderFloat("Noise Scale", &m_noiseScale, 0.0f, 200.0f, "%.3f", ImGuiSliderFlags_Logarithmic);
ImGui::SliderFloat("Noise Strength", &m_noiseStrength, 0.0f, 1.0f, "%.3f", ImGuiSliderFlags_Logarithmic);
ImGui::SliderFloat("Noise Speed", &m_noiseSpeed, 0.0f, 20.0f);
ImGui::SliderFloat("Mesopic Range Start", &m_mesopicRangeStart, 0.0f, 1.0f);
ImGui::SliderFloat("Mesopic Range End", &m_mesopicRangeEnd, 0.0f, 1.0f);
if (m_mesopicRangeStart > m_mesopicRangeEnd) m_mesopicRangeStart = m_mesopicRangeEnd;
}
}
ImGui::End();
/*if (ImGui::Begin("Model"))
{
nfdfilteritem_t meshFilter = { "3D mesh", "gltf" };
if (ImGui::Button("Open other model..."))
{
std::string path = ImGuiNfd::Load(&meshFilter, 1);
if (path != "") m_mesh = std::make_unique<Mesh>(path);
}
}
ImGui::End();*/
}
void Application::OnRender()
{
glBindFramebuffer(GL_FRAMEBUFFER, m_hdrFramebuffer);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
m_physicalSky.Render(m_camera);
glBindFramebuffer(GL_FRAMEBUFFER, GL_NONE);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
m_postprocessShader.Use();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_hdrTexture);
m_postprocessShader.SetInt("hdrTexture", 0);
m_postprocessShader.SetFloat("k", glm::pow(10.0f, m_exposure));
m_postprocessShader.SetFloat("L_white", m_max_white);
m_postprocessShader.SetFloat("AspectRatio", m_resolution.x / m_resolution.y);
m_postprocessShader.SetFloat("Time", m_window->GetTime());
m_postprocessShader.SetVec3("C_blue", m_blueTint);
m_postprocessShader.SetFloat("NoiseScale", m_noiseScale);
m_postprocessShader.SetFloat("NoiseStrength", m_noiseStrength);
m_postprocessShader.SetFloat("NoiseSpeed", m_noiseSpeed);
m_postprocessShader.SetVec3("MesopicRange", glm::vec3(m_mesopicRangeStart, m_mesopicRangeEnd, 0.0f));
m_postprocessShader.SetInt("Mode", static_cast<int>(m_displayMode));
glBindVertexArray(m_fullScreenQuadVao);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
GLenum errorCode = glGetError();
if (errorCode != GL_NO_ERROR) std::cerr << "[OpenGL] E: Something failed while rendering Physical Sky." << std::endl;
}