From a4bb53ce8fd514363c5583c8ee25bc75a9fe69a7 Mon Sep 17 00:00:00 2001 From: Hartmut Seichter Date: Tue, 23 Jul 2024 22:23:02 +0200 Subject: [PATCH] WiP for mesh and fixes for clang compilation --- src/binding/src/script_system.cpp | 72 +++++++-------- src/binding/src/script_visual.cpp | 116 +++++++++++-------------- src/core/include/pw/core/geometry.hpp | 27 +++++- src/core/include/pw/core/matrix.hpp | 4 +- src/core/include/pw/core/mesh.hpp | 32 +++++++ src/core/include/pw/core/serialize.hpp | 4 +- src/core/include/pw/core/vector.hpp | 8 +- src/core/tests/pwcore_test_matrix.cpp | 2 +- src/core/tests/pwcore_test_mesh.cpp | 47 +++++----- src/core/tests/pwcore_test_vector.cpp | 2 +- 10 files changed, 173 insertions(+), 141 deletions(-) create mode 100644 src/core/include/pw/core/mesh.hpp diff --git a/src/binding/src/script_system.cpp b/src/binding/src/script_system.cpp index 19a43af..c76c2df 100644 --- a/src/binding/src/script_system.cpp +++ b/src/binding/src/script_system.cpp @@ -1,9 +1,9 @@ -//#include "script_system.hpp" +// #include "script_system.hpp" -#include "pw/system/window.hpp" -#include "pw/system/input.hpp" #include "pw/system/display.hpp" +#include "pw/system/input.hpp" #include "pw/system/path.hpp" +#include "pw/system/window.hpp" #include "runtime_lua.hpp" @@ -13,49 +13,37 @@ namespace pw { -void register_system_function(sol::state&, sol::table &ns) -{ - ns.new_usertype("window", - "update",&window::update, - "on_update",sol::writeonly_property(&window::set_on_update), - "on_resize",sol::writeonly_property(&window::set_on_resize), - "title",sol::writeonly_property(&window::set_title), - "size",sol::property(&window::size,&window::set_size), - "client_size",sol::readonly_property(&window::client_size), - "position",sol::property(&window::position,&window::set_position), - "fullscreen",sol::property(&window::fullscreen,&window::set_fullscreen), - "visible",sol::property(&window::visible,&window::set_visible) - ); +void register_system_function(sol::state&, sol::table& ns) { + ns.new_usertype( + "window", "update", &window::update, "on_update", + sol::writeonly_property(&window::set_on_update), "on_resize", + sol::writeonly_property(&window::set_on_resize), "title", + sol::writeonly_property(&window::set_title), "size", + sol::property(&window::size, &window::set_size), "client_size", + sol::readonly_property(&window::client_size), "position", + sol::property(&window::position, &window::set_position), "fullscreen", + sol::property(&window::fullscreen, &window::set_fullscreen), "visible", + sol::property(&window::visible, &window::set_visible)); + ns.new_usertype( + "input", "new", sol::no_constructor, "get", &input::get, + "mouse_position", sol::readonly_property(&input::mouse_position), + "mouse_button", sol::readonly_property(&input::mouse_button), + "mouse_pressed", sol::readonly_property(&input::mouse_pressed), + "has_input", sol::readonly_property(&input::has_input), "input_string", + sol::readonly_property(&input::input_string)); - ns.new_usertype("input", - "new", sol::no_constructor, - "get",&input::get, - "mouse_position",sol::readonly_property(&input::mouse_position), - "mouse_button",sol::readonly_property(&input::mouse_button), - "mouse_pressed",sol::readonly_property(&input::mouse_pressed), - "has_input",sol::readonly_property(&input::has_input), - "input_string",sol::readonly_property(&input::input_string) - ); + ns.new_usertype("display", "all", &display::all, "name", + sol::readonly_property(&display::name)); - ns.new_usertype("display", - "all",&display::all, - "name",sol::readonly_property(&display::name) - ); - - ns.new_usertype("path" - ,"new",sol::no_constructor - ,"get",&path::get - ,"separator",sol::readonly_property(&path::separator) - ,"executable_path",sol::readonly_property(&path::executable_path) - ,"resource_paths",sol::readonly_property([](const path& p){return sol::as_table(p.resource_paths());}) - ); + ns.new_usertype( + "path", "new", sol::no_constructor, "get", &path::get, "separator", + sol::readonly_property(&path::separator), "executable_path", + sol::readonly_property(&path::executable_path), "resource_paths", + sol::readonly_property( + [](const path& p) { return sol::as_table(p.resource_paths()); })); } - - - PW_REGISTER_LUA(system) - -} +} // namespace pw diff --git a/src/binding/src/script_visual.cpp b/src/binding/src/script_visual.cpp index 2ee87a1..a4a0e92 100644 --- a/src/binding/src/script_visual.cpp +++ b/src/binding/src/script_visual.cpp @@ -1,92 +1,76 @@ #include "pw/core/debug.hpp" +#include "pw/visual/context.hpp" +#include "pw/visual/framebuffer.hpp" #include "pw/visual/pipeline.hpp" #include "pw/visual/shader.hpp" -#include "pw/visual/framebuffer.hpp" #include "pw/visual/texture.hpp" -#include "pw/visual/context.hpp" #include "pw/core/size.hpp" - #include "runtime_lua.hpp" namespace pw { -void register_visual_function(sol::state& lua,sol::table &ns) -{ +void register_visual_function(sol::state& lua, sol::table& ns) { -// ns.new_usertype("pipeline" -// ,"create",&pipeline::create -// ,"draw",&pipeline::draw -// ); + // ns.new_usertype("pipeline" + // ,"create",&pipeline::create + // ,"draw",&pipeline::draw + // ); - ns.new_usertype("shader" - ,sol::call_constructor,sol::constructors() - ,"ready",sol::readonly_property(&shader::ready) - ,"use",&shader::use - ,"build",&shader::build - ,"source",&shader::source - ,"set_source",&shader::set_source - ,"set_uniforms",&shader::set_uniforms - ,"set_uniform_float",&shader::set_uniform - ,"set_uniform_uint",&shader::set_uniform - ,"set_uniform_int",&shader::set_uniform - ,"set_uniform_mat4",&shader::set_uniform - ,"set_uniform_vec4",&shader::set_uniform - ,"set_uniform_texture",&shader::set_uniform + ns.new_usertype( + "shader", sol::call_constructor, sol::constructors(), "ready", + sol::readonly_property(&shader::ready), "use", &shader::use, "build", + &shader::build, "source", &shader::source, "set_source", + &shader::set_source, "set_uniforms", &shader::set_uniforms, + "set_uniform_float", &shader::set_uniform, "set_uniform_uint", + &shader::set_uniform, "set_uniform_int", + &shader::set_uniform, "set_uniform_mat4", + &shader::set_uniform, "set_uniform_vec4", + &shader::set_uniform, "set_uniform_texture", + &shader::set_uniform - ); + ); + ns["shader_type"] = ns.create_named( + "shader_type", "fragment", shader::code_type::fragment, "vertex", + shader::code_type::vertex, "geometry", shader::code_type::geometry, + "compute", shader::code_type::compute); - ns["shader_type"] = ns.create_named("shader_type" - ,"fragment",shader::code_type::fragment - ,"vertex",shader::code_type::vertex - ,"geometry",shader::code_type::geometry - ,"compute",shader::code_type::compute); + // new_enum( -// new_enum( + // ns.new_usertype("render_pass" + // ,"submit",&render_pass::submit + // ); + ns.new_usertype( + "renderer", sol::call_constructor, + sol::constructors(), "update", + &renderer::update, "ready", sol::readonly_property(&renderer::ready), + "change_count", sol::readonly_property(&renderer::change_count), + "release", &renderer::release, "draw", &renderer::draw); -// ns.new_usertype("render_pass" -// ,"submit",&render_pass::submit -// ); + ns.new_usertype( + "framebuffer", sol::call_constructor, + sol::constructors(), "create", &framebuffer::create, + "bind", &framebuffer::bind, "unbind", &framebuffer::unbind, "blit", + &framebuffer::blit); + ns.new_usertype("texture", sol::call_constructor, + sol::constructors(), "create", + &texture::create, "update", &texture::update, + "bind", &texture::bind, "unbind", &texture::unbind, + "native_handle", &texture::native_handle); - ns.new_usertype("renderer" - ,sol::call_constructor,sol::constructors() - ,"update",&renderer::update - ,"ready",sol::readonly_property(&renderer::ready) - ,"change_count",sol::readonly_property(&renderer::change_count) - ,"release",&renderer::release - ,"draw",&renderer::draw - ); - - ns.new_usertype("framebuffer" - ,sol::call_constructor,sol::constructors() - ,"create",&framebuffer::create - ,"bind",&framebuffer::bind - ,"unbind",&framebuffer::unbind - ,"blit",&framebuffer::blit); - - ns.new_usertype("texture" - ,sol::call_constructor,sol::constructors() - ,"create",&texture::create - ,"update",&texture::update - ,"bind",&texture::bind - ,"unbind",&texture::unbind - ,"native_handle",&texture::native_handle - ); - - ns.new_usertype("context" - ,sol::call_constructor,sol::constructors() - ,"clear",&context::clear - ,"clearcolor",sol::property(&context::clearcolor,&context::set_clearcolor) - ,"set_viewport",&context::set_viewport - ,"get_error",&context::get_error - ); + ns.new_usertype( + "context", sol::call_constructor, sol::constructors(), + "clear", &context::clear, "clearcolor", + sol::property(&context::clearcolor, &context::set_clearcolor), + "set_viewport", &context::set_viewport, "get_error", + &context::get_error); } PW_REGISTER_LUA(visual) -} +} // namespace pw diff --git a/src/core/include/pw/core/geometry.hpp b/src/core/include/pw/core/geometry.hpp index cf56fb0..eafacfb 100644 --- a/src/core/include/pw/core/geometry.hpp +++ b/src/core/include/pw/core/geometry.hpp @@ -31,6 +31,30 @@ namespace pw { +struct geometry { + + enum struct topology_type { + point_list, + line_list, + line_strip, + triange_list, + triangle_strip, + triangle_fan, + line_list_with_adjacency, + line_strip_with_adjacency, + triangle_list_with_adjacency, + triangle_strip_with_adjacency, + patch_list + }; + + std::vector> vertices{}; + std::vector indices{}; + topology_type topology{}; +}; + +} // namespace pw + +#if 0 /* * NOTE this needs to be rewritten to take into account for *any* kind of * geometry Some ideas are drafted down there to separate out the attribute @@ -109,7 +133,6 @@ struct attribute final { }; - -} // namespace pw +#endif #endif diff --git a/src/core/include/pw/core/matrix.hpp b/src/core/include/pw/core/matrix.hpp index 3e84e7f..38e91f4 100644 --- a/src/core/include/pw/core/matrix.hpp +++ b/src/core/include/pw/core/matrix.hpp @@ -69,7 +69,9 @@ struct matrix final { // template auto get(this auto&& self) -> decltype(auto) { static_assert(idx < Rows, "Out of bounds access to a member."); - return std::forward_like(self.m_[idx]); + // TODO: use forward_like when clang is catching up + // return std::forward_like(self.m_[idx]); + return std::forward(self).m_[idx]; } constexpr auto diagonal() const noexcept -> diag_type { diff --git a/src/core/include/pw/core/mesh.hpp b/src/core/include/pw/core/mesh.hpp new file mode 100644 index 0000000..8559dfc --- /dev/null +++ b/src/core/include/pw/core/mesh.hpp @@ -0,0 +1,32 @@ +/* + * Copyright (c) 1999-2021 Hartmut Seichter + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ +#ifndef PW_CORE_MESH_HPP +#define PW_CORE_MESH_HPP + +#include + +namespace pw { + +} // namespace pw + +#endif diff --git a/src/core/include/pw/core/serialize.hpp b/src/core/include/pw/core/serialize.hpp index a47f790..f3ac050 100644 --- a/src/core/include/pw/core/serialize.hpp +++ b/src/core/include/pw/core/serialize.hpp @@ -65,9 +65,7 @@ struct serialize { } constexpr static std::string to_string(const color& v) { - std::stringstream ss; - ss << to_string(v.rgba); - return ss.str(); + return to_string(v.rgba); } }; diff --git a/src/core/include/pw/core/vector.hpp b/src/core/include/pw/core/vector.hpp index 2949e06..79f4acb 100644 --- a/src/core/include/pw/core/vector.hpp +++ b/src/core/include/pw/core/vector.hpp @@ -91,8 +91,8 @@ template struct vector final { } template - constexpr auto slice(std::integer_sequence,T offset = T{0}) const noexcept - -> vector { + constexpr auto slice(std::integer_sequence, T offset = T{0}) + const noexcept -> vector { return {{Scalar{v_[indices + offset]}...}}; } @@ -279,7 +279,9 @@ template struct vector final { // template auto get(this auto&& self) -> decltype(auto) { static_assert(idx < N, "Out of bounds access to a member."); - return std::forward_like(self.v_[idx]); + // TODO: use forward_like when clang is catching up + // return std::forward_like(self.v_[idx]); + return std::forward(self).v_[idx]; } // diff --git a/src/core/tests/pwcore_test_matrix.cpp b/src/core/tests/pwcore_test_matrix.cpp index b7d5161..2a89022 100644 --- a/src/core/tests/pwcore_test_matrix.cpp +++ b/src/core/tests/pwcore_test_matrix.cpp @@ -63,6 +63,6 @@ auto main() -> int { << pw::serialize::to_string(m33) << "\n to \n" << pw::serialize::to_string(m22_slice); - // octave style output + // octave/matlab style output std::print("\nm33=[{}]\n", m33); } diff --git a/src/core/tests/pwcore_test_mesh.cpp b/src/core/tests/pwcore_test_mesh.cpp index 8008541..b48acbe 100644 --- a/src/core/tests/pwcore_test_mesh.cpp +++ b/src/core/tests/pwcore_test_mesh.cpp @@ -1,46 +1,49 @@ +#include +#include +#include #include #include #include -// #include -#include #include #include #include namespace pw { -struct geometry_n { - enum struct topology_type { - point_list, - line_list, - line_strip, - triange_list, - triangle_strip, - triangle_fan, - line_list_with_adjacency, - line_strip_with_adjacency, - triangle_list_with_adjacency, - triangle_strip_with_adjacency, - patch_list +struct attribute final { + using mask_type = std::vector; + using int8_type = std::vector; + using int32_type = std::vector; + using float_type = std::vector; + using vector2_type = std::vector>; + using vector3_type = std::vector>; + using vector4_type = std::vector>; + + using attribute_data = + std::variant; + + enum attribute_type { + normals, + texture_coordinates, }; - std::vector> vertices{}; - std::vector indices{}; + using data_type = std::tuple; }; struct mesh_n { - geometry_n geometry{}; + geometry data{}; }; } // namespace pw auto main() -> int { - auto geom = pw::geometry_n{.vertices = {{1.2f, 2.4f, 4.8f}, - {2.4f, 1.2f, 4.8f}, - {1.2f, 4.8f, 2.4f}}, - .indices = {0, 1, 2}}; + auto geom = pw::geometry{.vertices = {{1.2f, 2.4f, 4.8f}, + {2.4f, 1.2f, 4.8f}, + {1.2f, 4.8f, 2.4f}}, + .indices = {0, 1, 2}}; for (auto i : geom.indices) { std::print("({}) ", pw::serialize::to_string(geom.vertices[i])); diff --git a/src/core/tests/pwcore_test_vector.cpp b/src/core/tests/pwcore_test_vector.cpp index c577c42..01c588d 100644 --- a/src/core/tests/pwcore_test_vector.cpp +++ b/src/core/tests/pwcore_test_vector.cpp @@ -69,7 +69,7 @@ auto main() -> int { std::print("y: {}\n", vec3f.get<1>()); std::print("tuple size {}\n", std::tuple_size_v); - auto& [x, y, z] = vec3f; + const auto& [x, y, z] = vec3f; std::print("x:{} y:{} z:{}\n", x, y, z); auto min_ab{vec3f.min(pw::vector{1.3f, 0.9f, 2.3f})};