Skip to content

Commit

Permalink
Working MeshLoading Sample (#3)
Browse files Browse the repository at this point in the history
Working MeshLoading Sample
  • Loading branch information
abhayMore authored Aug 28, 2024
1 parent 7b895f9 commit c7fc35e
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 20 deletions.
2 changes: 2 additions & 0 deletions Core/Graphics/Mesh.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "Mesh.h"
#define TINYOBJLOADER_IMPLEMENTATION
#define NULL nullptr
#include <tiny_obj_loader.h>
#undef NULL

namespace CGL::Graphics
{
Expand Down
4 changes: 2 additions & 2 deletions Core/Graphics/Mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace CGL::Graphics

struct MeshData
{
VertexBuffer VertexBuffer;
IndexBuffer IndexBuffer;
VertexBuffer VBuffer;
IndexBuffer IBuffer;
};


Expand Down
13 changes: 13 additions & 0 deletions Core/Graphics/RHI/OpenGL/OPENGLRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,19 @@ namespace CGL::Graphics
{
OPENGLEnableVertexAttributes<VertexTypes::PositionColor>();
}
else if (source.VertexType == typeid(VertexTypes::PositionTexture))
{
OPENGLEnableVertexAttributes<VertexTypes::PositionTexture>();
}
else if (source.VertexType == typeid(VertexTypes::PositionColorTexture))
{
OPENGLEnableVertexAttributes<VertexTypes::PositionColorTexture>();
}
else if (source.VertexType == typeid(VertexTypes::PositionNormalTexture))
{
OPENGLEnableVertexAttributes<VertexTypes::PositionNormalTexture>();
}

glBindVertexArray(0);

return vb;
Expand Down
89 changes: 89 additions & 0 deletions Core/Graphics/RHI/OpenGL/OPENGLVertexAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,93 @@ namespace CGL::Graphics
(void*)offsetof(Graphics::VertexTypes::PositionColor, Color)
);
}

template <>
void OPENGLEnableVertexAttributes<VertexTypes::PositionTexture>() {
glEnableVertexAttribArray(0); // Position Attribute
glVertexAttribPointer(
0,
sizeof(Graphics::VertexTypes::PositionTexture::Position) / sizeof(float),
GL_FLOAT,
GL_FALSE,
sizeof(Graphics::VertexTypes::PositionTexture),
(void*)offsetof(Graphics::VertexTypes::PositionTexture, Position)
);

glEnableVertexAttribArray(1); // Texture Attribute
glVertexAttribPointer(
1,
sizeof(Graphics::VertexTypes::PositionTexture::Texture) / sizeof(float),
GL_FLOAT,
GL_FALSE,
sizeof(Graphics::VertexTypes::PositionTexture),
(void*)offsetof(Graphics::VertexTypes::PositionTexture, Texture)
);
}

template <>
void OPENGLEnableVertexAttributes<VertexTypes::PositionColorTexture>() {
glEnableVertexAttribArray(0); // Position Attribute
glVertexAttribPointer(
0,
sizeof(Graphics::VertexTypes::PositionColorTexture::Position) / sizeof(float),
GL_FLOAT,
GL_FALSE,
sizeof(Graphics::VertexTypes::PositionColorTexture),
(void*)offsetof(Graphics::VertexTypes::PositionColorTexture, Position)
);

glEnableVertexAttribArray(1); // Color Attribute
glVertexAttribPointer(
1,
sizeof(Graphics::VertexTypes::PositionColorTexture::Color) / sizeof(float),
GL_FLOAT,
GL_FALSE,
sizeof(Graphics::VertexTypes::PositionColorTexture),
(void*)offsetof(Graphics::VertexTypes::PositionColorTexture, Color)
);

glEnableVertexAttribArray(2); // Texture Attribute
glVertexAttribPointer(
2,
sizeof(Graphics::VertexTypes::PositionColorTexture::Texture) / sizeof(float),
GL_FLOAT,
GL_FALSE,
sizeof(Graphics::VertexTypes::PositionColorTexture),
(void*)offsetof(Graphics::VertexTypes::PositionColorTexture, Texture)
);
}

template <>
void OPENGLEnableVertexAttributes<VertexTypes::PositionNormalTexture>() {
glEnableVertexAttribArray(0); // Position Attribute
glVertexAttribPointer(
0,
sizeof(Graphics::VertexTypes::PositionNormalTexture::Position) / sizeof(float),
GL_FLOAT,
GL_FALSE,
sizeof(Graphics::VertexTypes::PositionNormalTexture),
(void*)offsetof(Graphics::VertexTypes::PositionNormalTexture, Position)
);

glEnableVertexAttribArray(1); // Normal Attribute
glVertexAttribPointer(
1,
sizeof(Graphics::VertexTypes::PositionNormalTexture::Normal) / sizeof(float),
GL_FLOAT,
GL_FALSE,
sizeof(Graphics::VertexTypes::PositionNormalTexture),
(void*)offsetof(Graphics::VertexTypes::PositionNormalTexture, Normal)
);

glEnableVertexAttribArray(2); // Texture Attribute
glVertexAttribPointer(
2,
sizeof(Graphics::VertexTypes::PositionNormalTexture::Texture) / sizeof(float),
GL_FLOAT,
GL_FALSE,
sizeof(Graphics::VertexTypes::PositionNormalTexture),
(void*)offsetof(Graphics::VertexTypes::PositionNormalTexture, Texture)
);
}
}
3 changes: 3 additions & 0 deletions Core/Graphics/RHI/OpenGL/OPENGLVertexAttributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ namespace CGL::Graphics

template <>
void OPENGLEnableVertexAttributes<VertexTypes::PositionColorTexture>();

template <>
void OPENGLEnableVertexAttributes<VertexTypes::PositionNormalTexture>();
}
11 changes: 11 additions & 0 deletions Samples/ModelLoading/Assets/OpenGL/ModelLoadingPS.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#version 460 core

in vec3 vColor;
in vec3 vNormal;

out vec4 outColor;

void main()
{
outColor = vec4(vNormal, 1.0);
}
27 changes: 27 additions & 0 deletions Samples/ModelLoading/Assets/OpenGL/ModelLoadingVS.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#version 460 core

layout(std140, binding = 0) uniform Matrices
{
mat4 WorldMatrix;
mat4 ViewMatrix;
mat4 ProjMatrix;
};

layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec3 inNormal;
layout(location = 2) in vec3 inColor;

out vec3 vColor;
out vec3 vNormal;

void main()
{
vec4 position = vec4(inPosition, 1.0);
position = WorldMatrix * position;
position = ViewMatrix * position;
position = ProjMatrix * position;

gl_Position = position;
vNormal = inNormal;
vColor = inColor;
}
35 changes: 23 additions & 12 deletions Samples/ModelLoading/ModelLoading.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
#include "ModelLoading.h"
#include <Core/Application/AssetFinder.h>

#if defined(CGL_RHI_OPENGL) || defined(CGL_RHI_VULKAN)
#define CGL_UPLOAD_MATRIX(mat) mat
#else
#define CGL_UPLOAD_MATRIX(mat) mat.Transpose()
#endif

namespace CGL
{
CGL_DEFINE_LOG_CATEGORY(ModelLoading);

static constexpr byte s_vertexShader[] =
{
#if defined(CGL_RHI_DX11)
#include "ModelLoadingVS.hlsl.h"
#include "ModelLoadingVS.hlsl.h"
#elif defined(CGL_RHI_OPENGL)
#include "ModelLoadingVS.vert.h"
#endif
};

static constexpr byte s_pixelShader[] =
{
#if defined(CGL_RHI_DX11)
#include "ModelLoadingPS.hlsl.h"
#include "ModelLoadingPS.hlsl.h"
#elif defined(CGL_RHI_OPENGL)
#include "ModelLoadingPS.frag.h"
#endif
};

Expand Down Expand Up @@ -94,15 +104,16 @@ namespace CGL
vbs.Type = Graphics::BufferType::Vertex;
vbs.TypeSize = sizeof(decltype(vb)::value_type);
vbs.Count = u32(vb.size());
meshData.VertexBuffer = GetRenderer()->CreateVertexBuffer(vbs);
vbs.VertexType = typeid(decltype(vb)::value_type);
meshData.VBuffer = GetRenderer()->CreateVertexBuffer(vbs);

// Create index buffer
Graphics::BufferSource ibs;
ibs.Data = (void*)indices.data();
ibs.Type = Graphics::BufferType::Index;
ibs.TypeSize = sizeof(u32);
ibs.Count = u32(indices.size());
meshData.IndexBuffer = GetRenderer()->CreateIndexBuffer(ibs);
meshData.IBuffer = GetRenderer()->CreateIndexBuffer(ibs);
}

// Create constant buffer
Expand All @@ -122,14 +133,14 @@ namespace CGL
}

f32 time = 0.0f;
void ModelLoading::OnUpdate([[maybe_unused]] const SDL_Event& e)
void ModelLoading::OnUpdate([[maybe_unused]] const SDL_Event& e, f32 deltaTime)
{
time += 0.0001f;
time += deltaTime;
FrameData data
{
.World = SM::Matrix::CreateRotationY(time),
.View = m_camera.GetViewMatrix().Transpose(),
.Projection = m_camera.GetProjectionMatrix().Transpose()
.World = CGL_UPLOAD_MATRIX(SM::Matrix::CreateRotationY(time).Transpose()),
.View = CGL_UPLOAD_MATRIX(m_camera.GetViewMatrix()),
.Projection = CGL_UPLOAD_MATRIX(m_camera.GetProjectionMatrix())
};

GetRenderer()->SetConstantBufferData(m_constantBuffer, data);
Expand All @@ -144,10 +155,10 @@ namespace CGL
for (auto& subMesh : m_mesh.SubMeshes)
{
auto& meshData = subMesh.GetMeshData();
GetRenderer()->SetVertexBuffer(meshData.VertexBuffer);
GetRenderer()->SetIndexBuffer(meshData.IndexBuffer);
GetRenderer()->SetVertexBuffer(meshData.VBuffer);
GetRenderer()->SetIndexBuffer(meshData.IBuffer);

GetRenderer()->DrawIndexed(meshData.IndexBuffer.IndicesCount);
GetRenderer()->DrawIndexed(meshData.IBuffer.IndicesCount);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Samples/ModelLoading/ModelLoading.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace CGL

private:
bool OnInit() override final;
void OnUpdate(const SDL_Event& e) override final;
void OnUpdate(const SDL_Event& e, f32 deltaTime) override final;
void OnRender() override final;
void OnResize(u32 width, u32 height) override final;
void OnShutdown() override final;
Expand Down
6 changes: 4 additions & 2 deletions Samples/ModelLoading/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ target("ModelLoading")
set_group("Samples")

add_rules("RHICompat", "CopyOBJ")
add_packages("libsdl", "tinyobjloader")
add_packages("libsdl", "directxmath", "tinyobjloader")

add_includedirs("..", "$(projectdir)")
add_files("**.cpp")
Expand All @@ -35,7 +35,9 @@ target("ModelLoading")
add_files("Assets/**.hlsl")
elseif rhi == "VULKAN" or rhi == "OPENGL" then
add_rules("utils.bin2c", { extensions = { ".vert", ".frag" } })
add_files("Assets/**.vert", "Assets/**.frag")
if rhi == "OPENGL" then
add_files("Assets/OpenGL/**.vert", "Assets/OpenGL/**.frag")
end
elseif rhi == "METAL" then
add_rules("utils.bin2c", { extensions = { ".metal" } })
add_files("Assets/**.metal")
Expand Down
4 changes: 1 addition & 3 deletions Samples/SpinningCube/Assets/OpenGL/SpinningCubeVS.vert
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ out vec3 fragColor;
void main()
{
vec4 position = vec4(inPosition, 1.0);
position = WorldMatrix * position;
position = ViewMatrix * position;
position = ProjMatrix * position;
position = ProjMatrix * ViewMatrix * WorldMatrix * position;

gl_Position = position;
fragColor = inColor;
Expand Down

0 comments on commit c7fc35e

Please sign in to comment.