Skip to content

Commit b6d6748

Browse files
committed
refactor(gfx): moved uniforms from GraphicsApi to Material class
1 parent 23570f6 commit b6d6748

File tree

17 files changed

+887
-443
lines changed

17 files changed

+887
-443
lines changed

src/examples/demo/DemoApp.h

+17-16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include "lepus/gfx/GraphicsEngine/Apis/ApiGL/Types/GLShader.h"
2+
13
#include <imgui/imgui.h>
24
#include <imgui/backends/imgui_impl_glfw.h>
35
#include <imgui/backends/imgui_impl_opengl3.h>
@@ -140,18 +142,23 @@ class DemoApp : public system::BaseApp
140142

141143
// Register shader with the API.
142144
auto& api = engine.GetApi<gfx::GraphicsApiGL>();
143-
api.GetOptions<gfx::GraphicsApiGLOptions>().RegisterShader(&vertShader, &fragShader);
145+
api.GetSceneGraph().SetCamera(&m_Camera);
146+
147+
lepus::gfx::Material baseMaterial;
148+
lepus::gfx::GLShader baseShader(lepus::gfx::ShaderInfo("RGBVertex"));
149+
baseShader.SetGLProgram(gfx::ShaderCompilerGLSL::Singleton().BuildProgram(vertShader, fragShader));
150+
baseMaterial.SetShader(&baseShader);
144151

145152
// Set up engine for drawing.
146153
engine.Setup();
147154
m_Camera.Transform().Origin(m_Camera.Transform().Forward() * -2.f);
148155

149156
// Instantiate two Renderables in the scene graph, each with its own transform, using the same cube mesh data.
150157
auto cubeMesh = lepus::gfx::GLMesh(lepus::utility::Primitives::Cube());
151-
auto cube = lepus::gfx::Renderable<lepus::gfx::GLMesh>(&cubeMesh, lepus::math::Transform());
152-
auto cube2 = lepus::gfx::Renderable<lepus::gfx::GLMesh>(&cubeMesh, lepus::math::Transform());
153-
auto cube3 = lepus::gfx::Renderable<lepus::gfx::GLMesh>(&cubeMesh, lepus::math::Transform());
154-
auto cube4 = lepus::gfx::Renderable<lepus::gfx::GLMesh>(&cubeMesh, lepus::math::Transform());
158+
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);
161+
auto cube4 = lepus::gfx::Renderable<lepus::gfx::GLMesh>(&cubeMesh, lepus::math::Transform(), baseMaterial);
155162

156163
cube.GetTransform()->Origin(lepus::types::Vector3(0.f, 0.f, -2.f));
157164
cube2.GetTransform()->Origin(lepus::types::Vector3(2.f, 0.f, 0.f));
@@ -169,12 +176,6 @@ class DemoApp : public system::BaseApp
169176

170177
auto cubeNode = api.GetSceneGraph().AddChild(&cube);
171178

172-
// Update projection and view matrices with data from the camera object.
173-
m_UniformState.projMatrix = m_Camera.BuildPerspectiveMatrix();
174-
((lepus::gfx::GLMatrixUniformBinding*)api.GetUniform<lepus::gfx::GLMatrixUniformBinding>(LEPUS_GFX_UNIFORMS_GLOBAL_PROJECTION_MATRIX))->Value((float*)m_UniformState.projMatrix.data());
175-
m_UniformState.viewMatrix = m_Camera.BuildViewMatrix();
176-
((lepus::gfx::GLMatrixUniformBinding*)api.GetUniform<lepus::gfx::GLMatrixUniformBinding>(LEPUS_GFX_UNIFORMS_GLOBAL_VIEW_MATRIX))->Value((float*)m_UniformState.viewMatrix.data());
177-
178179
// Initialise the FOV variable and set up a callback so we can let the user adjust it with the mouse scroll wheel.
179180
m_FOV = m_Camera.FOV();
180181
glfwSetScrollCallback(reinterpret_cast<GLFWwindow*>(windowing->GetWindowPtr()), DemoAppGLFWCallbacks::scroll);
@@ -235,7 +236,7 @@ class DemoApp : public system::BaseApp
235236

236237
inline void UpdateInput(KeyboardState& keys, std::shared_ptr<system::WindowingGLFW> windowing)
237238
{
238-
GLFWwindow* window = (GLFWwindow*)windowing->GetWindowPtr();
239+
auto window = static_cast<GLFWwindow*>(windowing->GetWindowPtr());
239240

240241
keys.w = glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS;
241242
keys.a = glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS;
@@ -246,11 +247,11 @@ class DemoApp : public system::BaseApp
246247

247248
inline void UpdateUniforms(gfx::GraphicsApi* api)
248249
{
249-
m_UniformState.projMatrix = m_Camera.BuildPerspectiveMatrix();
250-
m_UniformState.viewMatrix = m_Camera.BuildViewMatrix();
250+
// m_UniformState.projMatrix = m_Camera.BuildPerspectiveMatrix();
251+
// m_UniformState.viewMatrix = m_Camera.BuildViewMatrix();
251252

252-
api->GetUniform<lepus::gfx::GLMatrixUniformBinding>(LEPUS_GFX_UNIFORMS_GLOBAL_PROJECTION_MATRIX)->Value(m_UniformState.projMatrix.data());
253-
api->GetUniform<lepus::gfx::GLMatrixUniformBinding>(LEPUS_GFX_UNIFORMS_GLOBAL_VIEW_MATRIX)->Value(m_UniformState.viewMatrix.data());
253+
// api->GetUniform<lepus::gfx::GLMatrixUniformBinding>(LEPUS_GFX_UNIFORMS_GLOBAL_PROJECTION_MATRIX)->Value(m_UniformState.projMatrix.data());
254+
// api->GetUniform<lepus::gfx::GLMatrixUniformBinding>(LEPUS_GFX_UNIFORMS_GLOBAL_VIEW_MATRIX)->Value(m_UniformState.viewMatrix.data());
254255
}
255256

256257
inline void Tick(float deltaTime, const KeyboardState& keys)

src/lepus/gfx/Camera.h

+86-80
Original file line numberDiff line numberDiff line change
@@ -11,85 +11,91 @@ namespace lepus
1111
{
1212
namespace gfx
1313
{
14-
class Camera
15-
{
16-
protected:
17-
lepus::math::Transform m_Transform;
18-
float m_FOV;
19-
float m_Near, m_Far;
20-
21-
public:
22-
Camera()
23-
{
24-
m_FOV = LEPUS_GFX_CAMERA_DEFAULT_FOV;
25-
m_Near = LEPUS_GFX_CAMERA_DEFAULT_CLIP_NEAR;
26-
m_Far = LEPUS_GFX_CAMERA_DEFAULT_CLIP_FAR;
27-
m_Transform = lepus::math::Transform();
28-
}
29-
30-
/// @brief Gets field of view
31-
/// @return Camera's field of view (in degrees)
32-
inline float FOV() const
33-
{
34-
return m_FOV;
35-
}
36-
37-
/// @brief Sets field of view
38-
/// @param fov New field of view (in degrees) to be used by this camera
39-
inline void FOV(float fov)
40-
{
41-
m_FOV = fov;
42-
}
43-
44-
/// @brief Gets the Camera's Transform.
45-
/// @return A reference to the Transform used by the Camera.
46-
inline lepus::math::Transform& Transform() { return m_Transform; }
47-
48-
/// @brief Generates a perspective projection matrix using the current FOV angle and near/far clipping planes.
49-
/// @return A 4x4 projection matrix that can be used to apply camera perspective to vertices.
50-
lepus::math::Matrix4x4 BuildPerspectiveMatrix() const
51-
{
52-
lepus::math::Matrix4x4 projMatrix = lepus::math::Matrix4x4();
53-
54-
float hypot = tanf((180.f - m_FOV) * DEG2RAD * 0.5f);
55-
56-
projMatrix.set<0, 0>(hypot);
57-
projMatrix.set<1, 1>(hypot);
58-
59-
projMatrix.set<2, 2>(-(m_Far / (m_Far - m_Near)));
60-
projMatrix.set<2, 3>(-((m_Far * m_Near) / (m_Far - m_Near)));
61-
62-
projMatrix.set<3, 2>(-1.f);
63-
64-
return projMatrix;
65-
}
66-
67-
/// @brief Generates a look-at view matrix using the Camera's Transform.
68-
/// @return A 4x4 view matrix that can be used to rotate and translate vertices according to eye position and camera orientation.
69-
lepus::math::Matrix4x4 BuildViewMatrix() const
70-
{
71-
lepus::math::Matrix4x4 pos = lepus::math::Matrix4x4::Identity();
72-
73-
auto f = m_Transform.Forward() * (-1.f / m_Transform.Forward().Magnitude());
74-
auto s = m_Transform.Right() * (1.f / m_Transform.Right().Magnitude());
75-
auto u = m_Transform.Up() * (1.f / m_Transform.Up().Magnitude());
76-
77-
auto e = m_Transform.Origin();
78-
79-
pos.set<0, 3>(-e.x());
80-
pos.set<1, 3>(-e.y());
81-
pos.set<2, 3>(-e.z());
82-
83-
lepus::math::Matrix4x4 lookAt = lepus::math::Matrix4x4::Identity();
84-
85-
lookAt.set<0, 0>(s.x());lookAt.set<0, 1>(s.y());lookAt.set<0, 2>(s.z());
86-
lookAt.set<1, 0>(u.x());lookAt.set<1, 1>(u.y());lookAt.set<1, 2>(u.z());
87-
lookAt.set<2, 0>(f.x());lookAt.set<2, 1>(f.y());lookAt.set<2, 2>(f.z());
88-
89-
return lookAt.Multiply(pos);
90-
}
91-
};
92-
}
93-
}
14+
class Camera
15+
{
16+
protected:
17+
lepus::math::Transform m_Transform;
18+
float m_FOV;
19+
float m_Near, m_Far;
20+
21+
public:
22+
Camera()
23+
{
24+
m_FOV = LEPUS_GFX_CAMERA_DEFAULT_FOV;
25+
m_Near = LEPUS_GFX_CAMERA_DEFAULT_CLIP_NEAR;
26+
m_Far = LEPUS_GFX_CAMERA_DEFAULT_CLIP_FAR;
27+
m_Transform = lepus::math::Transform();
28+
}
29+
30+
/// @brief Gets field of view
31+
/// @return Camera's field of view (in degrees)
32+
inline float FOV() const
33+
{
34+
return m_FOV;
35+
}
36+
37+
/// @brief Sets field of view
38+
/// @param fov New field of view (in degrees) to be used by this camera
39+
inline void FOV(float fov)
40+
{
41+
m_FOV = fov;
42+
}
43+
44+
/// @brief Gets the Camera's Transform.
45+
/// @return A reference to the Transform used by the Camera.
46+
inline lepus::math::Transform& Transform() { return m_Transform; }
47+
48+
/// @brief Generates a perspective projection matrix using the current FOV angle and near/far clipping planes.
49+
/// @return A 4x4 projection matrix that can be used to apply camera perspective to vertices.
50+
lepus::math::Matrix4x4 BuildPerspectiveMatrix() const
51+
{
52+
lepus::math::Matrix4x4 projMatrix = lepus::math::Matrix4x4();
53+
54+
float hypot = tanf((180.f - m_FOV) * (float)(DEG2RAD) * 0.5f);
55+
56+
projMatrix.set<0, 0>(hypot);
57+
projMatrix.set<1, 1>(hypot);
58+
59+
projMatrix.set<2, 2>(-(m_Far / (m_Far - m_Near)));
60+
projMatrix.set<2, 3>(-((m_Far * m_Near) / (m_Far - m_Near)));
61+
62+
projMatrix.set<3, 2>(-1.f);
63+
64+
return projMatrix;
65+
}
66+
67+
/// @brief Generates a look-at view matrix using the Camera's Transform.
68+
/// @return A 4x4 view matrix that can be used to rotate and translate vertices according to eye position and camera orientation.
69+
lepus::math::Matrix4x4 BuildViewMatrix() const
70+
{
71+
lepus::math::Matrix4x4 pos = lepus::math::Matrix4x4::Identity();
72+
73+
auto f = m_Transform.Forward() * (-1.f / m_Transform.Forward().Magnitude());
74+
auto s = m_Transform.Right() * (1.f / m_Transform.Right().Magnitude());
75+
auto u = m_Transform.Up() * (1.f / m_Transform.Up().Magnitude());
76+
77+
auto e = m_Transform.Origin();
78+
79+
pos.set<0, 3>(-e.x());
80+
pos.set<1, 3>(-e.y());
81+
pos.set<2, 3>(-e.z());
82+
83+
lepus::math::Matrix4x4 lookAt = lepus::math::Matrix4x4::Identity();
84+
85+
lookAt.set<0, 0>(s.x());
86+
lookAt.set<0, 1>(s.y());
87+
lookAt.set<0, 2>(s.z());
88+
lookAt.set<1, 0>(u.x());
89+
lookAt.set<1, 1>(u.y());
90+
lookAt.set<1, 2>(u.z());
91+
lookAt.set<2, 0>(f.x());
92+
lookAt.set<2, 1>(f.y());
93+
lookAt.set<2, 2>(f.z());
94+
95+
return lookAt.Multiply(pos);
96+
}
97+
};
98+
} // namespace gfx
99+
} // namespace lepus
94100

95101
#endif

0 commit comments

Comments
 (0)