Skip to content

Commit

Permalink
Fixed vector-matrix multiplication and model loading with nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
denyskryvytskyi committed Mar 18, 2024
1 parent 2d05b7d commit bfa76ee
Show file tree
Hide file tree
Showing 61 changed files with 143 additions and 70 deletions.
2 changes: 1 addition & 1 deletion Engine/src/Platform/OpenGL/OpenGLShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void OpenGLShader::SetVector4f(const std::string& name, const lia::vec4& value)

void OpenGLShader::SetMatrix4(const std::string& name, const lia::mat4& matrix)
{
glUniformMatrix4fv(GetUniformLocation(name), 1, GL_FALSE, matrix.elementsPtr());
glUniformMatrix4fv(GetUniformLocation(name), 1, GL_TRUE, matrix.elementsPtr());
}

int OpenGLShader::GetUniformLocation(const std::string& name)
Expand Down
3 changes: 0 additions & 3 deletions Engine/src/Renderer/TextRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ void TextRenderer::RenderText(std::string_view text, const std::string& fontName
const auto& glyphs = gFontManager.GetGlyphs(fontName);

s_data.topGlyphOffsetY = glyphs.at(topGlyph).offset.y * s_data.pixelToCamera.y;
s_data.shader->Bind();
s_data.shader->SetVector4f("textColor", color);
s_data.shader->SetInteger("u_texture", 0);

Expand Down Expand Up @@ -143,8 +142,6 @@ void TextRenderer::RenderText(std::string_view text, const std::string& fontName

RenderCommand::DrawIndexed(s_data.vao, quadIndexCount);

glyph.texture->Unbind();

// now advance cursors for next glyph
currentGlyphPosX += s_data.pixelToCamera.x * scale.x * static_cast<float>(glyph.advance >> 6); // bitshift by 6 to get value in pixels (1/64th times 2^6 = 64)
}
Expand Down
8 changes: 5 additions & 3 deletions Engine/src/Resources/MeshLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ static void LoadMeshFromFile(std::vector<LoadedMeshesInfo>& loadedMeshesInfo, co
info.name = meshName;
info.root = root;

#ifdef ASSIMP_MODE
ImportModel(meshPath, info);
#endif

std::lock_guard<std::mutex> lock(meshLoadingMutex);
loadedMeshesInfo.emplace_back(info);
Expand Down Expand Up @@ -169,9 +171,9 @@ void MeshLibrary::LoadPrimitives()
for (std::uint32_t x = 0; x <= xSegmentsSphere; ++x) {
float xSegment = static_cast<float>(x) / static_cast<float>(xSegmentsSphere);
float ySegment = static_cast<float>(y) / static_cast<float>(ySegmentsSphere);
float xPos = std::cos(xSegment * TAU) * std::sin(ySegment * PI); // TAU is 2PI
float yPos = std::cos(ySegment * PI);
float zPos = std::sin(xSegment * TAU) * std::sin(ySegment * PI);
float xPos = std::cos(xSegment * TAU) * std::sin(ySegment * PI) / 2.0f; // TAU is 2PI
float yPos = std::cos(ySegment * PI) / 2.0f;
float zPos = std::sin(xSegment * TAU) * std::sin(ySegment * PI) / 2.0f;

MeshVertex vertex;
vertex.Position = { xPos, yPos, zPos };
Expand Down
25 changes: 15 additions & 10 deletions Engine/src/Resources/ModelImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ void LoadMaterialTextures(aiMaterial* mat, aiTextureType type, const Material::T
}
}

void ProcessMesh(aiMesh* mesh, const aiScene* scene, LoadedMeshInfo& info)
void ProcessMesh(aiMesh* mesh, const aiScene* scene, lia::mat4 worldMatrix, LoadedMeshInfo& info)
{
for (size_t i = 0; i < mesh->mNumVertices; ++i) {
// process vertix position, normal and UV
// process vertex position, normal and UV

const lia::vec4 finalNodePos = worldMatrix * lia::vec4(mesh->mVertices[i].x, mesh->mVertices[i].y, mesh->mVertices[i].z, 1.0f);
MeshVertex vertex;
vertex.Position.x = mesh->mVertices[i].x;
vertex.Position.y = mesh->mVertices[i].y;
vertex.Position.z = mesh->mVertices[i].z;
vertex.Position.x = finalNodePos.x;
vertex.Position.y = finalNodePos.y;
vertex.Position.z = finalNodePos.z;

if (mesh->mNormals) {
vertex.Normal.x = mesh->mNormals[i].x;
Expand Down Expand Up @@ -59,29 +61,32 @@ void ProcessMesh(aiMesh* mesh, const aiScene* scene, LoadedMeshInfo& info)
// process textures
if (mesh->mMaterialIndex >= 0) {
aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];

LoadMaterialTextures(material, aiTextureType_DIFFUSE, Material::TextureSlot::Diffuse, info.textures);
LoadMaterialTextures(material, aiTextureType_SPECULAR, Material::TextureSlot::Specular, info.textures);
LoadMaterialTextures(material, aiTextureType_EMISSIVE, Material::TextureSlot::Emission, info.textures);
LoadMaterialTextures(material, aiTextureType_NORMALS, Material::TextureSlot::Normal, info.textures);
LoadMaterialTextures(material, aiTextureType_HEIGHT, Material::TextureSlot::Normal, info.textures);
LoadMaterialTextures(material, aiTextureType_OPACITY, Material::TextureSlot::Transparency, info.textures);
}
}

void ProcessNode(aiNode* node, const aiScene* scene, LoadedMeshesInfo& loadedMeshesInfo)
void ProcessNode(aiNode* node, const aiScene* scene, lia::mat4 worldMatrix, LoadedMeshesInfo& loadedMeshesInfo)
{
auto m = node->mTransformation;
worldMatrix = worldMatrix * lia::mat4(m.a1, m.a2, m.a3, m.a4, m.b1, m.b2, m.b3, m.b4, m.c1, m.c2, m.c3, m.c4, m.d1, m.d2, m.d3, m.d4);

// process meshes
for (size_t i = 0; i < node->mNumMeshes; ++i) {
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];

LoadedMeshInfo meshInfo;
ProcessMesh(mesh, scene, meshInfo);
ProcessMesh(mesh, scene, worldMatrix, meshInfo);
loadedMeshesInfo.meshes.emplace_back(std::move(meshInfo));
}

// process children
for (size_t i = 0; i < node->mNumChildren; ++i) {
ProcessNode(node->mChildren[i], scene, loadedMeshesInfo);
ProcessNode(node->mChildren[i], scene, worldMatrix, loadedMeshesInfo);
}
}

Expand All @@ -100,7 +105,7 @@ void ImportModel(const std::string& path, LoadedMeshesInfo& info)

{
PROFILE_SCOPE(fmt::format("Process model {} nodes: ", path));
ProcessNode(scene->mRootNode, scene, info);
ProcessNode(scene->mRootNode, scene, lia::mat4(), info);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Engine/src/Scene/Components/SceneComponents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void SpriteComponent::LoadTexture()
texture = textures::Get(textureName);
}
},
string_id(textureName));
string_id(textureName), true);
}
} else {
EL_CORE_WARN("Texture loading is failed. Please call SetTexture function first or set members using ctor.")
Expand Down
1 change: 0 additions & 1 deletion Engine/src/Scene/Systems/Render2dSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,5 @@ void Render2dSystem::OnRender(float dt)
elv::TextRenderer::RenderText(textComponent.text, textComponent.fontName, rectTransform.pos, rectTransform.scale, textComponent.color);
}
}
//
}
} // namespace elv
2 changes: 1 addition & 1 deletion Engine/vendor/lia
Submodule lia updated 3 files
+12 −3 lia/mat4.h
+12 −1 lia/mathbase.h
+6 −0 lia/tests.cpp
2 changes: 1 addition & 1 deletion Games/Invaders/assets/shaders/texture_shader.vert
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ flat out int v_TextureUnit;

void main()
{
gl_Position = u_ViewProjection * a_Position;
gl_Position = a_Position * u_ViewProjection;
v_UV = a_UV;
v_Color = a_Color;
v_TextureUnit = int(a_TextureUnit);
Expand Down
2 changes: 1 addition & 1 deletion Games/Invaders/src/Invaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ void Invaders::OnCreate()
elv::gAudioManager.AddSound("powerup", "powerup.wav");

// Preload textures
elv::gTextureManager.Load("shield", "Effects/shield1.png");
elv::textures::Load("shield", "assets/images/Effects/shield1.png");

auto& scene = elv::GetScene();

Expand Down
2 changes: 1 addition & 1 deletion Games/Pong/assets/shaders/texture_shader.vert
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ flat out int v_TextureUnit;

void main()
{
gl_Position = u_ViewProjection * a_Position;
gl_Position = a_Position * u_ViewProjection;
v_UV = a_UV;
v_Color = a_Color;
v_TextureUnit = int(a_TextureUnit);
Expand Down
2 changes: 1 addition & 1 deletion Games/TRON/assets/shaders/texture_shader.vert
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ flat out int v_TextureUnit;

void main()
{
gl_Position = u_ViewProjection * a_Position;
gl_Position = a_Position * u_ViewProjection;
v_UV = a_UV;
v_Color = a_Color;
v_TextureUnit = int(a_TextureUnit);
Expand Down
2 changes: 1 addition & 1 deletion Sandbox2D/assets/shaders/texture_shader.vert
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ flat out int v_TextureUnit;

void main()
{
gl_Position = u_ViewProjection * a_Position;
gl_Position = a_Position * u_ViewProjection;
v_UV = a_UV;
v_Color = a_Color;
v_TextureUnit = int(a_TextureUnit);
Expand Down
Binary file added Sandbox3D/assets/images/sphere.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Sandbox3D/assets/models/backpack/albedo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions Sandbox3D/assets/models/backpack/backpack.fbx
Git LFS file not shown
16 changes: 0 additions & 16 deletions Sandbox3D/assets/models/backpack/backpack.mtl

This file was deleted.

3 changes: 0 additions & 3 deletions Sandbox3D/assets/models/backpack/backpack.obj

This file was deleted.

Binary file removed Sandbox3D/assets/models/backpack/diffuse.jpg
Binary file not shown.
Binary file added Sandbox3D/assets/models/backpack/metallic.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Sandbox3D/assets/models/backpack/roughness.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 0 additions & 3 deletions Sandbox3D/assets/models/backpack/source_attribution.txt

This file was deleted.

Binary file removed Sandbox3D/assets/models/backpack/specular.jpg
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
3 changes: 0 additions & 3 deletions Sandbox3D/assets/models/tank/tank.fbx

This file was deleted.

64 changes: 64 additions & 0 deletions Sandbox3D/assets/models/walle/model.mtl
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Blender MTL File: 'walle.blend'
# Material Count: 4

newmtl Hand
Ns 225.000000
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.800000 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.0 0.0 0.0
Ni 1.450000
d 1.000000
illum 2
map_Kd textures/Hand_albedo.jpg
map_Ks textures/Hand_metallic.jpg
map_bump textures/Hand_normal.png
map_Ns textures/Hand_roughness.jpg
refl textures/Hand_metallic.jpg

newmtl Head
Ns 225.000000
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.800000 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.0 0.0 0.0
Ni 1.450000
d 1.000000
illum 2
map_Kd textures/Head_albedo.jpg
map_Ks textures/Head_metallic.jpg
map_bump textures/Head_normal.png
map_Ns textures/Head_roughness.jpg
map_d textures/Head_opacity.jpg
refl textures/Head_metallic.jpg

newmtl Tors
Ns 225.000000
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.800000 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.0 0.0 0.0
Ni 1.450000
d 1.000000
illum 2
map_bump textures/Tors_normal.png
map_Kd textures/Tors_albedo.jpg
map_Ks textures/Tors_metallic.jpg
map_Ke textures/Tors_emissive.jpg
map_Ns textures/Tors_roughness.jpg
refl textures/Tors_metallic.jpg

newmtl Track
Ns 225.000000
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.800000 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.0 0.0 0.0
Ni 1.450000
d 1.000000
illum 2
map_bump textures/Track_normal.png
map_Kd textures/Track_albedo.jpg
map_Ks textures/Track_metallic.jpg
map_Ns textures/Track_roughness.jpg
refl textures/Track_metallic.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions Sandbox3D/assets/models/walle/walle.obj
Git LFS file not shown
5 changes: 3 additions & 2 deletions Sandbox3D/assets/shaders/colors.vert
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ out vec3 v_FragPos;

void main()
{
gl_Position = u_ViewProjection * u_Model * vec4(a_Position, 1.0);
v_FragPos = vec3(u_Model * vec4(a_Position, 1.0));
vec4 worldPos = vec4(a_Position, 1.0) * u_Model;
gl_Position = worldPos * u_ViewProjection;
v_FragPos = vec3(worldPos);
v_Normal = mat3(transpose(u_InversedNormalModel)) * a_Normal;
}
2 changes: 1 addition & 1 deletion Sandbox3D/assets/shaders/default.vert
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ out vec2 v_UV;

void main()
{
gl_Position = u_ViewProjection * u_Model * vec4(a_Position, 1.0);
gl_Position = vec4(a_Position, 1.0) * u_Model * u_ViewProjection;
v_UV = a_UV;
}
2 changes: 1 addition & 1 deletion Sandbox3D/assets/shaders/light_cube.vert
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ uniform mat4 u_Model;

void main()
{
gl_Position = u_ViewProjection * u_Model * vec4(a_Position, 1.0);
gl_Position = vec4(a_Position, 1.0) * u_Model * u_ViewProjection;
}
2 changes: 1 addition & 1 deletion Sandbox3D/assets/shaders/text2d.vert
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ out vec2 v_uv;

void main()
{
gl_Position = projection * vec4(pos, 0.0, 1.0);
gl_Position = vec4(pos, 0.0, 1.0) * projection;
v_uv = uv;
}
2 changes: 1 addition & 1 deletion Sandbox3D/assets/shaders/texture_shader.vert
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ flat out int v_TextureUnit;

void main()
{
gl_Position = u_ViewProjection * a_Position;
gl_Position = a_Position * u_ViewProjection;
v_UV = a_UV;
v_Color = a_Color;
v_TextureUnit = int(a_TextureUnit);
Expand Down
5 changes: 3 additions & 2 deletions Sandbox3D/assets/shaders/textured_cube.vert
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ out vec2 v_UV;

void main()
{
gl_Position = u_ViewProjection * u_Model * vec4(a_Position, 1.0);
v_FragPos = vec3(u_Model * vec4(a_Position, 1.0));
vec4 worldPos = vec4(a_Position, 1.0) * u_Model;
gl_Position = vec4(a_Position, 1.0) * u_Model * u_ViewProjection;
v_FragPos = vec3(worldPos);
v_Normal = mat3(transpose(u_InversedNormalModel)) * a_Normal;
v_UV = a_UV;
}
Loading

0 comments on commit bfa76ee

Please sign in to comment.