Skip to content

Commit e60ee39

Browse files
committed
refactor(gfx): refactored material attributes
1 parent b6d6748 commit e60ee39

File tree

9 files changed

+95
-23
lines changed

9 files changed

+95
-23
lines changed

Content/GLSL/Unlit/SolidColour.frag

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#version 330
2+
3+
out vec4 color;
4+
5+
uniform vec3 colour;
6+
7+
void main() {
8+
color = vec4(colour, 1.0);
9+
}

Content/GLSL/Unlit/SolidColour.vert

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#version 330
2+
3+
uniform mat4 VIEW;
4+
uniform mat4 MODEL;
5+
uniform mat4 PROJ;
6+
7+
layout(location = 0) in vec3 position;
8+
9+
void main() {
10+
gl_Position = PROJ * VIEW * MODEL * vec4(position, 1.0);
11+
}

src/examples/demo/DemoApp.h

+26-9
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,30 @@ class DemoApp : public system::BaseApp
135135
engine::ConsoleLogger::Global().LogInfo("", "main", "Demo starting!");
136136

137137
// Load & compile shaders.
138-
std::string vertShaderSrc = system::FileSystem::Read("../../Content/GLSL/Unlit/RGBVertex.vert"), fragShaderSrc = system::FileSystem::Read("../../Content/GLSL/Unlit/RGBVertex.frag");
138+
std::string
139+
vertShaderSrc = system::FileSystem::Read("../../Content/GLSL/Unlit/RGBVertex.vert"),
140+
fragShaderSrc = system::FileSystem::Read("../../Content/GLSL/Unlit/RGBVertex.frag"),
141+
vertShaderSrc2 = system::FileSystem::Read("../../Content/GLSL/Unlit/SolidColour.vert"),
142+
fragShaderSrc2 = system::FileSystem::Read("../../Content/GLSL/Unlit/SolidColour.frag");
139143
gfx::ShaderCompiledResult
140144
vertShader = gfx::ShaderCompilerGLSL::Singleton().CompileShader(vertShaderSrc.c_str(), vertShaderSrc.length(), gfx::VertexShader),
141-
fragShader = gfx::ShaderCompilerGLSL::Singleton().CompileShader(fragShaderSrc.c_str(), fragShaderSrc.length(), gfx::FragmentShader);
145+
fragShader = gfx::ShaderCompilerGLSL::Singleton().CompileShader(fragShaderSrc.c_str(), fragShaderSrc.length(), gfx::FragmentShader),
146+
vertShader2 = gfx::ShaderCompilerGLSL::Singleton().CompileShader(vertShaderSrc2.c_str(), vertShaderSrc2.length(), gfx::VertexShader),
147+
fragShader2 = gfx::ShaderCompilerGLSL::Singleton().CompileShader(fragShaderSrc2.c_str(), fragShaderSrc2.length(), gfx::FragmentShader);
142148

143149
// Register shader with the API.
144150
auto& api = engine.GetApi<gfx::GraphicsApiGL>();
145151
api.GetSceneGraph().SetCamera(&m_Camera);
146152

147-
lepus::gfx::Material baseMaterial;
148-
lepus::gfx::GLShader baseShader(lepus::gfx::ShaderInfo("RGBVertex"));
153+
lepus::gfx::Material baseMaterial, otherMaterial;
154+
lepus::gfx::GLShader baseShader(lepus::gfx::ShaderInfo("RGBVertex")), redShader(lepus::gfx::ShaderInfo("SolidColour"));
149155
baseShader.SetGLProgram(gfx::ShaderCompilerGLSL::Singleton().BuildProgram(vertShader, fragShader));
156+
redShader.SetGLProgram(gfx::ShaderCompilerGLSL::Singleton().BuildProgram(vertShader2, fragShader2));
150157
baseMaterial.SetShader(&baseShader);
158+
otherMaterial.SetShader(&redShader);
159+
160+
types::Vector3 baseColour(1.f, 1.f, 1.f);
161+
auto baseColourIndex = otherMaterial.Attributes().Add<const types::Vector3&>("colour", baseColour);
151162

152163
// Set up engine for drawing.
153164
engine.Setup();
@@ -156,8 +167,8 @@ class DemoApp : public system::BaseApp
156167
// Instantiate two Renderables in the scene graph, each with its own transform, using the same cube mesh data.
157168
auto cubeMesh = lepus::gfx::GLMesh(lepus::utility::Primitives::Cube());
158169
auto cube = lepus::gfx::Renderable<lepus::gfx::GLMesh>(&cubeMesh, lepus::math::Transform(), baseMaterial);
159-
auto cube2 = lepus::gfx::Renderable<lepus::gfx::GLMesh>(&cubeMesh, lepus::math::Transform(), baseMaterial);
160-
auto cube3 = lepus::gfx::Renderable<lepus::gfx::GLMesh>(&cubeMesh, lepus::math::Transform(), baseMaterial);
170+
auto cube2 = lepus::gfx::Renderable<lepus::gfx::GLMesh>(&cubeMesh, lepus::math::Transform(), otherMaterial);
171+
auto cube3 = lepus::gfx::Renderable<lepus::gfx::GLMesh>(&cubeMesh, lepus::math::Transform(), otherMaterial);
161172
auto cube4 = lepus::gfx::Renderable<lepus::gfx::GLMesh>(&cubeMesh, lepus::math::Transform(), baseMaterial);
162173

163174
cube.GetTransform()->Origin(lepus::types::Vector3(0.f, 0.f, -2.f));
@@ -176,14 +187,14 @@ class DemoApp : public system::BaseApp
176187

177188
auto cubeNode = api.GetSceneGraph().AddChild(&cube);
178189

190+
auto window = static_cast<GLFWwindow*>(windowing->GetWindowPtr());
191+
179192
// Initialise the FOV variable and set up a callback so we can let the user adjust it with the mouse scroll wheel.
180193
m_FOV = m_Camera.FOV();
181-
glfwSetScrollCallback(reinterpret_cast<GLFWwindow*>(windowing->GetWindowPtr()), DemoAppGLFWCallbacks::scroll);
194+
glfwSetScrollCallback(window, DemoAppGLFWCallbacks::scroll);
182195

183196
float runningTime = glfwGetTime();
184197

185-
GLFWwindow* window = reinterpret_cast<GLFWwindow*>(windowing->GetWindowPtr());
186-
187198
// Set up mouse input for camera freelook.
188199
glfwGetCursorPos(window, &m_MouseState.lastX, &m_MouseState.lastY);
189200
glfwSetCursorPosCallback(reinterpret_cast<GLFWwindow*>(windowing->GetWindowPtr()), DemoAppGLFWCallbacks::cursorPos);
@@ -220,6 +231,12 @@ class DemoApp : public system::BaseApp
220231
Tick(deltaTime, keys);
221232
UpdateUniforms(&api);
222233

234+
baseColour.x(sinf(runningTime));
235+
baseColour.y(cosf(runningTime));
236+
baseColour.z(baseColour.x() * baseColour.y());
237+
238+
otherMaterial.Attributes().Set<const types::Vector3&>(baseColourIndex, baseColour);
239+
223240
engine.Render<unsigned char, gfx::GraphicsEngine::PixelFormat::RGBA32>(100, 149, 237);
224241

225242
float newRunningTime = glfwGetTime();

src/lepus/gfx/GraphicsEngine/Apis/ApiGL.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ namespace lepus
137137

138138
void UpdateUniforms() override;
139139

140-
void UpdateUniforms(const GLRenderable* renderable, const MaterialAttributes& materialAttribs, GLuint program, const lepus::math::Matrix4x4& worldMatrix);
140+
void UpdateUniforms(const GLRenderable* renderable, MaterialAttributes& materialAttribs, GLuint program, const lepus::math::Matrix4x4& worldMatrix);
141141

142142
inline GLSceneGraph& GetSceneGraph()
143143
{

src/lepus/gfx/GraphicsEngine/Apis/ApiGL/ApiGL.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void GraphicsApiGL::UpdateUniforms()
5555
// TODO: this method should only update "global" uniforms that aren't specific to any renderable in particular.
5656
}
5757

