major overhaul for Lua API and additions for using texture coordinates

Signed-off-by: Hartmut Seichter <hartmut@technotecture.com>
This commit is contained in:
Hartmut Seichter 2021-02-02 23:23:40 +01:00
parent 939195b851
commit 749bb67c6c
10 changed files with 242 additions and 142 deletions

View file

@ -31,7 +31,7 @@ vector3 table_to_vector3(sol::table t)
} }
template <typename elementType> template <typename elementType>
std::vector<elementType> convert_sequence(sol::table t) std::vector<elementType> convert_sequence(sol::state& lua,sol::table t)
{ {
const std::size_t sz = t.size(); const std::size_t sz = t.size();
std::vector<elementType> res(sz); std::vector<elementType> res(sz);
@ -46,46 +46,54 @@ std::vector<elementType> convert_sequence(sol::table t)
void register_core_function(sol::state& lua,sol::table& ns) void register_core_function(sol::state& lua,sol::table& ns)
{ {
typedef real_t Scalar; ns.set("pi",pw::pi<real_t>());
ns.set("pi",pw::pi<Scalar>());
ns.new_usertype<matrix4x4>("matrix4x4" ns.new_usertype<matrix4x4>("matrix4x4",
, sol::call_constructor,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 "inverse",sol::readonly_property(&matrix4x4::inverse),
, "inverse",&matrix4x4::inverse "identity",sol::readonly_property(matrix4x4::identity),
, "identity",sol::readonly_property(matrix4x4::identity) sol::meta_function::multiplication,[](const matrix4x4& a,const vector4& b) { return vector4(a * b); }
// , "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" ns.new_usertype<vector2>("vector2",
,sol::call_constructor,sol::constructors<vector2(),vector2(Scalar,Scalar)>() sol::call_constructor,sol::constructors<vector2(),vector2(real_t,real_t)>(),
,"x", sol::property(sol::resolve<const vector2::value_type&() const>(&vector2::x), [](vector2& v,vector2::value_type val){ v.x() = val;}) "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;}) "y", sol::property(sol::resolve<const vector2::value_type&() const>(&vector2::y), [](vector2& v,vector2::value_type val){ v.y() = val;}),
"dot",&vector2::dot,
sol::meta_function::addition,[](const vector2& a,const vector2& b){ return vector2(a + b); },
sol::meta_function::subtraction,[](const vector2& a,const vector2& b){ return vector2(a - b); },
"data",sol::property([](vector2& s) { return std::ref(s.data);} ),
"table",sol::property([](const vector2& s){ return sol::as_table(std::array<vector2::value_type,2>{s.x(),s.y()}); },
[](vector2& s,const sol::table& t) { s.data[0] = t[1]; s.data[1] = t[2];} )
); );
ns.new_usertype<vector3>("vector3" ns.new_usertype<vector3>("vector3"
,sol::constructors<vector3(),vector3(Scalar,Scalar,Scalar)>() ,sol::call_constructor,sol::constructors<vector3(),vector3(real_t,real_t,real_t)>()
,"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;})
,"cross",&vector3::cross ,"cross",&vector3::cross
,"transposed",&vector3::transposed ,"normalize",&vector3::normalize
,"norm",sol::readonly_property(&vector3::norm)
,"squared_norm",sol::readonly_property(&vector3::squared_norm)
,"dot",&vector3::dot
,"transposed",sol::readonly_property(&vector3::transposed)
,"normalized",sol::readonly_property(&vector3::normalized)
,"lerp",&vector3::lerp ,"lerp",&vector3::lerp
,"forward",&vector3::forward ,"forward",sol::readonly_property(&vector3::forward)
,"backward",&vector3::backward ,"backward",sol::readonly_property(&vector3::backward)
,"left",&vector3::left ,"left",sol::readonly_property(&vector3::left)
,"right",&vector3::right ,"right",sol::readonly_property(&vector3::right)
,"up",&vector3::up ,"up",sol::readonly_property(&vector3::up)
,"down",&vector3::down ,"down",sol::readonly_property(&vector3::down)
,sol::meta_function::addition,&vector3::operator+ ,sol::meta_function::addition,[](const vector3& a,const vector3& b){ return vector3(a + b); }
,sol::meta_function::subtraction,&vector3::operator- ,sol::meta_function::subtraction,[](const vector3& a,const vector3& b){ return vector3(a - b); }
,sol::meta_function::multiplication,&vector3::operator* ,"data",sol::property([](vector3& s) { return std::ref(s.data);} )
,"table",sol::property([](const vector3& s){ return sol::as_table(std::array<vector3::value_type,3>{s.x(),s.y(),s.z()}); },
[](vector3& s,const sol::table& t) { s.data[0] = t[1]; s.data[1] = t[2]; s.data[2] = t[3];} )
); );
ns.new_usertype<vector4>("vector4" ns.new_usertype<vector4>("vector4"
@ -94,50 +102,51 @@ void register_core_function(sol::state& lua,sol::table& ns)
,"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",sol::readonly_property(&vector4::project)
,sol::meta_function::multiplication,[](const matrix4x4& a,const vector4& b) { return vector4(a * b); }
); );
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::call_constructor,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",sol::readonly_property([](const quaternion& q){ return quaternion{ q.normalized() };}),
,"lerp",&quaternion::lerp "lerp",[](const quaternion& a,const quaternion& b,quaternion::value_type t) { return quaternion{ quaternion::lerp(a,b,t) }; },
,"slerp",&quaternion::slerp "slerp",[](const quaternion& a,const quaternion& b,quaternion::value_type t) { return quaternion{ quaternion::slerp(a,b,t) }; },
,"matrix",&quaternion::to_matrix "to_matrix",sol::readonly_property(&quaternion::to_matrix)
); );
ns.new_usertype<axisangle>("axisangle", ns.new_usertype<axisangle>("axisangle",
sol::constructors<axisangle(), axisangle(vector3,Scalar)>(), sol::call_constructor,sol::constructors<axisangle(), axisangle(vector3,axisangle::value_type)>(),
"axis",&axisangle::axis, "axis",sol::property(&axisangle::axis,&axisangle::axis),
"angle",&axisangle::angle, "angle",sol::property(&axisangle::angle,&axisangle::angle),
"from_matrix",&axisangle::from_matrix, "from_matrix",&axisangle::from_matrix,
"to_matrix",&axisangle::to_matrix "to_matrix",&axisangle::to_matrix
); );
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(size::value_type,size::value_type)>()
, "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::call_constructor, sol::constructors<sizef(),sizef(Scalar,Scalar)>() , sol::call_constructor, sol::constructors<sizef(),sizef(sizef::value_type,sizef::value_type)>()
, "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::call_constructor,sol::constructors<point(),point(point::value_type,point::value_type)>(),
"x",&point::x, "x",&point::x,
"y",&point::y "y",&point::y
); );
@ -157,14 +166,32 @@ void register_core_function(sol::state& lua,sol::table& ns)
"reset",&time::reset "reset",&time::reset
); );
ns.new_usertype<geometry>("geometry" ns.new_usertype<geometry>("geometry",
,sol::constructors<geometry(),geometry(geometry::primitive_topology_type,vector3_array,geometry::indices_t)>() sol::call_constructor,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::ref_vertices,&geometry::ref_vertices) "compute_normals", &geometry::compute_normals,
,"vertices", sol::property(&geometry::vertices,&geometry::set_vertices) "indices",sol::property( [](const geometry& g){ return sol::as_table(g.indices());},
,"indices", sol::property(&geometry::ref_indices,&geometry::ref_indices) [](geometry& g,sol::table t){
// ,"texture_coordinates", sol::property(&geometry::ref_texture_coordinates,&geometry::ref_texture_coordinates) std::vector<uint32_t> vals;
,"compute_normals", &geometry::compute_normals for (std::size_t i = 0; i < t.size();i++) vals.push_back(t[i+1]);
g.set_indices(vals); }),
"vertices",sol::property( [](const geometry& g){ return sol::as_table(g.vertices());},
[](geometry &g,sol::nested<vector3_array> val) {
g.set_vertices(val.value());
}
),
"normals",sol::property( [](const geometry& g){ return sol::as_table(g.vertices());},
[](geometry& g,sol::table t){
vector3_array vals;
for (std::size_t i = 0; i < t.size();i++) vals.push_back(t[i+1]);
g.set_normals(vals); }
),
"texture_coordinates",sol::property(
[](const geometry& g){ return sol::as_table(g.texture_coordinates());},
[](geometry &g,sol::nested<std::vector<vector2_array>> val)
{
g.set_texture_coordinates(val.value());
})
); );

