added proper license in core various poking into getting context > system > renderer properly setup
This commit is contained in:
parent
c7c7d5af77
commit
3cc0fde1e1
25 changed files with 628 additions and 236 deletions
|
@ -1,12 +1,12 @@
|
|||
|
||||
set(hdrs
|
||||
include/pw/visual/renderer.hpp
|
||||
include/pw/visual/mesh_renderer.hpp
|
||||
include/pw/visual/shader.hpp
|
||||
include/pw/visual/context.hpp
|
||||
)
|
||||
|
||||
set(srcs
|
||||
src/renderer.cpp
|
||||
src/mesh_renderer.cpp
|
||||
src/shader.cpp
|
||||
src/context.cpp
|
||||
)
|
||||
|
|
|
@ -1,17 +1,36 @@
|
|||
/*
|
||||
* Copyright (c) 1999-2019 Hartmut Seichter
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef PW_VISUAL_CONTEXT_HPP
|
||||
#define PW_VISUAL_CONTEXT_HPP
|
||||
|
||||
#include <pw/scene/component.hpp>
|
||||
#include <pw/core/size.hpp>
|
||||
|
||||
namespace pw {
|
||||
|
||||
class context {
|
||||
public:
|
||||
|
||||
struct size {
|
||||
int width,height;
|
||||
};
|
||||
|
||||
virtual bool make_current() = 0;
|
||||
virtual void resize() = 0;
|
||||
virtual size size() = 0;
|
||||
|
|
|
@ -1,9 +1,6 @@
|
|||
#ifndef PW_VISUAL_RENDERER_HPP
|
||||
#define PW_VISUAL_RENDERER_HPP
|
||||
|
||||
//#include <pw/scene/component.hpp>
|
||||
//#include <pw/scene/camera.hpp>
|
||||
|
||||
#include <pw/core/matrix.hpp>
|
||||
#include <pw/core/mesh.hpp>
|
||||
|
||||
|
@ -11,7 +8,7 @@
|
|||
|
||||
namespace pw {
|
||||
|
||||
class renderer {
|
||||
class mesh_renderer {
|
||||
public:
|
||||
|
||||
void render(const mesh& mesh,
|
||||
|
@ -29,5 +26,4 @@ protected:
|
|||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include <pw/core/globals.hpp>
|
||||
#include <pw/core/matrix.hpp>
|
||||
#include <pw/core/debug.hpp>
|
||||
|
||||
#include <map>
|
||||
|
||||
|
@ -27,6 +28,19 @@ public:
|
|||
|
||||
bool ready() const;
|
||||
|
||||
shader& bind(int location,float v);
|
||||
shader& bind(int location,matrix4x4f const & v);
|
||||
|
||||
int uniform_location(std::string const & name);
|
||||
|
||||
template<typename T> shader & bind(std::string const & name, T&& value)
|
||||
{
|
||||
int location = uniform_location(name);
|
||||
if (location == -1) debug::e() << "missing uniform: "<< name;
|
||||
else bind(location, std::forward<T>(value));
|
||||
return *this;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
std::map<code_type,std::string> _source;
|
||||
|
|
178
src/visual/src/mesh_renderer.cpp
Normal file
178
src/visual/src/mesh_renderer.cpp
Normal file
|
@ -0,0 +1,178 @@
|
|||
#include "pw/visual/mesh_renderer.hpp"
|
||||
|
||||
#include "pw/core/mesh.hpp"
|
||||
#include "pw/core/size.hpp"
|
||||
|
||||
#include "glad/glad.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace pw {
|
||||
|
||||
|
||||
struct mesh_renderer::impl {
|
||||
|
||||
GLuint _vao = 0;
|
||||
std::vector<GLuint> _vbos;
|
||||
|
||||
impl(mesh_renderer& )
|
||||
{
|
||||
}
|
||||
|
||||
void create(std::shared_ptr<mesh> mesh)
|
||||
{
|
||||
glGenVertexArrays(1,&_vao);
|
||||
glBindVertexArray(_vao);
|
||||
|
||||
size_t arrays_needed = 0;
|
||||
|
||||
// should bail out here
|
||||
if (!mesh->vertices().empty()) arrays_needed++;
|
||||
if (!mesh->indices().empty()) arrays_needed++;
|
||||
|
||||
// TODO: implement the other arrays
|
||||
|
||||
_vbos.resize(arrays_needed);
|
||||
|
||||
glGenBuffers(_vbos.size(), _vbos.data());
|
||||
|
||||
// vertices
|
||||
glBindBuffer(GL_ARRAY_BUFFER, _vbos[0]);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(mesh->vertices().front()) * mesh->vertices().size(), mesh->vertices().data(),
|
||||
GL_STATIC_DRAW);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
|
||||
|
||||
// indices
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vbos[1]);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(mesh->indices().front()) * mesh->indices().size(), mesh->indices().data(),
|
||||
GL_STATIC_DRAW);
|
||||
|
||||
|
||||
// stop binding
|
||||
glBindVertexArray(0);
|
||||
|
||||
}
|
||||
|
||||
void destroy()
|
||||
{
|
||||
glDeleteVertexArrays(1,&_vao);
|
||||
for (auto vbo : _vbos)
|
||||
glDeleteBuffers(1,&vbo);
|
||||
|
||||
_vbos.clear();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
struct renderer {
|
||||
|
||||
sizei _size;
|
||||
|
||||
GLuint _fbo_draw;
|
||||
GLuint _fbo_msaa;
|
||||
|
||||
GLuint rboColorId;
|
||||
GLuint rboDepthId;
|
||||
|
||||
bool create()
|
||||
{
|
||||
int max_msaa;
|
||||
|
||||
// query actual maximum MSAA
|
||||
glGetIntegerv(GL_MAX_SAMPLES,&max_msaa);
|
||||
|
||||
// create a 4x MSAA renderbuffer object for colorbuffer
|
||||
int msaa = 4;
|
||||
|
||||
// msaa = std::clamp(msaa,max_msaa);
|
||||
|
||||
glGenRenderbuffers(1, &rboColorId);
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, rboColorId);
|
||||
glRenderbufferStorageMultisample(GL_RENDERBUFFER, msaa, GL_RGB8, _size.width, _size.height);
|
||||
|
||||
// create a 4x MSAA renderbuffer object for depthbuffer
|
||||
|
||||
glGenRenderbuffers(1, &rboDepthId);
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, rboDepthId);
|
||||
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);
|
||||
|
||||
// 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
|
||||
rboColorId); // 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
|
||||
rboDepthId); // 4. rbo ID
|
||||
|
||||
// check FBO status
|
||||
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
/* 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. */
|
||||
|
||||
/* 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);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
class pipeline;
|
||||
|
||||
class pass
|
||||
{
|
||||
public:
|
||||
virtual void apply(pipeline& p);
|
||||
};
|
||||
|
||||
class pipeline
|
||||
{
|
||||
|
||||
public:
|
||||
void apply()
|
||||
{
|
||||
for (auto p : _passes) p.apply(*this);
|
||||
}
|
||||
|
||||
protected:
|
||||
std::vector<pass> _passes;
|
||||
};
|
||||
|
||||
|
||||
|
||||
//pipeline >
|
||||
// n*pass
|
||||
// shadow_pass
|
||||
// lighting_pass
|
||||
// mesh_pass
|
||||
// compositor_pass
|
||||
|
||||
//compositor
|
||||
// render to fbo > add a
|
||||
// glBlitFramebuffer .. https://stackoverflow.com/questions/29254574/using-glblitframebuffer-to-display-a-texture
|
||||
|
||||
|
||||
}
|
|
@ -1,69 +0,0 @@
|
|||
#include "pw/visual/renderer.hpp"
|
||||
|
||||
#include "pw/core/mesh.hpp"
|
||||
|
||||
#include "glad/glad.h"
|
||||
|
||||
namespace pw {
|
||||
|
||||
|
||||
struct renderer::impl {
|
||||
|
||||
GLuint _vao = 0;
|
||||
std::vector<GLuint> _vbos;
|
||||
|
||||
impl(renderer& )
|
||||
{
|
||||
}
|
||||
|
||||
void create(std::shared_ptr<mesh> mesh)
|
||||
{
|
||||
glGenVertexArrays(1,&_vao);
|
||||
glBindVertexArray(_vao);
|
||||
|
||||
size_t arrays_needed = 0;
|
||||
|
||||
// should bail out here
|
||||
if (!mesh->vertices().empty()) arrays_needed++;
|
||||
if (!mesh->indices().empty()) arrays_needed++;
|
||||
|
||||
// TODO: implement the other arrays
|
||||
|
||||
_vbos.resize(arrays_needed);
|
||||
|
||||
glGenBuffers(_vbos.size(), _vbos.data());
|
||||
|
||||
// vertices
|
||||
glBindBuffer(GL_ARRAY_BUFFER, _vbos[0]);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(mesh->vertices().front()) * mesh->vertices().size(), mesh->vertices().data(),
|
||||
GL_STATIC_DRAW);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
|
||||
|
||||
// indices
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vbos[1]);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(mesh->indices().front()) * mesh->indices().size(), mesh->indices().data(),
|
||||
GL_STATIC_DRAW);
|
||||
|
||||
|
||||
// stop binding
|
||||
glBindVertexArray(0);
|
||||
|
||||
}
|
||||
|
||||
void destroy()
|
||||
{
|
||||
glDeleteVertexArrays(1,&_vao);
|
||||
for (auto vbo : _vbos)
|
||||
glDeleteBuffers(1,&vbo);
|
||||
|
||||
_vbos.clear();
|
||||
}
|
||||
};
|
||||
|
||||
//pipeline > n*pass
|
||||
//compositor
|
||||
// render to fbo >
|
||||
|
||||
|
||||
}
|
|
@ -70,17 +70,12 @@ struct shader::impl
|
|||
|
||||
glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &log_length);
|
||||
|
||||
char* log_buffer = new char[log_length];
|
||||
std::unique_ptr<char[]> log_buffer(new char[log_length]);
|
||||
|
||||
glGetShaderInfoLog(shaderId, log_length, &log_length, log_buffer);
|
||||
glGetShaderInfoLog(shaderId, log_length, &log_length, log_buffer.get());
|
||||
|
||||
// TODO - handle errors!
|
||||
|
||||
std::string info_log_string(log_buffer);
|
||||
|
||||
delete [] log_buffer;
|
||||
|
||||
debug::e() << info_log_string;
|
||||
debug::e() << log_buffer.get();
|
||||
|
||||
break;
|
||||
}
|
||||
|
@ -99,9 +94,9 @@ struct shader::impl
|
|||
// attribute binding ...
|
||||
|
||||
/* Bind attribute index 0 (coordinates) to in_Position and attribute index 1 (color) to in_Color */
|
||||
/* Attribute locations must be setup before calling glLinkProgram. */
|
||||
// glBindAttribLocation(shaderprogram, 0, "in_Position");
|
||||
// glBindAttribLocation(shaderprogram, 1, "in_Color");
|
||||
/* Attribute locations must be setup before calling glLinkProgram. */
|
||||
// glBindAttribLocation(shaderprogram, 0, "in_Position");
|
||||
// glBindAttribLocation(shaderprogram, 1, "in_Color");
|
||||
|
||||
glLinkProgram(_shader_program);
|
||||
|
||||
|
@ -116,20 +111,16 @@ struct shader::impl
|
|||
glGetProgramiv(_shader_program, GL_INFO_LOG_LENGTH, &log_length);
|
||||
|
||||
/* The maxLength includes the NULL character */
|
||||
char* info_log = new char[log_length];
|
||||
std::unique_ptr<char []> info_log(new char[log_length]);
|
||||
|
||||
/* Notice that glGetProgramInfoLog, not glGetShaderInfoLog. */
|
||||
glGetProgramInfoLog(_shader_program, log_length, &log_length, info_log);
|
||||
glGetProgramInfoLog(_shader_program, log_length, &log_length, info_log.get());
|
||||
|
||||
|
||||
std::string info_log_string;
|
||||
|
||||
debug::e() << info_log_string;
|
||||
debug::e() << info_log.get();
|
||||
|
||||
|
||||
/* Handle the error in an appropriate way such as displaying a message or writing to a log file. */
|
||||
/* In this simple program, we'll just leave */
|
||||
delete [] info_log;
|
||||
|
||||
return;
|
||||
}
|
||||
|
@ -153,12 +144,15 @@ struct shader::impl
|
|||
}
|
||||
}
|
||||
|
||||
void set_uniform(const std::string& name,const matrix4x4f& m)
|
||||
int uniform_location(std::string const& name)
|
||||
{
|
||||
GLint l = glGetUniformLocation(_shader_program,name.c_str());
|
||||
glUniformMatrix4fv(l,1,GL_FALSE,m.data()); // TODO transpose?
|
||||
return glGetUniformLocation(_shader_program,name.c_str());
|
||||
}
|
||||
|
||||
void bind(int location,const matrix4x4f& m)
|
||||
{
|
||||
glUniformMatrix4fv(location,1,GL_FALSE,m.data()); // TODO transpose?
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
@ -173,5 +167,10 @@ bool shader::ready() const
|
|||
return _impl->is_valid();
|
||||
}
|
||||
|
||||
int shader::uniform_location(const std::string &name)
|
||||
{
|
||||
return _impl->uniform_location(name);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue