Clean up code for rendering and wrapping it to Lua

Now with an object oriented example.

Signed-off-by: Hartmut Seichter <hartmut@technotecture.com>
This commit is contained in:
Hartmut Seichter 2021-01-19 20:38:28 +01:00
parent 52c077af6c
commit 2c4cc29f97
21 changed files with 332 additions and 236 deletions

View file

@ -118,16 +118,16 @@ void register_core_function(sol::state& lua,sol::table& ns)
); );
ns.new_usertype<geometry>("geometry" ns.new_usertype<geometry>("geometry"
, sol::constructors<geometry(),geometry(geometry::topology_type)>() , sol::constructors<geometry()>()
, "topology", sol::property(&geometry::topology,&geometry::set_topology) , "primitive_type", sol::property(&geometry::primitive_type,&geometry::set_primitive_type)
, "vertices", sol::property(&geometry::vertices,&geometry::set_vertices) , "vertices", sol::property(&geometry::vertices,&geometry::set_vertices)
, "indices", sol::property(&geometry::indices,&geometry::set_indices) , "indices", sol::property(&geometry::indices,&geometry::set_indices)
, "", &geometry::compute_normals , "compute_normals", &geometry::compute_normals);
).new_enum<false>("geometry_type"
,"points", geometry::topology_type::points ns.new_enum<false>("primitive_type"
, "lines", geometry::topology_type::lines ,"points", geometry::primitive_type::points
, "line_strip", geometry::topology_type::line_strip ,"lines", geometry::primitive_type::lines
, "triangles", geometry::topology_type::triangles); ,"triangles", geometry::primitive_type::triangles);
ns.new_usertype<matrix_transform<real_t>>("matrixtransform" ns.new_usertype<matrix_transform<real_t>>("matrixtransform"

View file

@ -42,17 +42,14 @@ void register_visual_function(sol::state&,sol::table &ns)
,"submit",&render_pass::submit ,"submit",&render_pass::submit
); );
ns.new_usertype<material>("material"
,"color",sol::property(&material::_color));
ns.new_usertype<renderer>("renderer"
ns.new_usertype<renderer>("mesh_renderer"
,sol::constructors<renderer(),renderer(const geometry&)>() ,sol::constructors<renderer(),renderer(const geometry&)>()
,"create",&renderer::create ,"create",&renderer::create
,"ready",sol::readonly_property(&renderer::ready) ,"ready",sol::readonly_property(&renderer::ready)
,"release",&renderer::release ,"release",&renderer::release
,"draw",&renderer::draw ,"draw",&renderer::draw
); );
ns.new_usertype<framebuffer>("framebuffer" ns.new_usertype<framebuffer>("framebuffer"
,sol::constructors<framebuffer()>() ,sol::constructors<framebuffer()>()
@ -64,7 +61,6 @@ void register_visual_function(sol::state&,sol::table &ns)
ns.new_usertype<texture>("texture" ns.new_usertype<texture>("texture"
,sol::constructors<texture(),texture(texture::image_ref,texture::data_layout)>() ,sol::constructors<texture(),texture(texture::image_ref,texture::data_layout)>()
,"image",sol::property(&texture::set_image,&texture::image) ,"image",sol::property(&texture::set_image,&texture::image)
,"type",sol::property(&texture::set_type,&texture::type)
); );
} }

View file

