Thanks! I haven’t had any time to look at those posts you first gave me (had to do the Easter Brunch thing at my mother-in-law’s) but I’m going to look at them now…
As for my “current mess” yes, that’s what I’m trying to do, with the emphasis on “trying”… OK I got the general idea that I have to calculate the normal for each vertex and then “pass” that info on to my vertex shader (vshader_car.glsl) and then just “hand it off” to the fragment shader (fshader_car.glsl) who will re-normalize it and then display everything real nice & 3D like… But going from theory to code is more difficult for me…
For example my sphere’s are all created with triangles using a function shamelessly stolen from Angel’s textbook “Interactive Computer Graphics”. Originally that function had lines like this:
sphereVerts[k] = vec4(sin(thetar)*cos(phir), cos(thetar)*cos(phir), sin(phir), 1.0);
k++;
sphereVerts[k] = vec4(sin(thetar)*cos(phir20), cos(thetar)*cos(phir20), sin(phir20), 1.0);
k++;
(etc...)
And the color would get set with the vcolor attribute while I was transforming it, putting it in the modelview and then actually drawing an example of a sphere in display() like this: (I’m actually drawing a TON of sphere’s like this)
mv = mv*Translate(20.0, 1.7, 20.0)*Scale(3.0, 3.0, 3.0);
glUniformMatrix4fv(model_view, 1, GL_TRUE, mv);
glBindVertexArray(vao[2]);
glVertexAttrib4f(vColor, 0.0, 0.6, 1.0, 1.0);
glDrawArrays(GL_TRIANGLES, 0, 1140);
All worked well, I could make a bunch of objects for my little car to “drive around” as well as the drivers “head” and eyes by just transforming that original sphere over and over again…
OK, now to go from a flat vcolor to Phong shading I changed my gensphere() code to calculate the normal (sphereNormals) like this:
sphereNormals[k] = vec3(sin(thetar)*cos(phir), cos(thetar)*cos(phir), sin(phir));
sphereVerts[k] = vec4(sin(thetar)*cos(phir), cos(thetar)*cos(phir), sin(phir), 1.0);
k++;
sphereNormals[k] = vec3(sin(thetar)*cos(phir20), cos(thetar)*cos(phir20), sin(phir20));
sphereVerts[k] = vec4(sin(thetar)*cos(phir20), cos(thetar)*cos(phir20), sin(phir20), 1.0);
k++;
(etc...)
And now the calls to create the sphere’s have all been changed to use vAmbientDiffuseColor rather than vcolor like this:
mv = mv*Translate(20.0, 1.7, 20.0)*Scale(3.0, 3.0, 3.0);
glUniformMatrix4fv(model_view, 1, GL_TRUE, mv);
glBindVertexArray(vao[2]);
glVertexAttrib4f(vAmbientDiffuseColor, 0.0, 0.6, 1.0, 1.0);
glDrawArrays(GL_TRIANGLES, 0, 1140);
OK all fine and good (I hope), but now, unlike just having flat lights in my cpp file, I need to somehow get all this into my vshader and then my fshader, right?
So in my vertex shader I have:
in vec4 vAmbientDiffuseColor;
in vec3 sphereNormals;
.....
void
main()
{
vec4 sphereNormals = vec4(sphereNormals, 0.0);
AmbientDiffuseColor = vAmbientDiffuseColor;
....
vN = normalize(model_view * sphereNormals).xyz;
.....
out vec3 vN;
out vec4 AmbientDiffuseColor;
So I’m hoping that takes in my diffuse colors and my normals and passes them out…'Then my fragment shader looks like this:
in vec3 vN;
in vec4 AmbientDiffuseColor;
....
uniform vec4 ambient_light;
...
And then the main() function in the fragment shader has some stuff I admit I’m not totally following, I got it from Angel’s website (code examples for the textbook) and perhaps that’s my real problem… Here is the ENTIRE fragment shader code:
#version 150
in vec3 position;
in vec3 vN;
in vec4 AmbientDiffuseColor;
in vec4 SpecularColor;
in float SpecularExponent;
uniform vec4 light_position;
uniform vec4 light_color;
uniform vec4 ambient_light;
out vec4 fColor;
void main()
{
vec3 L = normalize( light_position.xyz - position.xyz);
vec3 E = normalize(-position.xyz);
vec3 N = normalize(vN);
vec3 H = normalize(L+E);
vec4 amb = AmbientDiffuseColor * ambient_light;
vec4 diff = max(dot(L,N), 0.0) * AmbientDiffuseColor * light_color;
vec4 spec = pow( max (dot(N,H), 0.0), SpecularExponent) * SpecularColor * light_color ;
if(dot(L,N) < 0.0){
spec = vec4(1,0,0,1);
}
fColor = amb + diff + spec;
}
But when I run this it looks pretty close to the same output I had without ANY shading or lighting changes with the possible exception of the fact that it’s darker on half of the stage…
Any idea’s? I’m going to try to really hit those links you posted as well as the code you just posted above…
Thanks in advance,
–Pete
PS. The reason I posted the zip file before I started all this (my first attachment) is because I’m totally open to suggestions on accomplishing this a different way. What I want is to add Phong shading with a low level ambient light. If you can think of a better way to do this I’ll take all the help I can get!
PSS. I added 3 screen shots if you’re interested. It shows how the program looks right now with my attempts at lighting/shading from the three “camera views”. Notice that if you’re in one of the two mobile cams (viewpoint is from the drivers “head” and chase cam is up and behind) that there is a big specular reflection on the stage! I’m assuming this is coming from this:
glVertexAttrib4fv(vAmbientDiffuseColor, vec4(.5, 0, 0, 1));
glVertexAttrib4fv(vSpecularColor, vec4(1.0f,1.0f,1.0f,1.0f));
glVertexAttrib1f(vSpecularExponent, 100.0);
glUniform4fv(light_color, 1, vec4(1,1,1,1));
glUniform4fv(ambient_light, 1, vec4(.5, .5, .5, 5));
but why does that work so easy? Is that all I need to do for each object? That can’t be right…