diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index aa0ea86..490122e 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -1,5 +1,6 @@ set(hdrs + include/pw/core/aabb.hpp include/pw/core/axisangle.hpp include/pw/core/color.hpp include/pw/core/core.hpp diff --git a/src/core/include/pw/core/aabb.hpp b/src/core/include/pw/core/aabb.hpp new file mode 100644 index 0000000..684da4f --- /dev/null +++ b/src/core/include/pw/core/aabb.hpp @@ -0,0 +1,52 @@ +/* + * 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_AABB_HPP +#define PW_CORE_AABB_HPP + +#include + +namespace pw { + +struct aabb { + + vector3 min; + vector3 max; + + aabb(const vector3 min_vec,const vector3 max_vec) + : min(min_vec) + , max(max_vec) + {} + + aabb() { + min.zero(); max.zero(); + } + + vector3 dimension() const { + return max - min; + } + +}; + +} + +#endif diff --git a/src/core/include/pw/core/mesh.hpp b/src/core/include/pw/core/mesh.hpp index 56345db..50b262c 100644 --- a/src/core/include/pw/core/mesh.hpp +++ b/src/core/include/pw/core/mesh.hpp @@ -25,6 +25,7 @@ #include #include +#include namespace pw { @@ -32,6 +33,7 @@ class mesh { public: typedef std::vector indexarray_t; + typedef std::vector valuearray_t; typedef std::vector vertex3array_t; typedef std::vector vertex2array_t; @@ -45,10 +47,12 @@ public: points }; - void set_indices(const indexarray_t& v) { _indices = v; } - void set_vertices(const vertex3array_t& v) { _vertices = v; } + mesh(); + void set_indices(const indexarray_t& v) { _indices = v; } const indexarray_t& indices() const { return _indices; } + + void set_vertices(const vertex3array_t& v); const vertex3array_t& vertices() const { return _vertices; } void set_topology(topology_type t) { _topology = t; } @@ -56,12 +60,13 @@ public: void apply(const matrix4x4& m); - void bounds() const; - void reset(); + aabb aabb() const { return _aabb; } + protected: + topology_type _topology; indexarray_t _indices; //!< indices according to topology type @@ -71,7 +76,14 @@ protected: std::vector _uvs; //!< storing multiple UV sets + struct aabb _aabb; + // TODO add weights, tangents etc. pp. + +private: + + void compute_bounds(); + }; } diff --git a/src/core/src/mesh.cpp b/src/core/src/mesh.cpp index 07892c4..b35d66b 100644 --- a/src/core/src/mesh.cpp +++ b/src/core/src/mesh.cpp @@ -1,27 +1,48 @@ #include "pw/core/mesh.hpp" - namespace pw { -void mesh::apply(const matrix4x4 &m) +mesh::mesh() { - for (auto &v : _vertices) - v = vector4(m * v.homogenous()).project(); } -void mesh::bounds() const +void mesh::set_vertices(const mesh::vertex3array_t &v) { - vector3 max_vec = _vertices.front(),min_vec = _vertices.front(); - // compute the AABB - for (auto const & v : _vertices) { - max_vec.x() = std::max(v.x(),max_vec.x()); - max_vec.y() = std::max(v.y(),max_vec.y()); - max_vec.z() = std::max(v.z(),max_vec.z()); - min_vec.x() = std::min(v.x(),min_vec.x()); - min_vec.y() = std::min(v.y(),min_vec.y()); - min_vec.z() = std::min(v.z(),min_vec.z()); + // first set vertices + _vertices = v; + + // update bounds + compute_bounds(); +} + +void mesh::apply(const matrix4x4 &m) +{ + // apply transformation to all vertices + for (auto &v : _vertices) + v = vector4(m * v.homogenous()).project(); + + // recompute bounds + compute_bounds(); +} + +void mesh::compute_bounds() +{ + // only do if there are vertices + if (_vertices.size()) { + + // 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()); + } } } - } diff --git a/src/visual/include/pw/visual/texture.hpp b/src/visual/include/pw/visual/texture.hpp index bad1efc..a4007b5 100644 --- a/src/visual/include/pw/visual/texture.hpp +++ b/src/visual/include/pw/visual/texture.hpp @@ -15,6 +15,7 @@ class texture { }; enum texture_shape { + shape_1d, shape_2d, shape_3d }; @@ -36,7 +37,6 @@ class texture { texture(shared_ptr i,texture_shape s,texture_type = color); - void set_image(shared_ptr i); shared_ptr get() const { return _image; } diff --git a/src/visual/include/pw/visual/vertex_array.hpp b/src/visual/include/pw/visual/vertex_array.hpp index 363849d..7b20d35 100644 --- a/src/visual/include/pw/visual/vertex_array.hpp +++ b/src/visual/include/pw/visual/vertex_array.hpp @@ -15,12 +15,11 @@ public: ~vertex_array(); - bool ready() const; - void create(const mesh &m); void destroy(); void draw(); + bool ready() const; protected: diff --git a/src/visual/src/pipeline.cpp b/src/visual/src/pipeline.cpp index e1952bf..4a2458a 100644 --- a/src/visual/src/pipeline.cpp +++ b/src/visual/src/pipeline.cpp @@ -120,8 +120,8 @@ struct triangle_renderer const char* vertex_shader_2 = R"( #version 400 uniform mat4 model_mat; + uniform mat4 view_mat = mat4 in vec3 vertex_p; - void main() { gl_Position = model_mat * vec4(vertex_p, 1.0); } diff --git a/src/visual/src/texture.cpp b/src/visual/src/texture.cpp index 2bad5f4..e816cbc 100644 --- a/src/visual/src/texture.cpp +++ b/src/visual/src/texture.cpp @@ -19,6 +19,8 @@ struct texture::impl { GLuint gl_shape() { switch (_host.shape()) { + case pw::texture::shape_1d: + return GL_TEXTURE_1D; case pw::texture::shape_2d: return GL_TEXTURE_2D; case pw::texture::shape_3d: