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:
parent
52c077af6c
commit
2c4cc29f97
21 changed files with 332 additions and 236 deletions
|
@ -118,16 +118,16 @@ void register_core_function(sol::state& lua,sol::table& ns)
|
|||
);
|
||||
|
||||
ns.new_usertype<geometry>("geometry"
|
||||
, sol::constructors<geometry(),geometry(geometry::topology_type)>()
|
||||
, "topology", sol::property(&geometry::topology,&geometry::set_topology)
|
||||
, sol::constructors<geometry()>()
|
||||
, "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<false>("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<false>("primitive_type"
|
||||
,"points", geometry::primitive_type::points
|
||||
,"lines", geometry::primitive_type::lines
|
||||
,"triangles", geometry::primitive_type::triangles);
|
||||
|
||||
|
||||
ns.new_usertype<matrix_transform<real_t>>("matrixtransform"
|
||||
|
|
|
@ -42,11 +42,8 @@ void register_visual_function(sol::state&,sol::table &ns)
|
|||
,"submit",&render_pass::submit
|
||||
);
|
||||
|
||||
ns.new_usertype<material>("material"
|
||||
,"color",sol::property(&material::_color));
|
||||
|
||||
|
||||
ns.new_usertype<renderer>("mesh_renderer"
|
||||
ns.new_usertype<renderer>("renderer"
|
||||
,sol::constructors<renderer(),renderer(const geometry&)>()
|
||||
,"create",&renderer::create
|
||||
,"ready",sol::readonly_property(&renderer::ready)
|
||||
|
@ -64,7 +61,6 @@ void register_visual_function(sol::state&,sol::table &ns)
|
|||
ns.new_usertype<texture>("texture"
|
||||
,sol::constructors<texture(),texture(texture::image_ref,texture::data_layout)>()
|
||||
,"image",sol::property(&texture::set_image,&texture::image)
|
||||
,"type",sol::property(&texture::set_type,&texture::type)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ set(hdrs
|
|||
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
|
||||
|
@ -35,6 +36,7 @@ set(srcs
|
|||
src/image.cpp
|
||||
src/debug.cpp
|
||||
src/geometry.cpp
|
||||
src/material.cpp
|
||||
src/resource.cpp
|
||||
src/serialize.cpp
|
||||
src/time.cpp
|
||||
|
|
|
@ -28,9 +28,6 @@
|
|||
#include <pw/core/aabb.hpp>
|
||||
#include <pw/core/resource.hpp>
|
||||
|
||||
#include <variant>
|
||||
#include <tuple>
|
||||
|
||||
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<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>;
|
||||
using indices_t = std::vector<index_t>;
|
||||
|
||||
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();
|
||||
|
||||
aabb get_aabb() const { return _aabb; }
|
||||
|
||||
void compute_normals();
|
||||
|
||||
void compute_bounds();
|
||||
void compute_tangent_space();
|
||||
|
||||
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<vertex2array_t> _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<vector3_array> _texture_coords; //! texture coordinates
|
||||
|
||||
uint64_t _change_count { 0 };
|
||||
};
|
||||
|
||||
}
|
||||
|
|
73
src/core/include/pw/core/material.hpp
Normal file
73
src/core/include/pw/core/material.hpp
Normal 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
|
|
@ -113,8 +113,8 @@ struct matrix_transform {
|
|||
matrix_<4,4,T> view_matrix; view_matrix.set_identity();
|
||||
|
||||
const vector3_<T> los = (target - position).normalized(); // line of sight
|
||||
const vector3_<T> sid = los.cross(up).normalized();
|
||||
const vector3_<T> upd = sid.cross(los).normalized();
|
||||
const vector3_<T> sid = vector3_<T>::cross(los,up).normalized();
|
||||
const vector3_<T> upd = vector3_<T>::cross(sid,los).normalized();
|
||||
|
||||
// set base vectors
|
||||
view_matrix.set_slice(sid, 0, 0);
|
||||
|
|
|
@ -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_<float>;
|
||||
using vector2d = vector2_<double>;
|
||||
using vector2i = vector2_<int>;
|
||||
using vector2 = vector2_<real_t>;
|
||||
using vector2_array = std::vector<vector2>;
|
||||
|
||||
using vector3f = vector3_<float>;
|
||||
using vector3d = vector3_<double>;
|
||||
using vector3 = vector3_<real_t>;
|
||||
using vector3_array = std::vector<vector3>;
|
||||
|
||||
using vector4f = vector4_<float>;
|
||||
using vector4d = vector4_<double>;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
7
src/core/src/material.cpp
Normal file
7
src/core/src/material.cpp
Normal file
|
@ -0,0 +1,7 @@
|
|||
#include "pw/core/material.hpp"
|
||||
|
||||
|
||||
namespace pw {
|
||||
|
||||
|
||||
};
|
|
@ -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<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 });
|
||||
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<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()) {
|
||||
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;
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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
|
||||
)
|
||||
|
||||
|
||||
|
|
|
@ -1,13 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <pw/core/vector.hpp>
|
||||
|
||||
|
||||
namespace pw {
|
||||
|
||||
struct material {
|
||||
vector4f _color = vector4f {0xFF,0x00,0xFF,0xFF};
|
||||
material() = default;
|
||||
};
|
||||
|
||||
}
|
|
@ -8,7 +8,6 @@
|
|||
|
||||
#include <pw/visual/shader.hpp>
|
||||
#include <pw/visual/renderer.hpp>
|
||||
#include <pw/visual/material.hpp>
|
||||
|
||||
|
||||
#include <map>
|
||||
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
#include <pw/core/geometry.hpp>
|
||||
#include <pw/core/buffer.hpp>
|
||||
|
||||
#include <pw/visual/material.hpp>
|
||||
#include <pw/visual/shader.hpp>
|
||||
#include <pw/visual/renderer.hpp>
|
||||
|
||||
|
|
|
@ -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};
|
||||
|
||||
|
||||
|
|
|
@ -13,8 +13,10 @@ namespace pw {
|
|||
|
||||
struct renderer::impl {
|
||||
|
||||
GLuint _vao = 0;
|
||||
std::vector<GLuint> _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,13 +96,11 @@ 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);
|
||||
|
@ -116,7 +112,6 @@ struct renderer::impl {
|
|||
debug::e() << "GL error: " << error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GLint get_mode(vertex_array::)
|
||||
|
||||
|
|
|
@ -39,30 +39,48 @@ 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);
|
||||
|
||||
|
||||
|
||||
// glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||
// glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue