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,18 +103,17 @@ void register_core_function(sol::state& lua,sol::table& ns)
"reset",&time::reset
);
auto mesh_type = ns.new_usertype<mesh>("mesh"
, sol::constructors<mesh()>()
, "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>("geometry"
, sol::constructors<geometry()>()
, "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>("image"
,"create",&image::create

View file

@ -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<real_t> ;
using indexarray_t = std::vector<index_t> ;
@ -41,17 +51,7 @@ public:
using vertex2array_t = std::vector<vector2> ;
using vertex3array_t = std::vector<vector3> ;
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

View file

@ -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());
}
}
}
}

View file

@ -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()) {

View file

@ -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();
};
};

View file

@ -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;

View file

@ -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()

View file

@ -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();

View file

@ -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};

View file

@ -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);
}