need to think about resource management

This commit is contained in:
Hartmut Seichter 2019-02-20 21:52:41 +01:00
parent d2de6d410f
commit dd908ead95
11 changed files with 180 additions and 23 deletions

View file

@ -12,6 +12,7 @@ set(hdrs
include/pw/core/quaternion.hpp
include/pw/core/image.hpp
include/pw/core/point.hpp
include/pw/core/resource.hpp
include/pw/core/rectangle.hpp
include/pw/core/serialize.hpp
include/pw/core/size.hpp
@ -30,10 +31,11 @@ set(misc
set(srcs
# src/buffer.cpp
src/core.cpp
src/image.cpp
src/debug.cpp
src/mesh.cpp
src/core.cpp
src/mesh.cpp
src/resource.cpp
src/serialize.cpp
src/time.cpp
src/image.cpp

View file

@ -31,6 +31,7 @@
namespace pw {
const static double __PW_PI = 3.1415926535897932384626433832795028841971693993751058209;
const static double __PW_PI_DOUBLE = 2.0 * __PW_PI;
template <typename T>
inline const T pi() { return static_cast<T>(__PW_PI); }
@ -59,6 +60,11 @@ inline T ping_pong(const T& t,const T& length) {
return length - std::abs(tn - length);
}
template <typename T>
inline T wrap_angle(const T& angle_in_radian) {
using std::floor;
return angle_in_radian - __PW_PI_DOUBLE * floor( angle_in_radian / __PW_PI_DOUBLE );
}
//void extractRotation(const matrix &A, Quaterniond &q,const unsigned int maxIter)
//{

View file

@ -72,6 +72,10 @@ struct matrixbase_ {
return *this / this->norm() ;
}
inline void normalize() {
*this /= this->norm();
}
using iterator = T*;
using const_iterator = const T*;
iterator begin() { return &derived().data[0]; }
@ -88,8 +92,12 @@ struct matrixbase_ {
}
static 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));
#endif
}
static const Derived lerp(const Derived &a,const Derived &b,const T& t) {
@ -97,16 +105,18 @@ struct matrixbase_ {
}
// inline Derived& operator *= (const T& b) { for (auto & e : *this) e *= b; return derived(); }
// inline Derived& operator /= (const T& b) { for (auto & e : *this) e /= b; return derived(); }
// inline Derived& operator += (const T& b) { for (auto & e : *this) e += b; return derived(); }
// inline Derived& operator -= (const T& b) { for (auto & e : *this) e -= b; return derived(); }
inline void operator *= (const T& b) { for (auto & e : *this) e *= b; }
inline void operator /= (const T& b) { for (auto & e : *this) e /= b; }
inline void operator += (const T& b) { for (auto & e : *this) e += b; }
inline void operator -= (const T& b) { for (auto & e : *this) e -= b; }
inline const Derived operator * (const T& b) const { Derived r(derived()); for (auto & e : r) e *= b; return r; }
inline const Derived operator / (const T& b) const { Derived r(derived()); for (auto & e : r) e /= b; return r; }
inline const Derived operator + (const T& b) const { Derived r(derived()); for (auto & e : r) e += b; return r; }
inline const Derived operator - (const T& b) const { Derived r(derived()); for (auto & e : r) e -= b; return r; }
};
//

View file

@ -26,6 +26,7 @@
#include <pw/core/globals.hpp>
#include <pw/core/vector.hpp>
#include <pw/core/aabb.hpp>
#include <pw/core/resource.hpp>
namespace pw {
@ -64,6 +65,11 @@ public:
aabb aabb() const { return _aabb; }
void compute_normals();
void set_normals(const mesh::vertex3array_t &v);
const mesh::vertex3array_t& normals() const;
protected:

View file

@ -0,0 +1,45 @@
/*
* Copyright (c) 1999-2019 Hartmut Seichter
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
#ifndef PW_CORE_RESOURCE_HPP
#define PW_CORE_RESOURCE_HPP
#include <pw/core/globals.hpp>
#include <atomic>
namespace pw {
class resource {
public:
using change_t = std::atomic_int_fast64_t;
int64_t changecount() { return _changecount; }
void dirty() { ++_changecount; };
protected:
change_t _changecount;
};
}
#endif

View file

@ -35,7 +35,7 @@ namespace pw {
class time {
public:
typedef std::chrono::time_point<std::chrono::high_resolution_clock> tick_t;
using tick_t = std::chrono::time_point<std::chrono::high_resolution_clock> ;
time(); /// c'tor
~time(); /// d'tor

View file

@ -1,4 +1,6 @@
#include "pw/core/mesh.hpp"
#include "pw/core/debug.hpp"
#include "pw/core/serialize.hpp"
namespace pw {
@ -6,6 +8,58 @@ mesh::mesh()
{
}
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 (auto 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();
// translate to actual indices
auto ci = _indices[ i];
auto li = _indices[il];
auto ri = _indices[ir];
// calculate delta vectors
auto dL = vector3( _vertices[li] - _vertices[ci] );
auto dR = vector3( _vertices[ri] - _vertices[ci] );
auto N = dR.cross(dL).normalized();
//auto dV = _vertices[idx0].cross(_vertices[idx1]);
// 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();
}
this->set_normals(normals);
for (auto N : normals) {
debug::s() << "( " << serialize::matrix(N.transposed()) << ") ";
}
// for triangle-strip
// for triangle-fan
// now set back
}
void mesh::set_vertices(const mesh::vertex3array_t &v)
{
// first set vertices
@ -15,12 +69,26 @@ void mesh::set_vertices(const mesh::vertex3array_t &v)
compute_bounds();
}
void mesh::set_normals(const mesh::vertex3array_t &v)
{
_normals = v;
}
const mesh::vertex3array_t &mesh::normals() const
{
return _normals;
}
void mesh::apply(const matrix4x4 &m)
{
// apply transformation to all vertices
for (auto &v : _vertices)
v = vector4(m * v.homogenous()).project();
// apply to normals
for(auto &n : _normals)
n = vector4(m * n.homogenous(0)).xyz();
// recompute bounds
compute_bounds();
}

View file

@ -0,0 +1,5 @@
#include "pw/core/resource.hpp"
namespace pw {
}