Skip to content

Commit

Permalink
[mesh_renderer] object_data massively reduced in size. model renamed …
Browse files Browse the repository at this point in the history
…to global_transform
  • Loading branch information
harrand committed Sep 4, 2023
1 parent 4b0ebbc commit 936c6de
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 36 deletions.
2 changes: 1 addition & 1 deletion demo/gl/tz_mesh_demo/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int main()
std::byte{0xff},
std::byte{0xff}
}});
mr.add_object(mesh, {.model = tz::translate({0.0f, 0.0f, -5.0f}), .bound_textures = {{{.texture = tex}}}});
mr.add_object(mesh, {.global_transform = tz::translate({0.0f, 0.0f, -5.0f}), .bound_textures = {{{.texture = tex}}}});
mr.append_to_render_graph();

tz::duration update_timer = tz::system_time();
Expand Down
25 changes: 6 additions & 19 deletions src/tz/ren/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ namespace tz::ren
resources:
- index buffer
- vertex buffer
- object buffer (contains the model matrix and list of texture ids used by each drawable)
- object buffer (contains the global_transform matrix and list of texture ids used by each drawable)
- draw indirect buffer (owned by the compute pass)
- camera buffer, containing camera data.
- an array of textures.
when: happens once every frame, after the compute pass completes.
1. retrieve the current vertex from the vertex buffer. remember
that in::vertex_id is actually the index_id in TZSL.
2. retrieve the texture and model matrix info from the object
2. retrieve the texture and global_transform matrix info from the object
buffer using the draw_id.
3. combine the model matrix with the projection and view matrices
3. combine the global_transform matrix with the projection and view matrices
from the camera buffer.
4. pass the relevant texture ids to the fragment shader.
--- vertex shading occurs... ---
Expand Down Expand Up @@ -400,7 +400,7 @@ namespace tz::ren

ImGui::Separator();
ImGui::TextColored(ImVec4{1.0f, 0.3f, 0.3f, 1.0f}, "OBJECT DATA");
imgui_helper_tooltip("An object represents a drawable element. It consists of a model matrix and a set of bound textures. Drawables are obviously associated with a mesh. Specifically, `object X` will use the mesh locator at element `X` of the draw-list. The draw-list is visible within the compute pass section, not here.");
imgui_helper_tooltip("An object represents a drawable element. It consists of a global_transform matrix and a set of bound textures. Drawables are obviously associated with a mesh. Specifically, `object X` will use the mesh locator at element `X` of the draw-list. The draw-list is visible within the compute pass section, not here.");
std::size_t object_count = ren.get_resource(this->object_buffer)->data_as<const object_data>().size();
static int object_id = 0;
if(object_count > 0)
Expand All @@ -423,19 +423,8 @@ namespace tz::ren
}
ImGui::Text("Parent: %s", parent_str.c_str());
ImGui::Separator();
ImGui::Text("Model Matrix");
for (int row = 0; row < 4; row++)
{
for (int col = 0; col < 4; col++)
{
std::string label = "##m" + std::to_string(row) + std::to_string(col);
constexpr float matrix_cell_width = 35.0f;
ImGui::SetNextItemWidth(matrix_cell_width);
ImGui::InputFloat(label.c_str(), &obj.model(row, col));
ImGui::SameLine();
}
ImGui::NewLine();
}
ImGui::Text("Global Transform Matrix");
tz::dbgui_model(obj.global_transform);

ImGui::Separator();
ImGui::Text("Textures");
Expand All @@ -454,8 +443,6 @@ namespace tz::ren
}

ImGui::Separator();
ImGui::Text("Model");
imgui_helper_tooltip("The list of object data does not contain info on which mesh it corresponds to. To do that, view the corresponding element of the draw list within the compute pass.");
}
ImGui::EndChild();
}
Expand Down
2 changes: 1 addition & 1 deletion src/tz/ren/mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace tz::ren
struct object_data
{
// represents the transform of the drawable, in world space.
tz::mat4 model = tz::mat4::identity();
tz::mat4 global_transform = tz::mat4::identity();
// array of bound textures. they all do not have to be used. no indication on whether they are colour, normal map, etc...
std::array<texture_locator, mesh_renderer_max_tex_count> bound_textures = {};
std::uint32_t parent = static_cast<std::uint32_t>(-1);
Expand Down
17 changes: 2 additions & 15 deletions src/tz/ren/shaders/mesh.vertex.tzsl
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ struct texture_locator

struct object_t
{
mat4 model;
mat4 global_transform;
texture_locator[MAX_TEX_COUNT] textures;
uint parent;
};

resource(id = 0) const buffer vertex_buffer
Expand All @@ -43,23 +42,11 @@ output(id = 0) vec2 out::texcoord[MAX_TEX_COUNT];
output(id = 9) vec3 out::normal;
output(id = 10) texture_locator out::textures[MAX_TEX_COUNT];

mat4 get_object_transform(object_t obj)
{
object_t o = obj;
mat4 ret = o.model;
while(o.parent != ~0u)
{
o = objects.data[o.parent];
ret *= o.model;
}
return ret;
}

void main()
{
vertex_t vtx = vertices.data[in::vertex_id];
object_t obj = objects.data[in::draw_id + in::base_instance];
out::position = camera.projection * camera.view * obj.model * vec4(vtx.position, 1.0f);
out::position = camera.projection * camera.view * obj.global_transform * vec4(vtx.position, 1.0f);

out::normal = vtx.normal;
for(uint i = 0; i < MAX_TEX_COUNT; i++)
Expand Down

0 comments on commit 936c6de

Please sign in to comment.