rendering to a frame buffer (but not yet drawing it again)

simple_math_single_header
Martin Felis 2018-02-13 12:44:34 +01:00
parent 469fbffb8e
commit b15e2f8421
5 changed files with 63 additions and 1 deletions

View File

@ -1,6 +1,6 @@
#version 330 core
out vec3 color;
layout(location = 0) out vec3 color;
void main() {
color = vec3(1, 0, 0);

View File

@ -122,12 +122,24 @@ glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data
mProgram = RenderProgram("data/shaders/vs_simple.glsl", "data/shaders/fs_simple.glsl");
bool load_result = mProgram.Load();
assert(load_result);
mRenderTarget = RenderTarget (width, height, RenderTarget::EnableColor | RenderTarget::EnableDepth);
}
void Renderer::Shutdown() {
glDeleteVertexArrays(1, &mMesh.mVertexArrayId);
}
void Renderer::RenderGl() {
// enable the render target
glBindFramebuffer(GL_FRAMEBUFFER, mRenderTarget.mFrameBufferId);
GLenum DrawBuffers[1] = { GL_COLOR_ATTACHMENT0 };
glDrawBuffers(1, DrawBuffers);
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
gLog ("Cannot render: frame buffer invalid!");
}
// clear color and depth
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(mProgram.mProgramId);

View File

@ -114,6 +114,7 @@ struct Renderer {
std::vector<Camera> cameras;
Mesh mMesh;
RenderProgram mProgram;
RenderTarget mRenderTarget;
Renderer() :
initialized(false),

View File

@ -10,6 +10,10 @@
using namespace SimpleMath;
//
// RenderProgram
//
RenderProgram::~RenderProgram() {
if (mProgramId > 0)
glDeleteProgram(mProgramId);
@ -109,4 +113,35 @@ bool RenderProgram::Load() {
return true;
}
//
// RenderTarget
//
RenderTarget::RenderTarget(int width, int height, int flags) {
glGenFramebuffers(1, &mFrameBufferId);
glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferId);
if (flags & EnableColor) {
glGenTextures(1, &mColorTexture);
glBindTexture(GL_TEXTURE_2D, mColorTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, mColorTexture, 0);
}
if (flags & EnableDepth) {
glGenRenderbuffers(1, &mDepthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, mDepthBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, mDepthBuffer);
}
}
RenderTarget::~RenderTarget() {
// TODO: cleanup
}

View File

@ -144,5 +144,19 @@ struct RenderProgram {
bool Load();
};
struct RenderTarget {
GLuint mFrameBufferId = -1;
GLuint mColorTexture = -1;
GLuint mDepthBuffer = -1;
typedef enum {
EnableColor = 1,
EnableDepth = 2
} Flags;
RenderTarget() {};
RenderTarget(int width, int height, int flags);
~RenderTarget();
};
#endif