58-
void GraphicsApiGL::UpdateUniforms(const GLRenderable* const renderable, const MaterialAttributes& materialAttribs, const GLuint program, const lepus::math::Matrix4x4& worldMatrix)
58+
void GraphicsApiGL::UpdateUniforms(const GLRenderable* const renderable, MaterialAttributes& materialAttribs, const GLuint program, const lepus::math::Matrix4x4& worldMatrix)
5959
{
6060
auto cam = m_Scene.Camera();
6161
auto proj = cam->BuildPerspectiveMatrix();
@@ -111,6 +111,9 @@ void GraphicsApiGL::UpdateUniforms(const GLRenderable* const renderable, const M
111111
case lepus::gfx::UniformType::FLOAT:
112112
glUniform1f(location, materialAttribs.Get<GLfloat>(i));
113113
break;
114+
case lepus::gfx::UniformType::VEC3:
115+
glUniform3fv(location, 1, materialAttribs.Get<GLfloat*>(i));
116+
break;
114117
case lepus::gfx::UniformType::INVALID:
115118
default:
116119
break;
@@ -130,7 +133,7 @@ void GraphicsApiGL::Draw()
130133
{
131134
if (!branchComplete && !currentNode->IsRoot())
132135
{
133-
auto renderable = (const GLRenderable*)(currentNode->GetTransformable());
136+
auto renderable = (GLRenderable*)(currentNode->GetTransformable());
134137
auto material = renderable->GetMaterial();
135138
if (renderable)
136139
{

src/lepus/gfx/GraphicsEngine/GraphicsApi/Uniforms.h

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace lepus
1010
INVALID = 0,
1111
MATRIX4,
1212
FLOAT,
13+
VEC3
1314
};
1415

1516
#pragma region Engine global uniform names

src/lepus/gfx/Material.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace lepus
3737
m_Shader = shader;
3838
}
3939

40-
[[nodiscard]] const lepus::gfx::MaterialAttributes& Attributes() const
40+
[[nodiscard]] lepus::gfx::MaterialAttributes& Attributes()
4141
{
4242
return m_Attributes;
4343
}

src/lepus/gfx/Material/MaterialAttributes.h

+37-6
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ namespace lepus
7171
m_AttribCount = 0;
7272
m_AttribNames = nullptr;
7373
m_AttribValues = nullptr;
74+
m_AttribTypes = nullptr;
7475
}
7576

7677
/// @brief Adds an attribute and returns its index.
@@ -80,9 +81,11 @@ namespace lepus
8081
this->_EnsureCapacity(1);
8182

8283
size_t numChars = strlen(name);
83-
m_AttribNames[m_AttribCount] = new char[numChars];
84+
m_AttribNames[m_AttribCount] = new char[numChars + 1];
85+
memset((void*)m_AttribNames[m_AttribCount], 0, sizeof(char) * (numChars + 1));
8486
memcpy((void*)m_AttribNames[m_AttribCount], name, numChars);
85-
m_AttribValues[m_AttribValues] = value;
87+
88+
Set<TValue>(m_AttribCount, value);
8689

8790
return m_AttribCount++;
8891
}
@@ -105,31 +108,53 @@ namespace lepus
105108
template <>
106109
inline void Set<const lepus::math::Matrix4x4&>(uint8_t index, const lepus::math::Matrix4x4& value)
107110
{
111+
// Clear existing data
112+
if (m_AttribTypes[index] == UniformType::MATRIX4)
113+
{
114+
delete[] static_cast<float*>(m_AttribValues[index]);
115+
}
116+
117+
m_AttribValues[index] = new float[4 * 4];
118+
108119
m_AttribTypes[index] = UniformType::MATRIX4;
109120
memcpy((void*)m_AttribValues[index], value.data(), sizeof(float) * 4 * 4);
110121
}
111122

123+
template <>
124+
inline void Set<const lepus::types::Vector3&>(uint8_t index, const lepus::types::Vector3& value)
125+
{
126+
if (m_AttribTypes[index] == UniformType::VEC3)
127+
{
128+
delete[] static_cast<float*>(m_AttribValues[index]);
129+
}
130+
131+
m_AttribValues[index] = new float[3];
132+
133+
m_AttribTypes[index] = UniformType::VEC3;
134+
memcpy((void*)m_AttribValues[index], value.GetData(), sizeof(float) * 3);
135+
}
136+
112137
template <typename TValue = void*>
113138
inline TValue Get(uint8_t index) const
114139
{
115140
assert(index < m_AttribCount);
116141

117-
return m_AttribValues[index];
142+
return (TValue)(m_AttribValues[index]);
118143
}
119144

120-
inline const char* GetName(uint8_t index) const
145+
[[nodiscard]] inline const char* GetName(uint8_t index) const
121146
{
122147
assert(index < m_AttribCount);
123148

124149
return m_AttribNames[index];
125150
}
126151

127-
inline const uint8_t Count() const
152+
[[nodiscard]] inline const uint8_t Count() const
128153
{
129154
return m_AttribCount;
130155
}
131156

132-
inline const UniformType GetType(uint8_t index) const
157+
[[nodiscard]] inline const UniformType GetType(uint8_t index) const
133158
{
134159
return m_AttribTypes[index];
135160
}
@@ -164,6 +189,12 @@ namespace lepus
164189

165190
return mat;
166191
}
192+
193+
template <>
194+
inline void MaterialAttributes::Set<const float* const>(uint8_t index, const float* const value)
195+
{
196+
memcpy(m_AttribValues[index], value, sizeof(value));
197+
}
167198
} // namespace gfx
168199
} // namespace lepus
169200

src/lepus/gfx/SceneGraph/Renderable.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace lepus
1717
{
1818
private:
1919
const MeshType* m_Mesh;
20-
const Material* m_Material;
20+
Material* m_Material;
2121
bool m_OwnsMaterial;
2222

2323
public:
@@ -36,7 +36,7 @@ namespace lepus
3636
}
3737
}
3838

39-
Renderable(const MeshType* mesh, lepus::math::Transform& transform, const Material& material)
39+
Renderable(const MeshType* mesh, lepus::math::Transform& transform, Material& material)
4040
: Renderable(mesh, transform, false)
4141
{
4242
m_Material = &material;
@@ -56,7 +56,7 @@ namespace lepus
5656
}
5757
}
5858

59-
Renderable(const MeshType* mesh, lepus::math::Transform&& transform, const Material& material)
59+
Renderable(const MeshType* mesh, lepus::math::Transform&& transform, Material& material)
6060
: Renderable(mesh, transform, false)
6161
{
6262
m_Material = &material;
@@ -85,7 +85,7 @@ namespace lepus
8585
return m_Mesh;
8686
}
8787

88-
[[nodiscard]] const Material* GetMaterial() const
88+
[[nodiscard]] Material* const GetMaterial()
8989
{
9090
return m_Material;
9191
}

0 commit comments

Comments
 (0)