View file

@ -30,8 +30,8 @@ namespace pw {
template <typename T> template <typename T>
struct axisangle_ { struct axisangle_ {
typedef T value_type; using value_type = T;
typedef vector3_<T> axis_type; using axis_type = vector3_<T>;
axis_type axis; axis_type axis;
T angle; T angle;
@ -41,9 +41,9 @@ struct axisangle_ {
angle(0) angle(0)
{} {}
axisangle_(const vector3_<T> &axis,const T &angle) axisangle_(vector3_<T> axis,T angle)
: axis(axis) : axis(std::move(axis))
, angle(angle) , angle(std::move(angle))
{ {
} }

View file

@ -68,17 +68,12 @@ public:
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); void set_texture_coordinates(std::vector<vector2_array> v);
const std::vector<vector2_array>& texture_coordinates() const { return _texture_coords;} const std::vector<vector2_array>& texture_coordinates() const { return _texture_coords;}
std::vector<vector2_array>& ref_texture_coordinates() { return _texture_coords; }
void transform(const matrix4x4& m); void transform(const matrix4x4& m);
void compute_normals(); void compute_normals();

View file

@ -91,16 +91,13 @@ struct matrixbase_ {
return derived().data[i]; return derived().data[i];
} }
static T dot(const Derived &a,const Derived &b) { static constexpr T dot(const Derived &a,const Derived &b)
#if 0 {
Derived r; for (size_t i = 0;i < a.size();i++) r[i] = a[i] * b[i];
return std::accumulate(std::begin(r), std::end(r), T(0));
#else
return std::inner_product(std::begin(a),std::end(a),std::begin(b),T(0)); return std::inner_product(std::begin(a),std::end(a),std::begin(b),T(0));
#endif
} }
static const Derived lerp(const Derived &a,const Derived &b,const T& t) { static constexpr Derived lerp(const Derived &a,const Derived &b,const T& t)
{
return a + (b - a) * t; return a + (b - a) * t;
} }

View file

@ -30,6 +30,8 @@ namespace pw {
template <typename T_> template <typename T_>
struct point_ { struct point_ {
using value_type = T_;
T_ x, y; T_ x, y;
point_() = default; point_() = default;

View file

@ -30,6 +30,8 @@ namespace pw {
template <typename T_> template <typename T_>
struct size_ { struct size_ {
using value_type = T_;
T_ width,height; T_ width,height;
size_() = default; size_() = default;

View file

@ -111,10 +111,10 @@ 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::set_texture_coordinates(std::vector<vector2_array> v)
void geometry::add_texture_coordinates(vector2_array v)
{ {
_texture_coords.push_back(v); _change_count++;
_texture_coords = v;
} }

View file

@ -33,53 +33,45 @@ w.title = "pixwerx - bare rendering"
w.size = pw.size(640,480) w.size = pw.size(640,480)
-- move window -- move window
w.position = pw.point.new(100,100) w.position = pw.point(100,100)
-- create a new geometry -- create a new geometry
local g = pw.geometry:new() local g = pw.geometry()
-- 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
-- create texture coordinates
g.texture_coordinates = { {
{ 1.0, 1.0 },
{ 1.0, 0.0 },
{ 0.0, 1.0 },
{ 0.0, 0.0 }
} }
z = -5.0 z = -5.0
s = 1 s = 1
-- indices
g.indices = {
0, 1, 2,
2, 3, 0
}
print(g.indices,#g.indices)
-- 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 = {
g.vertices:add(pw.vector3.new(-s,-s, z)) -- 1 { -s,-s, z },
g.vertices:add(pw.vector3.new( s,-s, z)) -- 2 { s,-s, z },
g.vertices:add(pw.vector3.new( s, s, z)) -- 3 { s, s, z }
}
--g.vertices = {
-- { -s , s , z },
--}
print(#g.vertices,g.vertices)
os.exit()
-- 0 --- 3 -- 0 --- 3
-- | \ | -- | \ |
-- 1 --- 2 -- 1 --- 2
g.indices:add(0)
g.indices:add(1)
g.indices:add(2)
g.indices:add(2)
g.indices:add(3)
g.indices:add(0)
@ -148,7 +140,7 @@ end
w.on_update = function(self) w.on_update = function(self)
-- 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
-- compute aspect ratio from canvas size -- compute aspect ratio from canvas size
aspect_ratio = self.client_size.width / self.client_size.height aspect_ratio = self.client_size.width / self.client_size.height

View file

@ -6,45 +6,114 @@
pw.script:load_all() pw.script:load_all()
-- vector3 -- vector3
local v1 = pw.vector3.new(3,2,1) local v1 = pw.vector3(3,2,1)
print("v1 ",v1.x,v1.y,v1.z) print("v1 ",v1.x,v1.y,v1.z)
-- quaternion local v2 = pw.vector3(1,2,3)
local q = pw.quaternion.new() print("v2 ",v2.x,v2.y,v2.z)
q = pw.quaternion.identity
print("q",q.x,q.y,q.z,q.w) local v_up = pw.vector3.up
qi = q.inverse print("v_up",v_up.x,v_up.y,v_up.z)
print("q.inverse",qi.x,qi.y,qi.z,qi.w)
local q2 = pw.quaternion.new(0,0,0,1) local v3 = v1 + v2
print(v1 + v2)
print("v3 ",v3.x,v3.y,v3.z)
print("v3.norm",v3.norm)
print("v3.squared_norm",v3.squared_norm)
print(v1,v1.arr,v1.data[2])
local v4 = v3
print("v4",v4)
print(v4.table)
v4.table = { 11, 22, 33 }
print("v4 ", v4.x, v4.y, v4.z)
local m4 = pw.matrix4x4.identity
v44 = m4 * v4
print("v44 ", v44.x, v44.y, v44.z)
-- quaternion
local q = pw.quaternion.identity
print("q",q.x,q.y,q.z,q.w)
qi = q.inverse
print("q.inverse",qi.x,qi.y,qi.z,qi.w)
local q2 = pw.quaternion(1,1,1,2)
local qn = q2.normalized
print("q2.normalized",qn.x,qn.y,qn.z,qn.w)
local ql = pw.quaternion.lerp(q,q2,0.5).normalized
print("q-qi lerp",ql.x,ql.y,ql.z,ql.w)
local qsl = pw.quaternion.slerp(q,q2,0.5).normalized
print("q-qi slerp",qsl.x,qsl.y,qsl.z,qsl.w)
-- bug!
-- qm = pw.quaternion.lerp(q,qi,0.5)
-- axisangle -- axisangle
local aa = pw.axisangle.new(v1,0.707) local aa = pw.axisangle(pw.vector3.up,0.707)
print("aa.axis",aa.axis.x,aa.axis.y,aa.axis.z) print("aa.axis",aa.axis.x,aa.axis.y,aa.axis.z)
print("aa.angle",aa.angle) print("aa.angle",aa.angle)
-- mesh local g = pw.geometry()
local somemesh = pw.mesh.new() g.indices = { 11, 22, 33 }
somemesh.indices:add(0)
somemesh.vertices:add(pw.vector3.new(3,4,5))
--
print(somemesh.vertices)
print(somemesh.indices)
for i=1,#somemesh.indices do -- alert! the returned table is a copy!
print(i," - ", somemesh.indices:get(i)) table.insert(g.indices,44)
print("#g.indices ",#g.indices)
-- to insert one needs to set the whole table
local temp = g.indices
temp[#temp + 1] = 44
g.indices = temp
for k,v in ipairs(g.indices) do
print("index: ",k,v)
end end
for k,v in ipairs(somemesh.indices) do g.vertices = {
print(k,v) { 11, 11, 11 },
{ 22, 22, 22 },
{ 33, 33, 33 }
}
-- now look at the vertices
for k,v in ipairs(g.vertices) do
print("vertex: ",k,v.x,v.y,v.z)
end end
for k,v in ipairs(somemesh.vertices) do
print(k,v) -- now look at the texture coordinates
g.texture_coordinates = {
{
{ 0, 0 },
{ 1, 0 },
{ 1, 1 },
{ 0, 1 }
}
}
for k,v in ipairs(g.texture_coordinates) do
for ki,vi in ipairs(v) do
print("texture_coord",k,ki,vi.x,vi.y)
end end
end

View file

@ -67,6 +67,22 @@ struct renderer::impl {
GL_STATIC_DRAW); GL_STATIC_DRAW);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
if (!m.normals().empty())
{
// normals
glBindBuffer(GL_ARRAY_BUFFER,_vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glBufferData(GL_ARRAY_BUFFER,
m.normals().size() * sizeof(vector3),
m.normals().data(),
GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
//
}
// stop binding // stop binding
glBindVertexArray(0); glBindVertexArray(0);