View Full Version : Changing color using a shader

02-23-2011, 09:50 PM

I am writing a deferred shader and as one of the first steps, to get familiar with GLSL and using shaders and the framebuffer I am trying to change the color of a mesh through a shader.
I have it linked to one of the buffers by calling glDrawBuffers with an array that holds the attachement and then binding the texture to my framebuffer:

GLint color_loc = glGetFragDataLocation(pass_prog,"out_Color");
GLenum draws [1];
draws[color_loc] = GL_COLOR_ATTACHMENT0;
glDrawBuffers(1, draws);

glBindTexture(GL_TEXTURE_2D, diffuseTexture);
glFramebufferTexture(GL_FRAMEBUFFER, draws[color_loc], diffuseTexture, 0);

I have an out_Color variable in my fragment shader (otherwise it wouldn't even compile), but I can't manage to change the color of the mesh by setting it through that variable inside the shader.
Does anyone has any idea why and could explain that to me?


02-23-2011, 10:31 PM
Can we look at your shader too to see how u r writing to the out_Color .

02-24-2011, 10:14 AM
Here is my fragment shader:

#version 330

uniform float u_Far;

in vec3 fs_Normal;
in vec4 fs_Position;

out vec4 out_Normal;
out vec4 out_Position;
out vec4 out_Color;

void main(void)
out_Normal = vec4(normalize(fs_Normal),0.0f);
out_Position = vec4(fs_Position.xyz,1.0f); //Tuck position into 0 1 range
out_Color = vec4(1.0f, 0.0f, 0.0f, 1.0f);//first three diffuse, last specular

02-24-2011, 07:00 PM
Could u try this. After you program is compiled, add this line and then link your program.

glBindFragDataLocation(pass_prog, 0, “out_Color”);

This will connect the out_Color to the 0th color attachment. Then use the following calls.

glBindFramebuffer( GL_DRAW_FRAMEBUFFER, framebufferId );
glFrameBufferTexture( GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, diffuseTexture, 0 );

See if this helps.