Can render color and depth buffer

simple_math_single_header
Martin Felis 2018-02-14 13:14:10 +01:00
parent 8d718eb71f
commit b5f3370570
4 changed files with 31 additions and 7 deletions

View File

@ -14,4 +14,6 @@ void main() {
cos(time + 768.0 * uv.y)
)
).xyz;
color = vec3((texture(rendered_texture, uv).x + 1) * 0.5 );
// * 1 * (cos(time * 0.3) * 0.5) + 0.5);
}

View File

@ -7,7 +7,7 @@ struct Renderer;
static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
1.0f, -1.0f, -1.2f,
0.0f, 1.0f, 0.0f
};
@ -135,7 +135,7 @@ glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data
assert(load_result);
// Render Target
mRenderTarget = RenderTarget (width, height, RenderTarget::EnableColor | RenderTarget::EnableDepth);
mRenderTarget = RenderTarget (width, height, RenderTarget::EnableColor | RenderTarget::EnableDepthTexture);
// Render Target Quad
glGenVertexArrays(1, &mRenderQuadVertexArrayId);
@ -174,6 +174,7 @@ void Renderer::RenderGl() {
// clear color and depth
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glUseProgram(mProgram.mProgramId);
@ -201,8 +202,9 @@ void Renderer::RenderGui() {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mRenderTarget.mColorTexture);
// glBindTexture(GL_TEXTURE_2D, mRenderTarget.mDepthTexture);
glUniform1i(muRenderQuadTexture, 0);
glUniform1f(muRenderQuadTime, 0.0f * (float)(glfwGetTime() * 10.0f));
glUniform1f(muRenderQuadTime, (float)(glfwGetTime() * 10.0f));
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, mRenderQuadVertexBufferId);
@ -217,7 +219,6 @@ void Renderer::RenderGui() {
glDrawArrays(GL_TRIANGLES, 0, 6); // starting from vertex 0; 3 vertices total
glDisableVertexAttribArray(0);
}
void Renderer::Resize (int width, int height) {

View File

@ -138,6 +138,11 @@ void RenderTarget::Cleanup() {
mColorTexture = -1;
}
if (mDepthTexture!= -1) {
glDeleteTextures(1, &mDepthTexture);
mDepthTexture = -1;
}
if (mDepthBuffer != -1) {
glDeleteRenderbuffers(1, &mDepthBuffer);
mDepthBuffer = -1;
@ -168,13 +173,27 @@ void RenderTarget::Resize(int width, int height) {
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, mColorTexture, 0);
}
if (mFlags & EnableDepth) {
if (mFlags & EnableDepthTexture) {
assert((mFlags & EnableDepth) == false);
glGenTextures(1, &mDepthTexture);
glBindTexture(GL_TEXTURE_2D, mDepthTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, mWidth, mHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, mDepthTexture, 0);
} else if (mFlags & EnableDepth) {
assert((mFlags & EnableDepthTexture) == false);
glGenRenderbuffers(1, &mDepthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, mDepthBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, mWidth, mHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, mDepthBuffer);
}
}

View File

@ -150,10 +150,12 @@ struct RenderTarget {
GLuint mFrameBufferId = -1;
GLuint mColorTexture = -1;
GLuint mDepthBuffer = -1;
GLuint mDepthTexture = -1;
typedef enum {
EnableColor = 1,
EnableDepth = 2
EnableDepth = 2,
EnableDepthTexture = 4
} Flags;
int mFlags = 0;