Converting codebase to SOL3

Signed-off-by: Hartmut Seichter <hartmut@technotecture.com>
This commit is contained in:
Hartmut Seichter 2021-01-29 00:00:02 +01:00
parent 874c87cedf
commit 0c8748befe
5 changed files with 137 additions and 92 deletions

View file

@ -17,6 +17,7 @@
namespace sol { namespace sol {
template <> struct is_automagical<pw::matrix4x4> : std::false_type {}; template <> struct is_automagical<pw::matrix4x4> : std::false_type {};
template <> struct is_automagical<pw::vector3> : std::false_type {}; template <> struct is_automagical<pw::vector3> : std::false_type {};
template <> struct is_automagical<pw::vector2> : std::false_type {};
template <> struct is_automagical<pw::vector4> : std::false_type {}; template <> struct is_automagical<pw::vector4> : std::false_type {};
template <> struct is_automagical<pw::quaternion> : std::false_type {}; template <> struct is_automagical<pw::quaternion> : std::false_type {};
template <> struct is_automagical<pw::rectangle> : std::false_type {}; template <> struct is_automagical<pw::rectangle> : std::false_type {};
@ -24,22 +25,22 @@ template <> struct is_automagical<pw::rectangle> : std::false_type {};
namespace pw { namespace pw {
vector3 table_to_vector3(sol::table t)
rectangle from_lua_table(sol::table t)
{ {
float v[4]; return vector3(t[0],t[1],t[2]);
}
for (std::size_t i {1}; i <= t.size();i++){ template <typename elementType>
v[i-1] = t[i]; std::vector<elementType> convert_sequence(sol::table t)
{
const std::size_t sz = t.size();
std::vector<elementType> res(sz);
for (std::size_t i = 1; i <= sz; i++) {
res[i - 1] = t[i];
} }
return rectangle(v); return res;
} }
vector3 vector3_from_lua_table(sol::table t)
{
return vector3();
}
void register_core_function(sol::state& lua,sol::table& ns) void register_core_function(sol::state& lua,sol::table& ns)
@ -51,16 +52,25 @@ void register_core_function(sol::state& lua,sol::table& ns)
ns.new_usertype<matrix4x4>("matrix4x4" ns.new_usertype<matrix4x4>("matrix4x4"
, sol::constructors<matrix4x4()>() , sol::call_constructor,sol::constructors<matrix4x4()>()
, "row",&matrix4x4::row , "row",&matrix4x4::row
, "column",&matrix4x4::column , "column",&matrix4x4::column
, "set_identity",&matrix4x4::set_identity , "set_identity",&matrix4x4::set_identity
, "inverse",&matrix4x4::inverse , "inverse",&matrix4x4::inverse
, "identity", &matrix4x4::identity , "identity",sol::readonly_property(matrix4x4::identity)
// , "set",[](sol::this_state s,sol::table t) { for (int i = 0;i < t.size();i++) s.Ldata[i] = t[i-1]; })
// , "get",[]() ->sol::table { return sol::table(); }
); );
ns.new_usertype<vector2>("vector2"
,sol::call_constructor,sol::constructors<vector2(),vector2(Scalar,Scalar)>()
,"x", sol::property(sol::resolve<const vector2::value_type&() const>(&vector2::x), [](vector2& v,vector2::value_type val){ v.x() = val;})
,"y", sol::property(sol::resolve<const vector2::value_type&() const>(&vector2::y), [](vector2& v,vector2::value_type val){ v.y() = val;})
);
ns.new_usertype<vector3>("vector3" ns.new_usertype<vector3>("vector3"
,sol::constructors<vector3(),vector3(Scalar,Scalar,Scalar)>() ,sol::call_constructor,sol::constructors<vector3(),vector3(Scalar,Scalar,Scalar)>()
,"x", sol::property(sol::resolve<const vector3::value_type&() const>(&vector3::x), [](vector3& v,vector3::value_type val){ v.x() = val;}) ,"x", sol::property(sol::resolve<const vector3::value_type&() const>(&vector3::x), [](vector3& v,vector3::value_type val){ v.x() = val;})
,"y", sol::property(sol::resolve<const vector3::value_type&() const>(&vector3::y), [](vector3& v,vector3::value_type val){ v.y() = val;}) ,"y", sol::property(sol::resolve<const vector3::value_type&() const>(&vector3::y), [](vector3& v,vector3::value_type val){ v.y() = val;})
,"z", sol::property(sol::resolve<const vector3::value_type&() const>(&vector3::z), [](vector3& v,vector3::value_type val){ v.z() = val;}) ,"z", sol::property(sol::resolve<const vector3::value_type&() const>(&vector3::z), [](vector3& v,vector3::value_type val){ v.z() = val;})
@ -79,29 +89,29 @@ void register_core_function(sol::state& lua,sol::table& ns)
); );
ns.new_usertype<vector4>("vector4" ns.new_usertype<vector4>("vector4"
,sol::constructors<vector4(),vector4(vector4::value_type,vector4::value_type,vector4::value_type,vector4::value_type)>() ,sol::call_constructor,sol::constructors<vector4(),vector4(vector4::value_type,vector4::value_type,vector4::value_type,vector4::value_type)>()
,"x", sol::property(sol::resolve<const vector4::value_type&() const>(&vector4::x), [](vector4& v,vector4::value_type val){ v.x() = val;}) ,"x", sol::property(sol::resolve<const vector4::value_type&() const>(&vector4::x), [](vector4& v,vector4::value_type val){ v.x() = val;})
,"y", sol::property(sol::resolve<const vector4::value_type&() const>(&vector4::y), [](vector4& v,vector4::value_type val){ v.y() = val;}) ,"y", sol::property(sol::resolve<const vector4::value_type&() const>(&vector4::y), [](vector4& v,vector4::value_type val){ v.y() = val;})
,"z", sol::property(sol::resolve<const vector4::value_type&() const>(&vector4::z), [](vector4& v,vector4::value_type val){ v.z() = val;}) ,"z", sol::property(sol::resolve<const vector4::value_type&() const>(&vector4::z), [](vector4& v,vector4::value_type val){ v.z() = val;})
,"w", sol::property(sol::resolve<const vector4::value_type&() const>(&vector4::w), [](vector4& v,vector4::value_type val){ v.w() = val;}) ,"w", sol::property(sol::resolve<const vector4::value_type&() const>(&vector4::w), [](vector4& v,vector4::value_type val){ v.w() = val;})
,"project",&vector4::project ,"project",&vector4::project
); );
ns.new_usertype<quaternion>("quaternion" ns.new_usertype<quaternion>("quaternion"
,sol::constructors<quaternion(), quaternion(quaternion::value_type,quaternion::value_type,quaternion::value_type,quaternion::value_type)>() ,sol::constructors<quaternion(), quaternion(quaternion::value_type,quaternion::value_type,quaternion::value_type,quaternion::value_type)>()
,"x", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::x), [](quaternion& v,quaternion::value_type val){ v.x() = val;}) ,"x", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::x), [](quaternion& v,quaternion::value_type val){ v.x() = val;})
,"y", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::y), [](quaternion& v,quaternion::value_type val){ v.y() = val;}) ,"y", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::y), [](quaternion& v,quaternion::value_type val){ v.y() = val;})
,"z", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::z), [](quaternion& v,quaternion::value_type val){ v.z() = val;}) ,"z", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::z), [](quaternion& v,quaternion::value_type val){ v.z() = val;})
,"w", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::w), [](quaternion& v,quaternion::value_type val){ v.w() = val;}) ,"w", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::w), [](quaternion& v,quaternion::value_type val){ v.w() = val;})
,"identity",sol::readonly_property(&quaternion::identity) ,"identity",sol::readonly_property(&quaternion::identity)
,"dot",&quaternion::dot ,"dot",&quaternion::dot
,"inverse",sol::readonly_property(&quaternion::inverse) ,"inverse",sol::readonly_property(&quaternion::inverse)
,"normalized",&quaternion::normalized ,"normalized",&quaternion::normalized
,"lerp",&quaternion::lerp ,"lerp",&quaternion::lerp
,"slerp",&quaternion::slerp ,"slerp",&quaternion::slerp
,"matrix",&quaternion::to_matrix ,"matrix",&quaternion::to_matrix
); );
ns.new_usertype<axisangle> ns.new_usertype<axisangle>
("axisangle", ("axisangle",
@ -114,18 +124,18 @@ void register_core_function(sol::state& lua,sol::table& ns)
ns.new_usertype<size>("size" ns.new_usertype<size>("size"
, sol::call_constructor,sol::constructors<size(),size(Scalar,Scalar)>() , sol::call_constructor, sol::constructors<size(),size(Scalar,Scalar)>()
, "width",&size::width , "width",&size::width
, "height",&size::height , "height",&size::height
, "cast_to_float",&size::cast<float> , "cast_to_float",&size::cast<float>
); );
ns.new_usertype<sizef>("sizef" ns.new_usertype<sizef>("sizef"
, sol::constructors<sizef(),sizef(Scalar,Scalar)>() , sol::call_constructor, sol::constructors<sizef(),sizef(Scalar,Scalar)>()
, "width",&sizef::width , "width",&sizef::width
, "height",&sizef::height , "height",&sizef::height
, "cast_to_int",&sizef::cast<int> , "cast_to_int",&sizef::cast<int>
); );
ns.new_usertype<point>("point", ns.new_usertype<point>("point",
sol::constructors<point(),point(Scalar,Scalar)>(), sol::constructors<point(),point(Scalar,Scalar)>(),
@ -151,14 +161,16 @@ void register_core_function(sol::state& lua,sol::table& ns)
ns.new_usertype<geometry>("geometry" ns.new_usertype<geometry>("geometry"
, sol::constructors<geometry(),geometry(geometry::primitive_topology_type,vector3_array,geometry::indices_t)>() , sol::constructors<geometry(),geometry(geometry::primitive_topology_type,vector3_array,geometry::indices_t)>()
, "primitive_topology", sol::property(&geometry::primitive_topology,&geometry::set_primitive_topology) , "primitive_topology", sol::property(&geometry::primitive_topology,&geometry::set_primitive_topology)
, "vertices", sol::property(&geometry::vertices,&geometry::set_vertices) , "vertices", sol::property(&geometry::ref_vertices,&geometry::ref_vertices)
, "indices", sol::property(&geometry::indices,&geometry::set_indices) , "indices", sol::property(&geometry::ref_indices,&geometry::ref_indices)
, "add_texture_coordinates",&geometry::add_texture_coordinates
// , "texture_coordinates",&geometry::texture_coordinates
, "compute_normals", &geometry::compute_normals); , "compute_normals", &geometry::compute_normals);
ns.new_enum<false>("primitive_topology_type" ns.new_enum<false>("primitive_topology_type"
,"point_list", geometry::primitive_topology_type::point_list ,"point_list", geometry::primitive_topology_type::point_list
,"line_list", geometry::primitive_topology_type::line_list ,"line_list", geometry::primitive_topology_type::line_list
,"triangle_list", geometry::primitive_topology_type::triangle_list); ,"triangle_list", geometry::primitive_topology_type::triangle_list);
ns.new_usertype<matrix_transform<real_t>>("matrixtransform" ns.new_usertype<matrix_transform<real_t>>("matrixtransform"
@ -166,11 +178,11 @@ void register_core_function(sol::state& lua,sol::table& ns)
,"look_at",&matrix_transform<real_t>::look_at ,"look_at",&matrix_transform<real_t>::look_at
,"perspective_projection",&matrix_transform<real_t>::perspective_projection); ,"perspective_projection",&matrix_transform<real_t>::perspective_projection);
// SOL3 // SOL3
// geoom_type["type"] = lua.create_table_with( // geoom_type["type"] = lua.create_table_with(
// "points", geometry::topology_type::points // "points", geometry::topology_type::points
// , "lines", geometry::topology_type::lines // , "lines", geometry::topology_type::lines
// , "line_strip", geometry::topology_type::line_strip // , "line_strip", geometry::topology_type::line_strip
// ); // );
@ -182,21 +194,21 @@ void register_core_function(sol::state& lua,sol::table& ns)
,"change_count",sol::property(&image::change_count,&image::set_change_count) ,"change_count",sol::property(&image::change_count,&image::set_change_count)
); );
image_type.create_named("pixel_layout" ns.create_named("pixel_layout"
,"rgb8", image::RGB8 ,"rgb8", image::RGB8
,"rgb32", image::RGBA8 ,"rgb32", image::RGBA8
,"gray", image::LUM ,"gray", image::LUM
); );
// image_type["layout"] = lua.create_table_with( // image_type["layout"] = lua.create_table_with(
// "rgb8", image::RGB8 // "rgb8", image::RGB8
// ,"rgb32", image::RGBA8 // ,"rgb32", image::RGBA8
// ,"gray", image::LUM); // ,"gray", image::LUM);
ns.new_usertype<rectangle>("rectangle" ns.new_usertype<rectangle>("rectangle"
,sol::constructors<rectangle(),rectangle(const point_<float>&,const size_<float>&)>() ,sol::constructors<rectangle(),rectangle(const point_<float>&,const size_<float>&)>()
); );
auto mathf_table = ns.create_named("mathf"); auto mathf_table = ns.create_named("mathf");
mathf_table.set_function("ping_pong",ping_pong<real_t>); mathf_table.set_function("ping_pong",ping_pong<real_t>);

View file

@ -13,7 +13,7 @@
namespace pw { namespace pw {
void register_visual_function(sol::state&,sol::table &ns) void register_visual_function(sol::state& lua,sol::table &ns)
{ {
ns.new_usertype<pipeline>("pipeline" ns.new_usertype<pipeline>("pipeline"
@ -36,16 +36,21 @@ void register_visual_function(sol::state&,sol::table &ns)
,"set_uniform_vec4",&shader::set_uniform<vector4&> ,"set_uniform_vec4",&shader::set_uniform<vector4&>
,"set_uniform_texture",&shader::set_uniform<texture&> ,"set_uniform_texture",&shader::set_uniform<texture&>
).new_enum<false>("shader_type" );
ns["shader_type"] = ns.create_named("shader_type"
,"fragment",shader::code_type::fragment ,"fragment",shader::code_type::fragment
,"vertex",shader::code_type::vertex ,"vertex",shader::code_type::vertex
,"geometry",shader::code_type::geometry ,"geometry",shader::code_type::geometry
,"compute",shader::code_type::compute); ,"compute",shader::code_type::compute);
// new_enum<false>(
ns.new_usertype<render_pass>("render_pass" // ns.new_usertype<render_pass>("render_pass"
,"submit",&render_pass::submit // ,"submit",&render_pass::submit
); // );
ns.new_usertype<renderer>("renderer" ns.new_usertype<renderer>("renderer"

View file

@ -65,12 +65,20 @@ public:
void set_vertices(vector3_array v); void set_vertices(vector3_array v);
const vector3_array& vertices() const { return _vertices; } const vector3_array& vertices() const { return _vertices; }
vector3_array& ref_vertices() { return _vertices;}
void set_indices(indices_t v); void set_indices(indices_t v);
const indices_t& indices() const; const indices_t& indices() const;
indices_t& ref_indices() { return _indices; }
void set_normals(vector3_array v); void set_normals(vector3_array v);
const vector3_array& normals() const; const vector3_array& normals() const;
void add_texture_coordinates(vector2_array v);
const std::vector<vector2_array>& texture_coordinates() const { return _texture_coords;}
void transform(const matrix4x4& m); void transform(const matrix4x4& m);
void compute_normals(); void compute_normals();
@ -90,7 +98,7 @@ protected:
vector3_array _tangents; //!< tangent data vector3_array _tangents; //!< tangent data
vector3_array _bitangents; //!< bitangent vector3_array _bitangents; //!< bitangent
std::vector<vector3_array> _texture_coords; //! texture coordinates std::vector<vector2_array> _texture_coords; //! texture coordinates
uint64_t _change_count { 0 }; uint64_t _change_count { 0 };
}; };

View file

@ -102,6 +102,13 @@ void geometry::set_normals(vector3_array v)
const vector3_array &geometry::normals() const { return _normals; } const vector3_array &geometry::normals() const { return _normals; }
void geometry::add_texture_coordinates(vector2_array v)
{
_texture_coords.push_back(v);
}
//void geometry::set_vertices(const geometry::vertex3array_t &v) //void geometry::set_vertices(const geometry::vertex3array_t &v)
//{ //{
// // first set vertices // // first set vertices

View file

@ -5,8 +5,8 @@
-- we need everything -- we need everything
pw.script:load_all() pw.script:load_all()
print(pw.path.get().executable_path) print("executable path:",pw.path.get().executable_path)
print(pw.path.get().resource_path) print("resource path:",pw.path.get().resource_path)
local img = pw.image.new() local img = pw.image.new()
if not img:create(pw.size(64,64),pw.pixel_layout.rgb8) then if not img:create(pw.size(64,64),pw.pixel_layout.rgb8) then
@ -31,20 +31,27 @@ w.position = pw.point.new(100,100)
-- create a new geometry -- create a new geometry
local g = pw.geometry:new() local g = pw.geometry:new()
-- create texture coordinates
local tc = { pw.vector2(1.0,1.0),
pw.vector2(1.0,0.0),
pw.vector2(0.0,1.0),
pw.vector2(0.0,0.0) }
g:add_texture_coordinates(tc)
--print(g)
g.primitive_topology = pw.primitive_topology_type.triangle_list -- meh g.primitive_topology = pw.primitive_topology_type.triangle_list -- meh
z = -5.0 z = -5.0
s = 1 s = 1
-- geometry can only build with indexed facesets -- geometry can only build with indexed facesets
g.vertices:add(pw.vector3:new(-s, s, z)) -- 0 g.vertices:add(pw.vector3(-s, s, z)) -- 0
g.vertices:add(pw.vector3:new(-s,-s, z)) -- 1 g.vertices:add(pw.vector3(-s,-s, z)) -- 1
g.vertices:add(pw.vector3:new( s,-s, z)) -- 2 g.vertices:add(pw.vector3( s,-s, z)) -- 2
g.vertices:add(pw.vector3:new( s, s, z)) -- 3 g.vertices:add(pw.vector3( s, s, z)) -- 3
-- 0 --- 3
-- | \ |
-- 1 --- 2
g.indices:add(0) g.indices:add(0)
g.indices:add(1) g.indices:add(1)
@ -55,9 +62,25 @@ g.indices:add(3)
g.indices:add(0) g.indices:add(0)
local mm = pw.matrix4x4:new() print(#g.vertices,g.vertices,g.vertices[1].x,g.vertices[1].y,g.vertices[1].z)
local mv = pw.matrix4x4:new() --print(g.vertices[1])
local mp = pw.matrix4x4:new()
local v3 = pw.vector3()
print(g.indices,#g.indices)
print(g.vertices,#g.vertices)
--os.exit()
-- 0 --- 3
-- | \ |
-- 1 --- 2
local mm = pw.matrix4x4.identity
local mv = pw.matrix4x4()
local mp = pw.matrix4x4()
local s = pw.shader:new() local s = pw.shader:new()
@ -82,7 +105,7 @@ uniform sampler2D tex_color;
out vec4 frag_color; out vec4 frag_color;
void main() { void main() {
frag_color = texture(tex_color,vec2(1.0,1.0)); frag_color = color * texture(tex_color,vec2(1.0,1.0));
} }
]]) ]])
@ -100,9 +123,9 @@ if not renderer:create(g) then
end end
-- camera position -- camera position
local cam_pos = pw.vector3:new(0,0,0) local cam_pos = pw.vector3(0,0,0)
local model_pos = pw.vector3:new(0,0,0) local model_pos = pw.vector3(0,0,0)
local ctx = pw.context:new() local ctx = pw.context:new()
@ -119,9 +142,6 @@ end
-- setup a lua callback function as callback -- setup a lua callback function as callback
w.on_update = function(self) w.on_update = function(self)
-- set identity on model matrix
mm:set_identity()
-- set view matrix with look_at - view matrix is moving the world - hence inverse! -- set view matrix with look_at - view matrix is moving the world - hence inverse!
mv = pw.matrixtransform.look_at(cam_pos,cam_pos + pw.vector3.forward(),pw.vector3.up()):inverse() mv = pw.matrixtransform.look_at(cam_pos,cam_pos + pw.vector3.forward(),pw.vector3.up()):inverse()
@ -135,21 +155,14 @@ w.on_update = function(self)
local color_red = pw.mathf.ping_pong(pw.time.now,1.0) local color_red = pw.mathf.ping_pong(pw.time.now,1.0)
local color_green = pw.mathf.ping_pong(pw.time.now + 1,1.0) local color_green = pw.mathf.ping_pong(pw.time.now + 1,1.0)
local color_blue = 1.0 - pw.mathf.ping_pong(pw.time.now,1.0) local color_blue = 1.0 - pw.mathf.ping_pong(pw.time.now,1.0)
local cl = pw.vector4:new( color_red, color_green, color_blue, 1.0 ) local cl = pw.vector4( color_red, color_green, color_blue, 1.0 )
img:generate_noise() img:generate_noise()
tx:update(img) tx:update(img)
-- print(img.change_count)
-- bind the shader -- bind the shader
s:use() s:use()
-- update the uniforms, currently the slow path -- update the uniforms, currently the slow path
s:set_uniform_mat4("model",mm) s:set_uniform_mat4("model",mm)