我有一个以C(0,0,0)为中心的球体。现在我计算顶点着色器内的法线。我没有把它传递给它。
#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 constant_col;
vec3 normal_pos,normal_light_pos;
vec3 Light_Pos = vec3(3.0f, 2.0f, 4.0f); //Light Source
float light_magn,norm_magn;
float dot_prod;
float angle;
float Light_in;
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);
//normalizing vectors
normal_pos = normalize(in_Position);
normal_light_pos = normalize(Light_Pos);
//calculating the vector's magnitude
light_magn = sqrt(pow(normal_light_pos.x,2) + pow(normal_light_pos.y,2) + pow(normal_light_pos.z,2));
norm_magn = sqrt(pow(normal_pos.x,2) + pow(normal_pos.y,2) + pow(normal_pos.z,2));
dot_prod = dot(normal_light_pos, normal_pos); //getting the dot product
angle = dot_prod/ (light_magn*norm_magn); //getting angle in radians
angle = clamp(angle, 0, 1);
Light_in = cos(angle); // here I calculate the light intensity
constant_col = vec3(1.0f,0.0f,0.0f); //set the sphere's color to red
ex_Color = constant_col * Light_in ; //for sphere
}
我的代码基于Lambertian余弦规则,基本上来自这里: http://en.wikipedia.org/wiki/Lambertian_reflectance
我得到的是:
答案 0 :(得分:3)
两个向量的标量(=点)乘积已经为您提供了这些向量的角度的余弦。所以你的cos(角度)完全是多余的。
另外,通常计算表面法线与从光源到表面上的归一化矢量之间的点积。但是,您在灯光位置和法线矢量之间做了一个点积,这是不正确的。你想要像
这样的东西dot_prod = dot( normalize(normal_light_pos - MV * gl_Vertex, normal_pos);
请注意,顶点仅与modelview相乘(不是modelviewprojection)。
说真的,你应该通过一些体面的教程,你的尝试有很多错误。