0

I have been reading a pdf file on OpenGL lighting.

It says for the Gouraud Shading:

• Gouraud shading
– Set vertex normals
– Calculate colors at vertices
– Interpolate colors across polygon
• Must calculate vertex normals!
• Must normalize vertex normals to unit length!

So that's what I did. Here is my Vertex and Fragment Shader file

V_Shader:

#version 330

layout(location = 0) in vec3 in_Position; //declare position
layout(location = 1) in vec3 in_Color;

// mvpmatrix is the result of multiplying the model, view, and projection matrices */
uniform mat4 MVP_matrix;
vec3 ambient;

out vec3 ex_Color;

void main(void) {

   // Multiply the MVP_ matrix by the vertex to obtain our final vertex position (mvp was created in *.cpp)
   gl_Position = MVP_matrix * vec4(in_Position, 1.0);
   ambient = vec3(0.0f,0.0f,1.0f);
   ex_Color = ambient * normalize(in_Position) ; //anti ex_Color=in_Color;
}

F_shader:

#version 330

in vec3 ex_Color;
out vec4 gl_FragColor;

void main(void) {
   gl_FragColor = vec4(ex_Color,1.0);
}

The interpolation is taken care by the fragment shader right?

so here is my sphere (it is low polygon btw):

enter image description here

Is this the standard way of implementing Gouraud Shading? (my sphere has a center of (0,0,0)) Thanks for your patience

wallyk
  • 56,922
  • 16
  • 83
  • 148
Trt Trt
  • 5,330
  • 13
  • 53
  • 86
  • Is that a blue ball with a white light, or the other way around? – Pubby Nov 13 '11 at 03:50
  • haven't specified any light color as you can see. just the ambient variable – Trt Trt Nov 13 '11 at 03:59
  • "I have been reading a pdf file on OpenGL lighting. here it is: www.cs.cmu.edu/~fp/courses/graphics/pdf-color/08-shading.pdf" That PDF explains how fixed-function GL works. And it doesn't explain in detail what's going on behind the functions. Why would you use this instead of any number of shader-based lighting tutorials to learn from? – Nicol Bolas Nov 13 '11 at 04:07
  • possible duplicate of [Issue Understanding Lighting in OpenGL](http://stackoverflow.com/questions/8088907/issue-understanding-lighting-in-opengl) – Nicol Bolas Nov 13 '11 at 04:18

1 Answers1

3
ex_Color = ambient * normalize(in_Position) ; //anti ex_Color=in_Color;

Allow me to quote myself, "It certainly doesn't qualify as 'lighting'." That didn't stop being true between the first time you asked this question and now.

This is not lighting. This is just normalizing the model-space position and multiplying it by the ambient color. Even if we assume that the model-space position is centered at zero and represents a point on the sphere, multiplying a light by a normal is meaningless. It is not lighting.

If you want to learn how lighting works, read this. Or this.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Another thing I wanted to ask is that in my case, normalize(in_Position) equals to the vertex normal right? because my center is (0,0,0) ? – Trt Trt Nov 13 '11 at 04:18
  • @TrtTrt: I would suggest doing things normally before trying cute optimizations like assuming that the model-space position is that of a sphere centered on zero. So just pass an actual normal, so you don't confuse people who look at your code. – Nicol Bolas Nov 13 '11 at 04:22
  • i am not assuming. it is on (0,0,0) – Trt Trt Nov 13 '11 at 04:27
  • so norm(N-C) will become norm(N) yeah? – Trt Trt Nov 13 '11 at 04:28
  • 2
    @TrtTrt: And if it isn't, then your shader doesn't work. Again, get it right _first_, then try tricks like that. Just pass a normal; it's not hard. You're making your shader confusing for people to read, and you're needlessly binding your shader to your vertex data, so that it can't be used with *any* other mesh. Also, see my edit. – Nicol Bolas Nov 13 '11 at 04:29
  • 2
    Please, please read Nicol's excellent tutorial (I believe the tutorial at arcsynthesis.org is yours, right ?), from the beginning. You're really not saving any time by only reading tidbits here and there, especially when they're based on long dead fixed pipeline methods. Take the time to go through this tutorial step by step, it will not only teach you how but, most importantly, WHY you are doing things, which might cut down on nonsense like multiplying light values by vertex positions. – Nicolas Lefebvre Nov 13 '11 at 11:13
  • 1
    @TrtTrt: No, normalize does not yield a normal. I told you in another post, that a normal is something different, than normalizing a vector. Normalizing a vector means, making it unit length, whereas a surface normal is the vector standing on the surface perpendicular. I know, the naming is confusing, but that is it so often in mathematics. – datenwolf Nov 13 '11 at 11:48