Reputation: 109
I'm trying to pass and use a texture to shader... but every time I try to use it in even the easiest way I get black box, sometimes white - depends on the texture I put him.. somehow....
Here's code fragment witch should work ( i've removed my wrappers to make it easier):
fbo->bind();
fbo->drawBuffer(0);
fbo->setClearColor( 1.0, 1.0, 1.0, 1.0);
fbo->cleanCurrentTexture();
GLuint location;
location = glGetUniformLocation( Program->getID(), "sampler");
glActiveTexture( GL_TEXTURE0 );
glBindTexture(GL_TEXTURE_2D, fbo2->getTexture(0) );
glUniform1i(location, 0);
Program->begin();
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glVertex3f( -1.0f, -1.0f, 0.0f );
glVertex3f( -1.0f, 1.0f, 0.0f );
glVertex3f( 1.0f, 1.0f, 0.0f );
glVertex3f( 1.0f, -1.0f, 0.0f );
glEnd();
Program->end();
fbo->unbind();
glColor3f(1.0,1.0,1.0);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, fbo->getTexture(0) );
glBegin(GL_QUADS);
glTexCoord2d( 0.0f, 0.0f);
glVertex3f( -1.0f, -1.0f, 0.0f );
glTexCoord2d( 0.0f, 1.0f);
glVertex3f( -1.0f, 1.0f, 0.0f );
glTexCoord2d( 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 0.0f );
glTexCoord2d( 1.0f, 0.0f);
glVertex3f( 1.0f, -1.0f, 0.0f );
glEnd();
glDisable( GL_TEXTURE_2D);
Vertex shader:
#version 120
void main()
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_TexCoord[0] = gl_MultiTexCoord0;
}
Fragment shader:
#version 120
uniform sampler2D sampler;
void main()
{
gl_FragColor = texture2D(sampler, gl_TexCoord[0].xy);
}
Ofcourse the texture I'm trying to bind exists and its complete. Here are it's parameters:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
Upvotes: 1
Views: 5925
Reputation: 3190
Just as Plow says in his comment, I believe this has to do with texture coordinates not being set. You are only setting texture coordinates for the second quad. The second quad is not being drawn with the shader program either, and it is being drawn in the exact same coordinates as the first, which means it should be either completely white (because glColor3f(1.0,1.0,1.0)
), or it should not be drawn at all (depending on your camera position and whether or not depth testing is enabled).
Try drawing only the first quad, with texture coordinates properly set up.
Upvotes: 1