diff --git a/src/binding/src/script_core.cpp b/src/binding/src/script_core.cpp index 5dfc151..8a355ad 100644 --- a/src/binding/src/script_core.cpp +++ b/src/binding/src/script_core.cpp @@ -103,18 +103,17 @@ void register_core_function(sol::state& lua,sol::table& ns) "reset",&time::reset ); - auto mesh_type = ns.new_usertype("mesh" - , sol::constructors() - , "topology", sol::property(&mesh::topology,&mesh::set_topology) - , "vertices", sol::property(&mesh::vertices,&mesh::set_vertices) - , "indices", sol::property(&mesh::indices,&mesh::set_indices) + auto geoom_type = ns.new_usertype("geometry" + , sol::constructors() + , "topology", sol::property(&geometry::topology,&geometry::set_topology) + , "vertices", sol::property(&geometry::vertices,&geometry::set_vertices) + , "indices", sol::property(&geometry::indices,&geometry::set_indices) ); - - mesh_type.new_enum("topology" - , "points", mesh::topology_type::points - , "lines", mesh::topology_type::lines - , "line_strip", mesh::topology_type::line_strip - ); + geoom_type.new_enum("topology" + , "points", geometry::topology_type::points + , "lines", geometry::topology_type::lines + , "line_strip", geometry::topology_type::line_strip + ); ns.new_usertype("image" ,"create",&image::create diff --git a/src/core/include/pw/core/mesh.hpp b/src/core/include/pw/core/mesh.hpp index 2dc719d..5fe6ea2 100644 --- a/src/core/include/pw/core/mesh.hpp +++ b/src/core/include/pw/core/mesh.hpp @@ -30,10 +30,20 @@ namespace pw { -class mesh { +class geometry { public: - using index_t = unsigned int ; + enum class topology_type { + triangles, + triangle_strip, + triangle_fan, + quads, + lines, + line_strip, + points + }; + + using index_t = size_t; using valuearray_t = std::vector ; using indexarray_t = std::vector ; @@ -41,17 +51,7 @@ public: using vertex2array_t = std::vector ; using vertex3array_t = std::vector ; - enum topology_type { - triangles, - triangle_strip, - triangle_fan, - quads, - lines, - line_strip, - points - }; - - mesh(); + geometry() = default; void set_indices(const indexarray_t& v) { _indices = v; } const indexarray_t& indices() const { return _indices; } @@ -59,11 +59,11 @@ public: void set_vertices(const vertex3array_t& v); const vertex3array_t& vertices() const { return _vertices; } - void set_normals(const mesh::vertex3array_t &v); - const mesh::vertex3array_t& normals() const; + void set_normals(const geometry::vertex3array_t &v); + const geometry::vertex3array_t& normals() const; - void set_topology(topology_type t) { _topology = t; } - topology_type topology() { return _topology; } + void set_topology(topology_type t) { _topology = t; } + topology_type topology() { return _topology; } void apply(const matrix4x4& m); @@ -77,11 +77,10 @@ public: protected: - - topology_type _topology; - + topology_type _topology = topology_type::triangles; indexarray_t _indices; //!< indices according to topology type - vertex3array_t _vertices; //!< geometry data + + vertex3array_t _vertices; //!< geometry data vertex3array_t _normals; //!< normal data vertex3array_t _colors; //!< color data diff --git a/src/core/src/mesh.cpp b/src/core/src/mesh.cpp index 27598ee..137e71f 100644 --- a/src/core/src/mesh.cpp +++ b/src/core/src/mesh.cpp @@ -4,27 +4,23 @@ namespace pw { -mesh::mesh() + +void geometry::compute_normals() { -} + // assumption is that we have some array as the vertices + vertex3array_t normals; normals.resize(_vertices.size()); -void mesh::compute_normals() -{ - - // assumption is that we have some array as the vertices - vertex3array_t normals; normals.resize(_vertices.size()); - - // for indexed-faceset + // for indexed-faceset for (size_t i = 1; i < _indices.size()-1;i++) - { - // left index and right index - auto il = (i - 1 + _indices.size()) % _indices.size(); - auto ir = (i + 1 + _indices.size()) % _indices.size(); + { + // left index and right index + auto il = (i - 1 + _indices.size()) % _indices.size(); + auto ir = (i + 1 + _indices.size()) % _indices.size(); - // translate to actual indices - auto ci = _indices[ i]; - auto li = _indices[il]; - auto ri = _indices[ir]; + // translate to actual indices + auto ci = _indices[ i]; + auto li = _indices[il]; + auto ri = _indices[ir]; // calculate edges between vertices auto edgeLeft = vector3( _vertices[li] - _vertices[ci] ); @@ -33,83 +29,86 @@ void mesh::compute_normals() // calculate counter clockwise and normalize auto N = edgeRight.cross(edgeLeft).normalized(); - // NOTE that addition is ugly - normals[ci] = N + normals[ci]; - normals[li] = N + normals[li]; - normals[ri] = N + normals[ri]; - } + // NOTE that addition is ugly + normals[ci] = N + normals[ci]; + normals[li] = N + normals[li]; + normals[ri] = N + normals[ri]; + } - for (auto & n : normals) { - n.normalize(); - } + for (auto & n : normals) { + n.normalize(); + } - this->set_normals(normals); + this->set_normals(normals); - for (auto N : normals) { - debug::s() << "( " << serialize::matrix(N.transposed()) << ") "; - } + for (auto N : normals) { + debug::s() << "( " << serialize::matrix(N.transposed()) << ") "; + } - // for triangle-strip + // for triangle-strip - // for triangle-fan + // for triangle-fan - // now set back + // now set back } -void mesh::set_vertices(const mesh::vertex3array_t &v) +void geometry::set_vertices(const geometry::vertex3array_t &v) { - // first set vertices - _vertices = v; + // first set vertices + _vertices = v; - // update bounds - compute_bounds(); + // + compute_normals(); + + // update bounds + compute_bounds(); } -void mesh::set_normals(const mesh::vertex3array_t &v) +void geometry::set_normals(const geometry::vertex3array_t &v) { - _normals = v; + _normals = v; } -const mesh::vertex3array_t &mesh::normals() const +const geometry::vertex3array_t &geometry::normals() const { - return _normals; + return _normals; } -void mesh::apply(const matrix4x4 &m) +void geometry::apply(const matrix4x4 &m) { - // apply transformation to all vertices + // apply transformation to all vertices for (auto &v : _vertices) - v = vector4(m * v.homogenous()).project(); + v = vector4(m * v.homogenous()).project(); - // apply to normals - for(auto &n : _normals) - n = vector4(m * n.homogenous(0)).xyz(); + // apply to normals + for(auto &n : _normals) + n = vector4(m * n.homogenous(0)).xyz(); - // recompute bounds - compute_bounds(); + // recompute bounds + compute_bounds(); } -void mesh::compute_bounds() +void geometry::compute_bounds() { - // only do if there are vertices - if (_vertices.size()) { + // only do if there are vertices + if (_vertices.size()) { - // reset - _aabb.max = _aabb.min = _vertices.front(); + // reset + _aabb.max = _aabb.min = _vertices.front(); - // update - for (auto const & v : _vertices) { - _aabb.max.x() = std::max(v.x(),_aabb.max.x()); - _aabb.max.y() = std::max(v.y(),_aabb.max.y()); - _aabb.max.z() = std::max(v.z(),_aabb.max.z()); - _aabb.min.x() = std::min(v.x(),_aabb.min.x()); - _aabb.min.y() = std::min(v.y(),_aabb.min.y()); - _aabb.min.z() = std::min(v.z(),_aabb.min.z()); - } - } + // update + for (auto const & v : _vertices) { + _aabb.max.x() = std::max(v.x(),_aabb.max.x()); + _aabb.max.y() = std::max(v.y(),_aabb.max.y()); + _aabb.max.z() = std::max(v.z(),_aabb.max.z()); + _aabb.min.x() = std::min(v.x(),_aabb.min.x()); + _aabb.min.y() = std::min(v.y(),_aabb.min.y()); + _aabb.min.z() = std::min(v.z(),_aabb.min.z()); + } + } } } diff --git a/src/core/tests/pwcore_test_mesh.cpp b/src/core/tests/pwcore_test_mesh.cpp index d7134e4..39f9bdd 100644 --- a/src/core/tests/pwcore_test_mesh.cpp +++ b/src/core/tests/pwcore_test_mesh.cpp @@ -8,9 +8,9 @@ int main(int argc,char **argv) { - pw::mesh amesh; + pw::geometry amesh; - pw::mesh::vertex3array_t vs = { {-1,-1,0},{1,-1,0},{0,1,0} }; + pw::geometry::vertex3array_t vs = { {-1,-1,0},{1,-1,0},{0,1,0} }; amesh.set_vertices(vs); for (auto v : amesh.vertices()) { diff --git a/src/geometry/include/pw/geometry/primitives.hpp b/src/geometry/include/pw/geometry/primitives.hpp index c034ccc..33a141c 100644 --- a/src/geometry/include/pw/geometry/primitives.hpp +++ b/src/geometry/include/pw/geometry/primitives.hpp @@ -7,13 +7,13 @@ namespace pw { struct primitives { - static mesh box(real_t size_x, real_t size_y, real_t size_z); + static geometry box(real_t size_x, real_t size_y, real_t size_z); - static mesh sphere(real_t radius,int divisions_latitude,int divisions_longitude); + static geometry sphere(real_t radius,int divisions_latitude,int divisions_longitude); - static mesh cone(); + static geometry cone(); - static mesh pyramid(); + static geometry pyramid(); }; }; diff --git a/src/geometry/src/primitives.cpp b/src/geometry/src/primitives.cpp index 683260a..b55e8d0 100644 --- a/src/geometry/src/primitives.cpp +++ b/src/geometry/src/primitives.cpp @@ -2,11 +2,11 @@ namespace pw { -mesh primitives::box(real_t size_x,real_t size_y, real_t size_z) +geometry primitives::box(real_t size_x,real_t size_y, real_t size_z) { - mesh m; + geometry m; - mesh::vertex3array_t vertices; + geometry::vertex3array_t vertices; // vertices.push_back( { -size_x / 2,-size_y / 2, size_z / 2 } ); // 0 // vertices.push_back( { size_x / 2,-size_y / 2, size_z / 2 } ); // 1 @@ -38,7 +38,7 @@ mesh primitives::box(real_t size_x,real_t size_y, real_t size_z) return m; } -mesh primitives::sphere(real_t radius,int divisions_latitude,int divisions_longitude) +geometry primitives::sphere(real_t radius,int divisions_latitude,int divisions_longitude) { using std::cos; using std::sin; @@ -46,9 +46,9 @@ mesh primitives::sphere(real_t radius,int divisions_latitude,int divisions_longi const real_t _division_lat = real_t(360.0) / divisions_latitude; const real_t _division_lon = real_t(360.0) / divisions_longitude; - mesh geom; + geometry geom; - geom.set_topology(mesh::topology_type::triangle_strip); + geom.set_topology(geometry::topology_type::triangle_strip); real_t x, y, z; diff --git a/src/scripts/demos/simple_000.lua b/src/scripts/demos/simple_000.lua index 3a54ff4..9d3f706 100644 --- a/src/scripts/demos/simple_000.lua +++ b/src/scripts/demos/simple_000.lua @@ -62,7 +62,7 @@ while w:update() do print(pw.input:get().mouse_position.x,pw.input:get().mouse_position.y,w.client_size.width,w.client_size.height) end - -- print("update") +-- print("update") end --local scene = pw:scene.new() diff --git a/src/visual/include/pw/visual/vertex_array.hpp b/src/visual/include/pw/visual/vertex_array.hpp index d342181..5e4237b 100644 --- a/src/visual/include/pw/visual/vertex_array.hpp +++ b/src/visual/include/pw/visual/vertex_array.hpp @@ -8,14 +8,17 @@ namespace pw { +/** + * @brief Connects mesh data with the render backend + */ class vertex_array { public: vertex_array(); - vertex_array(const mesh& m); + vertex_array(const geometry& m); ~vertex_array(); - void create(const mesh &m); + void create(const geometry &m); void release(); void draw(); diff --git a/src/visual/src/pipeline.cpp b/src/visual/src/pipeline.cpp index 4e946b7..c8ee0e5 100644 --- a/src/visual/src/pipeline.cpp +++ b/src/visual/src/pipeline.cpp @@ -39,7 +39,7 @@ struct triangle_renderer // GLuint shader_programme = 0; shader shader_p; - mesh amesh; + geometry amesh; vertex_array amesh_renderer; time::tick_t tick; @@ -73,7 +73,7 @@ struct triangle_renderer // 2 -- 3 // geometry - mesh::vertex3array_t vertices = { + geometry::vertex3array_t vertices = { {-s, s, z_val} // 0 ,{ s, s, z_val} // 1 ,{-s, -s, z_val} // 2 @@ -81,7 +81,7 @@ struct triangle_renderer }; // topology / indices - mesh::indexarray_t indices = { 2, 1, 0, + geometry::indexarray_t indices = { 2, 1, 0, 2, 3, 1}; diff --git a/src/visual/src/vertex_array.cpp b/src/visual/src/vertex_array.cpp index 0d0511d..24afb19 100644 --- a/src/visual/src/vertex_array.cpp +++ b/src/visual/src/vertex_array.cpp @@ -32,7 +32,7 @@ struct vertex_array::impl { return GL_TRUE == glIsVertexArray(_vao); } - void create(const mesh& m) + void create(const geometry& m) { // not sure ... if (ready()) { @@ -110,7 +110,7 @@ vertex_array::vertex_array() { } -vertex_array::vertex_array(const mesh &m) +vertex_array::vertex_array(const geometry &m) { vertex_array(); _impl->create(m); @@ -125,7 +125,7 @@ bool vertex_array::ready() const return _impl->ready(); } -void vertex_array::create(const mesh &m) +void vertex_array::create(const geometry &m) { _impl->create(m); }