I’m trying to make post processing for my project by using a texture on a quad (2 triangles) so I can add effects (there’s also a framebuffer and renderbuffer)
I got the idea from https://learnopengl.com/Advanced-OpenGL/Framebuffers
but the texture’s data (position & texture coordinates) are instead considered as (position & color) this is because it is using the default shader instead of the post processing shader.
Here’s a visualization (ignore the comic sans)
what should be happening
what’s happening
result
I’m pretty new to OpenGL but from what I can see glUseProgram() makes the window use a different shader but in reality it doesn’t seem to be the case, in the drawing loop I uniquely used the post processing shader but it still uses the default one.
code (shader class, the glsl shader code, quad vao & vbo, quad texture, framebuffer, renderbuffer, etc) (also alot of it is copy pasted cause I was scared to make a transcription error)
shader class:
// in the header file there is GLuint called 'program'
#include "shader.h"
void shader::createShader(const char* vertShaderCode, const char* fragShaderCode)
{
GLuint vertShader;
GLuint fragShader;
program = glCreateProgram();
vertShader = glCreateShader(GL_VERTEX_SHADER);
fragShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vertShader, 1, &vertShaderCode, 0);
glCompileShader(vertShader);
glShaderSource(fragShader, 1, &fragShaderCode, 0);
glCompileShader(fragShader);
glAttachShader(program, vertShader);
glAttachShader(program, fragShader);
glLinkProgram(program);
glDeleteShader(vertShader);
glDeleteShader(fragShader);
}
glsl shader code (Both shaders)
// THE DEFAULT SHADER CODE
// THE DEFAULT SHADER CODE
const char* vertexshadercode =
"#version 330rn"
""
"in layout(location=0) vec3 position;"
"in layout(location=1) vec3 color;"
"out vec4 outputColor;"
"void main()"
"{"
" gl_Position = vec4(position, 1+position.z*tan(0.9));"
//" gl_Position = vec4(position.x,position.y,1,1);"
"outputColor = vec4(color,1);"
" "
"}";
const char* fragmentshadercode =
"#version 330rn"
"in vec4 outputColor;"
"out vec4 fragColor;"
""
"void main()"
"{"
" fragColor = outputColor;"
"}";
// THE POST PROCESSING SHADER CODE
// THE POST PROCESSING SHADER CODE
const char* fvertexshadercode =
"#version 330 core"
"layout(location = 0) in vec2 aPos;"
"layout(location = 1) in vec2 aTexCoords;"
"out vec2 TexCoords;"
"void main()"
"{"
"TexCoords = aTexCoords;"
"gl_Position = vec4(pos.x, pos.y, 0.0, 1.0);"
"}";
const char* ffragmentshadercode =
"#version 330rn"
"in vec2 TexCoords;"
"out vec4 FragColor;"
"uniform sample2D screenTexture;"
"void main()"
"{"
" FragColor = texture(screenTexture,TexCoords);"
"}";
quad data, vao and vbo
float quadVertices[] = {
-1.0f, 1.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 0.0f, 0.0f,
1.0f, -1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 0.0f, 1.0f,
1.0f, -1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f
// POSITION ON THE LEFT AND TEXTURE COORDINATES ON THE RIGHT
};
unsigned int quadVAO, quadVBO;
glGenVertexArrays(1, &quadVAO);
glGenBuffers(1, &quadVBO);
glBindVertexArray(quadVAO);
glBindBuffer(GL_ARRAY_BUFFER, quadVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float)));
texture, framebuffer renderbuffer, etc
unsigned int framebuffer;
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
// create a color attachment texture
unsigned int textureColorbuffer;
glGenTextures(1, &textureColorbuffer);
glBindTexture(GL_TEXTURE_2D, textureColorbuffer);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 800,800, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureColorbuffer, 0);
// create a renderbuffer object for depth and stencil attachment (we won't be sampling these)
unsigned int rbo;
glGenRenderbuffers(1, &rbo);
glBindRenderbuffer(GL_RENDERBUFFER, rbo);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 800, 800); // use a single renderbuffer object for both a depth AND stencil buffer.
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo); // now actually attach it
drawing loop
while(!glfwWindowShouldClose(window))
{
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glEnable(GL_DEPTH_TEST);
glUseProgram(frameShader.program);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindVertexArray(VAO.buffer);
glDrawElements(GL_TRIANGLES, 9, GL_UNSIGNED_INT, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDisable(GL_DEPTH_TEST);
// clear all relevant buffers
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(quadVAO);
glBindTexture(GL_TEXTURE_2D, textureColorbuffer);
glDrawArrays(GL_TRIANGLES, 0, 6);
glfwSwapBuffers(window);
glfwPollEvents();
}
4