光线移动与相机位置

时间:2015-01-05 14:20:45

标签: python opengl kivy lighting glsles

我正在使用kivy来实现一个具有多个纹理和法线贴图的简单点光源渲染器。我的场景是一个简单的正方形,以我对其进行映射的原点为中心。我有4张地图: 弥散反照率 镜面反照率 粗糙度图 法线贴图

在我的python / kivy代码中,我设置了我的矩阵和其他制服,如下所示:

view_mat = Matrix().look_at(self.cam_pos*math.sin(self.cam_rot),0,self.cam_pos*math.cos(self.cam_rot),0.,0.,0.,0.,1.,0.)
projection_mat = Matrix().view_clip(-width/height, width/height, -1., 1., 1., 10000., 1.)
light_pos = //can vary with keyboard input
model_mat = Matrix() (identity)
light_pos = (model_mat.multiply(view_mat)).normal_matrix() //Computes the inverse transpose of the upper left corner of the modelView matrix

我对象的法线设置为(0,0,1),切线为(1,0,0)

我的片段着色器是:

void main() {

// Compute vertex eye space vertex, light and normal postion                                                                                                               
vec4 vertex_eye_space = view_mat * model_mat * vec4(v_pos, 1.0);
vec4 light_eye_space = view_mat * light_pos;
vec3 normal_eye_space = normalize((normal_mat * vec4(v_normal,0.0))).xyz;
vec3 tangent_eye_space = normalize((normal_mat * vec4(vec3(1.,0.,0.), 0.))).xyz;
vec3 bitangent_eye_space = normalize(cross(normal_eye_space, tangent_eye_space));
vec3 viewDirection_eye_space = normalize(-vertex_eye_space.xyz);

mat3 tbn_mat = mat3(tangent_eye_space, bitangent_eye_space, normal_eye_space);

viewDirection = normalize(tbn_mat * viewDirection_eye_space);
lightDirection = normalize(tbn_mat * (light_eye_space.xyz - vertex_eye_space.xyz));
vertexPosition = tbn_mat * vertex_eye_space.xyz;
texCoordinate = v_tc0;

vertex_normal = normal_eye_space;

gl_Position = projection_mat * vertex_eye_space;
}

相关的片段着色器:

void main() {

vec3 light_vector = normalize(lightDirection);
vec3 view_vector = normalize(viewDirection);

vec3 normal = normalize((texture2D(nmap, texCoordinate).rgb) * 2.0 - 1.0);
vec3 halfway_vector = normalize((light_vector + view_vector));

vec4 diff = pow(texture2D(diffuse, texCoordinate), vec4(2.2));

vec3 Diffuse = diff.xyz * clamp(dot(normal, light_vector), 0.0, 1.0);
gl_FragColor = vec4(Diffuse, 1.0);

}

现在,当我移动相机时,N.L值会发生变化,完全没有任何意义,尽管花费数天时间进行调试,但我无法弄清楚这段代码有什么问题。

0 个答案:

没有答案