Skip to content

Commit dfc4e50

Browse files
authored
Working ModelLoading Sample (#53)
* Working MeshLoading Sample (#3) Working MeshLoading Sample * Fix vertex shader matrices calulcation * PR changes : Added comments, refactored macro for tinyobjloader
1 parent 7b895f9 commit dfc4e50

File tree

11 files changed

+184
-18
lines changed

11 files changed

+184
-18
lines changed

Core/Graphics/Mesh.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
#include "Mesh.h"
22
#define TINYOBJLOADER_IMPLEMENTATION
3-
#include <tiny_obj_loader.h>
3+
// tinyobjloader uses NULL instead of nullptr for pointers,
4+
// which can cause issues on Linux and macOS. The code below
5+
// temporarily redefines NULL as nullptr to ensure compatibility.
6+
#if defined(__linux__) || defined(__APPLE__)
7+
#define NULL nullptr
8+
#include <tiny_obj_loader.h>
9+
#undef NULL
10+
#else
11+
#include <tiny_obj_loader.h>
12+
#endif
413

514
namespace CGL::Graphics
615
{

Core/Graphics/Mesh.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ namespace CGL::Graphics
1010

1111
struct MeshData
1212
{
13-
VertexBuffer VertexBuffer;
14-
IndexBuffer IndexBuffer;
13+
VertexBuffer VBuffer;
14+
IndexBuffer IBuffer;
1515
};
1616

1717

Core/Graphics/RHI/OpenGL/OPENGLRenderer.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,19 @@ namespace CGL::Graphics
185185
{
186186
OPENGLEnableVertexAttributes<VertexTypes::PositionColor>();
187187
}
188+
else if (source.VertexType == typeid(VertexTypes::PositionTexture))
189+
{
190+
OPENGLEnableVertexAttributes<VertexTypes::PositionTexture>();
191+
}
192+
else if (source.VertexType == typeid(VertexTypes::PositionColorTexture))
193+
{
194+
OPENGLEnableVertexAttributes<VertexTypes::PositionColorTexture>();
195+
}
196+
else if (source.VertexType == typeid(VertexTypes::PositionNormalTexture))
197+
{
198+
OPENGLEnableVertexAttributes<VertexTypes::PositionNormalTexture>();
199+
}
200+
188201
glBindVertexArray(0);
189202

190203
return vb;

Core/Graphics/RHI/OpenGL/OPENGLVertexAttributes.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,93 @@ namespace CGL::Graphics
3838
(void*)offsetof(Graphics::VertexTypes::PositionColor, Color)
3939
);
4040
}
41+
42+
template <>
43+
void OPENGLEnableVertexAttributes<VertexTypes::PositionTexture>() {
44+
glEnableVertexAttribArray(0); // Position Attribute
45+
glVertexAttribPointer(
46+
0,
47+
sizeof(Graphics::VertexTypes::PositionTexture::Position) / sizeof(float),
48+
GL_FLOAT,
49+
GL_FALSE,
50+
sizeof(Graphics::VertexTypes::PositionTexture),
51+
(void*)offsetof(Graphics::VertexTypes::PositionTexture, Position)
52+
);
53+
54+
glEnableVertexAttribArray(1); // Texture Attribute
55+
glVertexAttribPointer(
56+
1,
57+
sizeof(Graphics::VertexTypes::PositionTexture::Texture) / sizeof(float),
58+
GL_FLOAT,
59+
GL_FALSE,
60+
sizeof(Graphics::VertexTypes::PositionTexture),
61+
(void*)offsetof(Graphics::VertexTypes::PositionTexture, Texture)
62+
);
63+
}
64+
65+
template <>
66+
void OPENGLEnableVertexAttributes<VertexTypes::PositionColorTexture>() {
67+
glEnableVertexAttribArray(0); // Position Attribute
68+
glVertexAttribPointer(
69+
0,
70+
sizeof(Graphics::VertexTypes::PositionColorTexture::Position) / sizeof(float),
71+
GL_FLOAT,
72+
GL_FALSE,
73+
sizeof(Graphics::VertexTypes::PositionColorTexture),
74+
(void*)offsetof(Graphics::VertexTypes::PositionColorTexture, Position)
75+
);
76+
77+
glEnableVertexAttribArray(1); // Color Attribute
78+
glVertexAttribPointer(
79+
1,
80+
sizeof(Graphics::VertexTypes::PositionColorTexture::Color) / sizeof(float),
81+
GL_FLOAT,
82+
GL_FALSE,
83+
sizeof(Graphics::VertexTypes::PositionColorTexture),
84+
(void*)offsetof(Graphics::VertexTypes::PositionColorTexture, Color)
85+
);
86+
87+
glEnableVertexAttribArray(2); // Texture Attribute
88+
glVertexAttribPointer(
89+
2,
90+
sizeof(Graphics::VertexTypes::PositionColorTexture::Texture) / sizeof(float),
91+
GL_FLOAT,
92+
GL_FALSE,
93+
sizeof(Graphics::VertexTypes::PositionColorTexture),
94+
(void*)offsetof(Graphics::VertexTypes::PositionColorTexture, Texture)
95+
);
96+
}
97+
98+
template <>
99+
void OPENGLEnableVertexAttributes<VertexTypes::PositionNormalTexture>() {
100+
glEnableVertexAttribArray(0); // Position Attribute
101+
glVertexAttribPointer(
102+
0,
103+
sizeof(Graphics::VertexTypes::PositionNormalTexture::Position) / sizeof(float),
104+
GL_FLOAT,
105+
GL_FALSE,
106+
sizeof(Graphics::VertexTypes::PositionNormalTexture),
107+
(void*)offsetof(Graphics::VertexTypes::PositionNormalTexture, Position)
108+
);
109+
110+
glEnableVertexAttribArray(1); // Normal Attribute
111+
glVertexAttribPointer(
112+
1,
113+
sizeof(Graphics::VertexTypes::PositionNormalTexture::Normal) / sizeof(float),
114+
GL_FLOAT,
115+
GL_FALSE,
116+
sizeof(Graphics::VertexTypes::PositionNormalTexture),
117+
(void*)offsetof(Graphics::VertexTypes::PositionNormalTexture, Normal)
118+
);
119+
120+
glEnableVertexAttribArray(2); // Texture Attribute
121+
glVertexAttribPointer(
122+
2,
123+
sizeof(Graphics::VertexTypes::PositionNormalTexture::Texture) / sizeof(float),
124+
GL_FLOAT,
125+
GL_FALSE,
126+
sizeof(Graphics::VertexTypes::PositionNormalTexture),
127+
(void*)offsetof(Graphics::VertexTypes::PositionNormalTexture, Texture)
128+
);
129+
}
41130
}

Core/Graphics/RHI/OpenGL/OPENGLVertexAttributes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ namespace CGL::Graphics
1919

2020
template <>
2121
void OPENGLEnableVertexAttributes<VertexTypes::PositionColorTexture>();
22+
23+
template <>
24+
void OPENGLEnableVertexAttributes<VertexTypes::PositionNormalTexture>();
2225
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#version 460 core
2+
3+
in vec3 vColor;
4+
in vec3 vNormal;
5+
6+
out vec4 outColor;
7+
8+
void main()
9+
{
10+
outColor = vec4(vNormal, 1.0);
11+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#version 460 core
2+
3+
layout(std140, binding = 0) uniform Matrices
4+
{
5+
mat4 WorldMatrix;
6+
mat4 ViewMatrix;
7+
mat4 ProjMatrix;
8+
};
9+
10+
layout(location = 0) in vec3 inPosition;
11+
layout(location = 1) in vec3 inNormal;
12+
layout(location = 2) in vec3 inColor;
13+
14+
out vec3 vColor;
15+
out vec3 vNormal;
16+
17+
void main()
18+
{
19+
vec4 position = vec4(inPosition, 1.0);
20+
position = WorldMatrix * position;
21+
position = ViewMatrix * position;
22+
position = ProjMatrix * position;
23+
24+
gl_Position = position;
25+
vNormal = inNormal;
26+
vColor = inColor;
27+
}

Samples/ModelLoading/ModelLoading.cpp

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
#include "ModelLoading.h"
22
#include <Core/Application/AssetFinder.h>
33

4+
#if defined(CGL_RHI_OPENGL) || defined(CGL_RHI_VULKAN)
5+
#define CGL_UPLOAD_MATRIX(mat) mat
6+
#else
7+
#define CGL_UPLOAD_MATRIX(mat) mat.Transpose()
8+
#endif
9+
410
namespace CGL
511
{
612
CGL_DEFINE_LOG_CATEGORY(ModelLoading);
713

814
static constexpr byte s_vertexShader[] =
915
{
1016
#if defined(CGL_RHI_DX11)
11-
#include "ModelLoadingVS.hlsl.h"
17+
#include "ModelLoadingVS.hlsl.h"
18+
#elif defined(CGL_RHI_OPENGL)
19+
#include "ModelLoadingVS.vert.h"
1220
#endif
1321
};
1422

1523
static constexpr byte s_pixelShader[] =
1624
{
1725
#if defined(CGL_RHI_DX11)
18-
#include "ModelLoadingPS.hlsl.h"
26+
#include "ModelLoadingPS.hlsl.h"
27+
#elif defined(CGL_RHI_OPENGL)
28+
#include "ModelLoadingPS.frag.h"
1929
#endif
2030
};
2131

@@ -94,15 +104,16 @@ namespace CGL
94104
vbs.Type = Graphics::BufferType::Vertex;
95105
vbs.TypeSize = sizeof(decltype(vb)::value_type);
96106
vbs.Count = u32(vb.size());
97-
meshData.VertexBuffer = GetRenderer()->CreateVertexBuffer(vbs);
107+
vbs.VertexType = typeid(decltype(vb)::value_type);
108+
meshData.VBuffer = GetRenderer()->CreateVertexBuffer(vbs);
98109

99110
// Create index buffer
100111
Graphics::BufferSource ibs;
101112
ibs.Data = (void*)indices.data();
102113
ibs.Type = Graphics::BufferType::Index;
103114
ibs.TypeSize = sizeof(u32);
104115
ibs.Count = u32(indices.size());
105-
meshData.IndexBuffer = GetRenderer()->CreateIndexBuffer(ibs);
116+
meshData.IBuffer = GetRenderer()->CreateIndexBuffer(ibs);
106117
}
107118

108119
// Create constant buffer
@@ -122,14 +133,14 @@ namespace CGL
122133
}
123134

124135
f32 time = 0.0f;
125-
void ModelLoading::OnUpdate([[maybe_unused]] const SDL_Event& e)
136+
void ModelLoading::OnUpdate([[maybe_unused]] const SDL_Event& e, f32 deltaTime)
126137
{
127-
time += 0.0001f;
138+
time += deltaTime;
128139
FrameData data
129140
{
130-
.World = SM::Matrix::CreateRotationY(time),
131-
.View = m_camera.GetViewMatrix().Transpose(),
132-
.Projection = m_camera.GetProjectionMatrix().Transpose()
141+
.World = CGL_UPLOAD_MATRIX(SM::Matrix::CreateRotationY(time).Transpose()),
142+
.View = CGL_UPLOAD_MATRIX(m_camera.GetViewMatrix()),
143+
.Projection = CGL_UPLOAD_MATRIX(m_camera.GetProjectionMatrix())
133144
};
134145

135146
GetRenderer()->SetConstantBufferData(m_constantBuffer, data);
@@ -144,10 +155,10 @@ namespace CGL
144155
for (auto& subMesh : m_mesh.SubMeshes)
145156
{
146157
auto& meshData = subMesh.GetMeshData();
147-
GetRenderer()->SetVertexBuffer(meshData.VertexBuffer);
148-
GetRenderer()->SetIndexBuffer(meshData.IndexBuffer);
158+
GetRenderer()->SetVertexBuffer(meshData.VBuffer);
159+
GetRenderer()->SetIndexBuffer(meshData.IBuffer);
149160

150-
GetRenderer()->DrawIndexed(meshData.IndexBuffer.IndicesCount);
161+
GetRenderer()->DrawIndexed(meshData.IBuffer.IndicesCount);
151162
}
152163
}
153164

Samples/ModelLoading/ModelLoading.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace CGL
2626

2727
private:
2828
bool OnInit() override final;
29-
void OnUpdate(const SDL_Event& e) override final;
29+
void OnUpdate(const SDL_Event& e, f32 deltaTime) override final;
3030
void OnRender() override final;
3131
void OnResize(u32 width, u32 height) override final;
3232
void OnShutdown() override final;

Samples/ModelLoading/xmake.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ target("ModelLoading")
88
set_group("Samples")
99

1010
add_rules("RHICompat", "CopyOBJ")
11-
add_packages("libsdl", "tinyobjloader")
11+
add_packages("libsdl", "directxmath", "tinyobjloader")
1212

1313
add_includedirs("..", "$(projectdir)")
1414
add_files("**.cpp")
@@ -35,7 +35,9 @@ target("ModelLoading")
3535
add_files("Assets/**.hlsl")
3636
elseif rhi == "VULKAN" or rhi == "OPENGL" then
3737
add_rules("utils.bin2c", { extensions = { ".vert", ".frag" } })
38-
add_files("Assets/**.vert", "Assets/**.frag")
38+
if rhi == "OPENGL" then
39+
add_files("Assets/OpenGL/**.vert", "Assets/OpenGL/**.frag")
40+
end
3941
elseif rhi == "METAL" then
4042
add_rules("utils.bin2c", { extensions = { ".metal" } })
4143
add_files("Assets/**.metal")

Samples/SpinningCube/Assets/OpenGL/SpinningCubeVS.vert

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ out vec3 fragColor;
1515
void main()
1616
{
1717
vec4 position = vec4(inPosition, 1.0);
18+
1819
position = WorldMatrix * position;
1920
position = ViewMatrix * position;
2021
position = ProjMatrix * position;

0 commit comments

Comments
 (0)