rendering to a frame buffer (but not yet drawing it again)
parent
469fbffb8e
commit
b15e2f8421
|
@ -1,6 +1,6 @@
|
||||||
#version 330 core
|
#version 330 core
|
||||||
|
|
||||||
out vec3 color;
|
layout(location = 0) out vec3 color;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
color = vec3(1, 0, 0);
|
color = vec3(1, 0, 0);
|
||||||
|
|
|
@ -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");
|
mProgram = RenderProgram("data/shaders/vs_simple.glsl", "data/shaders/fs_simple.glsl");
|
||||||
bool load_result = mProgram.Load();
|
bool load_result = mProgram.Load();
|
||||||
assert(load_result);
|
assert(load_result);
|
||||||
|
|
||||||
|
mRenderTarget = RenderTarget (width, height, RenderTarget::EnableColor | RenderTarget::EnableDepth);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::Shutdown() {
|
void Renderer::Shutdown() {
|
||||||
glDeleteVertexArrays(1, &mMesh.mVertexArrayId);
|
glDeleteVertexArrays(1, &mMesh.mVertexArrayId);
|
||||||
}
|
}
|
||||||
void Renderer::RenderGl() {
|
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);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
glUseProgram(mProgram.mProgramId);
|
glUseProgram(mProgram.mProgramId);
|
||||||
|
|
|
@ -114,6 +114,7 @@ struct Renderer {
|
||||||
std::vector<Camera> cameras;
|
std::vector<Camera> cameras;
|
||||||
Mesh mMesh;
|
Mesh mMesh;
|
||||||
RenderProgram mProgram;
|
RenderProgram mProgram;
|
||||||
|
RenderTarget mRenderTarget;
|
||||||
|
|
||||||
Renderer() :
|
Renderer() :
|
||||||
initialized(false),
|
initialized(false),
|
||||||
|
|
|
@ -10,6 +10,10 @@
|
||||||
|
|
||||||
using namespace SimpleMath;
|
using namespace SimpleMath;
|
||||||
|
|
||||||
|
//
|
||||||
|
// RenderProgram
|
||||||
|
//
|
||||||
|
|
||||||
RenderProgram::~RenderProgram() {
|
RenderProgram::~RenderProgram() {
|
||||||
if (mProgramId > 0)
|
if (mProgramId > 0)
|
||||||
glDeleteProgram(mProgramId);
|
glDeleteProgram(mProgramId);
|
||||||
|
@ -109,4 +113,35 @@ bool RenderProgram::Load() {
|
||||||
return true;
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -144,5 +144,19 @@ struct RenderProgram {
|
||||||
bool Load();
|
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
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue