Skip to content

Commit 11c69c7

Browse files
committed
Minor improvement and changes
1 parent da43417 commit 11c69c7

38 files changed

Lines changed: 384 additions & 281 deletions

Common/Common.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,16 @@ void CommonUtil::updateFrameBuffer(FrameBuffer *framebuffer, const std::initiali
215215
const unsigned int height = target_desc.height;
216216
const unsigned int depth = target_desc.depth;
217217
const unsigned int multisamples = target_desc.nrSamples;
218+
const bool useMultiSampling = multisamples > 1;
219+
218220
const GLenum internal_format = fragcore::GLHelper::getGraphicFormat(target_desc.graphicFormat);
219221

220222
GLenum texture_type = GL_TEXTURE_2D;
221223
if (depth > 1) {
222224
texture_type = GL_TEXTURE_2D_ARRAY;
223225
}
224226

225-
if (multisamples > 0) {
227+
if (useMultiSampling) {
226228
texture_type = GL_TEXTURE_2D_MULTISAMPLE;
227229
}
228230

@@ -234,7 +236,7 @@ void CommonUtil::updateFrameBuffer(FrameBuffer *framebuffer, const std::initiali
234236
glObjectLabel(GL_TEXTURE, framebuffer->attachments[attachment_index], texture_attachment_name.size(),
235237
texture_attachment_name.data());
236238

237-
if (multisamples > 1) {
239+
if (useMultiSampling) {
238240
glTexImage2DMultisample(texture_type, multisamples, internal_format, width, height, GL_TRUE);
239241
} else {
240242
if (depth > 1) {
@@ -246,7 +248,7 @@ void CommonUtil::updateFrameBuffer(FrameBuffer *framebuffer, const std::initiali
246248
}
247249
framebuffer->attachmentSize[attachment_index] = {width, height, depth};
248250

249-
if (multisamples == 0) {
251+
if (!useMultiSampling) {
250252

251253
glTexParameteri(texture_type, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
252254
glTexParameteri(texture_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
@@ -289,7 +291,7 @@ void CommonUtil::updateFrameBuffer(FrameBuffer *framebuffer, const std::initiali
289291
const GLenum internal_format = fragcore::GLHelper::getGraphicFormat(depthstencil.graphicFormat);
290292

291293
GLenum texture_type = GL_TEXTURE_2D;
292-
if (multisamples > 0) {
294+
if (multisamples > 1) {
293295
texture_type = GL_TEXTURE_2D_MULTISAMPLE;
294296
}
295297

@@ -305,7 +307,7 @@ void CommonUtil::updateFrameBuffer(FrameBuffer *framebuffer, const std::initiali
305307
framebuffer->attachmentSize[framebuffer->depthIndex] = {depth_width, depth_height, depth_depth};
306308

307309
/* */
308-
if (multisamples > 1) {
310+
if (multisamples <= 1) {
309311

310312
const float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
311313
glTexParameterfv(texture_type, GL_TEXTURE_BORDER_COLOR, borderColor);

Common/Importer/Scene.cpp

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,26 @@ namespace glsample {
7979
const unsigned char white[] = {255, 255, 255, 255};
8080
const unsigned char black[] = {0, 0, 0, 255};
8181

82-
this->default_textures[TextureType::Diffuse] = glsample::CommonUtil::createColorTexture(
82+
this->default_textures[TextureTypeBinding::Diffuse] = glsample::CommonUtil::createColorTexture(
8383
1, 1, fragcore::Color(white[0] / 255.0f, white[1] / 255.0f, white[2] / 255.0f, white[3] / 255.0f));
84-
this->default_textures[TextureType::AlphaMask] = this->default_textures[TextureType::Diffuse];
85-
this->default_textures[TextureType::Emission] = this->default_textures[TextureType::Diffuse];
86-
this->default_textures[TextureType::Irradiance] = this->default_textures[TextureType::Diffuse];
87-
this->default_textures[TextureType::AmbientOcclusion] = this->default_textures[TextureType::Diffuse];
88-
this->default_textures[TextureType::DepthBuffer] = this->default_textures[TextureType::Diffuse];
89-
this->default_textures[TextureType::Specular_Roughness] = this->default_textures[TextureType::Diffuse];
90-
this->default_textures[TextureType::AmbientOcclusion] = this->default_textures[TextureType::Diffuse];
91-
92-
this->default_textures[TextureType::Displacement] = glsample::CommonUtil::createColorTexture(
84+
this->default_textures[TextureTypeBinding::AlphaMask] = this->default_textures[TextureTypeBinding::Diffuse];
85+
this->default_textures[TextureTypeBinding::Emission] = this->default_textures[TextureTypeBinding::Diffuse];
86+
this->default_textures[TextureTypeBinding::Irradiance] =
87+
this->default_textures[TextureTypeBinding::Diffuse];
88+
this->default_textures[TextureTypeBinding::AmbientOcclusion] =
89+
this->default_textures[TextureTypeBinding::Diffuse];
90+
this->default_textures[TextureTypeBinding::DepthBuffer] =
91+
this->default_textures[TextureTypeBinding::Diffuse];
92+
this->default_textures[TextureTypeBinding::Specular_Roughness] =
93+
this->default_textures[TextureTypeBinding::Diffuse];
94+
this->default_textures[TextureTypeBinding::AmbientOcclusion] =
95+
this->default_textures[TextureTypeBinding::Diffuse];
96+
97+
this->default_textures[TextureTypeBinding::Displacement] = glsample::CommonUtil::createColorTexture(
9398
1, 1, fragcore::Color(black[0] / 255.0f, black[1] / 255.0f, black[2] / 255.0f, black[3] / 255.0f));
94-
this->default_textures[TextureType::Metal] = this->default_textures[TextureType::Diffuse];
99+
this->default_textures[TextureTypeBinding::Metal] = this->default_textures[TextureTypeBinding::Diffuse];
95100

96-
this->default_textures[TextureType::Normal] = glsample::CommonUtil::createColorTexture(
101+
this->default_textures[TextureTypeBinding::Normal] = glsample::CommonUtil::createColorTexture(
97102
1, 1,
98103
fragcore::Color(normalForward[0] / 255.0f, normalForward[1] / 255.0f, normalForward[2] / 255.0f,
99104
normalForward[3] / 255.0f));
@@ -302,9 +307,9 @@ namespace glsample {
302307
stageLightBase->pointCount = 0;
303308
size_t light_count = 0;
304309
for (light_count = 0; light_count < getLights().size(); light_count++) {
305-
const Light *light = getLights()[light_count];
310+
Light *light = getLights()[light_count];
306311

307-
switch (light->lightType) {
312+
switch (light->getLightType()) {
308313

309314
case Light::LightType::Directional: {
310315

@@ -317,23 +322,23 @@ namespace glsample {
317322
lightData->lightDirection = glm::vec4(light_direction, 1);
318323

319324
/* Shadow Setup. */
320-
lightData->lightShadow.shadow[0] = light->shadow;
325+
lightData->lightShadow.shadow[0] = light->getShadowStrength();
321326
lightData->lightShadow.shadow[1] = light->bias;
322327

323-
if (light->shadow > 0) {
324-
// TODO: frustum.
325-
const float near_plane = -(dirLight->shadowDistance / 2.0f) * 2;
326-
const float far_plane = (dirLight->shadowDistance / 2.0f) * 2;
327-
const glm::mat4 lightProjection =
328-
glm::ortho(-dirLight->shadowDistance, dirLight->shadowDistance, -dirLight->shadowDistance,
329-
dirLight->shadowDistance, near_plane, far_plane);
328+
if (light->getShadowStrength() > 0) {
329+
330+
const float near_plane = -(dirLight->getShadowDistance());
331+
const float far_plane = (dirLight->getShadowDistance());
332+
333+
const glm::mat4 lightProjection = glm::ortho(
334+
-dirLight->getShadowDistance(), dirLight->getShadowDistance(), -dirLight->getShadowDistance(),
335+
dirLight->getShadowDistance(), near_plane, far_plane);
330336

331337
const glm::mat4 lightView = glm::lookAt(
332338
dirLight->getPosition(), dirLight->getPosition() + light_direction * 100.0f, dirLight->up());
333339
const glm::mat4 lightSpaceMatrix = lightProjection * lightView;
334340

335-
const glm::mat4 biasMatrix(0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.5, 0.5,
336-
0.5, 1.0);
341+
light->shadowData.lightSpaceMatrix = lightSpaceMatrix;
337342
lightData->lightShadow.lightSpaceMatrix = lightSpaceMatrix;
338343
}
339344

@@ -350,6 +355,7 @@ namespace glsample {
350355

351356
stageLightBase->pointCount++;
352357
} break;
358+
default:
353359
case Light::LightType::Spot:
354360
break;
355361
}
@@ -475,12 +481,19 @@ namespace glsample {
475481
if (light) {
476482
GlobalSceneState *globalScene = this->stageCommonRobin.buffers[getRoundRobinIndex()];
477483

484+
/* */
485+
globalScene->camera.far = light->getShadowDistance();
486+
globalScene->camera.near = 0;
487+
globalScene->camera.position = glm::vec4(light->getPosition(), 1.0f);
478488
globalScene->camera.proj = light->getProjectionMatrix();
479489
globalScene->camera.view = light->getViewMatrix();
480-
globalScene->camera.viewProj = (globalScene->camera.proj * globalScene->camera.view);
490+
globalScene->camera.viewProj =
491+
light->shadowData.lightSpaceMatrix; // (globalScene->camera.proj * globalScene->camera.view);
481492

482493
/* */
483494
globalScene->proj[0] = light->getProjectionMatrix();
495+
496+
this->updateBuffers(); // TODO: update only camera
484497
}
485498

486499
/* */
@@ -561,10 +574,10 @@ namespace glsample {
561574
glDepthFunc(GL_LEQUAL);
562575
glCullFace(GL_BACK);
563576

564-
this->frameIndex++;
577+
this->renderPassFrameIndex++;
565578
}
566579

567-
void Scene::bindTexture(const MaterialObject &material, const TextureType texture_type) {
580+
void Scene::bindTexture(const MaterialObject &material, const TextureTypeBinding texture_type) {
568581

569582
/* */
570583
const unsigned int textureMapIndex = texture_type;
@@ -614,17 +627,17 @@ namespace glsample {
614627
/* Only bind if different material the current material binded. */
615628
if (this->currentBindedMaterial != material) {
616629

617-
this->bindTexture(*material, TextureType::Diffuse);
618-
this->bindTexture(*material, TextureType::Normal);
619-
this->bindTexture(*material, TextureType::AlphaMask);
620-
this->bindTexture(*material, TextureType::Emission);
621-
this->bindTexture(*material, TextureType::AmbientOcclusion);
622-
this->bindTexture(*material, TextureType::Displacement);
623-
this->bindTexture(*material, TextureType::Specular_Roughness);
624-
this->bindTexture(*material, TextureType::Metal);
630+
this->bindTexture(*material, TextureTypeBinding::Diffuse);
631+
this->bindTexture(*material, TextureTypeBinding::Normal);
632+
this->bindTexture(*material, TextureTypeBinding::AlphaMask);
633+
this->bindTexture(*material, TextureTypeBinding::Emission);
634+
this->bindTexture(*material, TextureTypeBinding::AmbientOcclusion);
635+
this->bindTexture(*material, TextureTypeBinding::Displacement);
636+
this->bindTexture(*material, TextureTypeBinding::Specular_Roughness);
637+
this->bindTexture(*material, TextureTypeBinding::Metal);
625638
// this->bindTexture(material, TextureType::Irradiance); //TODO: enable once material has been binded
626639
// with irradiance texture
627-
this->bindTexture(*material, TextureType::DepthBuffer);
640+
this->bindTexture(*material, TextureTypeBinding::DepthBuffer);
628641

629642
/* */
630643
const RenderQueue domain = getQueueDomain(*material);
@@ -787,7 +800,7 @@ namespace glsample {
787800
RenderQueue Scene::getQueueDomain(const MaterialObject &material) const noexcept {
788801
const bool useGeometryAlpha = material.clipping < 1;
789802
const bool useBlending = material.transparent[3] < 1.0f ||
790-
(material.texture_index[TextureType::AlphaMask] >= 0 && !useGeometryAlpha);
803+
(material.texture_index[TextureTypeBinding::AlphaMask] >= 0 && !useGeometryAlpha);
791804
const bool useWireframe = material.wireframe_mode;
792805

793806
if (useWireframe) {
@@ -932,16 +945,16 @@ namespace glsample {
932945
}
933946

934947
glm::ivec3 size = light->getSize();
935-
if (ImGui::DragInt2("Size", &size[0])) {
948+
if (ImGui::DragInt2("Size", &size[0], 1.0f, 0, 0, "%d")) {
936949
size = glm::max(size, glm::ivec3(128));
937950
light->setSize(size);
938951
}
939952

940953
FrameBuffer *framebuffer = light->getFrameBuffer();
941-
if (framebuffer) {
942-
ImGui::Image(static_cast<ImTextureID>(framebuffer->attachments[framebuffer->depthIndex]),
943-
ImVec2(512, 512), ImVec2(1, 1), ImVec2(0, 0));
944-
}
954+
if (framebuffer) {
955+
ImGui::Image(static_cast<ImTextureID>(framebuffer->attachments[framebuffer->depthIndex]),
956+
ImVec2(512, 512), ImVec2(1, 1), ImVec2(0, 0));
957+
}
945958

946959
ImGui::DragFloat("Shadow Strength", &light->shadow, 1, 0.0f, 1.0f);
947960
ImGui::DragFloat("Shadow Bias", &light->bias, 1, 0.0f, 1.0f, "%.5f");
@@ -1004,7 +1017,8 @@ namespace glsample {
10041017
ImGui::PushID(mat_tex_index);
10051018

10061019
/* */
1007-
const std::string texType = std::string(magic_enum::enum_name((TextureType)mat_tex_index));
1020+
const std::string texType =
1021+
std::string(magic_enum::enum_name((TextureTypeBinding)mat_tex_index));
10081022
ImGui::Image(tex, ImVec2(96, 96), ImVec2(1, 1), ImVec2(0, 0));
10091023
ImGui::SameLine();
10101024
ImGui::Text("%s (%ld)", texType.c_str(), mat_tex_index);

Common/Importer/Scene.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
namespace glsample {
2727

28-
enum TextureType : unsigned int {
28+
enum TextureTypeBinding : unsigned int {
2929
Diffuse = 0, /* */
3030
Normal = 1, /* */
3131
AlphaMask = 2, /* */
@@ -120,11 +120,11 @@ namespace glsample {
120120
}
121121

122122
protected:
123-
void bindTexture(const MaterialObject &material, const TextureType texture_type);
123+
void bindTexture(const MaterialObject &material, const TextureTypeBinding texture_type);
124124
int computeMaterialPriority(const MaterialObject &material) const noexcept;
125125
RenderQueue getQueueDomain(const MaterialObject &material) const noexcept;
126126
size_t getRoundRobinIndex() const noexcept {
127-
return this->frameIndex % UniformDataStructure::bufferRoundRobinSize;
127+
return this->renderPassFrameIndex % UniformDataStructure::bufferRoundRobinSize;
128128
}
129129

130130
protected:
@@ -276,7 +276,7 @@ namespace glsample {
276276

277277
UniformDataStructure UBOStructure;
278278

279-
unsigned int frameIndex = 0;
279+
unsigned int renderPassFrameIndex = 0;
280280
static const unsigned int frameChainCount = 3;
281281
};
282282
} // namespace glsample

Common/Scene/Light.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ namespace glsample {
6767
DirectionalLight() {
6868
this->lightType = LightType::Directional;
6969
this->setShadowDistance(50.0f);
70-
this->rotateTowards(glm::vec3(-1));
70+
this->rotateTowards(glm::vec3(1));
7171
}
7272

7373
void setSize(const glm::ivec3 &size) override {
@@ -96,8 +96,8 @@ namespace glsample {
9696
void setShadowDistance(float distance) override {
9797
Light::setShadowDistance(distance);
9898

99-
const float near_plane = -(getShadowDistance() / 2.0f) * 2;
100-
const float far_plane = (getShadowDistance() / 2.0f) * 2;
99+
const float near_plane = -(getShadowDistance());
100+
const float far_plane = (getShadowDistance());
101101
const glm::mat4 lightProjection =
102102
glm::ortho(-getShadowDistance(), getShadowDistance(), -getShadowDistance(), getShadowDistance(),
103103
near_plane, far_plane);
@@ -116,6 +116,7 @@ namespace glsample {
116116

117117
void calcFrustumPlanes(const Vector3 &position, const Vector3 &look_forward, const Vector3 &up,
118118
const Vector3 &right) override {
119+
119120
const float distance = this->getShadowDistance();
120121

121122
this->planes[NEAR_PLANE] = {position - distance * look_forward, look_forward};

Common/Transform.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "Scene/Transform.h"
22
#include <glm/ext/matrix_float3x3.hpp>
3+
#include <glm/geometric.hpp>
34
#include <glm/gtc/quaternion.hpp>
45

56
using namespace glsample;
@@ -82,6 +83,6 @@ template <> glm::quat TransformGLM::operator*(const glm::quat &quat) const noexc
8283
return this->getRotation() * quat;
8384
}
8485

85-
template <> glm::vec3 TransformGLM::up() const noexcept { return this->quat * glm::vec3(0, 1, 0); }
86-
template <> glm::vec3 TransformGLM::right() const noexcept { return this->quat * glm::vec3(1, 0, 0); }
87-
template <> glm::vec3 TransformGLM::forward() const noexcept { return this->quat * glm::vec3(0, 0, 1); }
86+
template <> glm::vec3 TransformGLM::up() const noexcept { return glm::normalize(this->quat * glm::vec3(0, 1, 0)); }
87+
template <> glm::vec3 TransformGLM::right() const noexcept { return glm::normalize(this->quat * glm::vec3(1, 0, 0)); }
88+
template <> glm::vec3 TransformGLM::forward() const noexcept { return glm::normalize(this->quat * glm::vec3(0, 0, 1)); }

0 commit comments

Comments
 (0)