Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1066642 Posts in 43538 Topics- by 35569 Members - Latest Member: nicoper

November 25, 2014, 11:37:49 PM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)OpenGL, GLSL, FBO oh my!
Pages: [1]
Print
Author Topic: OpenGL, GLSL, FBO oh my!  (Read 1188 times)
Cirdan
Level 0
*


View Profile Email
« on: February 12, 2012, 12:06:11 AM »

Really banging my head against the desk with this problem. Been stabing around, tirelessly searching google, trying every combination possible...and I just can't get this to work.

I am rendering sprites to a Framebuffer. I know this is working perfectly fine because I can render out the framebuffer and see exactly what I drew to it. I want to pass this fbo texture to a shader, so I can eventually do light calculations. For what ever reason, it just will not work for me at all. I am following what various tutorials and sample code has told me, but it just will not work. Angry. My shader is compiling and linking without errors. I can set the gl_fragcolor to a color in the shader with great success. It's using the texture that is giving me problems. Right now, when I set gl_fragcolor to the texture, I get a dark gray color.

Vertex shader:
Code:
#version 120

varying vec4 texCoord;

void main()
{
texCoord = gl_MultiTexCoord0;
gl_Position = ftransform();

}

Fragment shader
Code:
#version 120

uniform sampler2D diffuseMap;
uniform sampler2D lightingMap;

varying vec4 texCoord;

void main()
{
vec4 color = texture2D(diffuseMap, gl_TexCoord[0].st);
vec4 light = texture2D(lightingMap, texCoord.st);

gl_FragColor = color;
}

How I'm using the shader.
Code:
graphics->useShader(mLightingCombinedShader);
mLightingCombinedShader->setUniformTexture("diffuseMap", mDiffuse->getTextureId());
graphics->drawPrimitive(mScreenRect, 0, 0);
graphics->clearShader();

And the relavant code:

Code:

void ShaderProgram::setUniformTexture(const std::string& name, GLuint t) {
GLint var = getUniformLocation(name);
glActiveTexture(GL_TEXTURE0 + ShaderProgram::mTextureUnits);
glBindTexture(GL_TEXTURE_2D, t);
glUniform1i(var, ShaderProgram::mTextureUnits);
}

void GraphicsDevice::useShader(ShaderProgram* p) {
glUseProgram(p->getId());
}

void GraphicsDevice::clearShader() {
glUseProgram(0);
}

void GraphicsDevice::drawPrimitive(Primitive *p, float x, float y) {
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, p->getVbo());

glPushMatrix();

glTranslatef(x, y, 0.f);

glVertexPointer(2, GL_FLOAT, sizeof(Vertex), (void*)offsetof(Vertex, x));
glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), (void*)offsetof(Vertex, tx));

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

glPopMatrix();

glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableClientState(GL_VERTEX_ARRAY);

}

Logged
mjau
Level 3
***



View Profile
« Reply #1 on: February 12, 2012, 02:07:17 AM »

Do you unset the FBO (or set it to something else) before using the texture? A shader can't have the same texture as both input and output. Also check if glError has something to say, or try apitrace
Logged
Polly
Level 4
****


View Profile
« Reply #2 on: February 12, 2012, 08:01:59 AM »

You're using gl_TexCoord in your fragment shader without assigning a value to it. Either add ..

Code:
gl_TexCoord[0] = gl_MultiTexCoord0;

.. to your vertex shader, or ( since you're already passing that value as a varying vec4 ) swap out gl_TexCoord[0] with texCoord in your fragment shader.
Logged
Cirdan
Level 0
*


View Profile Email
« Reply #3 on: February 12, 2012, 05:48:58 PM »

Thanks for the help guys, the problem ended up being that I didn't enable texture coordinates for the rectangle I was drawing.  Big Laff
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic