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

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