some small refactorings

This commit is contained in:
Hartmut Seichter 2019-02-18 19:31:53 +01:00
parent d8fac9045b
commit 8eda3df225
23 changed files with 314 additions and 268 deletions

View file

@ -17,7 +17,7 @@ public:
shader();
~shader();
enum code_type {
enum class code_type {
vertex,
fragment,
geometry,

View file

@ -9,12 +9,12 @@ namespace pw {
class texture {
enum texture_type {
enum class data_type {
color,
normal
};
enum texture_shape {
enum class data_layout {
shape_1d,
shape_2d,
shape_3d
@ -35,16 +35,16 @@ class texture {
texture();
texture(shared_ptr<image> i,texture_shape s,texture_type = color);
texture(shared_ptr<image> i,data_layout s,data_type = data_type::color);
void set_image(shared_ptr<image> i);
shared_ptr<image> get() const { return _image; }
void set_type(texture_type t);
texture_type type() const { return _type; }
void set_type(data_type t);
data_type type() const { return _type; }
void set_shape(texture_shape s);
texture_shape shape() const { return _shape; }
void set_shape(data_layout s);
data_layout shape() const { return _shape; }
void set_wrap(wrap_mode w);
wrap_mode wrap() const { return _wrap; }
@ -55,8 +55,8 @@ protected:
shared_ptr<image> _image;
texture_type _type;
texture_shape _shape;
data_type _type;
data_layout _shape;
wrap_mode _wrap;
struct impl;

View file

@ -16,7 +16,7 @@ public:
~vertex_array();
void create(const mesh &m);
void destroy();
void release();
void draw();
bool ready() const;

View file

@ -1,10 +1,10 @@
#include "pw/core/size.hpp"
#include "pw/core/matrix.hpp"
#include "pw/core/mesh.hpp"
#include "pw/core/timer.hpp"
#include "pw/core/time.hpp"
#include "pw/core/axisangle.hpp"
#include "pw/core/serialize.hpp"
#include "pw/core/transform_tools.hpp"
#include "pw/core/matrix_transform.hpp"
#include "pw/core/debug.hpp"
#include "pw/visual/pipeline.hpp"
@ -20,295 +20,301 @@ class command {
};
class mesh_command : command {
// shader
// vertexarray
// shader
// vertexarray
};
class queue {
// vector<commands> ...
// vector<commands> ...
};
struct triangle_renderer
{
// GLuint vbo = 0;
// GLuint vao = 0;
// GLuint shader_programme = 0;
// GLuint vbo = 0;
// GLuint vao = 0;
// GLuint shader_programme = 0;
shader shader_p;
mesh amesh;
shader shader_p;
mesh amesh;
vertex_array amesh_renderer;
timer::tick_t tick;
time::tick_t tick;
triangle_renderer()
{
}
triangle_renderer()
{
}
void setup()
{
void setup()
{
const float z_val = -5.f;
mesh::vertex3array_t vertices = {
mesh::vertex3array_t vertices = {
{ 0.0f, 0.5f, z_val} // 0
,{ 0.5f, -0.5f, z_val} // 1
,{-0.5f, -0.5f, z_val} // 2
};
,{ 0.5f, 0.0f, z_val} // 1
,{-0.5f, 0.0f, z_val} // 2
};
// actual indices
mesh::indexarray_t indices = { 0, 1, 2};
mesh::indexarray_t indices = { 0, 1, 2};
amesh.set_indices(indices);
amesh.set_vertices(vertices);
amesh.set_indices(indices);
amesh.set_vertices(vertices);
amesh_renderer.create(amesh);
const char* vertex_shader_2 = R"(
#version 400
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
#version 400
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
in vec3 vertex_p;
in vec3 vertex_p;
void main() {
gl_Position = projection * view * model * vec4(vertex_p, 1.0);
}
)";
void main() {
gl_Position = projection * view * model * vec4(vertex_p, 1.0);
}
)";
const char *fragment_shader_2 = R"(
#version 400
uniform vec4 input_color = vec4(1.0, 0.0, 0.0, 1.0);
out vec4 frag_colour;
void main() {
frag_colour = input_color;
})";
#version 400
uniform vec4 input_color = vec4(1.0, 0.0, 0.0, 1.0);
out vec4 frag_colour;
void main() {
frag_colour = input_color;
})";
shader_p.set_source(vertex_shader_2,shader::vertex);
shader_p.set_source(fragment_shader_2,shader::fragment);
shader_p.set_source(vertex_shader_2,shader::code_type::vertex);
shader_p.set_source(fragment_shader_2,shader::code_type::fragment);
if (!shader_p.build())
exit(-1);
}
}
void draw()
{
void draw()
{
shader_p.use();
shader_p.use();
auto v_col = ping_pong(static_cast<float>(timer::now()),1.0f);
auto v_col = ping_pong(static_cast<float>(time::now()),1.0f);
vector4f col({0.5f,1-v_col,v_col,1.0f});
matrix4x4f model_mat; model_mat.set_identity();
matrix4x4f model_mat; model_mat.set_identity();
auto v_angle = ping_pong(static_cast<float>(timer::now()),::pw::pi<float>());
auto v_angle = ping_pong(static_cast<float>(time::now()),2 * ::pw::pi<float>());
axisangle rot(vector3::forward(),v_angle);
model_mat = rot.to_matrix();
model_mat = rot.to_matrix();
matrix4x4f view_mat = transform_tools<float>::look_at(vector3({0,0,0}),
matrix4x4f view_mat = matrix_transform<float>::look_at(vector3({0,0,0}),
vector3::forward(),
vector3::up());
matrix4x4f proj_mat = transform_tools<float>::perspective_projection(deg_to_rad(60.0f),
view_mat.set_identity();
matrix4x4f proj_mat = matrix_transform<float>::perspective_projection(deg_to_rad(33.0f),
1.3f,
0.2f,1000.f);
// materials should carry this
// glEnable(GL_CULL_FACE);
// highly inefficient - should be cached -
//proj_mat = transform_tools<float>::orthographic_projection(0.5,0.2f,100.f);
// highly inefficient - should be cached -
shader_p.set("input_color",col);
shader_p.set("model",model_mat);
shader_p.set("view",view_mat);
shader_p.set("projection",proj_mat);
// new version with ...
shader::uniform_set us;
us["input_color"] = col;
shader_p.set_uniforms(us);
// shader_p.set_uniforms(us);
amesh_renderer.draw();
// debug::d() << 100 * (timer::now() - t0) << "ms";
}
// debug::d() << 100 * (timer::now() - t0) << "ms";
}
};
struct pipeline::impl {
sizei _size;
sizei _size;
GLuint _fbo_draw;
GLuint _fbo_msaa;
GLuint _fbo_draw;
GLuint _fbo_msaa;
GLuint _rbo_color;
GLuint _rbo_depth;
GLuint _rbo_color;
GLuint _rbo_depth;
//testing
triangle_renderer tr;
//testing
triangle_renderer tr;
bool create(sizei size);
bool create(sizei size);
void draw();
void draw();
impl() = default;
~impl() = default;
impl() = default;
~impl() = default;
};
GLuint generate_multisample_texture(const sizei& s,int samples)
{
GLuint texture;
glGenTextures(1, &texture);
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, texture);
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, GL_RGB8, s.width, s.height, GL_TRUE);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, texture);
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, GL_RGB8, s.width, s.height, GL_TRUE);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0);
return texture;
return texture;
}
bool pipeline::impl::create(sizei size)
{
_size = size;
_size = size;
int max_msaa;
int max_msaa;
// query actual maximum MSAA
glGetIntegerv(GL_MAX_SAMPLES,&max_msaa);
// query actual maximum MSAA
glGetIntegerv(GL_MAX_SAMPLES,&max_msaa);
// create a 4x MSAA renderbuffer object for colorbuffer
int msaa = std::min(max_msaa,4);
// create a 4x MSAA renderbuffer object for colorbuffer
int msaa = std::min(max_msaa,4);
debug::d() << "OpenGL multisampling: " << max_msaa << " choosen:" << msaa;
debug::d() << "OpenGL multisampling: " << max_msaa << " choosen:" << msaa;
glGenRenderbuffers(1, &_rbo_color);
glBindRenderbuffer(GL_RENDERBUFFER, _rbo_color);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, msaa, GL_RGBA8, _size.width, _size.height);
glGenRenderbuffers(1, &_rbo_color);
glBindRenderbuffer(GL_RENDERBUFFER, _rbo_color);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, msaa, GL_RGBA8, _size.width, _size.height);
// create a 4x MSAA renderbuffer object for depthbuffer
// create a 4x MSAA renderbuffer object for depthbuffer
glGenRenderbuffers(1, &_rbo_depth);
glBindRenderbuffer(GL_RENDERBUFFER, _rbo_depth);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, msaa, GL_DEPTH_COMPONENT, _size.width, _size.height);
glGenRenderbuffers(1, &_rbo_depth);
glBindRenderbuffer(GL_RENDERBUFFER, _rbo_depth);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, msaa, GL_DEPTH_COMPONENT, _size.width, _size.height);
// create a 4x MSAA framebuffer object
glGenFramebuffers(1, &_fbo_msaa);
glBindFramebuffer(GL_FRAMEBUFFER, _fbo_msaa);
// create a 4x MSAA framebuffer object
glGenFramebuffers(1, &_fbo_msaa);
glBindFramebuffer(GL_FRAMEBUFFER, _fbo_msaa);
// attach colorbuffer image to FBO
glFramebufferRenderbuffer(GL_FRAMEBUFFER, // 1. fbo target: GL_FRAMEBUFFER
GL_COLOR_ATTACHMENT0, // 2. color attachment point
GL_RENDERBUFFER, // 3. rbo target: GL_RENDERBUFFER
_rbo_color); // 4. rbo ID
// attach colorbuffer image to FBO
glFramebufferRenderbuffer(GL_FRAMEBUFFER, // 1. fbo target: GL_FRAMEBUFFER
GL_COLOR_ATTACHMENT0, // 2. color attachment point
GL_RENDERBUFFER, // 3. rbo target: GL_RENDERBUFFER
_rbo_color); // 4. rbo ID
// attach depthbuffer image to FBO
glFramebufferRenderbuffer(GL_FRAMEBUFFER, // 1. fbo target: GL_FRAMEBUFFER
GL_DEPTH_ATTACHMENT, // 2. depth attachment point
GL_RENDERBUFFER, // 3. rbo target: GL_RENDERBUFFER
_rbo_depth); // 4. rbo ID
// attach depthbuffer image to FBO
glFramebufferRenderbuffer(GL_FRAMEBUFFER, // 1. fbo target: GL_FRAMEBUFFER
GL_DEPTH_ATTACHMENT, // 2. depth attachment point
GL_RENDERBUFFER, // 3. rbo target: GL_RENDERBUFFER
_rbo_depth); // 4. rbo ID
// check FBO status
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
return false;
// check FBO status
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
return false;
// reset
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// reset
glBindFramebuffer(GL_FRAMEBUFFER, 0);
tr.setup();
tr.setup();
return true;
return true;
}
void pipeline::impl::draw()
{
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, _fbo_msaa);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, _fbo_msaa);
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//
// draw pass
//
glViewport(0,0,800,600);
tr.draw();
//
// draw pass
//
tr.draw();
// reset
// reset
// actuall blitting
// actuall blitting
#if 0
/* We are going to blit into the window (default framebuffer) */
glBindFramebuffer (GL_DRAW_FRAMEBUFFER, 0);
glDrawBuffer (GL_BACK); /* Use backbuffer as color dst. */
/* We are going to blit into the window (default framebuffer) */
glBindFramebuffer (GL_DRAW_FRAMEBUFFER, 0);
glDrawBuffer (GL_BACK); /* Use backbuffer as color dst. */
/* Read from your FBO */
glBindFramebuffer (GL_READ_FRAMEBUFFER, _fbo_draw );
glReadBuffer (GL_COLOR_ATTACHMENT0); /* Use Color Attachment 0 as color src. */
/* Read from your FBO */
glBindFramebuffer (GL_READ_FRAMEBUFFER, _fbo_draw );
glReadBuffer (GL_COLOR_ATTACHMENT0); /* Use Color Attachment 0 as color src. */
/* Copy the color and depth buffer from your FBO to the default framebuffer */
glBlitFramebuffer (0,0, _size.width, _size.height,
0,0, _size.width, _size.height,
GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT,
GL_NEAREST);
/* Copy the color and depth buffer from your FBO to the default framebuffer */
glBlitFramebuffer (0,0, _size.width, _size.height,
0,0, _size.width, _size.height,
GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT,
GL_NEAREST);
#else
glBindFramebuffer(GL_READ_FRAMEBUFFER, _fbo_msaa); // src FBO (multi-sample)
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, _fbo_draw); // dst FBO (single-sample)
glBindFramebuffer(GL_READ_FRAMEBUFFER, _fbo_msaa); // src FBO (multi-sample)
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, _fbo_draw); // dst FBO (single-sample)
glBlitFramebuffer(0, 0, _size.width, _size.height, // src rect
0, 0, _size.width, _size.height, // dst rect
GL_COLOR_BUFFER_BIT, // buffer mask
GL_LINEAR); // scale filter
glBlitFramebuffer(0, 0, _size.width, _size.height, // src rect
0, 0, _size.width, _size.height, // dst rect
GL_COLOR_BUFFER_BIT, // buffer mask
GL_LINEAR); // scale filter
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
// debug::d() << _size.width << "x" << _size.height;
// debug::d() << _size.width << "x" << _size.height;
#endif
GLenum err;
while((err = glGetError()) != GL_NO_ERROR)
{
std::string error;
GLenum err;
while((err = glGetError()) != GL_NO_ERROR)
{
std::string error;
switch(err) {
case GL_INVALID_OPERATION: error="INVALID_OPERATION"; break;
case GL_INVALID_ENUM: error="INVALID_ENUM"; break;
case GL_INVALID_VALUE: error="INVALID_VALUE"; break;
case GL_OUT_OF_MEMORY: error="OUT_OF_MEMORY"; break;
case GL_INVALID_FRAMEBUFFER_OPERATION: error="INVALID_FRAMEBUFFER_OPERATION"; break;
}
switch(err) {
case GL_INVALID_OPERATION: error="INVALID_OPERATION"; break;
case GL_INVALID_ENUM: error="INVALID_ENUM"; break;
case GL_INVALID_VALUE: error="INVALID_VALUE"; break;
case GL_OUT_OF_MEMORY: error="OUT_OF_MEMORY"; break;
case GL_INVALID_FRAMEBUFFER_OPERATION: error="INVALID_FRAMEBUFFER_OPERATION"; break;
}
debug::e() << "OpenGL error:" << err << " " << error;
}
debug::e() << "OpenGL error:" << err << " " << error;
}
}
@ -318,23 +324,23 @@ void pipeline::impl::draw()
//
pipeline::pipeline()
: _impl(std::make_unique<pipeline::impl>())
: _impl(std::make_unique<pipeline::impl>())
{
}
pipeline::~pipeline()
{
//
//
}
void pipeline::draw()
{
_impl->draw();
_impl->draw();
}
bool pipeline::create(size s)
{
return _impl->create(s.cast<int>());
return _impl->create(s.cast<int>());
}
}

View file

@ -37,16 +37,16 @@ struct shader::impl
{
GLuint shader_type = 0;
switch (s.first) {
case shader::vertex:
case shader::code_type::vertex:
shader_type = GL_VERTEX_SHADER;
break;
case shader::compute:
case shader::code_type::compute:
shader_type = GL_COMPUTE_SHADER;
break;
case shader::geometry:
case shader::code_type::geometry:
shader_type = GL_GEOMETRY_SHADER;
break;
case shader::fragment:
case shader::code_type::fragment:
shader_type = GL_FRAGMENT_SHADER;
break;
}
@ -210,6 +210,7 @@ void shader::set_uniforms(shader::uniform_set s)
// },
// u.second);
std::tuple<std::string,int,uniform_t> uuu;
std::visit([u](auto&& arg) {
using T = std::decay_t<decltype(arg)>;

View file

@ -19,11 +19,11 @@ struct texture::impl {
GLuint gl_shape() {
switch (_host.shape()) {
case pw::texture::shape_1d:
case data_layout::shape_1d:
return GL_TEXTURE_1D;
case pw::texture::shape_2d:
case data_layout::shape_2d:
return GL_TEXTURE_2D;
case pw::texture::shape_3d:
case data_layout::shape_3d:
return GL_TEXTURE_3D;
}
}
@ -74,7 +74,7 @@ texture::texture()
_impl = make_unique<impl>(*this);
}
texture::texture(shared_ptr<image> i, texture::texture_shape s, texture::texture_type t)
texture::texture(shared_ptr<image> i, texture::data_layout s, texture::data_type t)
{
texture();
set_image(i);
@ -87,12 +87,12 @@ void texture::set_image(shared_ptr<image> i)
_image = i;
}
void texture::set_type(texture::texture_type t)
void texture::set_type(texture::data_type t)
{
_type = t;
}
void texture::set_shape(texture_shape s)
void texture::set_shape(data_layout s)
{
_shape = s;
}

View file

@ -20,7 +20,7 @@ struct vertex_array::impl {
~impl()
{
destroy();
release();
}
bool ready() const
@ -32,7 +32,7 @@ struct vertex_array::impl {
{
// not sure ...
if (ready()) {
destroy();
release();
}
glGenVertexArrays(1,&_vao);
@ -54,6 +54,7 @@ struct vertex_array::impl {
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);
@ -68,7 +69,7 @@ struct vertex_array::impl {
}
void destroy()
void release()
{
glDeleteVertexArrays(1,&_vao);
for (auto vbo : _vbos)
@ -114,9 +115,9 @@ void vertex_array::create(const mesh &m)
_impl->create(m);
}
void vertex_array::destroy()
void vertex_array::release()
{
_impl->destroy();
_impl->release();
}
void vertex_array::draw()