-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPointlights.cpp
More file actions
275 lines (220 loc) · 9.3 KB
/
Pointlights.cpp
File metadata and controls
275 lines (220 loc) · 9.3 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
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
#include <GL/glew.h>
#include <GLSample.h>
#include <GLSampleWindow.h>
#include <ImageImport.h>
#include <ShaderLoader.h>
#include <glm/gtc/matrix_transform.hpp>
namespace glsample {
/**
* @brief
*/
class PointLight : public GLSampleWindow {
public:
PointLight() : GLSampleWindow() {
this->setTitle("PointLight");
this->pointLightSettingComponent = std::make_shared<PointLightSettingComponent>(this->uniformStageBuffer);
this->addUIComponent(this->pointLightSettingComponent);
/* */
this->camera.setPosition(glm::vec3(18.5f));
this->camera.lookAt(glm::vec3(5.f));
}
using PointLightSource = struct point_light_binary_t {
glm::vec3 position;
float range;
glm::vec4 color;
float intensity;
float constant_attenuation;
float linear_attenuation;
float quadratic_attenuation;
};
static const size_t nrPointLights = 4;
struct uniform_buffer_block {
glm::mat4 model{};
glm::mat4 view{};
glm::mat4 proj{};
glm::mat4 modelView{};
glm::mat4 modelViewProjection{};
/* Material. */
glm::vec4 ambientColor = glm::vec4(0.075f, 0.075f, 0.075f, 1.0f);
PointLightSource pointLights[nrPointLights] = {};
} uniformStageBuffer;
/* */
MeshObject plan;
/* Textures. */
unsigned int diffuse_texture{};
/* Program. */
unsigned int pointLight_program{};
/* Uniforms. */
unsigned int uniform_buffer_index{};
unsigned int uniform_buffer_binding = 0;
unsigned int uniform_buffer{};
const size_t nrUniformBuffer = 3;
size_t uniformAlignBufferSize = sizeof(uniform_buffer_block);
CameraController camera;
class PointLightSettingComponent : public nekomimi::UIComponent {
public:
PointLightSettingComponent(struct uniform_buffer_block &uniform) : uniform(uniform) {
this->setName("Point Light Settings");
}
void draw() override {
for (size_t i = 0; i < sizeof(uniform.pointLights) / sizeof(uniform.pointLights[0]); i++) {
ImGui::PushID(1000 + i);
if (ImGui::CollapsingHeader(fmt::format("Light {}", i).c_str(), &lightvisible[i],
ImGuiTreeNodeFlags_CollapsingHeader)) {
ImGui::ColorEdit4("Color", &this->uniform.pointLights[i].color[0],
ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_Float);
ImGui::DragFloat3("Position", &this->uniform.pointLights[i].position[0]);
ImGui::DragFloat3("Attenuation", &this->uniform.pointLights[i].constant_attenuation);
ImGui::DragFloat("Range", &this->uniform.pointLights[i].range);
ImGui::DragFloat("Intensity", &this->uniform.pointLights[i].intensity);
}
ImGui::PopID();
}
ImGui::ColorEdit4("Ambient Color", &this->uniform.ambientColor[0],
ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_Float);
ImGui::Checkbox("Animate Lights", &this->animate);
}
bool animate = true;
private:
struct uniform_buffer_block &uniform;
bool lightvisible[nrPointLights] = {true, true, true, true};
};
std::shared_ptr<PointLightSettingComponent> pointLightSettingComponent;
const std::string vertexShaderPath = "Shaders/pointlights/pointlights.vert.spv";
const std::string fragmentShaderPath = "Shaders/pointlights/pointlights.frag.spv";
void Release() override {
/* */
glDeleteProgram(this->pointLight_program);
/* */
glDeleteTextures(1, (const GLuint *)&this->diffuse_texture);
glDeleteBuffers(1, &this->uniform_buffer);
/* */
glDeleteVertexArrays(1, &this->plan.vao);
glDeleteBuffers(1, &this->plan.vbo);
glDeleteBuffers(1, &this->plan.ibo);
}
void Initialize() override {
/* */
const std::string diffuseTexturePath = this->getResult()["texture"].as<std::string>();
{
/* Load shader source. */
const std::vector<uint32_t> vertex_binary =
IOUtil::readFileData<uint32_t>(this->vertexShaderPath, this->getFileSystem());
const std::vector<uint32_t> fragment_binary =
IOUtil::readFileData<uint32_t>(this->fragmentShaderPath, this->getFileSystem());
fragcore::ShaderCompiler::CompilerConvertOption compilerOptions;
compilerOptions.target = fragcore::ShaderLanguage::GLSL;
compilerOptions.glslVersion = this->getShaderVersion();
/* Load graphic pipeline. */
this->pointLight_program =
ShaderLoader::loadGraphicProgram(compilerOptions, &vertex_binary, &fragment_binary);
}
/* Setup graphic pipeline. */
glUseProgram(this->pointLight_program);
this->uniform_buffer_index = glGetUniformBlockIndex(this->pointLight_program, "UniformBufferBlock");
glUniform1i(glGetUniformLocation(this->pointLight_program, "DiffuseTexture"), 0);
glUniformBlockBinding(this->pointLight_program, this->uniform_buffer_index, this->uniform_buffer_binding);
glUseProgram(0);
/* load Textures */
TextureImporter textureImporter(this->getFileSystem());
this->diffuse_texture = textureImporter.loadImage2D(diffuseTexturePath);
/* Align uniform buffer in respect to driver requirement. */
GLint minMapBufferSize = 0;
glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &minMapBufferSize);
this->uniformAlignBufferSize = Math::align<size_t>(this->uniformAlignBufferSize, (size_t)minMapBufferSize);
/* Create uniform buffer. */
glGenBuffers(1, &this->uniform_buffer);
glBindBuffer(GL_UNIFORM_BUFFER, this->uniform_buffer);
glBufferData(GL_UNIFORM_BUFFER, this->uniformAlignBufferSize * this->nrUniformBuffer, nullptr,
GL_DYNAMIC_DRAW);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
/* Load geometry. */
CommonUtil::loadPlan(this->plan, 1, 1, 1);
/* Init lights. */
const glm::vec4 colors[] = {glm::vec4(1, 0, 0, 1), glm::vec4(0, 1, 0, 1), glm::vec4(0, 0, 1, 1),
glm::vec4(1, 0, 1, 1)};
for (size_t i = 0; i < this->nrPointLights; i++) {
uniformStageBuffer.pointLights[i].range = 45.0f;
uniformStageBuffer.pointLights[i].position =
glm::vec3(i * -1.0f, i * 1.0f, i * -1.5f) * 12.0f + glm::vec3(2.0f);
uniformStageBuffer.pointLights[i].color = colors[i];
uniformStageBuffer.pointLights[i].constant_attenuation = 1.0f;
uniformStageBuffer.pointLights[i].linear_attenuation = 0.1f;
uniformStageBuffer.pointLights[i].quadratic_attenuation = 0.05f;
uniformStageBuffer.pointLights[i].intensity = 1.0f;
}
}
void onResize(int width, int height) override { this->camera.setAspect((float)width / (float)height); }
void draw() override {
size_t width = 0, height = 0;
this->getCurrentFrameBufferSize(&width, &height);
/* */
glViewport(0, 0, width, height);
glClearColor(0.05f, 0.05f, 0.05f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* Render. */
{
/* */
glBindBufferRange(GL_UNIFORM_BUFFER, this->uniform_buffer_binding, this->uniform_buffer,
(this->getFrameCount() % this->nrUniformBuffer) * this->uniformAlignBufferSize,
this->uniformAlignBufferSize);
glUseProgram(this->pointLight_program);
glDisable(GL_CULL_FACE);
/* Bind texture. */
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, this->diffuse_texture);
/* Draw triangle. */
glBindVertexArray(this->plan.vao);
glDrawElements(GL_TRIANGLES, this->plan.nrIndicesElements, GL_UNSIGNED_INT, nullptr);
glBindVertexArray(0);
}
}
void update() override {
/* Update Camera. */
this->camera.update(this->getTimer().deltaTime<float>());
/* Animate the point lights. */
if (this->pointLightSettingComponent->animate) {
for (size_t i = 0;
i < sizeof(uniformStageBuffer.pointLights) / sizeof(uniformStageBuffer.pointLights[0]); i++) {
this->uniformStageBuffer.pointLights[i].position =
glm::vec3(10.0f * std::cos(this->getTimer().getElapsed<float>() * Math::PI + 1.3 * i), 10,
10.0f * std::sin(this->getTimer().getElapsed<float>() * Math::PI + 1.3 * i));
}
}
/* Update uniform stage buffer values. */
this->uniformStageBuffer.model = glm::mat4(1.0f);
this->uniformStageBuffer.model =
glm::rotate(this->uniformStageBuffer.model, glm::radians(270.0f), glm::vec3(1.0f, 0.0f, 0.0f));
this->uniformStageBuffer.model = glm::scale(this->uniformStageBuffer.model, glm::vec3(45.95f));
this->uniformStageBuffer.view = this->camera.getViewMatrix();
this->uniformStageBuffer.proj = this->camera.getProjectionMatrix();
this->uniformStageBuffer.modelViewProjection =
this->uniformStageBuffer.proj * this->uniformStageBuffer.view * this->uniformStageBuffer.model;
/* Update uniform buffer. */
glBindBuffer(GL_UNIFORM_BUFFER, this->uniform_buffer);
void *uniformPointer = glMapBufferRange(
GL_UNIFORM_BUFFER, ((this->getFrameCount() + 1) % this->nrUniformBuffer) * this->uniformAlignBufferSize,
this->uniformAlignBufferSize,
GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT | GL_MAP_UNSYNCHRONIZED_BIT);
memcpy(uniformPointer, &this->uniformStageBuffer, sizeof(this->uniformStageBuffer));
glUnmapBuffer(GL_UNIFORM_BUFFER);
}
};
class PointLightsGLSample : public GLSample<PointLight> {
public:
PointLightsGLSample() : GLSample<PointLight>() {}
void customOptions(cxxopts::OptionAdder &options) override {
options("T,texture", "Texture Path", cxxopts::value<std::string>()->default_value("asset/diffuse.png"));
}
};
} // namespace glsample
int main(int argc, const char **argv) {
try {
glsample::PointLightsGLSample sample;
sample.run(argc, argv);
} catch (const std::exception &ex) {
std::cerr << cxxexcept::getStackMessage(ex) << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}