experimenting around with scene and rendering components

This commit is contained in:
Hartmut Seichter 2020-12-01 23:22:19 +01:00
parent 8915080b64
commit 0cd3c99119
10 changed files with 117 additions and 117 deletions

View file

@ -103,17 +103,16 @@ void register_core_function(sol::state& lua,sol::table& ns)
"reset",&time::reset "reset",&time::reset
); );
auto mesh_type = ns.new_usertype<mesh>("mesh" auto geoom_type = ns.new_usertype<geometry>("geometry"
, sol::constructors<mesh()>() , sol::constructors<geometry()>()
, "topology", sol::property(&mesh::topology,&mesh::set_topology) , "topology", sol::property(&geometry::topology,&geometry::set_topology)
, "vertices", sol::property(&mesh::vertices,&mesh::set_vertices) , "vertices", sol::property(&geometry::vertices,&geometry::set_vertices)
, "indices", sol::property(&mesh::indices,&mesh::set_indices) , "indices", sol::property(&geometry::indices,&geometry::set_indices)
); );
geoom_type.new_enum("topology"
mesh_type.new_enum("topology" , "points", geometry::topology_type::points
, "points", mesh::topology_type::points , "lines", geometry::topology_type::lines
, "lines", mesh::topology_type::lines , "line_strip", geometry::topology_type::line_strip
, "line_strip", mesh::topology_type::line_strip
); );
ns.new_usertype<image>("image" ns.new_usertype<image>("image"

View file

@ -30,18 +30,10 @@
namespace pw { namespace pw {
class mesh { class geometry {
public: public:
using index_t = unsigned int ; enum class topology_type {
using valuearray_t = std::vector<real_t> ;
using indexarray_t = std::vector<index_t> ;
using vertex2array_t = std::vector<vector2> ;
using vertex3array_t = std::vector<vector3> ;
enum topology_type {
triangles, triangles,
triangle_strip, triangle_strip,
triangle_fan, triangle_fan,
@ -51,7 +43,15 @@ public:
points points
}; };
mesh(); using index_t = size_t;
using valuearray_t = std::vector<real_t> ;
using indexarray_t = std::vector<index_t> ;
using vertex2array_t = std::vector<vector2> ;
using vertex3array_t = std::vector<vector3> ;
geometry() = default;
void set_indices(const indexarray_t& v) { _indices = v; } void set_indices(const indexarray_t& v) { _indices = v; }
const indexarray_t& indices() const { return _indices; } const indexarray_t& indices() const { return _indices; }
@ -59,8 +59,8 @@ public:
void set_vertices(const vertex3array_t& v); void set_vertices(const vertex3array_t& v);
const vertex3array_t& vertices() const { return _vertices; } const vertex3array_t& vertices() const { return _vertices; }
void set_normals(const mesh::vertex3array_t &v); void set_normals(const geometry::vertex3array_t &v);
const mesh::vertex3array_t& normals() const; const geometry::vertex3array_t& normals() const;
void set_topology(topology_type t) { _topology = t; } void set_topology(topology_type t) { _topology = t; }
topology_type topology() { return _topology; } topology_type topology() { return _topology; }
@ -77,10 +77,9 @@ public:
protected: protected:
topology_type _topology = topology_type::triangles;
topology_type _topology;
indexarray_t _indices; //!< indices according to topology type 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 _normals; //!< normal data
vertex3array_t _colors; //!< color data vertex3array_t _colors; //!< color data

View file

@ -4,13 +4,9 @@
namespace pw { namespace pw {
mesh::mesh()
{
}
void mesh::compute_normals() void geometry::compute_normals()
{ {
// assumption is that we have some array as the vertices // assumption is that we have some array as the vertices
vertex3array_t normals; normals.resize(_vertices.size()); vertex3array_t normals; normals.resize(_vertices.size());
@ -59,26 +55,29 @@ void mesh::compute_normals()
// 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 // first set vertices
_vertices = v; _vertices = v;
//
compute_normals();
// update bounds // update bounds
compute_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) for (auto &v : _vertices)
@ -92,7 +91,7 @@ void mesh::apply(const matrix4x4 &m)
compute_bounds(); compute_bounds();
} }
void mesh::compute_bounds() void geometry::compute_bounds()
{ {
// only do if there are vertices // only do if there are vertices
if (_vertices.size()) { if (_vertices.size()) {

View file

@ -8,9 +8,9 @@
int main(int argc,char **argv) { 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); amesh.set_vertices(vs);
for (auto v : amesh.vertices()) { for (auto v : amesh.vertices()) {

View file

@ -7,13 +7,13 @@ namespace pw {
struct primitives { 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();
}; };
}; };

View file

@ -2,11 +2,11 @@
namespace pw { 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 } ); // 0
// vertices.push_back( { size_x / 2,-size_y / 2, size_z / 2 } ); // 1 // 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; 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::cos;
using std::sin; 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_lat = real_t(360.0) / divisions_latitude;
const real_t _division_lon = real_t(360.0) / divisions_longitude; 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; real_t x, y, z;

View file

@ -8,14 +8,17 @@
namespace pw { namespace pw {
/**
* @brief Connects mesh data with the render backend
*/
class vertex_array { class vertex_array {
public: public:
vertex_array(); vertex_array();
vertex_array(const mesh& m); vertex_array(const geometry& m);
~vertex_array(); ~vertex_array();
void create(const mesh &m); void create(const geometry &m);
void release(); void release();
void draw(); void draw();

View file

@ -39,7 +39,7 @@ struct triangle_renderer
// GLuint shader_programme = 0; // GLuint shader_programme = 0;
shader shader_p; shader shader_p;
mesh amesh; geometry amesh;
vertex_array amesh_renderer; vertex_array amesh_renderer;
time::tick_t tick; time::tick_t tick;
@ -73,7 +73,7 @@ struct triangle_renderer
// 2 -- 3 // 2 -- 3
// geometry // geometry
mesh::vertex3array_t vertices = { geometry::vertex3array_t vertices = {
{-s, s, z_val} // 0 {-s, s, z_val} // 0
,{ s, s, z_val} // 1 ,{ s, s, z_val} // 1
,{-s, -s, z_val} // 2 ,{-s, -s, z_val} // 2
@ -81,7 +81,7 @@ struct triangle_renderer
}; };
// topology / indices // topology / indices
mesh::indexarray_t indices = { 2, 1, 0, geometry::indexarray_t indices = { 2, 1, 0,
2, 3, 1}; 2, 3, 1};

View file

@ -32,7 +32,7 @@ struct vertex_array::impl {
return GL_TRUE == glIsVertexArray(_vao); return GL_TRUE == glIsVertexArray(_vao);
} }
void create(const mesh& m) void create(const geometry& m)
{ {
// not sure ... // not sure ...
if (ready()) { 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(); vertex_array();
_impl->create(m); _impl->create(m);
@ -125,7 +125,7 @@ bool vertex_array::ready() const
return _impl->ready(); return _impl->ready();
} }
void vertex_array::create(const mesh &m) void vertex_array::create(const geometry &m)
{ {
_impl->create(m); _impl->create(m);
} }