From 2c4cc29f9715f5117dea179efb846d77a99dc915 Mon Sep 17 00:00:00 2001 From: Hartmut Seichter Date: Tue, 19 Jan 2021 20:38:28 +0100 Subject: [PATCH] Clean up code for rendering and wrapping it to Lua Now with an object oriented example. Signed-off-by: Hartmut Seichter --- src/binding/src/script_core.cpp | 16 +-- src/binding/src/script_visual.cpp | 16 +-- src/core/CMakeLists.txt | 26 +++-- src/core/include/pw/core/geometry.hpp | 68 +++++------- src/core/include/pw/core/material.hpp | 73 ++++++++++++ src/core/include/pw/core/matrix_transform.hpp | 4 +- src/core/include/pw/core/vector.hpp | 11 +- src/core/src/geometry.cpp | 105 ++++++++++++------ src/core/src/material.cpp | 7 ++ src/core/tests/pwcore_test_mesh.cpp | 49 ++++---- src/geometry/src/primitives.cpp | 4 +- src/scripts/demos/simple_002.lua | 4 +- src/visual/CMakeLists.txt | 2 - src/visual/include/pw/visual/material.hpp | 13 --- src/visual/include/pw/visual/pipeline.hpp | 4 +- src/visual/include/pw/visual/texture.hpp | 11 +- src/visual/src/material.cpp | 0 src/visual/src/pass.cpp | 1 - src/visual/src/pipeline.cpp | 10 +- src/visual/src/renderer.cpp | 85 +++++++------- src/visual/src/texture.cpp | 59 ++++++---- 21 files changed, 332 insertions(+), 236 deletions(-) create mode 100644 src/core/include/pw/core/material.hpp create mode 100644 src/core/src/material.cpp delete mode 100644 src/visual/include/pw/visual/material.hpp delete mode 100644 src/visual/src/material.cpp diff --git a/src/binding/src/script_core.cpp b/src/binding/src/script_core.cpp index ca4dde8..5016a6d 100644 --- a/src/binding/src/script_core.cpp +++ b/src/binding/src/script_core.cpp @@ -118,16 +118,16 @@ void register_core_function(sol::state& lua,sol::table& ns) ); ns.new_usertype("geometry" - , sol::constructors() - , "topology", sol::property(&geometry::topology,&geometry::set_topology) + , sol::constructors() + , "primitive_type", sol::property(&geometry::primitive_type,&geometry::set_primitive_type) , "vertices", sol::property(&geometry::vertices,&geometry::set_vertices) , "indices", sol::property(&geometry::indices,&geometry::set_indices) - , "", &geometry::compute_normals - ).new_enum("geometry_type" - ,"points", geometry::topology_type::points - , "lines", geometry::topology_type::lines - , "line_strip", geometry::topology_type::line_strip - , "triangles", geometry::topology_type::triangles); + , "compute_normals", &geometry::compute_normals); + + ns.new_enum("primitive_type" + ,"points", geometry::primitive_type::points + ,"lines", geometry::primitive_type::lines + ,"triangles", geometry::primitive_type::triangles); ns.new_usertype>("matrixtransform" diff --git a/src/binding/src/script_visual.cpp b/src/binding/src/script_visual.cpp index 53a1f54..d8ec985 100644 --- a/src/binding/src/script_visual.cpp +++ b/src/binding/src/script_visual.cpp @@ -42,17 +42,14 @@ void register_visual_function(sol::state&,sol::table &ns) ,"submit",&render_pass::submit ); - ns.new_usertype("material" - ,"color",sol::property(&material::_color)); - - ns.new_usertype("mesh_renderer" + ns.new_usertype("renderer" ,sol::constructors() - ,"create",&renderer::create - ,"ready",sol::readonly_property(&renderer::ready) - ,"release",&renderer::release - ,"draw",&renderer::draw - ); + ,"create",&renderer::create + ,"ready",sol::readonly_property(&renderer::ready) + ,"release",&renderer::release + ,"draw",&renderer::draw + ); ns.new_usertype("framebuffer" ,sol::constructors() @@ -64,7 +61,6 @@ void register_visual_function(sol::state&,sol::table &ns) ns.new_usertype("texture" ,sol::constructors() ,"image",sol::property(&texture::set_image,&texture::image) - ,"type",sol::property(&texture::set_type,&texture::type) ); } diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 31b1f56..f592f11 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -1,26 +1,27 @@ set(hdrs - include/pw/core/aabb.hpp + include/pw/core/aabb.hpp include/pw/core/axisangle.hpp - include/pw/core/color.hpp + include/pw/core/color.hpp include/pw/core/core.hpp include/pw/core/debug.hpp include/pw/core/globals.hpp + include/pw/core/material.hpp include/pw/core/math.hpp include/pw/core/matrixbase.hpp include/pw/core/matrix.hpp 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/resource.hpp + include/pw/core/rectangle.hpp include/pw/core/serialize.hpp include/pw/core/size.hpp - include/pw/core/time.hpp - include/pw/core/geometry.hpp + include/pw/core/time.hpp + include/pw/core/geometry.hpp include/pw/core/image.hpp include/pw/core/vector.hpp - include/pw/core/matrix_transform.hpp + include/pw/core/matrix_transform.hpp ) set(misc @@ -30,14 +31,15 @@ set(misc ) set(srcs - # src/buffer.cpp - src/core.cpp + # src/buffer.cpp + src/core.cpp src/image.cpp src/debug.cpp - src/geometry.cpp - src/resource.cpp + src/geometry.cpp + src/material.cpp + src/resource.cpp src/serialize.cpp - src/time.cpp + src/time.cpp src/image.cpp ) diff --git a/src/core/include/pw/core/geometry.hpp b/src/core/include/pw/core/geometry.hpp index dd7a84b..cd1476a 100644 --- a/src/core/include/pw/core/geometry.hpp +++ b/src/core/include/pw/core/geometry.hpp @@ -28,9 +28,6 @@ #include #include -#include -#include - namespace pw { /* @@ -43,68 +40,53 @@ namespace pw { class geometry { public: - enum class topology_type { - triangles, - triangle_strip, - triangle_fan, - quads, + enum class primitive_type { + points, lines, - line_strip, - points + triangles, + polygons }; using index_t = uint32_t; //< needs to be compatible with GL_UNSIGNED_INT - - using valuearray_t = std::vector ; - using indexarray_t = std::vector ; - - using vertex2array_t = std::vector ; - using vertex3array_t = std::vector ; - - using __attribute_t = std::variant; - - using __attribute_set = std::tuple; + using indices_t = std::vector; geometry() = default; - geometry(topology_type t); + geometry(primitive_type t, vector3_array v, indices_t i); - void set_indices(const indexarray_t& v) { _indices = v; } - const indexarray_t& indices() const { return _indices; } + void set_primitive_type(primitive_type t) { _primitive_type = t; } + primitive_type primitive_type() { return _primitive_type; } - void set_vertices(const vertex3array_t& v); - const vertex3array_t& vertices() const { return _vertices; } + void set_vertices(vector3_array v); + const vector3_array& vertices() const { return _vertices; } - void set_normals(const geometry::vertex3array_t &v); - const geometry::vertex3array_t& normals() const; + void set_indices(indices_t v); + const indices_t& indices() const; - void set_topology(topology_type t) { _topology = t; } - topology_type topology() { return _topology; } + void set_normals(vector3_array v); + const vector3_array& normals() const; void transform(const matrix4x4& m); - void reset(); + void compute_normals(); - aabb get_aabb() const { return _aabb; } + void compute_tangent_space(); - void compute_normals(); - - void compute_bounds(); + aabb bounds() const; protected: - topology_type _topology = topology_type::triangles; - indexarray_t _indices; //!< indices according to topology type + enum primitive_type _primitive_type = primitive_type::triangles; - vertex3array_t _vertices; //!< geometry data - vertex3array_t _normals; //!< normal data - vertex3array_t _colors; //!< color data + vector3_array _vertices; //!< vertex data + indices_t _indices; //!< indices - std::vector _uvs; //!< storing multiple UV sets + vector3_array _normals; //!< normal data + vector3_array _tangents; //!< tangent data + vector3_array _bitangents; //!< bitangent - struct aabb _aabb; - - // TODO add weights, tangents etc. pp. + std::vector _texture_coords; //! texture coordinates + uint64_t _change_count { 0 }; }; } diff --git a/src/core/include/pw/core/material.hpp b/src/core/include/pw/core/material.hpp new file mode 100644 index 0000000..7e1dd98 --- /dev/null +++ b/src/core/include/pw/core/material.hpp @@ -0,0 +1,73 @@ +/* + * Copyright (c) 1999-2021 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_MATERIAL_HPP +#define PW_CORE_MATERIAL_HPP + + +#include + +namespace pw { + +class material { +public: + enum class texture_type { + diffuse, + specular, + ambient, + emissive, + height, + normals, + shininess, + opacity, + displacement, + lightmap, + reflection, + base_color, + // more to implement .... + }; + + enum class shading_type { + flat, + gouraud, + phong, + blinn_phong + // well ... + }; + + material() = default; + + using texture_t = std::tuple; + + + +protected: + + vector4 _color = vector4 { 1.0, 0.0, 1.0, 1.0 }; + + std::vector _textures; + +}; + +} + +#endif diff --git a/src/core/include/pw/core/matrix_transform.hpp b/src/core/include/pw/core/matrix_transform.hpp index 194d7f1..085a356 100644 --- a/src/core/include/pw/core/matrix_transform.hpp +++ b/src/core/include/pw/core/matrix_transform.hpp @@ -113,8 +113,8 @@ struct matrix_transform { matrix_<4,4,T> view_matrix; view_matrix.set_identity(); const vector3_ los = (target - position).normalized(); // line of sight - const vector3_ sid = los.cross(up).normalized(); - const vector3_ upd = sid.cross(los).normalized(); + const vector3_ sid = vector3_::cross(los,up).normalized(); + const vector3_ upd = vector3_::cross(sid,los).normalized(); // set base vectors view_matrix.set_slice(sid, 0, 0); diff --git a/src/core/include/pw/core/vector.hpp b/src/core/include/pw/core/vector.hpp index 4093a1e..05ae7fa 100644 --- a/src/core/include/pw/core/vector.hpp +++ b/src/core/include/pw/core/vector.hpp @@ -84,11 +84,11 @@ struct vector3_ : matrix_<3,1,T> { inline auto xy() const { return vector2_( { x(),y() } ); } inline auto homogenous(T w = 1) const { return matrix_<4,1,T>( { x(),y(),z(),w } ); } - inline const vector3_ cross(const vector3_& rhs) const { + inline static constexpr vector3_ cross(const vector3_& lhs,const vector3_& rhs) { return vector3_( { - (*this)[1] * rhs[2] - rhs[1] * (*this)[2], - (*this)[2] * rhs[0] - rhs[2] * (*this)[0], - (*this)[0] * rhs[1] - rhs[0] * (*this)[1] + lhs[1] * rhs[2] - rhs[1] * lhs[2], + lhs[2] * rhs[0] - rhs[2] * lhs[0], + lhs[0] * rhs[1] - rhs[0] * lhs[1] } ); } @@ -142,11 +142,14 @@ struct vector4_ : matrix_<4,1,T> { using vector2f = vector2_; using vector2d = vector2_; +using vector2i = vector2_; using vector2 = vector2_; +using vector2_array = std::vector; using vector3f = vector3_; using vector3d = vector3_; using vector3 = vector3_; +using vector3_array = std::vector; using vector4f = vector4_; using vector4d = vector4_; diff --git a/src/core/src/geometry.cpp b/src/core/src/geometry.cpp index eea386f..5387203 100644 --- a/src/core/src/geometry.cpp +++ b/src/core/src/geometry.cpp @@ -8,7 +8,7 @@ namespace pw { void geometry::compute_normals() { // assumption is that we have some array as the vertices - vertex3array_t normals; normals.resize(_vertices.size()); + vector3_array normals; _normals.resize(_vertices.size()); // for indexed-faceset for (size_t i = 1; i < _indices.size()-1;i++) @@ -27,7 +27,7 @@ void geometry::compute_normals() auto edgeRight = vector3( _vertices[ri] - _vertices[ci] ); // calculate counter clockwise and normalize - auto N = edgeRight.cross(edgeLeft).normalized(); + auto N = vector3::cross(edgeRight,edgeLeft).normalized(); // NOTE that addition is ugly normals[ci] = N + normals[ci]; @@ -39,11 +39,7 @@ void geometry::compute_normals() n.normalize(); } - this->set_normals(normals); - - for (auto N : normals) { - debug::s() << "( " << serialize::matrix(N.transposed()) << ") "; - } +// this->set_normals(normals); // for triangle-strip @@ -55,29 +51,74 @@ void geometry::compute_normals() // now set back } -geometry::geometry(geometry::topology_type t) - : _topology(t) +void geometry::compute_tangent_space() +{ + if (_primitive_type == primitive_type::triangles) { + + for (size_t i = 0; i < _indices.size();i += 3) { + + const auto& v0 = _vertices[_indices[i + 0]]; + const auto& v1 = _vertices[_indices[i + 1]]; + const auto& v2 = _vertices[_indices[i + 2]]; + + auto normal = vector3::cross(v1-v0,v2-v0); + + + + } + } + +} + +geometry::geometry(enum primitive_type t + ,vector3_array v + ,indices_t i) + : _primitive_type(t) + , _vertices(std::move(v)) + , _indices(std::move(i)) { } -void geometry::set_vertices(const geometry::vertex3array_t &v) +void geometry::set_vertices(vector3_array v) { - // first set vertices + _change_count++; _vertices = v; - - // update bounds - compute_bounds(); } -void geometry::set_normals(const geometry::vertex3array_t &v) +void geometry::set_indices(indices_t v) { + _change_count++; + _indices = v; +} + +const geometry::indices_t &geometry::indices() const { return _indices; } + +void geometry::set_normals(vector3_array v) +{ + _change_count++; _normals = v; } -const geometry::vertex3array_t &geometry::normals() const -{ - return _normals; -} +const vector3_array &geometry::normals() const { return _normals; } + +//void geometry::set_vertices(const geometry::vertex3array_t &v) +//{ +// // first set vertices +// _vertices = v; + +// // update bounds +// compute_bounds(); +//} + +//void geometry::set_normals(const geometry::vertex3array_t &v) +//{ +// _normals = v; +//} + +//const geometry::vertex3array_t &geometry::normals() const +//{ +// return _normals; +//} void geometry::transform(const matrix4x4 &m) { @@ -86,31 +127,33 @@ void geometry::transform(const matrix4x4 &m) v = vector4(m * v.homogenous()).project(); // apply to normals - for(auto &n : _normals) - n = vector4(m * n.homogenous(0)).xyz(); +// for(auto &n : _normals) +// n = vector4(m * n.homogenous(0)).xyz(); - // recompute bounds - compute_bounds(); } -void geometry::compute_bounds() +aabb geometry::bounds() const { + aabb b; + // only do if there are vertices if (_vertices.size()) { // reset - _aabb.max = _aabb.min = _vertices.front(); + b.max = b.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()); + b.max.x() = std::max(v.x(),b.max.x()); + b.max.y() = std::max(v.y(),b.max.y()); + b.max.z() = std::max(v.z(),b.max.z()); + b.min.x() = std::min(v.x(),b.min.x()); + b.min.y() = std::min(v.y(),b.min.y()); + b.min.z() = std::min(v.z(),b.min.z()); } } + + return b; } } diff --git a/src/core/src/material.cpp b/src/core/src/material.cpp new file mode 100644 index 0000000..dc7160c --- /dev/null +++ b/src/core/src/material.cpp @@ -0,0 +1,7 @@ +#include "pw/core/material.hpp" + + +namespace pw { + + +}; diff --git a/src/core/tests/pwcore_test_mesh.cpp b/src/core/tests/pwcore_test_mesh.cpp index 8c94818..c0e56a6 100644 --- a/src/core/tests/pwcore_test_mesh.cpp +++ b/src/core/tests/pwcore_test_mesh.cpp @@ -8,39 +8,46 @@ int main(int argc,char **argv) { - pw::geometry amesh; + pw::geometry geo; - pw::geometry::vertex3array_t vs = { {-1,-1,0},{1,-1,0},{0,1,0} }; - amesh.set_vertices(vs); + pw::vector3_array vs = { {-1,-1,0},{1,-1,0},{0,1,0} }; - for (auto v : amesh.vertices()) { - std::cout << pw::serialize::matrix(v.transposed()) << std::endl; - } - auto scale = pw::matrix_transform::scale_matrix({2,2,2}); + geo.set_vertices(vs); - amesh.transform(scale); - std::cout << "after scale" << std::endl; - for (auto v : amesh.vertices()) { - std::cout << pw::serialize::matrix(v.transposed()) << std::endl; - } - pw::axisangle aa; +// amesh.set_vertices(vs); - aa.axis = pw::vector3({ 0, 0, 1 }); - aa.angle = pw::deg_to_rad(90.0f); +// for (auto v : amesh.vertices()) { +// std::cout << pw::serialize::matrix(v.transposed()) << std::endl; +// } - auto rot_mat = aa.to_matrix(); +// auto scale = pw::matrix_transform::scale_matrix({2,2,2}); - amesh.transform(rot_mat); +// amesh.transform(scale); - std::cout << "after rotate" << std::endl; +// std::cout << "after scale" << std::endl; - for (auto v : amesh.vertices()) { - std::cout << pw::serialize::matrix(v.transposed()) << std::endl; - } +// for (auto v : amesh.vertices()) { +// std::cout << pw::serialize::matrix(v.transposed()) << std::endl; +// } + +// pw::axisangle aa; + +// aa.axis = pw::vector3({ 0, 0, 1 }); +// aa.angle = pw::deg_to_rad(90.0f); + +// auto rot_mat = aa.to_matrix(); + +// amesh.transform(rot_mat); + +// std::cout << "after rotate" << std::endl; + +// for (auto v : amesh.vertices()) { +// std::cout << pw::serialize::matrix(v.transposed()) << std::endl; +// } diff --git a/src/geometry/src/primitives.cpp b/src/geometry/src/primitives.cpp index b55e8d0..533565d 100644 --- a/src/geometry/src/primitives.cpp +++ b/src/geometry/src/primitives.cpp @@ -6,7 +6,7 @@ geometry primitives::box(real_t size_x,real_t size_y, real_t size_z) { geometry m; - geometry::vertex3array_t vertices; + vector3_array 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 @@ -48,7 +48,7 @@ geometry primitives::sphere(real_t radius,int divisions_latitude,int divisions_l geometry geom; - geom.set_topology(geometry::topology_type::triangle_strip); +// geom.set_primitive_type(geometry::primitive_type::triangle_strip); real_t x, y, z; diff --git a/src/scripts/demos/simple_002.lua b/src/scripts/demos/simple_002.lua index e2894d7..dcfff71 100644 --- a/src/scripts/demos/simple_002.lua +++ b/src/scripts/demos/simple_002.lua @@ -19,7 +19,7 @@ w.position = pw.point.new(100,100) -- create a new geometry local g = pw.geometry:new() -g.topology = pw.geometry.triangles -- meh +g.primitive_type = pw.primitive_type.triangles -- meh g.vertices:clear() @@ -79,7 +79,7 @@ if not s:build() then print("Error!") end -local renderer = pw.mesh_renderer:new() +local renderer = pw.renderer:new() if not renderer:create(g) then print("couldn't create renderer") diff --git a/src/visual/CMakeLists.txt b/src/visual/CMakeLists.txt index f4c98af..b9c870f 100644 --- a/src/visual/CMakeLists.txt +++ b/src/visual/CMakeLists.txt @@ -5,7 +5,6 @@ set(hdrs include/pw/visual/pipeline.hpp include/pw/visual/texture.hpp include/pw/visual/renderer.hpp - include/pw/visual/material.hpp ) set(srcs @@ -17,7 +16,6 @@ set(srcs src/target.cpp src/texture.cpp src/renderer.cpp - src/material.cpp ) diff --git a/src/visual/include/pw/visual/material.hpp b/src/visual/include/pw/visual/material.hpp deleted file mode 100644 index 14bc499..0000000 --- a/src/visual/include/pw/visual/material.hpp +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#include - - -namespace pw { - -struct material { - vector4f _color = vector4f {0xFF,0x00,0xFF,0xFF}; - material() = default; -}; - -} diff --git a/src/visual/include/pw/visual/pipeline.hpp b/src/visual/include/pw/visual/pipeline.hpp index 2069d5d..c8a464f 100644 --- a/src/visual/include/pw/visual/pipeline.hpp +++ b/src/visual/include/pw/visual/pipeline.hpp @@ -8,7 +8,6 @@ #include #include -#include #include @@ -41,8 +40,7 @@ struct render_pass { void submit(const geometry& g, const matrix4x4& model_mat, const matrix4x4& view_mat, - const matrix4x4& projection_mat, - const material& mat); + const matrix4x4& projection_mat); shader _shader; renderer _renderer; diff --git a/src/visual/include/pw/visual/texture.hpp b/src/visual/include/pw/visual/texture.hpp index fc87fe8..622637b 100644 --- a/src/visual/include/pw/visual/texture.hpp +++ b/src/visual/include/pw/visual/texture.hpp @@ -21,9 +21,9 @@ public: }; enum texture_dimension { + dimension_r, dimension_s, - dimension_t, - dimension_r + dimension_t }; enum wrap_mode { @@ -36,16 +36,13 @@ public: using image_ref = shared_ptr<::pw::image>; texture(); - texture(image_ref i,data_layout s,data_type = data_type::color); + texture(image_ref i,data_layout s); ~texture(); void set_image(image_ref i); image_ref image() const { return _image; } - void set_type(data_type t); - data_type type() const { return _type; } - void set_shape(data_layout s); data_layout shape() const { return _shape; } @@ -57,11 +54,11 @@ public: uint32_t native_handle() const; uint32_t native_sampler_handle() const; + protected: image_ref _image; - data_type _type = data_type::color; data_layout _shape = data_layout::shape_2d; wrap_mode _wrap = wrap_mode::wrap_clamp_to_edge; diff --git a/src/visual/src/material.cpp b/src/visual/src/material.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/src/visual/src/pass.cpp b/src/visual/src/pass.cpp index fdc0096..5ba48bd 100644 --- a/src/visual/src/pass.cpp +++ b/src/visual/src/pass.cpp @@ -5,7 +5,6 @@ #include #include -#include #include #include diff --git a/src/visual/src/pipeline.cpp b/src/visual/src/pipeline.cpp index 36049db..064b075 100644 --- a/src/visual/src/pipeline.cpp +++ b/src/visual/src/pipeline.cpp @@ -10,7 +10,6 @@ #include "pw/visual/pipeline.hpp" #include "pw/visual/shader.hpp" #include "pw/visual/renderer.hpp" -#include "pw/visual/material.hpp" #include "pw/visual/framebuffer.hpp" #include "pw/visual/texture.hpp" @@ -22,8 +21,7 @@ namespace pw { void render_pass::submit(const geometry &g, const matrix4x4 &model_mat, const matrix4x4 &view_mat, - const matrix4x4 &projection_mat, - const material &mat) + const matrix4x4 &projection_mat) { if (!_shader.ready()) { // @@ -37,7 +35,7 @@ void render_pass::submit(const geometry &g, // new version with ... shader::uniform_cache_t us; - us.push_back(std::make_tuple("input_color",mat._color,-1)); +// us.push_back(std::make_tuple("input_color",mat._color,-1)); us.push_back(std::make_tuple("model",model_mat,-1)); us.push_back(std::make_tuple("view",view_mat,-1)); us.push_back(std::make_tuple("projection",projection_mat,-1)); @@ -76,7 +74,7 @@ struct triangle_renderer // 2 -- 3 // geometry - geometry::vertex3array_t vertices = { + vector3_array vertices = { {-s, s, z_val} // 0 ,{ s, s, z_val} // 1 ,{-s, -s, z_val} // 2 @@ -84,7 +82,7 @@ struct triangle_renderer }; // topology / indices - geometry::indexarray_t indices = { 2, 1, 0, + geometry::indices_t indices = { 2, 1, 0, 2, 3, 1}; diff --git a/src/visual/src/renderer.cpp b/src/visual/src/renderer.cpp index 0e7ea88..7958b19 100644 --- a/src/visual/src/renderer.cpp +++ b/src/visual/src/renderer.cpp @@ -13,8 +13,10 @@ namespace pw { struct renderer::impl { - GLuint _vao = 0; - std::vector _vbos; + uint32_t _vao { 0 }; + uint32_t _ebo { 0 }; + uint32_t _vbo { 0 }; + GLint _mesh_elements = { 0 }; impl() = default; @@ -36,47 +38,43 @@ struct renderer::impl { release(); } - glGenVertexArrays(1,&_vao); - // TODO check if valid? - glBindVertexArray(_vao); - - size_t arrays_needed = 0; - - // should bail out here - if (!m.vertices().empty()) arrays_needed++; - if (!m.indices().empty()) arrays_needed++; - - _mesh_elements = m.indices().size(); - // TODO: implement the other arrays + // + glGenVertexArrays(1,&_vao); + glGenBuffers(1, &_vbo); + glGenBuffers(1, &_ebo); - _vbos.resize(arrays_needed); + glBindVertexArray(_vao); - glGenBuffers(_vbos.size(), _vbos.data()); - - // vertices - glBindBuffer(GL_ARRAY_BUFFER, _vbos[0]); - glBufferData(GL_ARRAY_BUFFER, - sizeof(m.vertices().front()) * m.vertices().size(), - m.vertices().data(), - GL_STATIC_DRAW); - - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr); + // TODO binding separate VBOs to the + // vertexarray should be avoided // indices - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vbos[1]); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, - sizeof(m.indices().front()) * m.indices().size(), m.indices().data(), + m.indices().size() * sizeof (uint32_t), + m.indices().data(), GL_STATIC_DRAW); + + // vertices + glBindBuffer(GL_ARRAY_BUFFER, _vbo); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr); + glBufferData(GL_ARRAY_BUFFER, + m.vertices().size() * sizeof(vector3), + m.vertices().data(), + GL_STATIC_DRAW); + glEnableVertexAttribArray(0); + + // stop binding glBindVertexArray(0); + // get errors auto error = glGetError(); if (error != GL_NO_ERROR) { - debug::e() << "GL error: " << error; + debug::e() << __PRETTY_FUNCTION__ << " GL error: " << error; } return ready(); @@ -85,11 +83,11 @@ struct renderer::impl { void release() { - glDeleteVertexArrays(1,&_vao); - for (auto vbo : _vbos) - glDeleteBuffers(1,&vbo); - _vbos.clear(); + glDeleteBuffers(1,&_vbo); + glDeleteBuffers(1,&_ebo); + + glDeleteVertexArrays(1,&_vao); } void draw() @@ -98,23 +96,20 @@ struct renderer::impl { glEnable(GL_CULL_FACE); glCullFace(GL_BACK); glFrontFace(GL_CCW); -#endif glClearColor(0.0,1.0,0,1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); +#endif - if (glIsVertexArray(_vao)) + + glBindVertexArray(_vao); + glDrawElements(GL_TRIANGLES, _mesh_elements, GL_UNSIGNED_INT, nullptr); + glBindVertexArray(0); + + auto error = glGetError(); + if (error != GL_NO_ERROR) { - - glBindVertexArray(_vao); - glDrawElements(GL_TRIANGLES, _mesh_elements, GL_UNSIGNED_INT, nullptr); - glBindVertexArray(0); - - auto error = glGetError(); - if (error != GL_NO_ERROR) - { - debug::e() << "GL error: " << error; - } + debug::e() << "GL error: " << error; } } diff --git a/src/visual/src/texture.cpp b/src/visual/src/texture.cpp index 3b8dbc7..6546257 100644 --- a/src/visual/src/texture.cpp +++ b/src/visual/src/texture.cpp @@ -39,29 +39,47 @@ struct texture::impl { return 0; } - void create() + void bind() + { + glBindTexture(gl_shape(), _texture_id); + } + + void unbind() + { + glBindTexture(gl_shape(), 0); + } + + void create_sampler() + { + bind(); + + glGenSamplers(1, &_texture_sampler); + glSamplerParameteri(_texture_sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glSamplerParameteri(_texture_sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + + // glSamplerParameteri(_texture_sampler, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); + glSamplerParameteri(_texture_sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glSamplerParameteri(_texture_sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + unbind(); + } + + void create(const class image& i) { glGenTextures(1, &_texture_id); - glBindTexture(gl_shape(), _texture_id); - -// glTexImage1D(gl_shape(), 0, GL_R8, cosAngleResolution, 0, -// GL_RED, GL_UNSIGNED_BYTE, &textureData[0]); -// glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_BASE_LEVEL, 0); -// glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAX_LEVEL, 0); -// glBindTexture(GL_TEXTURE_1D, 0); - - glGenSamplers(1, &_texture_sampler); - glSamplerParameteri(_texture_sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glSamplerParameteri(_texture_sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glSamplerParameteri(_texture_sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); // glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); - glBindTexture(gl_shape(),0); - } + // glTexImage1D(gl_shape(), 0, GL_R8, cosAngleResolution, 0, + // GL_RED, GL_UNSIGNED_BYTE, &textureData[0]); + // glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_BASE_LEVEL, 0); + // glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAX_LEVEL, 0); + // glBindTexture(GL_TEXTURE_1D, 0); + } + void destroy() { @@ -85,17 +103,15 @@ texture::texture() { } -texture::texture(image_ref i, texture::data_layout s, texture::data_type t) +texture::texture(image_ref i, texture::data_layout s) { texture(); set_image(i); - set_shape(s); - set_type(t); + set_shape(s); } texture::~texture() { - } void texture::set_image(image_ref i) @@ -103,11 +119,6 @@ void texture::set_image(image_ref i) _image = i; } -void texture::set_type(texture::data_type t) -{ - _type = t; -} - void texture::set_shape(data_layout s) { _shape = s;