debugged 2D pipeline
This commit is contained in:
parent
db8d271d07
commit
f284cb769f
38
shaders/gui.frag.glsl
Normal file
38
shaders/gui.frag.glsl
Normal file
@ -0,0 +1,38 @@
|
||||
// INPUT DATA
|
||||
|
||||
#ifdef TEXTURABLE
|
||||
in vec2 texCoord;
|
||||
#endif
|
||||
|
||||
#ifdef DIFFUSE_TEXTURE
|
||||
uniform sampler2D diffuseTexture;
|
||||
#else
|
||||
uniform vec3 materialKd;
|
||||
#endif
|
||||
|
||||
#ifdef ALPHA_MASK
|
||||
uniform sampler2D alphaMask;
|
||||
#endif
|
||||
|
||||
// OUTPUT COLOR
|
||||
|
||||
layout(location = 0)out vec4 outColor;
|
||||
|
||||
// MAIN PROGRAM
|
||||
|
||||
void main(void)
|
||||
{
|
||||
#ifdef DIFFUSE_TEXTURE
|
||||
vec3 color = texture(diffuseTexture, texCoord).rgb;
|
||||
#else
|
||||
vec3 color = materialKd;
|
||||
#endif
|
||||
|
||||
#ifdef ALPHA_MASK
|
||||
float alpha = texture(alphaMask, texCoord).r;
|
||||
#else
|
||||
float alpha = 1.0;
|
||||
#endif
|
||||
|
||||
outColor = vec4(color, alpha);
|
||||
}
|
18
shaders/gui.vert.glsl
Normal file
18
shaders/gui.vert.glsl
Normal file
@ -0,0 +1,18 @@
|
||||
layout(location = 0)in vec2 inPosition;
|
||||
|
||||
#ifdef TEXTURABLE
|
||||
layout(location = 0)in vec2 inTexCoord;
|
||||
|
||||
out vec2 texCoord;
|
||||
#endif
|
||||
|
||||
uniform mat4 transformMatrix;
|
||||
uniform mat4 orthoMatrix;
|
||||
uniform float depth;
|
||||
|
||||
void main(void) {
|
||||
#ifdef TEXTURABLE
|
||||
texCoord = inTexCoord;
|
||||
#endif
|
||||
gl_Position = orthoMatrix * transformMatrix * vec4(inPosition, depth, 1.0);
|
||||
}
|
111
src/guipipeline.cpp
Normal file
111
src/guipipeline.cpp
Normal file
@ -0,0 +1,111 @@
|
||||
#include "sparrowrenderer.h"
|
||||
#include "guipipeline.h"
|
||||
#include "texture.h"
|
||||
#include "scene.h"
|
||||
#include "mesh.h"
|
||||
#include "shader.h"
|
||||
#include <glm/ext.hpp>
|
||||
#include "shadersource.h"
|
||||
#include <resource.h>
|
||||
|
||||
RESOURCE_PACK(shaders)
|
||||
|
||||
GuiBuffer::GuiBuffer(int width, int height) : FrameBuffer()
|
||||
{
|
||||
bindFBO();
|
||||
glGenRenderbuffers(1, &m_rbo);
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, m_rbo);
|
||||
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT32, width, height);
|
||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_rbo);
|
||||
Texture* tex = new Texture(GL_RGBA, GL_RGBA8UI, width, height, GL_UNSIGNED_BYTE, GL_TEXTURE_RECTANGLE);
|
||||
addTexture(tex, GL_COLOR_ATTACHMENT0);
|
||||
initColorAttachments();
|
||||
}
|
||||
|
||||
GuiBuffer::~GuiBuffer()
|
||||
{
|
||||
glDeleteRenderbuffers(1, &m_rbo);
|
||||
}
|
||||
|
||||
GuiPipeline::GuiPipeline() :
|
||||
m_width(512),
|
||||
m_height(512),
|
||||
m_guiBuffer(NULL)
|
||||
{
|
||||
Resource::ResourceMap shaderMap;
|
||||
Resource::getResourcePack_shaders(shaderMap);
|
||||
m_guiShader = new ShaderSource();
|
||||
m_guiShader->setSource(shaderMap["shaders/gui.vert.glsl"], ShaderSource::VERTEX);
|
||||
m_guiShader->setSource(shaderMap["shaders/gui.frag.glsl"], ShaderSource::FRAGMENT);
|
||||
m_renderTarget = FrameBuffer::screen;
|
||||
}
|
||||
|
||||
void GuiPipeline::renderGL(Scene *scene)
|
||||
{
|
||||
if(m_renderTarget == NULL)
|
||||
return;
|
||||
|
||||
// GEOMETRY PASS
|
||||
m_renderTarget->bindFBO();
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glDepthFunc(GL_LESS);
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_ONE, GL_ONE);
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 1.f);
|
||||
glClearDepth(1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
// loop on geometry
|
||||
for(SceneIterator<GeometryNode*>* geometryIt = scene->getGeometry();
|
||||
geometryIt->isValid(); geometryIt->next())
|
||||
{
|
||||
|
||||
GeometryNode* node = geometryIt->getItem();
|
||||
Shader *shader = m_meshShaders[node->mesh->getFlags()];
|
||||
if(shader == NULL)
|
||||
continue;
|
||||
shader->bind();
|
||||
|
||||
shader->bindFloat(shader->getLocation("depth"), node->mesh->getDepth());
|
||||
shader->bindMat4(shader->getLocation("transformMatrix"), node->modelMatrix);
|
||||
shader->bindMat4(shader->getLocation("orthoMatrix"), m_orthoMatrix);
|
||||
|
||||
// draw geometry
|
||||
node->mesh->draw(shader);
|
||||
}
|
||||
}
|
||||
|
||||
void GuiPipeline::resizeGL(int w, int h)
|
||||
{
|
||||
// updating dimensions
|
||||
m_width = w;
|
||||
m_height = h;
|
||||
|
||||
// rebuilding FrameBuffers
|
||||
/*if(m_guiBuffer != NULL)
|
||||
delete m_guiBuffer;
|
||||
m_guiBuffer = new GuiBuffer(w, h);*/
|
||||
m_orthoMatrix = glm::ortho(0.f, float(m_width), float(m_height), 0.f);
|
||||
}
|
||||
|
||||
void GuiPipeline::setSources(ShaderSource *guiSource)
|
||||
{
|
||||
if(m_guiShader != NULL)
|
||||
delete m_guiShader;
|
||||
m_guiShader = guiSource;
|
||||
}
|
||||
|
||||
void GuiPipeline::refreshScene(Scene *scene)
|
||||
{
|
||||
for(auto it : m_meshShaders)
|
||||
delete it.second;
|
||||
m_meshShaders.clear();
|
||||
m_meshTypes.clear();
|
||||
scene->getMeshTypes(m_meshTypes);
|
||||
for(unsigned int type : m_meshTypes)
|
||||
{
|
||||
if(type & (1 << Mesh::MESH_2D))
|
||||
m_meshShaders[type] = m_guiShader->compile(type, 0);
|
||||
else
|
||||
m_meshShaders[type] = NULL;
|
||||
}
|
||||
}
|
51
src/guipipeline.h
Normal file
51
src/guipipeline.h
Normal file
@ -0,0 +1,51 @@
|
||||
#ifndef DEFERREDPIPELINE_H
|
||||
#define DEFERREDPIPELINE_H
|
||||
|
||||
#include "pipeline.h"
|
||||
#include "framebuffer.h"
|
||||
#include <unordered_map>
|
||||
#include <glm/mat4x4.hpp>
|
||||
|
||||
class Shader;
|
||||
class Camera;
|
||||
|
||||
class GuiBuffer : public FrameBuffer
|
||||
{
|
||||
unsigned int m_rbo;
|
||||
public:
|
||||
GuiBuffer(int width, int height);
|
||||
~GuiBuffer();
|
||||
};
|
||||
|
||||
class GuiPipeline : public Pipeline
|
||||
{
|
||||
Camera *m_camera;
|
||||
|
||||
int m_width;
|
||||
int m_height;
|
||||
|
||||
glm::mat4 m_orthoMatrix;
|
||||
|
||||
// shaders
|
||||
std::vector<unsigned int> m_meshTypes;
|
||||
|
||||
std::unordered_map<unsigned int, Shader*> m_meshShaders;
|
||||
|
||||
ShaderSource *m_guiShader;
|
||||
|
||||
// framebuffers
|
||||
GuiBuffer *m_guiBuffer;
|
||||
const FrameBuffer *m_renderTarget;
|
||||
|
||||
public:
|
||||
GuiPipeline();
|
||||
|
||||
void refreshScene(Scene *scene);
|
||||
|
||||
virtual void renderGL(Scene *scene);
|
||||
virtual void resizeGL(int w, int h);
|
||||
|
||||
void setSources(ShaderSource *guiSource);
|
||||
};
|
||||
|
||||
#endif // DEFERREDPIPELINE_H
|
Loading…
x
Reference in New Issue
Block a user