我的灯光是否正确?

时间:2011-11-13 03:47:02

标签: c++ opengl

我一直在阅读pdf file on OpenGL lighting

它说的是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!

这就是我所做的。 这是我的Vertex和Fragment Shader文件

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);
}

片段着色器右边的插值是什么?

所以这是我的球体(它是低多边形btw):

enter image description here

这是实施Gouraud Shading的标准方法吗? (我的球体的中心为(0,0,0)) 谢谢你的耐心

1 个答案:

答案 0 :(得分:3)

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

请允许我quote myself,“它肯定不符合'照明'的条件。”在你第一次提出这个问题和现在这个问题之间,这并没有停止。

这不是照明。这只是标准化模型空间位置并将其乘以环境颜色。即使我们假设模型空间位置以零为中心并且代表球体上的一个点,将光乘以法线也是没有意义的。这是照明。

如果您想了解灯光的工作原理,read this. Or this.