I’m trying to write a basic first step application where I make a texture go from black to white using a shader. I try to do this by binding the output of this shader to the texture using a framebuffer. Emphasis on trying, by the way.
The fragment shader is quite simple (the vertex shader isn’t worth mentioning):
#version 130
out vec4 outputColor;
uniform sampler2D currentStateTexture;
void main()
{
ivec2 cord = new ivec2(0, 0);
float color = texelFetch(currentStateTexture, cord, 0).x + 1.0/64.0;
if(color > 1.0)
outputColor = new vec4(0.0, 0.0, 0.0, 1.0);
else
outputColor = new vec4(color, color, color, 0.0);
}
Now, assuming that I properly bind the out variable to a texture and send in another texture to currentStateTexture and then continuously switch texture between each lap of the program this should do what I want it to IE slowly fade the color up to white, and then it goes back to black and starts over. It doesn’t matter that the texture coordinates are hard coded because the texture’s only got one color (though that will matter later when I do the “real” program so tips on how to get proper coordinates for texelFetch would be appreciated).
The problem is that the program gets stuck at dark grey and I have no idea why. The code (minus all the fluff):
Called in main, before the program starts:
/* Load the texture */
stateTextures[0] = loadTexture("text1.png",examTexSize,0,1);
stateTextures[1] = loadTexture("text2.png",examTexSize,0,1);
/* Generate the framebuffer object */
glGenFramebuffers(1, &updateFramebuffer);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, updateFramebuffer);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
stateTextures[1],0);
The (intersting parts of the) draw function:
void draw() {
/* Run the vertex + fragment shader */
glUseProgram(examProgramObject);
/* Enable texturing on texture unit 0, and tell the shader to use unit0 as the sampler for the image */
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, stateTextures[0]);
glUniform1i(imageTextureUnit,0);
/* Draw to update the state */
glBindFramebuffer(GL_DRAW_FRAMEBUFFER,updateFramebuffer);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
stateTextures[1],0);
glDrawBuffer(GL_COLOR_ATTACHMENT0);
glBegin(GL_QUADS);
glTexCoord2f(0.0,0.0);
glVertex2f(0.0,0.0);
glTexCoord2f(512.0,0.0);
glVertex2f(1.0,0.0);
glTexCoord2f(512.0,512.0);
glVertex2f(1.0,1.0);
glTexCoord2f(0.0,512.0);
glVertex2f(0.0, 1.0);
glEnd();
/* Draw to visualize*/
glBindFramebuffer(GL_DRAW_FRAMEBUFFER,0);
glDrawBuffer(GL_BACK);
glBegin(GL_QUADS);
glTexCoord2f(0.0,0.0);
glVertex2i(0,0);
glTexCoord2f(512.0,0.0);
glVertex2i(512,0);
glTexCoord2f(512.0,512.0);
glVertex2i(512,512);
glTexCoord2f(0.0,512.0);
glVertex2i(0,512);
glEnd();
glUseProgram(0);
}
Called last thing before the whole loop starts over:
GLuint tmpi = stateTextures[0];
stateTextures[0] = stateTextures[1];
stateTextures[1] = tmpi;
Two days of slapping my knee and swearing because the damn program ought to work hasn’t gotten me any nearer to a solution. I just don’t know what it is I’m doing or not doing that makes the program malfunction.