Drawing to a texture

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.

Just guessing: what is your FPS? 500? 2000?
It might be the case that all your gradient is shown, but you see only grey as an average because of the high refresh rate.

You should either limit the FPS or change fragment shader to take time delta into account.

If it’s not the case then: have you tried something simpler? a raw texture output to full screen?

The program I’m using says my FPS is 75 (so, either the program I’m using limits my FPS to my screen refresh rate or it’s lying/mistaken). I can’t quite verify this, but I could put the texture copying where the FPS is written, because that’s only done something like once every fifth second. That should make any correct result very easy to see since my texture now only updates once every fifth second.

When I do that, no result can be seen. With two white textures the result is pitch black and it stays pitch black. With a black and a white texture the result flashes (slowly) between pitch black and dark grey (which wouldn’t be the result at all if the texture writing acually worked, because the new texture would get the old texture’s value + 1/8).

A short question: is this approach supposed to change the actual texture files or is it just the (supposed) internally loaded texture that gets altered? Just out of curiosity.