PDA

View Full Version : Basic shader problem, ambience specifically..



Hiram
05-15-2011, 07:03 PM
Hi guys, I'm implementing Blinn-Phong at the moment, doing ambience currently. I'm having a lot of trouble getting the right result!

I'm working on simple ambience using the background colour. I have diffuse working at the moment which is fine, but whenever I add in ambience, it doesn't actually have the background colour.

This is my vertex shader code. My fragment shader code, for testing purposes is simply gl_Color = ambience; which at the moments gives me a dark gray instead of the ambient colour (the background colour is blue, so I expect to see a shade of blue).



varying float diffuse;
varying float ambience;
varying float specular;

uniform vec3 bg_colour;

void main()
{
gl_Position = gl_ModelViewMatrix * gl_Vertex;
gl_Position = gl_Position / gl_Position.w;

vec3 light = vec3(3, 4, 1);
vec3 light_dir = gl_ModelViewMatrix * vec4(light, 0.0);
vec3 normal = gl_NormalMatrix * gl_Normal;

vec3 ambient_colour = bg_colour;

float ambient_intensity = 0.5;
float diffuse_intensity = 0.5;

ambience = ambient_colour * ambient_intensity;

diffuse = max(dot(normalize(normal), normalize(light_dir)), 0) * diffuse_intensity;

gl_Position = gl_ProjectionMatrix * gl_Position;
}

As you can see, I pass the background colour in from my C++ code via a uniform variable. I know the problem is something basic that I'm overlooking, but I can't figure out what it is.

Edit: Playing around with the line "ambience = ambient_colour * ambient_intensity;" I've figured out that's part of the problem. ambient_colour being of type vec3, and ambient_intensity being of type float, the result is of course going to be a scaled vec3, which of course means that ambience gets assigned ambient_colour.x, not the overall colour.

So I guess my question now is, how do I do this correctly?

BionicBytes
05-16-2011, 12:22 AM
varying float ambience;
That's your problem right there.

Since you want to pass a colour not an intensity to the fragment program, you need to declare ambience as a vec3, eg


vec3 ambience
That will solve your issue.

the result is of course going to be a scaled vec3, which of course means that ambience gets assigned ambient_colour.x, not the overall colour.
No the result is that each component of the Vec3 will be scaled by 0.5. However in your case only a single component of (ambient_colour * ambient_intensity) is ever passed to the fragment program.

Hiram
05-16-2011, 01:21 AM
varying float ambience;
That's your problem right there.

Since you want to pass a colour not an intensity to the fragment program, you need to declare ambience as a vec3, eg


vec3 ambience
That will solve your issue.


I actually realised this on the bus this afternoon! Thanks heaps for replying though.


the result is of course going to be a scaled vec3, which of course means that ambience gets assigned ambient_colour.x, not the overall colour.
No the result is that each component of the Vec3 will be scaled by 0.5. However in your case only a single component of (ambient_colour * ambient_intensity) is ever passed to the fragment program.

Sorry, looking back on what I said, I worded that very poorly, I did actually mean what you have said. I definitely shouldn't be writing messages to anyone at 1AM anymore :D

BionicBytes
05-16-2011, 02:26 AM
Sometimes just by posing the question you end up answering it!