From 49a74cd83724df712127cced1e8a57deed994758 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Sat, 30 Nov 2019 07:26:20 +0700 Subject: [PATCH 1/4] json_utils: parse trs2 and trs3 --- headers/enduro2d/utils/json_utils.hpp | 52 ++++++++++++++++++++ sources/enduro2d/utils/json_utils.cpp | 18 +++++++ untests/sources/untests_utils/json_utils.cpp | 21 ++++++++ 3 files changed, 91 insertions(+) diff --git a/headers/enduro2d/utils/json_utils.hpp b/headers/enduro2d/utils/json_utils.hpp index e694124e..842c98e5 100644 --- a/headers/enduro2d/utils/json_utils.hpp +++ b/headers/enduro2d/utils/json_utils.hpp @@ -134,6 +134,58 @@ namespace e2d::json_utils try_parse_value(const rapidjson::Value& root, unit& v) noexcept { return try_parse_value(root, v.value); } + + template < typename T > + bool try_parse_value(const rapidjson::Value& root, trs2& t) noexcept { + trs2 tt = trs2::identity(); + + if ( root.HasMember("translation") ) { + if ( !try_parse_value(root["translation"], tt.translation) ) { + return false; + } + } + + if ( root.HasMember("rotation") ) { + if ( !try_parse_value(root["rotation"], tt.rotation) ) { + return false; + } + } + + if ( root.HasMember("scale") ) { + if ( !try_parse_value(root["scale"], tt.scale) ) { + return false; + } + } + + t = tt; + return true; + } + + template < typename T > + bool try_parse_value(const rapidjson::Value& root, trs3& t) noexcept { + trs3 tt = trs3::identity(); + + if ( root.HasMember("translation") ) { + if ( !try_parse_value(root["translation"], tt.translation) ) { + return false; + } + } + + if ( root.HasMember("rotation") ) { + if ( !try_parse_value(root["rotation"], tt.rotation) ) { + return false; + } + } + + if ( root.HasMember("scale") ) { + if ( !try_parse_value(root["scale"], tt.scale) ) { + return false; + } + } + + t = tt; + return true; + } } namespace e2d::json_utils diff --git a/sources/enduro2d/utils/json_utils.cpp b/sources/enduro2d/utils/json_utils.cpp index 2fe767b6..d3803881 100644 --- a/sources/enduro2d/utils/json_utils.cpp +++ b/sources/enduro2d/utils/json_utils.cpp @@ -192,6 +192,24 @@ namespace } }] }, + "t2" : { + "type" : "object", + "additionalProperties" : false, + "properties" : { + "translation" : { "$ref": "#/common_definitions/v2" }, + "rotation" : { "type" : "number" }, + "scale" : { "$ref": "#/common_definitions/v2" } + } + }, + "t3" : { + "type" : "object", + "additionalProperties" : false, + "properties" : { + "translation" : { "$ref": "#/common_definitions/v3" }, + "rotation" : { "$ref": "#/common_definitions/q4" }, + "scale" : { "$ref": "#/common_definitions/v3" } + } + }, "color" : { "anyOf" : [{ "type" : "number" diff --git a/untests/sources/untests_utils/json_utils.cpp b/untests/sources/untests_utils/json_utils.cpp index fa5790ce..563e70b5 100644 --- a/untests/sources/untests_utils/json_utils.cpp +++ b/untests/sources/untests_utils/json_utils.cpp @@ -62,6 +62,18 @@ namespace "b3_4" : [1,2,3,4,5,6], "b3_5" : [1,2,3,4,5,6,7], + "t2" : { + "translation" : [1,2], + "rotation" : 3, + "scale" : [4,5] + }, + + "t3" : { + "translation" : [1,2,3], + "rotation" : [4,5,6,7], + "scale" : [8,9,10] + }, + "c0" : 0.5, "c1" : { "r" : 0.1, "b" : 0.2 }, "c2" : { "r" : 0.1, "g" : 0.2, "b" : 0.3, "a" : 0.4 }, @@ -189,6 +201,15 @@ TEST_CASE("json_utils") { REQUIRE_FALSE(json_utils::try_parse_value(doc["s2"], s3)); REQUIRE_FALSE(json_utils::try_parse_value(doc["s2"], s4)); } + { + t2f t2; + REQUIRE(json_utils::try_parse_value(doc["t2"], t2)); + REQUIRE(t2 == make_trs2(v2f(1,2), radf(3), v2f(4,5))); + + t3f t3; + REQUIRE(json_utils::try_parse_value(doc["t3"], t3)); + REQUIRE(t3 == make_trs3(v3f(1,2,3), q4f(4,5,6,7), v3f(8,9,10))); + } { color c0, c1, c2; REQUIRE(json_utils::try_parse_value(doc["c0"], c0)); From 59c7d8763b2608e0b40d849c5f459c1d774d03a7 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Sat, 30 Nov 2019 07:35:50 +0700 Subject: [PATCH 2/4] add 3d transform for renderer component --- headers/enduro2d/high/components/renderer.hpp | 49 +++++++++++++++++++ .../scripts/emmy/components/renderer.lua | 14 +++++- .../high_binds/components/renderer_binds.cpp | 18 ++++++- sources/enduro2d/high/components/renderer.cpp | 13 +++++ sources/enduro2d/high/components/scene.cpp | 4 +- .../render_system_drawer.cpp | 30 ++++++------ .../render_system_drawer.hpp | 6 +-- 7 files changed, 113 insertions(+), 21 deletions(-) diff --git a/headers/enduro2d/high/components/renderer.hpp b/headers/enduro2d/high/components/renderer.hpp index 2349fe12..01742ce9 100644 --- a/headers/enduro2d/high/components/renderer.hpp +++ b/headers/enduro2d/high/components/renderer.hpp @@ -17,6 +17,18 @@ namespace e2d public: renderer() = default; + renderer& transform(const t3f& transform) noexcept; + [[nodiscard]] const t3f& transform() const noexcept; + + renderer& translation(const v3f& translation) noexcept; + [[nodiscard]] const v3f& translation() const noexcept; + + renderer& rotation(const q4f& rotation) noexcept; + [[nodiscard]] const q4f& rotation() const noexcept; + + renderer& scale(const v3f& scale) noexcept; + [[nodiscard]] const v3f& scale() const noexcept; + renderer& properties(render::property_block&& value) noexcept; renderer& properties(const render::property_block& value); @@ -29,6 +41,7 @@ namespace e2d [[nodiscard]] vector& materials() noexcept; [[nodiscard]] const vector& materials() const noexcept; private: + t3f transform_ = t3f::identity(); render::property_block properties_; vector materials_; }; @@ -50,6 +63,42 @@ namespace e2d namespace e2d { + inline renderer& renderer::transform(const t3f& transform) noexcept { + transform_ = transform; + return *this; + } + + inline const t3f& renderer::transform() const noexcept { + return transform_; + } + + inline renderer& renderer::translation(const v3f& translation) noexcept { + transform_.translation = translation; + return *this; + } + + inline const v3f& renderer::translation() const noexcept { + return transform_.translation; + } + + inline renderer& renderer::rotation(const q4f& rotation) noexcept { + transform_.rotation = rotation; + return *this; + } + + inline const q4f& renderer::rotation() const noexcept { + return transform_.rotation; + } + + inline renderer& renderer::scale(const v3f& scale) noexcept { + transform_.scale = scale; + return *this; + } + + inline const v3f& renderer::scale() const noexcept { + return transform_.scale; + } + inline renderer& renderer::properties(render::property_block&& value) noexcept { properties_ = std::move(value); return *this; diff --git a/samples/bin/library/scripts/emmy/components/renderer.lua b/samples/bin/library/scripts/emmy/components/renderer.lua index 52a6b486..3522a35a 100644 --- a/samples/bin/library/scripts/emmy/components/renderer.lua +++ b/samples/bin/library/scripts/emmy/components/renderer.lua @@ -4,7 +4,19 @@ local renderer = { enabled = true, ---@type boolean - disabled = false + disabled = false, + + ---@type t3f + transform = t3f.identity(), + + ---@type v3f + translation = v3f.zero(), + + ---@type q4f + rotation = q4f.identity(), + + ---@type v3f + scale = v3f.unit() } ---@overload fun(self: renderer) diff --git a/sources/enduro2d/high/bindings/high_binds/components/renderer_binds.cpp b/sources/enduro2d/high/bindings/high_binds/components/renderer_binds.cpp index f4518383..47162599 100644 --- a/sources/enduro2d/high/bindings/high_binds/components/renderer_binds.cpp +++ b/sources/enduro2d/high/bindings/high_binds/components/renderer_binds.cpp @@ -48,7 +48,23 @@ namespace e2d::bindings::high c.owner().component>().remove(); } } - ) + ), + + "transform", sol::property( + [](const gcomponent& c) -> t3f { return c->transform(); }, + [](gcomponent& c, const t3f& v) { c->transform(v); }), + + "translation", sol::property( + [](const gcomponent& c) -> v3f { return c->translation(); }, + [](gcomponent& c, const v3f& v) { c->translation(v); }), + + "rotation", sol::property( + [](const gcomponent& c) -> q4f { return c->rotation(); }, + [](gcomponent& c, const q4f& v) { c->rotation(v); }), + + "scale", sol::property( + [](const gcomponent& c) -> v3f { return c->scale(); }, + [](gcomponent& c, const v3f& v) { c->scale(v); }) ); } } diff --git a/sources/enduro2d/high/components/renderer.cpp b/sources/enduro2d/high/components/renderer.cpp index e32ee6ee..efe2f6f8 100644 --- a/sources/enduro2d/high/components/renderer.cpp +++ b/sources/enduro2d/high/components/renderer.cpp @@ -13,6 +13,10 @@ namespace e2d "required" : [], "additionalProperties" : false, "properties" : { + "transform" : { "$ref": "#/common_definitions/t3" }, + "materials" : { "$ref": "#/definitions/materials" } + }, + "definitions" : { "materials" : { "type" : "array", "items" : { "$ref": "#/common_definitions/address" } @@ -24,6 +28,15 @@ namespace e2d renderer& component, const fill_context& ctx) const { + if ( ctx.root.HasMember("transform") ) { + t3f transform = component.transform(); + if ( !json_utils::try_parse_value(ctx.root["transform"], transform) ) { + the().error("RENDERER: Incorrect formatting of 'transform' property"); + return false; + } + component.transform(transform); + } + if ( ctx.root.HasMember("properties") ) { //TODO(BlackMat): add properties parsing } diff --git a/sources/enduro2d/high/components/scene.cpp b/sources/enduro2d/high/components/scene.cpp index 1ade833f..2cf7bc93 100644 --- a/sources/enduro2d/high/components/scene.cpp +++ b/sources/enduro2d/high/components/scene.cpp @@ -13,7 +13,7 @@ namespace e2d "required" : [], "additionalProperties" : false, "properties" : { - "depth" : { "type" : "number" } + "depth" : { "type" : "integer" } } })json"; @@ -22,7 +22,7 @@ namespace e2d const fill_context& ctx) const { if ( ctx.root.HasMember("depth") ) { - auto depth = component.depth(); + i32 depth = component.depth(); if ( !json_utils::try_parse_value(ctx.root["depth"], depth) ) { the().error("SCENE: Incorrect formatting of 'depth' property"); return false; diff --git a/sources/enduro2d/high/systems/render_system_impl/render_system_drawer.cpp b/sources/enduro2d/high/systems/render_system_impl/render_system_drawer.cpp index 838a51bf..18bcdba8 100644 --- a/sources/enduro2d/high/systems/render_system_impl/render_system_drawer.cpp +++ b/sources/enduro2d/high/systems/render_system_impl/render_system_drawer.cpp @@ -92,16 +92,20 @@ namespace e2d::render_system_impl return; } + const m4f& model_m = + math::make_trs_matrix4(node_r->transform()) * + node->world_matrix(); + if ( auto mdl_r = gcomponent{owner} ) { - draw(node, *node_r, *mdl_r); + draw(model_m, *node_r, *mdl_r); } if ( auto spn_p = gcomponent{owner} ) { - draw(node, *node_r, *spn_p); + draw(model_m, *node_r, *spn_p); } if ( auto spr_r = gcomponent{owner} ) { - draw(node, *node_r, *spr_r); + draw(model_m, *node_r, *spr_r); } } @@ -110,7 +114,7 @@ namespace e2d::render_system_impl } void drawer::context::draw( - const const_node_iptr& node, + const m4f& model_m, const renderer& node_r, const model_renderer& mdl_r) { @@ -124,7 +128,7 @@ namespace e2d::render_system_impl try { property_cache_ .merge(batcher_.flush()) - .property(matrix_m_property_hash, node->world_matrix()) + .property(matrix_m_property_hash, model_m) .merge(node_r.properties()); const std::size_t submesh_count = math::min( @@ -151,7 +155,7 @@ namespace e2d::render_system_impl } void drawer::context::draw( - const const_node_iptr& node, + const m4f& model_m, const renderer& node_r, const spine_player& spine_r) { @@ -175,7 +179,6 @@ namespace e2d::render_system_impl material_asset::ptr multiply_mat_a; material_asset::ptr screen_mat_a; - const m4f& sm = node->world_matrix(); unsigned short quad_indices[6] = { 0, 1, 2, 2, 3, 0 }; for ( int i = 0; i < skeleton->slotsCount; ++i ) { @@ -399,7 +402,7 @@ namespace e2d::render_system_impl for ( std::size_t j = 0; j < batch_vertex_count; ++j ) { batcher_type::vertex_type& vert = batch_vertices[j]; - vert.v = v3f(v4f(vertices[j * 2], vertices[j * 2 + 1], 0.f, 1.f) * sm); + vert.v = v3f(v4f(vertices[j * 2], vertices[j * 2 + 1], 0.f, 1.f) * model_m); vert.t = v2f(uvs[j * 2], uvs[j * 2 + 1]); vert.c = vert_color; } @@ -431,7 +434,7 @@ namespace e2d::render_system_impl } void drawer::context::draw( - const const_node_iptr& node, + const m4f& model_m, const renderer& node_r, const sprite_renderer& spr_r) { @@ -467,17 +470,16 @@ namespace e2d::render_system_impl const f32 tw = tex_r.size.x / tex_s.x; const f32 th = tex_r.size.y / tex_s.y; - const m4f& sm = node->world_matrix(); const color32& tc = spr_r.tint(); const batcher_type::index_type indices[] = { 0u, 1u, 2u, 2u, 3u, 0u}; const batcher_type::vertex_type vertices[] = { - { v3f(p1 * sm), {tx + 0.f, ty + 0.f}, tc }, - { v3f(p2 * sm), {tx + tw, ty + 0.f}, tc }, - { v3f(p3 * sm), {tx + tw, ty + th }, tc }, - { v3f(p4 * sm), {tx + 0.f, ty + th }, tc }}; + { v3f(p1 * model_m), {tx + 0.f, ty + 0.f}, tc }, + { v3f(p2 * model_m), {tx + tw, ty + 0.f}, tc }, + { v3f(p3 * model_m), {tx + tw, ty + th }, tc }, + { v3f(p4 * model_m), {tx + 0.f, ty + th }, tc }}; const render::sampler_min_filter tex_min_f = spr_r.filtering() ? render::sampler_min_filter::linear diff --git a/sources/enduro2d/high/systems/render_system_impl/render_system_drawer.hpp b/sources/enduro2d/high/systems/render_system_impl/render_system_drawer.hpp index 1255bfd3..6c00c6d2 100644 --- a/sources/enduro2d/high/systems/render_system_impl/render_system_drawer.hpp +++ b/sources/enduro2d/high/systems/render_system_impl/render_system_drawer.hpp @@ -44,17 +44,17 @@ namespace e2d::render_system_impl void flush(); private: void draw( - const const_node_iptr& node, + const m4f& model_m, const renderer& node_r, const model_renderer& mdl_r); void draw( - const const_node_iptr& node, + const m4f& model_m, const renderer& node_r, const sprite_renderer& spr_r); void draw( - const const_node_iptr& node, + const m4f& model_m, const renderer& node_r, const spine_player& spine_r); private: From 4b266de4346e65273cd4152a8d612a5898537c3f Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Sat, 30 Nov 2019 09:52:34 +0700 Subject: [PATCH 3/4] change node transform type to trs2 --- headers/enduro2d/high/node.hpp | 26 +++++++++---------- headers/enduro2d/high/world.hpp | 4 +-- .../bin/library/scripts/emmy/high/node.lua | 19 ++++++++------ .../bin/library/scripts/emmy/high/world.lua | 2 +- .../high/bindings/high_binds/node_binds.cpp | 20 +++++++------- .../high/bindings/high_binds/world_binds.cpp | 2 +- sources/enduro2d/high/components/actor.cpp | 12 ++++----- sources/enduro2d/high/node.cpp | 24 ++++++++--------- sources/enduro2d/high/world.cpp | 4 +-- 9 files changed, 57 insertions(+), 56 deletions(-) diff --git a/headers/enduro2d/high/node.hpp b/headers/enduro2d/high/node.hpp index 456c1f17..9d4342ad 100644 --- a/headers/enduro2d/high/node.hpp +++ b/headers/enduro2d/high/node.hpp @@ -30,31 +30,31 @@ namespace e2d virtual ~node() noexcept; static node_iptr create(); - static node_iptr create(const t3f& transform); + static node_iptr create(const t2f& transform); static node_iptr create(const node_iptr& parent); - static node_iptr create(const node_iptr& parent, const t3f& transform); + static node_iptr create(const node_iptr& parent, const t2f& transform); static node_iptr create(gobject owner); - static node_iptr create(gobject owner, const t3f& transform); + static node_iptr create(gobject owner, const t2f& transform); static node_iptr create(gobject owner, const node_iptr& parent); - static node_iptr create(gobject owner, const node_iptr& parent, const t3f& transform); + static node_iptr create(gobject owner, const node_iptr& parent, const t2f& transform); void owner(gobject owner) noexcept; gobject owner() const noexcept; - void transform(const t3f& transform) noexcept; - const t3f& transform() const noexcept; + void transform(const t2f& transform) noexcept; + const t2f& transform() const noexcept; - void translation(const v3f& translation) noexcept; - const v3f& translation() const noexcept; + void translation(const v2f& translation) noexcept; + const v2f& translation() const noexcept; - void rotation(const q4f& rotation) noexcept; - const q4f& rotation() const noexcept; + void rotation(const radf& rotation) noexcept; + const radf& rotation() const noexcept; - void scale(const v3f& scale) noexcept; - const v3f& scale() const noexcept; + void scale(const v2f& scale) noexcept; + const v2f& scale() const noexcept; const m4f& local_matrix() const noexcept; const m4f& world_matrix() const noexcept; @@ -145,7 +145,7 @@ namespace e2d void update_local_matrix_() const noexcept; void update_world_matrix_() const noexcept; private: - t3f transform_; + t2f transform_; gobject owner_; node* parent_{nullptr}; node_children children_; diff --git a/headers/enduro2d/high/world.hpp b/headers/enduro2d/high/world.hpp index 46c91fd9..1d12599e 100644 --- a/headers/enduro2d/high/world.hpp +++ b/headers/enduro2d/high/world.hpp @@ -28,8 +28,8 @@ namespace e2d gobject instantiate(const node_iptr& parent); gobject instantiate(const prefab& prefab, const node_iptr& parent); - gobject instantiate(const node_iptr& parent, const t3f& transform); - gobject instantiate(const prefab& prefab, const node_iptr& parent, const t3f& transform); + gobject instantiate(const node_iptr& parent, const t2f& transform); + gobject instantiate(const prefab& prefab, const node_iptr& parent, const t2f& transform); void destroy_instance(gobject& inst) noexcept; void finalize_instances() noexcept; diff --git a/samples/bin/library/scripts/emmy/high/node.lua b/samples/bin/library/scripts/emmy/high/node.lua index 2856bf52..717ec9a5 100644 --- a/samples/bin/library/scripts/emmy/high/node.lua +++ b/samples/bin/library/scripts/emmy/high/node.lua @@ -1,16 +1,19 @@ ---@class node local node = { - ---@type t3f - transform = t3f.identity(), + ---@type gobject + owner = nil, - ---@type v3f - translation = v3f.zero(), + ---@type t2f + transform = t2f.identity(), - ---@type q4f - rotation = q4f.identity(), + ---@type v2f + translation = v2f.zero(), - ---@type v3f - scale = v3f.unit(), + ---@type number + rotation = 0, + + ---@type v2f + scale = v2f.unit(), ---@type m4f local_matrix = m4f.identity(), diff --git a/samples/bin/library/scripts/emmy/high/world.lua b/samples/bin/library/scripts/emmy/high/world.lua index 8c2849a8..43cfd182 100644 --- a/samples/bin/library/scripts/emmy/high/world.lua +++ b/samples/bin/library/scripts/emmy/high/world.lua @@ -4,7 +4,7 @@ local world = { ---@overload fun(prefab: prefab): gobject ---@overload fun(prefab: prefab, parent: node): gobject ----@overload fun(prefab: prefab, parent: node, transform: t3f): gobject +---@overload fun(prefab: prefab, parent: node, transform: t2f): gobject ---@return gobject function world:instantiate(...) end diff --git a/sources/enduro2d/high/bindings/high_binds/node_binds.cpp b/sources/enduro2d/high/bindings/high_binds/node_binds.cpp index 1d4cd022..da52edfb 100644 --- a/sources/enduro2d/high/bindings/high_binds/node_binds.cpp +++ b/sources/enduro2d/high/bindings/high_binds/node_binds.cpp @@ -15,25 +15,23 @@ namespace e2d::bindings::high sol::no_constructor, "owner", sol::property( - [](const node& n) -> gobject { - return n.owner(); - }), + [](const node& n) -> gobject { return n.owner(); }), "transform", sol::property( - [](const node& n) -> t3f { return n.transform(); }, - sol::resolve(&node::transform)), + [](const node& n) -> t2f { return n.transform(); }, + [](node& n, const t2f& v) { n.transform(v); }), "translation", sol::property( - [](const node& n) -> v3f { return n.translation(); }, - sol::resolve(&node::translation)), + [](const node& n) -> v2f { return n.translation(); }, + [](node& n, const v2f& v) { n.translation(v); }), "rotation", sol::property( - [](const node& n) -> q4f { return n.rotation(); }, - sol::resolve(&node::rotation)), + [](const node& n) -> f32 { return n.rotation().value; }, + [](node& n, f32 v) { n.rotation(make_rad(v)); }), "scale", sol::property( - [](const node& n) -> v3f { return n.scale(); }, - sol::resolve(&node::scale)), + [](const node& n) -> v2f { return n.scale(); }, + [](node& n, const v2f& v) { n.scale(v); }), "local_matrix", sol::property( [](const node& n) -> m4f { return n.local_matrix(); }), diff --git a/sources/enduro2d/high/bindings/high_binds/world_binds.cpp b/sources/enduro2d/high/bindings/high_binds/world_binds.cpp index eba22d3f..9b34c31a 100644 --- a/sources/enduro2d/high/bindings/high_binds/world_binds.cpp +++ b/sources/enduro2d/high/bindings/high_binds/world_binds.cpp @@ -21,7 +21,7 @@ namespace e2d::bindings::high [](world& w, const prefab& prefab, const node_iptr& parent) -> gobject { return w.instantiate(prefab, parent); }, - [](world& w, const prefab& prefab, const node_iptr& parent, const t3f& transform) -> gobject { + [](world& w, const prefab& prefab, const node_iptr& parent, const t2f& transform) -> gobject { return w.instantiate(prefab, parent, transform); } ) diff --git a/sources/enduro2d/high/components/actor.cpp b/sources/enduro2d/high/components/actor.cpp index 7f1b5125..75df445d 100644 --- a/sources/enduro2d/high/components/actor.cpp +++ b/sources/enduro2d/high/components/actor.cpp @@ -13,9 +13,9 @@ namespace e2d "required" : [], "additionalProperties" : false, "properties" : { - "translation" : { "$ref": "#/common_definitions/v3" }, - "rotation" : { "$ref": "#/common_definitions/q4" }, - "scale" : { "$ref": "#/common_definitions/v3" } + "translation" : { "$ref": "#/common_definitions/v2" }, + "rotation" : { "type" : "number" }, + "scale" : { "$ref": "#/common_definitions/v2" } } })json"; @@ -28,7 +28,7 @@ namespace e2d } if ( ctx.root.HasMember("translation") ) { - auto translation = component.node()->translation(); + v2f translation = component.node()->translation(); if ( !json_utils::try_parse_value(ctx.root["translation"], translation) ) { the().error("ACTOR: Incorrect formatting of 'translation' property"); return false; @@ -37,7 +37,7 @@ namespace e2d } if ( ctx.root.HasMember("rotation") ) { - auto rotation = component.node()->rotation(); + radf rotation = component.node()->rotation(); if ( !json_utils::try_parse_value(ctx.root["rotation"], rotation) ) { the().error("ACTOR: Incorrect formatting of 'rotation' property"); return false; @@ -46,7 +46,7 @@ namespace e2d } if ( ctx.root.HasMember("scale") ) { - auto scale = component.node()->scale(); + v2f scale = component.node()->scale(); if ( !json_utils::try_parse_value(ctx.root["scale"], scale) ) { the().error("ACTOR: Incorrect formatting of 'scale' property"); return false; diff --git a/sources/enduro2d/high/node.cpp b/sources/enduro2d/high/node.cpp index 0f4dd309..0448897d 100644 --- a/sources/enduro2d/high/node.cpp +++ b/sources/enduro2d/high/node.cpp @@ -21,7 +21,7 @@ namespace e2d return node_iptr(new node()); } - node_iptr node::create(const t3f& transform) { + node_iptr node::create(const t2f& transform) { node_iptr n = create(); n->transform(transform); return n; @@ -35,7 +35,7 @@ namespace e2d return child; } - node_iptr node::create(const node_iptr& parent, const t3f& transform) { + node_iptr node::create(const node_iptr& parent, const t2f& transform) { node_iptr n = create(parent); n->transform(transform); return n; @@ -45,7 +45,7 @@ namespace e2d return node_iptr(new node(std::move(owner))); } - node_iptr node::create(gobject owner, const t3f& transform) { + node_iptr node::create(gobject owner, const t2f& transform) { node_iptr n = create(owner); n->transform(transform); return n; @@ -59,7 +59,7 @@ namespace e2d return child; } - node_iptr node::create(gobject owner, const node_iptr& parent, const t3f& transform) { + node_iptr node::create(gobject owner, const node_iptr& parent, const t2f& transform) { node_iptr n = create(owner, parent); n->transform(transform); return n; @@ -73,39 +73,39 @@ namespace e2d return owner_; } - void node::transform(const t3f& transform) noexcept { + void node::transform(const t2f& transform) noexcept { transform_ = transform; mark_dirty_local_matrix_(); } - const t3f& node::transform() const noexcept { + const t2f& node::transform() const noexcept { return transform_; } - void node::translation(const v3f& translation) noexcept { + void node::translation(const v2f& translation) noexcept { transform_.translation = translation; mark_dirty_local_matrix_(); } - const v3f& node::translation() const noexcept { + const v2f& node::translation() const noexcept { return transform_.translation; } - void node::rotation(const q4f& rotation) noexcept { + void node::rotation(const radf& rotation) noexcept { transform_.rotation = rotation; mark_dirty_local_matrix_(); } - const q4f& node::rotation() const noexcept { + const radf& node::rotation() const noexcept { return transform_.rotation; } - void node::scale(const v3f& scale) noexcept { + void node::scale(const v2f& scale) noexcept { transform_.scale = scale; mark_dirty_local_matrix_(); } - const v3f& node::scale() const noexcept { + const v2f& node::scale() const noexcept { return transform_.scale; } diff --git a/sources/enduro2d/high/world.cpp b/sources/enduro2d/high/world.cpp index 2d77f72a..3e130584 100644 --- a/sources/enduro2d/high/world.cpp +++ b/sources/enduro2d/high/world.cpp @@ -211,11 +211,11 @@ namespace e2d return inst; } - gobject world::instantiate(const node_iptr& parent, const t3f& transform) { + gobject world::instantiate(const node_iptr& parent, const t2f& transform) { return instantiate(prefab(), parent, transform); } - gobject world::instantiate(const prefab& prefab, const node_iptr& parent, const t3f& transform) { + gobject world::instantiate(const prefab& prefab, const node_iptr& parent, const t2f& transform) { gobject inst = new_instance(*this, prefab); inst.component()->node()->transform(transform); From a131739b2505abf0052a1bf4e81d0505e9682093 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Sat, 30 Nov 2019 10:03:54 +0700 Subject: [PATCH 4/4] fix tests and samples --- samples/bin/library/scenes/sample_07.json | 4 +- samples/bin/library/scenes/scene_prefab.json | 10 ++-- .../library/scenes/spine_scene_prefab.json | 6 +- .../bin/library/scripts/sample_07/gnome.lua | 2 +- samples/sources/sample_03/sample_03.cpp | 48 +++++++++------ untests/sources/untests_high/node.cpp | 58 +++++++++---------- 6 files changed, 69 insertions(+), 59 deletions(-) diff --git a/samples/bin/library/scenes/sample_07.json b/samples/bin/library/scenes/sample_07.json index 7d8a9360..bfc3d104 100644 --- a/samples/bin/library/scenes/sample_07.json +++ b/samples/bin/library/scenes/sample_07.json @@ -11,7 +11,7 @@ "prototype" : "../prefabs/gnome_prefab.json", "components" : { "actor" : { - "translation" : [0,0,0], + "translation" : [0,0], "scale" : 20 }, "behaviour" : { @@ -28,7 +28,7 @@ "outline_color" : [0,0,0,255] }, "actor" : { - "translation" : [-315,-235,0], + "translation" : [-315,-235], "scale" : 1 }, "behaviour" : { diff --git a/samples/bin/library/scenes/scene_prefab.json b/samples/bin/library/scenes/scene_prefab.json index f1d07b21..6a0c19ed 100644 --- a/samples/bin/library/scenes/scene_prefab.json +++ b/samples/bin/library/scenes/scene_prefab.json @@ -8,7 +8,7 @@ "prototype" : "../prefabs/gnome_prefab.json", "components" : { "actor" : { - "translation" : [0,0,0], + "translation" : [0,0], "scale" : 20 } } @@ -19,7 +19,7 @@ "blending" : "additive" }, "actor" : { - "translation" : [-50,-50,0] + "translation" : [-50,-50] } } }, { @@ -29,7 +29,7 @@ "tint" : [255,0,0,255] }, "actor" : { - "translation" : [50,-50,0] + "translation" : [50,-50] } } }, { @@ -42,7 +42,7 @@ "outline_color" : [0,0,0,255] }, "actor" : { - "translation" : [0,180,0], + "translation" : [0,180], "scale" : 3 } } @@ -56,7 +56,7 @@ "outline_color" : [0,0,0,255] }, "actor" : { - "translation" : [0.5,-180.5,0], + "translation" : [0.5,-180.5], "scale" : 3 } } diff --git a/samples/bin/library/scenes/spine_scene_prefab.json b/samples/bin/library/scenes/spine_scene_prefab.json index 8eebb6fa..5254139b 100644 --- a/samples/bin/library/scenes/spine_scene_prefab.json +++ b/samples/bin/library/scenes/spine_scene_prefab.json @@ -8,7 +8,7 @@ "prototype" : "../prefabs/coin_prefab.json", "components" : { "actor" : { - "translation" : [350,250,0], + "translation" : [350,250], "scale" : 0.25 } } @@ -16,7 +16,7 @@ "prototype" : "../prefabs/raptor_prefab.json", "components" : { "actor" : { - "translation" : [300,-350,0], + "translation" : [300,-350], "scale" : 0.25 } } @@ -24,7 +24,7 @@ "prototype" : "../prefabs/dragon_prefab.json", "components" : { "actor" : { - "translation" : [-100,0,0], + "translation" : [-100,0], "scale" : 0.9 } } diff --git a/samples/bin/library/scripts/sample_07/gnome.lua b/samples/bin/library/scripts/sample_07/gnome.lua index 2882c7aa..fd5af887 100644 --- a/samples/bin/library/scripts/sample_07/gnome.lua +++ b/samples/bin/library/scripts/sample_07/gnome.lua @@ -21,7 +21,7 @@ end ---@param go gobject local function update_gnome_rotation(meta, go) local time = the_engine.time - go.actor.node.rotation = q4f.make_from_euler_angles(0, time, 0) + go.renderer.rotation = q4f.make_from_euler_angles(0, time, 0) end -- ----------------------------------------------------------------------------- diff --git a/samples/sources/sample_03/sample_03.cpp b/samples/sources/sample_03/sample_03.cpp index daefe26b..723995f4 100644 --- a/samples/sources/sample_03/sample_03.cpp +++ b/samples/sources/sample_03/sample_03.cpp @@ -9,7 +9,10 @@ using namespace e2d; namespace { - struct rotator { + struct node_rotator { + }; + + struct renderer_rotator { v3f axis; }; @@ -61,14 +64,21 @@ namespace ecs::registry& owner, const systems::update_event& event) override { - owner.for_joined_components( - [&event](const ecs::const_entity&, const rotator& rot, actor& act){ + owner.for_joined_components( + [&event](const ecs::const_entity&, const node_rotator&, actor& act){ const node_iptr node = act.node(); if ( node ) { - const q4f q = math::make_quat_from_axis_angle(make_rad(event.time), rot.axis); - node->rotation(q); + node->rotation(make_rad(event.time)); } }); + + owner.for_joined_components( + [&event](const ecs::const_entity&, const renderer_rotator& rot, renderer& r){ + const q4f q = math::make_quat_from_axis_angle( + make_rad(event.time), + rot.axis); + r.rotation(q); + }); } }; @@ -97,20 +107,20 @@ namespace { prefab prefab; prefab.prototype() - .component(rotator{v3f::unit_y()}) + .component(v3f::unit_y()) .component(renderer().materials({model_mat})) .component(model_res); the().instantiate( prefab, scene_i.component()->node(), - make_trs3(v3f{0,50.f,0}, q4f::identity(), v3f{20.f})); + make_trs2(v2f{0,50.f}, radf::zero(), v2f{20.f})); } { prefab prefab; prefab.prototype() - .component(rotator{v3f::unit_z()}) + .component() .component() .component(sprite_renderer(sprite_res) .materials({{"normal", sprite_mat}})); @@ -118,13 +128,13 @@ namespace the().instantiate( prefab, scene_i.component()->node(), - math::make_translation_trs3(v3f{0,-50.f,0})); + math::make_translation_trs2(v2f{0,-50.f})); } { prefab prefab_a; prefab_a.prototype() - .component(rotator{v3f::unit_z()}) + .component() .component() .component(sprite_renderer() .filtering(false) @@ -135,10 +145,10 @@ namespace for ( std::size_t i = 0; i < 2; ++i ) for ( std::size_t j = 0; j < 5; ++j ) { - t3f trans{ - {-80.f + j * 40.f, -200.f + i * 40.f, 0}, - q4f::identity(), - {2.f,2.f,1.f}}; + t2f trans{ + {-80.f + j * 40.f, -200.f + i * 40.f}, + radf::zero(), + {2.f,2.f}}; gobject inst = the().instantiate( prefab_a, scene_i.component()->node(), @@ -146,11 +156,11 @@ namespace prefab prefab_b = prefab_a; prefab_b.prototype() - .component(rotator{v3f::unit_z()}) - .component(node::create(make_trs3( - v3f{20.f,0.f,0.f}, - q4f::identity(), - v3f{0.3f,0.3f,3.f}))); + .component() + .component(node::create(make_trs2( + v2f{20.f,0.f}, + radf::zero(), + v2f{0.3f,0.3f}))); the().instantiate( prefab_b, diff --git a/untests/sources/untests_high/node.cpp b/untests/sources/untests_high/node.cpp index 5c78f4ca..73ee891d 100644 --- a/untests/sources/untests_high/node.cpp +++ b/untests/sources/untests_high/node.cpp @@ -655,82 +655,82 @@ TEST_CASE("node") { SECTION("transform") { auto p = node::create(); - REQUIRE(p->transform() == t3f::identity()); - REQUIRE(p->translation() == v3f::zero()); - REQUIRE(p->rotation() == q4f::identity()); - REQUIRE(p->scale() == v3f::unit()); + REQUIRE(p->transform() == t2f::identity()); + REQUIRE(p->translation() == v2f::zero()); + REQUIRE(p->rotation() == radf::zero()); + REQUIRE(p->scale() == v2f::unit()); - p->translation(v3f(1,2,3)); - REQUIRE(p->translation() == v3f(1,2,3)); + p->translation(v2f(1,2)); + REQUIRE(p->translation() == v2f(1,2)); - p->rotation(q4f(1,2,3,4)); - REQUIRE(p->rotation() == q4f(1,2,3,4)); + p->rotation(radf(1.f)); + REQUIRE(p->rotation() == radf(1.f)); - p->scale(v3f(1,2,3)); - REQUIRE(p->scale() == v3f(1,2,3)); + p->scale(v2f(1,2)); + REQUIRE(p->scale() == v2f(1,2)); } SECTION("local_matrix") { { auto p = node::create(); - p->transform(math::make_translation_trs3(v3f{10.f,0.f,0.f})); + p->transform(math::make_translation_trs2(v2f{10.f,0.f})); auto n = node::create(p); - n->transform(math::make_translation_trs3(v3f{20.f,0.f,0.f})); - REQUIRE(n->local_matrix() == math::make_translation_matrix4(20.f,0.f,0.f)); + n->transform(math::make_translation_trs2(v2f{20.f,0.f})); + REQUIRE(n->local_matrix() == math::make_translation_matrix4(20.f,0.f)); auto v = v4f(5.f,0.f,0.f,1.f); REQUIRE(v * n->local_matrix() == v4f{25.f,0.f,0.f,1.f}); - n->transform(math::make_scale_trs3(v3f(1.f,2.f,3.f))); - REQUIRE(n->local_matrix() == math::make_scale_matrix4(1.f,2.f,3.f)); + n->transform(math::make_scale_trs2(v2f(1.f,2.f))); + REQUIRE(n->local_matrix() == math::make_scale_matrix4(1.f,2.f)); } } SECTION("world_matrix") { { auto p = node::create(); - p->translation({10.f,0.f,0.f}); + p->translation({10.f,0.f}); auto n = node::create(p); - n->translation({20.f,0.f,0.f}); + n->translation({20.f,0.f}); auto v = v4f(5.f,0.f,0.f,1.f); REQUIRE(v * n->world_matrix() == v4f{35.f,0.f,0.f,1.f}); - n->transform(math::make_scale_trs3(v3f(1.f,2.f,3.f))); + n->transform(math::make_scale_trs2(v2f(1.f,2.f))); REQUIRE(n->world_matrix() == - math::make_scale_matrix4(1.f,2.f,3.f) * - math::make_translation_matrix4(10.f,0.f,0.f)); + math::make_scale_matrix4(1.f,2.f) * + math::make_translation_matrix4(10.f,0.f)); } { auto n = node::create(); - n->translation({20.f,0.f,0.f}); + n->translation({20.f,0.f}); REQUIRE(n->world_matrix() == - math::make_translation_matrix4(20.f,0.f,0.f)); + math::make_translation_matrix4(20.f,0.f)); auto p = node::create(); - p->transform(math::make_translation_trs3(v3f{10.f,0.f,0.f})); + p->transform(math::make_translation_trs2(v2f{10.f,0.f})); p->add_child(n); REQUIRE(n->world_matrix() == - math::make_translation_matrix4(30.f,0.f,0.f)); + math::make_translation_matrix4(30.f,0.f)); } { auto p1 = node::create(); - p1->translation({10.f,0.f,0.f}); + p1->translation({10.f,0.f}); auto p2 = node::create(); - p2->transform(math::make_translation_trs3(v3f{20.f,0.f,0.f})); + p2->transform(math::make_translation_trs2(v2f{20.f,0.f})); auto n = node::create(p2); - n->transform(math::make_translation_trs3(v3f{30.f,0.f,0.f})); + n->transform(math::make_translation_trs2(v2f{30.f,0.f})); REQUIRE(n->world_matrix() == - math::make_translation_matrix4(50.f,0.f,0.f)); + math::make_translation_matrix4(50.f,0.f)); p1->add_child(p2); REQUIRE(n->world_matrix() == - math::make_translation_matrix4(60.f,0.f,0.f)); + math::make_translation_matrix4(60.f,0.f)); } } SECTION("lifetime") {