@ -1,26 +1,27 @@
set(hdrs set(hdrs
include/pw/core/aabb.hpp include/pw/core/aabb.hpp
include/pw/core/axisangle.hpp include/pw/core/axisangle.hpp
include/pw/core/color.hpp include/pw/core/color.hpp
include/pw/core/core.hpp include/pw/core/core.hpp
include/pw/core/debug.hpp include/pw/core/debug.hpp
include/pw/core/globals.hpp include/pw/core/globals.hpp
include/pw/core/material.hpp
include/pw/core/math.hpp include/pw/core/math.hpp
include/pw/core/matrixbase.hpp include/pw/core/matrixbase.hpp
include/pw/core/matrix.hpp include/pw/core/matrix.hpp
include/pw/core/quaternion.hpp include/pw/core/quaternion.hpp
include/pw/core/image.hpp include/pw/core/image.hpp
include/pw/core/point.hpp include/pw/core/point.hpp
include/pw/core/resource.hpp include/pw/core/resource.hpp
include/pw/core/rectangle.hpp include/pw/core/rectangle.hpp
include/pw/core/serialize.hpp include/pw/core/serialize.hpp
include/pw/core/size.hpp include/pw/core/size.hpp
include/pw/core/time.hpp include/pw/core/time.hpp
include/pw/core/geometry.hpp include/pw/core/geometry.hpp
include/pw/core/image.hpp include/pw/core/image.hpp
include/pw/core/vector.hpp include/pw/core/vector.hpp
include/pw/core/matrix_transform.hpp include/pw/core/matrix_transform.hpp
) )
set(misc set(misc
@ -30,14 +31,15 @@ set(misc
) )
set(srcs set(srcs
# src/buffer.cpp # src/buffer.cpp
src/core.cpp src/core.cpp
src/image.cpp src/image.cpp
src/debug.cpp src/debug.cpp
src/geometry.cpp src/geometry.cpp
src/resource.cpp src/material.cpp
src/resource.cpp
src/serialize.cpp src/serialize.cpp
src/time.cpp src/time.cpp
src/image.cpp src/image.cpp
) )

View file

@ -28,9 +28,6 @@
#include <pw/core/aabb.hpp> #include <pw/core/aabb.hpp>
#include <pw/core/resource.hpp> #include <pw/core/resource.hpp>
#include <variant>
#include <tuple>
namespace pw { namespace pw {
/* /*
@ -43,68 +40,53 @@ namespace pw {
class geometry { class geometry {
public: public:
enum class topology_type { enum class primitive_type {
triangles, points,
triangle_strip,
triangle_fan,
quads,
lines, lines,
line_strip, triangles,
points polygons
}; };
using index_t = uint32_t; //< needs to be compatible with GL_UNSIGNED_INT using index_t = uint32_t; //< needs to be compatible with GL_UNSIGNED_INT
using indices_t = std::vector<index_t>;
using valuearray_t = std::vector<real_t> ;
using indexarray_t = std::vector<index_t> ;
using vertex2array_t = std::vector<vector2> ;
using vertex3array_t = std::vector<vector3> ;
using __attribute_t = std::variant<valuearray_t,indexarray_t,vertex2array_t,vertex3array_t>;
using __attribute_set = std::tuple<uint32_t,__attribute_t,int>;
geometry() = default; 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; } void set_primitive_type(primitive_type t) { _primitive_type = t; }
const indexarray_t& indices() const { return _indices; } primitive_type primitive_type() { return _primitive_type; }
void set_vertices(const vertex3array_t& v); void set_vertices(vector3_array v);
const vertex3array_t& vertices() const { return _vertices; } const vector3_array& vertices() const { return _vertices; }
void set_normals(const geometry::vertex3array_t &v); void set_indices(indices_t v);
const geometry::vertex3array_t& normals() const; const indices_t& indices() const;
void set_topology(topology_type t) { _topology = t; } void set_normals(vector3_array v);
topology_type topology() { return _topology; } const vector3_array& normals() const;
void transform(const matrix4x4& m); void transform(const matrix4x4& m);
void reset(); void compute_normals();
aabb get_aabb() const { return _aabb; } void compute_tangent_space();
void compute_normals(); aabb bounds() const;
void compute_bounds();
protected: protected:
topology_type _topology = topology_type::triangles; enum primitive_type _primitive_type = primitive_type::triangles;
indexarray_t _indices; //!< indices according to topology type
vertex3array_t _vertices; //!< geometry data vector3_array _vertices; //!< vertex data
vertex3array_t _normals; //!< normal data indices_t _indices; //!< indices
vertex3array_t _colors; //!< color data
std::vector<vertex2array_t> _uvs; //!< storing multiple UV sets vector3_array _normals; //!< normal data
vector3_array _tangents; //!< tangent data
vector3_array _bitangents; //!< bitangent
struct aabb _aabb; std::vector<vector3_array> _texture_coords; //! texture coordinates
// TODO add weights, tangents etc. pp.
uint64_t _change_count { 0 };
}; };
} }

View file

@ -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 <pw/core/vector.hpp>
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<texture_type,std::string,uint32_t>;
protected:
vector4 _color = vector4 { 1.0, 0.0, 1.0, 1.0 };
std::vector<texture_t> _textures;
};
}
#endif

View file

@ -113,8 +113,8 @@ struct matrix_transform {
matrix_<4,4,T> view_matrix; view_matrix.set_identity(); matrix_<4,4,T> view_matrix; view_matrix.set_identity();
const vector3_<T> los = (target - position).normalized(); // line of sight const vector3_<T> los = (target - position).normalized(); // line of sight
const vector3_<T> sid = los.cross(up).normalized(); const vector3_<T> sid = vector3_<T>::cross(los,up).normalized();
const vector3_<T> upd = sid.cross(los).normalized(); const vector3_<T> upd = vector3_<T>::cross(sid,los).normalized();
// set base vectors // set base vectors
view_matrix.set_slice(sid, 0, 0); view_matrix.set_slice(sid, 0, 0);

View file

@ -84,11 +84,11 @@ struct vector3_ : matrix_<3,1,T> {
inline auto xy() const { return vector2_( { x(),y() } ); } 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 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_( { return vector3_( {
(*this)[1] * rhs[2] - rhs[1] * (*this)[2], lhs[1] * rhs[2] - rhs[1] * lhs[2],
(*this)[2] * rhs[0] - rhs[2] * (*this)[0], lhs[2] * rhs[0] - rhs[2] * lhs[0],
(*this)[0] * rhs[1] - rhs[0] * (*this)[1] lhs[0] * rhs[1] - rhs[0] * lhs[1]
} }
); );
} }
@ -142,11 +142,14 @@ struct vector4_ : matrix_<4,1,T> {
using vector2f = vector2_<float>; using vector2f = vector2_<float>;
using vector2d = vector2_<double>; using vector2d = vector2_<double>;
using vector2i = vector2_<int>;
using vector2 = vector2_<real_t>; using vector2 = vector2_<real_t>;
using vector2_array = std::vector<vector2>;
using vector3f = vector3_<float>; using vector3f = vector3_<float>;
using vector3d = vector3_<double>; using vector3d = vector3_<double>;
using vector3 = vector3_<real_t>; using vector3 = vector3_<real_t>;
using vector3_array = std::vector<vector3>;
using vector4f = vector4_<float>; using vector4f = vector4_<float>;
using vector4d = vector4_<double>; using vector4d = vector4_<double>;

View file

@ -8,7 +8,7 @@ namespace pw {
void geometry::compute_normals() void geometry::compute_normals()
{ {
// assumption is that we have some array as the vertices // 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 indexed-faceset
for (size_t i = 1; i < _indices.size()-1;i++) 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] ); auto edgeRight = vector3( _vertices[ri] - _vertices[ci] );
// calculate counter clockwise and normalize // calculate counter clockwise and normalize
auto N = edgeRight.cross(edgeLeft).normalized(); auto N = vector3::cross(edgeRight,edgeLeft).normalized();
// NOTE that addition is ugly // NOTE that addition is ugly
normals[ci] = N + normals[ci]; normals[ci] = N + normals[ci];
@ -39,11 +39,7 @@ void geometry::compute_normals()
n.normalize(); n.normalize();
} }
this->set_normals(normals); // this->set_normals(normals);
for (auto N : normals) {
debug::s() << "( " << serialize::matrix(N.transposed()) << ") ";
}
// for triangle-strip // for triangle-strip
@ -55,29 +51,74 @@ void geometry::compute_normals()
// now set back // now set back
} }
geometry::geometry(geometry::topology_type t) void geometry::compute_tangent_space()
: _topology(t) {
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; _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; _normals = v;
} }
const geometry::vertex3array_t &geometry::normals() const const vector3_array &geometry::normals() const { return _normals; }
{
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) void geometry::transform(const matrix4x4 &m)
{ {
@ -86,31 +127,33 @@ void geometry::transform(const matrix4x4 &m)
v = vector4(m * v.homogenous()).project(); v = vector4(m * v.homogenous()).project();
// apply to normals // apply to normals
for(auto &n : _normals) // for(auto &n : _normals)
n = vector4(m * n.homogenous(0)).xyz(); // 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 // only do if there are vertices
if (_vertices.size()) { if (_vertices.size()) {
// reset // reset
_aabb.max = _aabb.min = _vertices.front(); b.max = b.min = _vertices.front();
// update // update
for (auto const & v : _vertices) { for (auto const & v : _vertices) {
_aabb.max.x() = std::max(v.x(),_aabb.max.x()); b.max.x() = std::max(v.x(),b.max.x());
_aabb.max.y() = std::max(v.y(),_aabb.max.y()); b.max.y() = std::max(v.y(),b.max.y());
_aabb.max.z() = std::max(v.z(),_aabb.max.z()); b.max.z() = std::max(v.z(),b.max.z());
_aabb.min.x() = std::min(v.x(),_aabb.min.x()); b.min.x() = std::min(v.x(),b.min.x());
_aabb.min.y() = std::min(v.y(),_aabb.min.y()); b.min.y() = std::min(v.y(),b.min.y());
_aabb.min.z() = std::min(v.z(),_aabb.min.z()); b.min.z() = std::min(v.z(),b.min.z());
} }
} }
return b;
} }
} }

View file

@ -0,0 +1,7 @@
#include "pw/core/material.hpp"
namespace pw {
};

View file

@ -8,39 +8,46 @@
int main(int argc,char **argv) { 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} }; pw::vector3_array vs = { {-1,-1,0},{1,-1,0},{0,1,0} };
amesh.set_vertices(vs);
for (auto v : amesh.vertices()) {
std::cout << pw::serialize::matrix(v.transposed()) << std::endl;
}
auto scale = pw::matrix_transform<float>::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 }); // for (auto v : amesh.vertices()) {
aa.angle = pw::deg_to_rad(90.0f); // std::cout << pw::serialize::matrix(v.transposed()) << std::endl;
// }
auto rot_mat = aa.to_matrix(); // auto scale = pw::matrix_transform<float>::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()) { // for (auto v : amesh.vertices()) {
std::cout << pw::serialize::matrix(v.transposed()) << std::endl; // 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;
// }

View file

@ -6,7 +6,7 @@ geometry primitives::box(real_t size_x,real_t size_y, real_t size_z)
{ {
geometry m; 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 } ); // 0
// vertices.push_back( { size_x / 2,-size_y / 2, size_z / 2 } ); // 1 // 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; geometry geom;
geom.set_topology(geometry::topology_type::triangle_strip); // geom.set_primitive_type(geometry::primitive_type::triangle_strip);
real_t x, y, z; real_t x, y, z;

View file

@ -19,7 +19,7 @@ w.position = pw.point.new(100,100)
-- create a new geometry -- create a new geometry
local g = pw.geometry:new() local g = pw.geometry:new()
g.topology = pw.geometry.triangles -- meh g.primitive_type = pw.primitive_type.triangles -- meh
g.vertices:clear() g.vertices:clear()
@ -79,7 +79,7 @@ if not s:build() then
print("Error!") print("Error!")
end end
local renderer = pw.mesh_renderer:new() local renderer = pw.renderer:new()
if not renderer:create(g) then if not renderer:create(g) then
print("couldn't create renderer") print("couldn't create renderer")

View file

@ -5,7 +5,6 @@ set(hdrs
include/pw/visual/pipeline.hpp include/pw/visual/pipeline.hpp
include/pw/visual/texture.hpp include/pw/visual/texture.hpp
include/pw/visual/renderer.hpp include/pw/visual/renderer.hpp
include/pw/visual/material.hpp
) )
set(srcs set(srcs
@ -17,7 +16,6 @@ set(srcs
src/target.cpp src/target.cpp
src/texture.cpp src/texture.cpp
src/renderer.cpp src/renderer.cpp
src/material.cpp
) )

View file

@ -1,13 +0,0 @@
#pragma once
#include <pw/core/vector.hpp>
namespace pw {
struct material {
vector4f _color = vector4f {0xFF,0x00,0xFF,0xFF};
material() = default;
};
}

View file

@ -8,7 +8,6 @@
#include <pw/visual/shader.hpp> #include <pw/visual/shader.hpp>
#include <pw/visual/renderer.hpp> #include <pw/visual/renderer.hpp>
#include <pw/visual/material.hpp>
#include <map> #include <map>
@ -41,8 +40,7 @@ struct render_pass {
void submit(const geometry& g, void submit(const geometry& g,
const matrix4x4& model_mat, const matrix4x4& model_mat,
const matrix4x4& view_mat, const matrix4x4& view_mat,
const matrix4x4& projection_mat, const matrix4x4& projection_mat);
const material& mat);
shader _shader; shader _shader;
renderer _renderer; renderer _renderer;

View file

@ -21,9 +21,9 @@ public:
}; };
enum texture_dimension { enum texture_dimension {
dimension_r,
dimension_s, dimension_s,
dimension_t, dimension_t
dimension_r
}; };
enum wrap_mode { enum wrap_mode {
@ -36,16 +36,13 @@ public:
using image_ref = shared_ptr<::pw::image>; using image_ref = shared_ptr<::pw::image>;
texture(); texture();
texture(image_ref i,data_layout s,data_type = data_type::color); texture(image_ref i,data_layout s);
~texture(); ~texture();
void set_image(image_ref i); void set_image(image_ref i);
image_ref image() const { return _image; } image_ref image() const { return _image; }
void set_type(data_type t);
data_type type() const { return _type; }
void set_shape(data_layout s); void set_shape(data_layout s);
data_layout shape() const { return _shape; } data_layout shape() const { return _shape; }
@ -57,11 +54,11 @@ public:
uint32_t native_handle() const; uint32_t native_handle() const;
uint32_t native_sampler_handle() const; uint32_t native_sampler_handle() const;
protected: protected:
image_ref _image; image_ref _image;
data_type _type = data_type::color;
data_layout _shape = data_layout::shape_2d; data_layout _shape = data_layout::shape_2d;
wrap_mode _wrap = wrap_mode::wrap_clamp_to_edge; wrap_mode _wrap = wrap_mode::wrap_clamp_to_edge;

View file

@ -5,7 +5,6 @@
#include <pw/core/geometry.hpp> #include <pw/core/geometry.hpp>
#include <pw/core/buffer.hpp> #include <pw/core/buffer.hpp>
#include <pw/visual/material.hpp>
#include <pw/visual/shader.hpp> #include <pw/visual/shader.hpp>
#include <pw/visual/renderer.hpp> #include <pw/visual/renderer.hpp>

View file

@ -10,7 +10,6 @@
#include "pw/visual/pipeline.hpp" #include "pw/visual/pipeline.hpp"
#include "pw/visual/shader.hpp" #include "pw/visual/shader.hpp"
#include "pw/visual/renderer.hpp" #include "pw/visual/renderer.hpp"
#include "pw/visual/material.hpp"
#include "pw/visual/framebuffer.hpp" #include "pw/visual/framebuffer.hpp"
#include "pw/visual/texture.hpp" #include "pw/visual/texture.hpp"
@ -22,8 +21,7 @@ namespace pw {
void render_pass::submit(const geometry &g, void render_pass::submit(const geometry &g,
const matrix4x4 &model_mat, const matrix4x4 &model_mat,
const matrix4x4 &view_mat, const matrix4x4 &view_mat,
const matrix4x4 &projection_mat, const matrix4x4 &projection_mat)
const material &mat)
{ {
if (!_shader.ready()) if (!_shader.ready())
{ // { //
@ -37,7 +35,7 @@ void render_pass::submit(const geometry &g,
// new version with ... // new version with ...
shader::uniform_cache_t us; 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("model",model_mat,-1));
us.push_back(std::make_tuple("view",view_mat,-1)); us.push_back(std::make_tuple("view",view_mat,-1));
us.push_back(std::make_tuple("projection",projection_mat,-1)); us.push_back(std::make_tuple("projection",projection_mat,-1));
@ -76,7 +74,7 @@ struct triangle_renderer
// 2 -- 3 // 2 -- 3
// geometry // geometry
geometry::vertex3array_t vertices = { vector3_array vertices = {
{-s, s, z_val} // 0 {-s, s, z_val} // 0
,{ s, s, z_val} // 1 ,{ s, s, z_val} // 1
,{-s, -s, z_val} // 2 ,{-s, -s, z_val} // 2
@ -84,7 +82,7 @@ struct triangle_renderer
}; };
// topology / indices // topology / indices
geometry::indexarray_t indices = { 2, 1, 0, geometry::indices_t indices = { 2, 1, 0,
2, 3, 1}; 2, 3, 1};

View file

@ -13,8 +13,10 @@ namespace pw {
struct renderer::impl { struct renderer::impl {
GLuint _vao = 0; uint32_t _vao { 0 };
std::vector<GLuint> _vbos; uint32_t _ebo { 0 };
uint32_t _vbo { 0 };
GLint _mesh_elements = { 0 }; GLint _mesh_elements = { 0 };
impl() = default; impl() = default;
@ -36,47 +38,43 @@ struct renderer::impl {
release(); 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(); _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()); // TODO binding separate VBOs to the
// vertexarray should be avoided
// 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);
// indices // indices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vbos[1]); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 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); 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 // stop binding
glBindVertexArray(0); glBindVertexArray(0);
// get errors
auto error = glGetError(); auto error = glGetError();
if (error != GL_NO_ERROR) { if (error != GL_NO_ERROR) {
debug::e() << "GL error: " << error; debug::e() << __PRETTY_FUNCTION__ << " GL error: " << error;
} }
return ready(); return ready();
@ -85,11 +83,11 @@ struct renderer::impl {
void release() 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() void draw()
@ -98,23 +96,20 @@ struct renderer::impl {
glEnable(GL_CULL_FACE); glEnable(GL_CULL_FACE);
glCullFace(GL_BACK); glCullFace(GL_BACK);
glFrontFace(GL_CCW); glFrontFace(GL_CCW);
#endif
glClearColor(0.0,1.0,0,1); glClearColor(0.0,1.0,0,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 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)
{ {
debug::e() << "GL error: " << 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;
}
} }
} }

View file

@ -39,29 +39,47 @@ struct texture::impl {
return 0; 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); 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_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
// glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_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() 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(); texture();
set_image(i); set_image(i);
set_shape(s); set_shape(s);
set_type(t);
} }
texture::~texture() texture::~texture()
{ {
} }
void texture::set_image(image_ref i) void texture::set_image(image_ref i)
@ -103,11 +119,6 @@ void texture::set_image(image_ref i)
_image = i; _image = i;
} }
void texture::set_type(texture::data_type t)
{
_type = t;
}
void texture::set_shape(data_layout s) void texture::set_shape(data_layout s)
{ {
_shape = s; _shape = s;