Skip to content

Commit

Permalink
[mesh_renderer] dbgui improvements, and new add_object method which i…
Browse files Browse the repository at this point in the history
…s much better, and deals in TRS
  • Loading branch information
harrand committed Sep 4, 2023
1 parent 936c6de commit ca3fdc1
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 20 deletions.
6 changes: 5 additions & 1 deletion demo/gl/tz_mesh_demo/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ int main()
std::byte{0xff},
std::byte{0xff}
}});
mr.add_object(mesh, {.global_transform = tz::translate({0.0f, 0.0f, -5.0f}), .bound_textures = {{{.texture = tex}}}});
mr.add_object
({
.mesh = mesh,
.bound_textures = {{{.texture = tex}}}
});
mr.append_to_render_graph();

tz::duration update_timer = tz::system_time();
Expand Down
9 changes: 9 additions & 0 deletions src/tz/core/data/trs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,13 @@ namespace tz
trs cpy = *this;
return cpy.combine(t);
}

void trs::dbgui()
{
ImGui::InputFloat("Position Limits", &this->dbgui_slider_scale[0], 0.1f, 1.0f);
ImGui::SliderFloat3("Position", this->translate.data().data(), -this->dbgui_slider_scale[0], this->dbgui_slider_scale[0]);
ImGui::SliderFloat4("Quaternion", this->rotate.data().data(), 0.01f, 0.2f);
ImGui::InputFloat("Scale Limits", &this->dbgui_slider_scale[1], 0.1f, 1.0f);
ImGui::SliderFloat3("Scale", this->scale.data().data(), 0.0f, this->dbgui_slider_scale[1]);
}
}
4 changes: 4 additions & 0 deletions src/tz/core/data/trs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ namespace tz
tz::vec3 translate = tz::vec3::zero();
tz::quat rotate = tz::quat::zero();
tz::vec3 scale = tz::vec3::filled(1.0f);
#if TZ_DEBUG
tz::vec2 dbgui_slider_scale = tz::vec2::filled(1.0f);
#endif // TZ_DEBUG

trs lerp(const trs& rhs, float factor) const;
tz::mat4 matrix() const;
trs& combine(const trs& rhs);
trs combined(const trs& rhs) const;
void dbgui();
};
}

Expand Down
39 changes: 27 additions & 12 deletions src/tz/ren/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ namespace tz::ren
// represents the data of the camera.
struct camera_data
{
tz::mat4 view = tz::mat4::identity();
tz::mat4 view = tz::view({0.0f, 0.0f, 5.0f}, tz::vec3::zero());
tz::mat4 projection = tz::perspective(3.14159f * 0.5f, 1920.0f/1080.0f, 0.1f, 1000.0f);
};

Expand All @@ -101,15 +101,21 @@ namespace tz::ren
return static_cast<tz::hanval>(hanval);
}

mesh_renderer::object_handle mesh_renderer::add_object(mesh_handle m, object_data data)
mesh_renderer::object_handle mesh_renderer::add_object(object_init_data init)
{
std::size_t hanval = this->draw_count();
auto mesh_id = static_cast<std::size_t>(static_cast<tz::hanval>(m));
auto mesh_id = static_cast<std::size_t>(static_cast<tz::hanval>(init.mesh));
// draw list at this position is now equal to the associated mesh_locator.
tz::assert(hanval < this->compute_pass.get_draw_list_meshes().size(), "ran out of objects! can only have %zu", this->compute_pass.get_draw_list_meshes().size());
this->compute_pass.get_draw_list_meshes()[hanval] = this->render_pass.meshes[mesh_id];

// now need to fill the object data
this->render_pass.get_object_datas()[hanval] = data;
this->render_pass.get_object_datas()[hanval] =
{
.global_transform = init.trs.matrix(),
.bound_textures = init.bound_textures
};

// finally, increment the draw count.
this->compute_pass.set_draw_count(this->compute_pass.get_draw_count() + 1);

Expand Down Expand Up @@ -260,6 +266,10 @@ namespace tz::ren
if(draw_list_limit > 0)
{
constexpr float slider_height = 160.0f;
if(ImGui::Button("+"))
{
draw_id = std::clamp(draw_id + 1, 0, draw_list_limit - 1);
}
ImGui::VSliderInt("##drawelem", ImVec2{18.0f, slider_height}, &draw_id, 0, draw_list_limit - 1);
std::string drawname = "Draw " + std::to_string(draw_id);
bool is_active_draw = std::cmp_less(draw_id, this->get_draw_count());
Expand Down Expand Up @@ -287,6 +297,10 @@ namespace tz::ren
ImGui::Text("To see bound textures and transform, see Object %d within the render pass section", draw_id);
}
ImGui::EndChild();
if(ImGui::Button("-"))
{
draw_id = std::clamp(draw_id - 1, 0, draw_list_limit - 1);
}
}
else
{
Expand Down Expand Up @@ -406,6 +420,10 @@ namespace tz::ren
if(object_count > 0)
{
constexpr float slider_height = 160.0f;
if(ImGui::Button("+"))
{
object_id = std::clamp(object_id + 1, 0, static_cast<int>(object_count) - 1);
}
ImGui::VSliderInt("##object", ImVec2{18.0f, slider_height}, &object_id, 0, object_count - 1);
// TODO: object information display
std::string objname = "Object " + std::to_string(object_id);
Expand All @@ -416,13 +434,6 @@ namespace tz::ren
if(ImGui::BeginChild(objname.c_str(), ImVec2(0, slider_height), false, ImGuiWindowFlags_ChildWindow))
{
ImGui::TextColored(ImVec4{1.0f, 0.3f, 0.3f, 1.0f}, objname.c_str());
std::string parent_str = std::to_string(obj.parent);
if(obj.parent == static_cast<std::uint32_t>(-1))
{
parent_str = "none";
}
ImGui::Text("Parent: %s", parent_str.c_str());
ImGui::Separator();
ImGui::Text("Global Transform Matrix");
tz::dbgui_model(obj.global_transform);

Expand All @@ -445,6 +456,10 @@ namespace tz::ren
ImGui::Separator();
}
ImGui::EndChild();
if(ImGui::Button("-"))
{
object_id = std::clamp(object_id - 1, 0, static_cast<int>(object_count) - 1);
}
}

ImGui::Separator();
Expand Down Expand Up @@ -680,7 +695,7 @@ namespace tz::ren
{
if(ImGui::Button("Add first-mesh drawable"))
{
this->add_object(static_cast<tz::hanval>(0));
this->add_object({.mesh = static_cast<tz::hanval>(0)});
}
imgui_helper_tooltip("Press this to add a drawable with all the defaults, using the first mesh.");
}
Expand Down
14 changes: 7 additions & 7 deletions src/tz/ren/mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "tz/gl/renderer.hpp"
#include "tz/core/data/vector.hpp"
#include "tz/core/matrix.hpp"
#include "tz/core/data/transform_hierarchy.hpp"
#include <cstdint>
#include <array>

Expand Down Expand Up @@ -71,8 +72,6 @@ namespace tz::ren
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);
float pad0[3] = {};
};

/**
Expand All @@ -91,19 +90,19 @@ namespace tz::ren
using object_handle = tz::handle<object_tag_t>;
using texture_handle = detail::texture_handle;

struct stored_assets
struct object_init_data
{
std::vector<mesh_handle> meshes = {};
std::vector<texture_handle> textures = {};
std::vector<object_handle> objects = {};
tz::trs trs = {};
mesh_handle mesh = tz::nullhand;
std::array<texture_locator, mesh_renderer_max_tex_count> bound_textures = {};
};

std::size_t mesh_count() const;
std::size_t draw_count() const;
void clear();
void clear_draws();
mesh_handle add_mesh(mesh_t m);
object_handle add_object(mesh_handle m, object_data data = {});
object_handle add_object(object_init_data init);
texture_handle add_texture(tz::vec2ui dimensions, std::span<const std::byte> image_data);
void append_to_render_graph();
void dbgui();
Expand Down Expand Up @@ -147,6 +146,7 @@ namespace tz::ren
mesh_locator add_mesh_impl(const mesh_renderer::mesh_t& m);
void dbgui_impl();

tz::transform_hierarchy<std::uint32_t> object_hierarchy = {};
compute_pass_t compute_pass = {};
render_pass_t render_pass;
};
Expand Down

0 comments on commit ca3fdc1

Please sign in to comment.