Skip to content

Commit

Permalink
Fixed Skinned Mesh, improvements and other changes
Browse files Browse the repository at this point in the history
  • Loading branch information
voldien committed Jan 8, 2025
1 parent 9f47c2c commit 6cd6f40
Show file tree
Hide file tree
Showing 68 changed files with 1,235 additions and 290 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,14 @@ FOREACH(GLSL ${GLSL_SOURCE_FILES})
SET(SPIRV "${FILE_NAME}.spv")
IF(GLSLLANGVALIDATOR)
IF(CMAKE_BUILD_TYPE STREQUAL "Release")
SET(SPIRV_BUILD_SETTINGS "")
SET(SPIRV_BUILD_SETTINGS -g0)
ELSE()
SET(SPIRV_BUILD_SETTINGS -g -Od)
SET(SPIRV_BUILD_SETTINGS -g -gVS -Od)
ENDIF()
ADD_CUSTOM_COMMAND(
OUTPUT ${SPIRV}
COMMAND ${CMAKE_COMMAND} -E make_directory "${EXECUTABLE_OUTPUT_PATH}/Shaders/"
COMMAND ${GLSLLANGVALIDATOR} -Dgl_InstanceID=gl_InstanceIndex -Dgl_VertexID=gl_VertexIndex -I${CMAKE_CURRENT_SOURCE_DIR}/Shaders/common --target-env vulkan1.1 ${SPIRV_BUILD_SETTINGS} -o ${SPIRV} ${GLSL}
COMMAND ${GLSLLANGVALIDATOR} --define-macro gl_InstanceID=gl_InstanceIndex --define-macro gl_VertexID=gl_VertexIndex -I${CMAKE_CURRENT_SOURCE_DIR}/Shaders/common --target-env vulkan1.1 ${SPIRV_BUILD_SETTINGS} -o ${SPIRV} ${GLSL}
DEPENDS ${GLSL})
ELSEIF(GLSLC)
IF(CMAKE_BUILD_TYPE STREQUAL "Release")
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2024 Valdemar Lindberg
Copyright (c) 2025 Valdemar Lindberg

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
18 changes: 12 additions & 6 deletions Samples/FrustumCullingCompute/FrustumCullingCompute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ namespace glsample {
ImGui::TextUnformatted("Frustum Culling Settings");
ImGui::DragFloat("Shadow Strength", &this->uniform.shadowStrength, 1, 0.0f, 1.0f);
ImGui::DragFloat("Shadow Bias", &this->uniform.bias, 1, 0.0f, 1.0f);
ImGui::ColorEdit4("Light", &this->uniform.lightColor[0], ImGuiColorEditFlags_Float | ImGuiColorEditFlags_HDR);
ImGui::ColorEdit4("Ambient", &this->uniform.ambientColor[0], ImGuiColorEditFlags_Float | ImGuiColorEditFlags_HDR);
ImGui::ColorEdit4("Light", &this->uniform.lightColor[0],
ImGuiColorEditFlags_Float | ImGuiColorEditFlags_HDR);
ImGui::ColorEdit4("Ambient", &this->uniform.ambientColor[0],
ImGuiColorEditFlags_Float | ImGuiColorEditFlags_HDR);
ImGui::DragFloat3("Direction", &this->uniform.direction[0]);
ImGui::DragFloat("Distance", &this->distance);
ImGui::Checkbox("WireFrame", &this->showWireFrame);
Expand Down Expand Up @@ -162,12 +164,14 @@ namespace glsample {
/* Align uniform buffer in respect to driver requirement. */
GLint minMapBufferSize;
glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &minMapBufferSize);
this->uniformAlignBufferSize = fragcore::Math::align<size_t>(this->uniformAlignBufferSize, (size_t)minMapBufferSize);
this->uniformAlignBufferSize =
fragcore::Math::align<size_t>(this->uniformAlignBufferSize, (size_t)minMapBufferSize);

// Create uniform buffer.
glGenBuffers(1, &this->uniform_buffer);
glBindBufferARB(GL_UNIFORM_BUFFER, this->uniform_buffer);
glBufferData(GL_UNIFORM_BUFFER, this->uniformAlignBufferSize * this->nrUniformBuffer, nullptr, GL_DYNAMIC_DRAW);
glBufferData(GL_UNIFORM_BUFFER, this->uniformAlignBufferSize * this->nrUniformBuffer, nullptr,
GL_DYNAMIC_DRAW);
glBindBufferARB(GL_UNIFORM_BUFFER, 0);

{
Expand Down Expand Up @@ -227,7 +231,8 @@ namespace glsample {

/* */
glBindBufferRange(GL_UNIFORM_BUFFER, this->uniform_buffer_index, uniform_buffer,
(getFrameCount() % nrUniformBuffer) * this->uniformAlignBufferSize, this->uniformAlignBufferSize);
(getFrameCount() % nrUniformBuffer) * this->uniformAlignBufferSize,
this->uniformAlignBufferSize);

{

Expand Down Expand Up @@ -298,7 +303,8 @@ namespace glsample {
glBindBufferARB(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);
this->uniformAlignBufferSize,
GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT | GL_MAP_UNSYNCHRONIZED_BIT);
memcpy(uniformPointer, &this->uniform, sizeof(uniform));
glUnmapBufferARB(GL_UNIFORM_BUFFER);
}
Expand Down
53 changes: 45 additions & 8 deletions Samples/SimpleOcean/SimpleOcean.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "GLUIComponent.h"
#include "PostProcessing/MistPostProcessing.h"
#include "SampleHelper.h"
#include "Skybox.h"
Expand All @@ -23,8 +24,7 @@ namespace glsample {
SimpleOcean() : GLSampleWindow() {
this->setTitle("Simple Ocean");

this->simpleOceanSettingComponent =
std::make_shared<SimpleOceanSettingComponent>(this->uniform_stage_buffer);
this->simpleOceanSettingComponent = std::make_shared<SimpleOceanSettingComponent>(*this);
this->addUIComponent(this->simpleOceanSettingComponent);

/* Default camera position and orientation. */
Expand Down Expand Up @@ -97,9 +97,10 @@ namespace glsample {
size_t uniformAlignBufferSize = sizeof(uniform_buffer_block);
size_t oceanUniformSize = 0;

class SimpleOceanSettingComponent : public nekomimi::UIComponent {
class SimpleOceanSettingComponent : public GLUIComponent<SimpleOcean> {
public:
SimpleOceanSettingComponent(struct uniform_buffer_block &uniform) : uniform(uniform) {
SimpleOceanSettingComponent(SimpleOcean &sample)
: GLUIComponent(sample), uniform(this->getRefSample().uniform_stage_buffer) {
this->setName("Simple Ocean Settings");
}

Expand Down Expand Up @@ -149,13 +150,24 @@ namespace glsample {
ImGui::ColorEdit4("Ocean Base Color", &this->uniform.ocean.oceanColor[0],
ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_Float);

ImGui::TextUnformatted("Fog Settings");
ImGui::DragInt("Fog Type", (int *)&this->getRefSample().mistprocessing.mistsettings.fogSettings.fogType);
ImGui::ColorEdit4("Fog Color", &this->getRefSample().mistprocessing.mistsettings.fogSettings.fogColor[0],
ImGuiColorEditFlags_Float | ImGuiColorEditFlags_HDR);
ImGui::DragFloat("Fog Density", &this->getRefSample().mistprocessing.mistsettings.fogSettings.fogDensity);
ImGui::DragFloat("Fog Intensity", &this->getRefSample().mistprocessing.mistsettings.fogSettings.fogIntensity);
ImGui::DragFloat("Fog Start", &this->getRefSample().mistprocessing.mistsettings.fogSettings.fogStart);
ImGui::DragFloat("Fog End", &this->getRefSample().mistprocessing.mistsettings.fogSettings.fogEnd);

/* */
ImGui::TextUnformatted("Debug");
ImGui::Checkbox("WireFrame", &this->showWireFrame);
ImGui::Checkbox("Use MistFog", &this->useMistFogPost);
}

bool showWireFrame = false;
bool useGerstner = false;
bool useMistFogPost = false;

private:
struct uniform_buffer_block &uniform;
Expand Down Expand Up @@ -295,7 +307,7 @@ namespace glsample {
}

/* */
glDisable(GL_CULL_FACE);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glDepthFunc(GL_LESS);
Expand All @@ -318,6 +330,20 @@ namespace glsample {
glActiveTexture(GL_TEXTURE0 + 10);
glBindTexture(GL_TEXTURE_2D, this->irradiance_texture);

/* */
glActiveTexture(GL_TEXTURE0 + (int)GBuffer::Depth);
glBindTexture(GL_TEXTURE_2D, this->getFrameBuffer()->depthbuffer);

glCullFace(GL_FRONT);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
/* Draw triangle. */
glBindVertexArray(this->plan.vao);
glDrawElements(GL_TRIANGLES, this->plan.nrIndicesElements, GL_UNSIGNED_INT, nullptr);

/* */

glCullFace(GL_BACK);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
/* Draw triangle. */
glBindVertexArray(this->plan.vao);
glDrawElements(GL_TRIANGLES, this->plan.nrIndicesElements, GL_UNSIGNED_INT, nullptr);
Expand All @@ -341,8 +367,10 @@ namespace glsample {
}

/* Post processing. */
this->mistprocessing.render(this->irradiance_texture, this->getFrameBuffer()->attachement0,
this->getFrameBuffer()->depthbuffer);
if (this->simpleOceanSettingComponent->useMistFogPost) {
this->mistprocessing.render(this->irradiance_texture, this->getFrameBuffer()->attachement0,
this->getFrameBuffer()->depthbuffer);
}
}

void update() override {
Expand All @@ -365,10 +393,19 @@ namespace glsample {
this->uniform_stage_buffer.ocean.modelViewProjection = this->uniform_stage_buffer.ocean.proj *
this->uniform_stage_buffer.ocean.view *
this->uniform_stage_buffer.ocean.model;
this->uniform_stage_buffer.ocean.camera.position = glm::vec4(this->camera.getPosition(), 0);

this->uniform_stage_buffer.ocean.time = elapsedTime;

this->uniform_stage_buffer.ocean.camera.position = glm::vec4(this->camera.getPosition(), 0);
this->uniform_stage_buffer.ocean.camera.near = this->camera.getNear();
this->uniform_stage_buffer.ocean.camera.far = this->camera.getFar();
}

this->mistprocessing.mistsettings.proj = this->camera.getProjectionMatrix();
this->mistprocessing.mistsettings.fogSettings.cameraNear = this->camera.getNear();
this->mistprocessing.mistsettings.fogSettings.cameraFar = this->camera.getFar();
this->mistprocessing.mistsettings.viewRotation = camera.getRotationMatrix();

/* */
glBindBuffer(GL_UNIFORM_BUFFER, this->uniform_buffer);
uint8_t *uniformPointer = static_cast<uint8_t *>(glMapBufferRange(
Expand Down
60 changes: 38 additions & 22 deletions Samples/SkinnedMesh/SkinnedMesh.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "GLUIComponent.h"
#include <GL/glew.h>
#include <GLSample.h>
#include <GLSampleWindow.h>
Expand All @@ -20,29 +21,29 @@ namespace glsample {
SkinnedMesh() : GLSampleWindow() {
this->setTitle("Skinned Mesh");

this->skinnedSettingComponent = std::make_shared<SkinnedMeshSettingComponent>(this->uniformStageBuffer);
this->skinnedSettingComponent = std::make_shared<SkinnedMeshSettingComponent>(*this);
this->addUIComponent(this->skinnedSettingComponent);

this->camera.setPosition(glm::vec3(18.5f));
this->camera.setPosition(glm::vec3(25.5f));
this->camera.lookAt(glm::vec3(0.f));
this->camera.enableLook(true);
this->camera.enableNavigation(true);
}

struct uniform_buffer_block {
glm::mat4 model;
glm::mat4 view;
glm::mat4 proj;
glm::mat4 modelView;
glm::mat4 ViewProj;
glm::mat4 modelViewProjection;
glm::mat4 model{};
glm::mat4 view{};
glm::mat4 proj{};
glm::mat4 modelView{};
glm::mat4 ViewProj{};
glm::mat4 modelViewProjection{};

/* light source. */
glm::vec4 direction = glm::vec4(1.0f / sqrt(2.0f), -1.0f / sqrt(2.0f), 0, 0.0f);
glm::vec4 lightColor = glm::vec4(0.5f, 0.5f, 0.6f, 1.0f);
glm::vec4 ambientColor = glm::vec4(0.05, 0.05, 0.05, 1.0f);

/* Material color. */
glm::vec4 ambientColor = glm::vec4(0.05, 0.05, 0.05, 1.0f);
glm::vec4 tintColor = glm::vec4(1);

} uniformStageBuffer;
Expand All @@ -51,16 +52,16 @@ namespace glsample {

Scene scene;

unsigned int skinned_graphic_program;
unsigned int skinned_debug_weight_program;
unsigned int skinned_bone_program;
unsigned int axis_orientation_program;
unsigned int skinned_graphic_program{};
unsigned int skinned_debug_weight_program{};
unsigned int skinned_bone_program{};
unsigned int axis_orientation_program{};

/* */
unsigned int uniform_buffer_binding = 0;
unsigned int uniform_skeleton_buffer_binding = 1;
unsigned int uniform_buffer;
unsigned int uniform_skeleton_buffer;
unsigned int uniform_buffer{};
unsigned int uniform_skeleton_buffer{};
const size_t nrUniformBuffer = 3;
size_t uniformAlignBufferSize = sizeof(uniform_buffer_block);
size_t uniformSkeletonBufferSize = 0;
Expand All @@ -79,12 +80,11 @@ namespace glsample {
const std::string vertexAxisShaderPath = "Shaders/skinnedmesh/skinnedmesh_debug.vert.spv";
const std::string fragmentAxisShaderPath = "Shaders/skinnedmesh/skinnedmesh_debug.frag.spv";

class SkinnedMeshSettingComponent : public nekomimi::UIComponent {
class SkinnedMeshSettingComponent : public GLUIComponent<SkinnedMesh> {

public:
SkinnedMeshSettingComponent(struct uniform_buffer_block &uniform) : uniform(uniform) {
this->setName("Skinned Mesh");
}
SkinnedMeshSettingComponent(SkinnedMesh &sample)
: GLUIComponent(sample, "SkinnedMesh"), uniform(this->getRefSample().uniformStageBuffer) {}
void draw() override {
ImGui::TextUnformatted("Light Settings");
ImGui::ColorEdit4("Light", &this->uniform.lightColor[0],
Expand All @@ -100,6 +100,8 @@ namespace glsample {
ImGui::Checkbox("Show Bone", &this->showBone);
ImGui::Checkbox("Show Weight", &this->showWeight);
ImGui::Checkbox("Show Axis", &this->showAxis);

this->getRefSample().scene.renderUI();
}

bool showWireFrame = false;
Expand Down Expand Up @@ -178,8 +180,8 @@ namespace glsample {
int uniform_buffer_index = glGetUniformBlockIndex(this->skinned_graphic_program, "UniformBufferBlock");
int uniform_skeleton_buffer_index =
glGetUniformBlockIndex(this->skinned_graphic_program, "UniformSkeletonBufferBlock");
glUniform1i(glGetUniformLocation(this->skinned_graphic_program, "DiffuseTexture"), 0);
glUniform1i(glGetUniformLocation(this->skinned_graphic_program, "NormalTexture"), 1);
glUniform1i(glGetUniformLocation(this->skinned_graphic_program, "DiffuseTexture"), TextureType::Diffuse);
glUniform1i(glGetUniformLocation(this->skinned_graphic_program, "NormalTexture"), TextureType::Normal);
glUniformBlockBinding(this->skinned_graphic_program, uniform_buffer_index, this->uniform_buffer_binding);
glUniformBlockBinding(this->skinned_graphic_program, uniform_skeleton_buffer_index,
this->uniform_skeleton_buffer_binding);
Expand Down Expand Up @@ -316,6 +318,17 @@ namespace glsample {
this->uniformStageBuffer.proj * this->uniformStageBuffer.view * this->uniformStageBuffer.model;
}

/* Update Bone Transformation. */
for (auto it = skeleton.bones.begin(); it != skeleton.bones.end(); it++) {
glm::mat4 nodeGlobalTransform = glm::mat4(1);
Bone *bone = &(*it).second;
if (bone->armature_bone) {
nodeGlobalTransform = bone->armature_bone->modelGlobalTransform;
}

bone->finalTransform = nodeGlobalTransform * bone->offsetBoneMatrix;
}

/* */
{
glBindBuffer(GL_UNIFORM_BUFFER, this->uniform_buffer);
Expand All @@ -339,7 +352,10 @@ namespace glsample {
int i = 0;
for (auto it = skeleton.bones.begin(); it != skeleton.bones.end(); it++) {

memcpy(&uniformPointer[i++][0], &(*it).second.inverseBoneMatrix[0][0], sizeof(uniformPointer[0]));
const size_t bone_index = (*it).second.boneIndex;

memcpy(&uniformPointer[bone_index][0][0], &(*it).second.finalTransform[0][0],
sizeof(uniformPointer[0]));
}
}
glUnmapBuffer(GL_UNIFORM_BUFFER);
Expand Down
4 changes: 4 additions & 0 deletions Samples/Terrain/Terrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,10 @@ namespace glsample {
glActiveTexture(GL_TEXTURE0 + TextureType::Displacement);
glBindTexture(GL_TEXTURE_2D, this->terrain_heightMap);

/* */
glActiveTexture(GL_TEXTURE0 + TextureType::DepthBuffer);
glBindTexture(GL_TEXTURE_2D, this->getFrameBuffer()->depthbuffer);

/* */
glActiveTexture(GL_TEXTURE0 + TextureType::Irradiance);
glBindTexture(GL_TEXTURE_2D, this->irradiance_texture);
Expand Down
16 changes: 9 additions & 7 deletions Shaders/common/common.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@
/* Constants. */
#define PI 3.1415926535897932384626433832795
#define PI_HALF 1.5707963267948966192313216916398
#define E_CONSTANT 2.7182818284590
layout(constant_id = 0) const float EPSILON = 1.19209e-07;

struct Camera {
float near;
float far;
float aspect;
float fov;
vec4 position;
vec4 viewDir;
vec4 position_size;
float near; /* */
float far; /* */
float aspect; /* */
float fov; /* */
vec4 position; /* */
vec4 viewDir; /* */
vec4 position_size; /* */
ivec4 screen_width_padding;
};

struct FogSettings {
Expand Down
17 changes: 17 additions & 0 deletions Shaders/common/scene.glsl
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
#include "common.glsl"
#include "material.glsl"

struct common_data {
Camera camera;
Frustum frustum;
};

struct Node {
mat4 model;
};

layout(set = 1, binding = 0, std140) uniform UniformCommonBufferBlock {
common_data constant;

mat4 view;
mat4 proj;
}
constantCommon;

layout(set = 1, binding = 2, std140) uniform UniformSkeletonBufferBlock { mat4 gBones[512]; }
skeletonUBO2;

layout(binding = 0) uniform sampler2D DiffuseTexture;
layout(binding = 1) uniform sampler2D NormalTexture;
layout(binding = 2) uniform sampler2D AlphaMaskedTexture;
Expand Down
3 changes: 3 additions & 0 deletions Shaders/compute/irradiance_env.comp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#extension GL_ARB_shading_language_include : enable
#extension GL_GOOGLE_include_directive : enable

precision mediump float;
precision mediump int;

layout(local_size_x = 16, local_size_y = 16, local_size_z = 1) in;

layout(set = 0, binding = 0) uniform sampler2D sourceEnvTexture;
Expand Down
Loading

0 comments on commit 6cd6f40

Please sign in to comment.