我正在尝试实现凹凸贴图,但我不知道问题出在哪里,着色器似乎没问题。我很确定法线,切线和bitangets计算得很好,问题出现在着色器中,但如果有人想在这里查看代码的其他部分,那就是:
(https://github.com/CarlosCarrera/OpenglElements)
VertexShader.vsh
#version 120
attribute vec3 coord3d;
attribute vec3 normals;
attribute vec2 texcoord;
attribute vec3 tangents;
attribute vec3 bitangents;
varying vec3 LightDir;
varying vec2 f_texcoord;
varying vec3 ViewDir;
//Light
uniform vec4 LightPosition;
uniform vec3 LightIntensity;
//Matrices
uniform mat4 ModelViewMatrix;
uniform mat3 NormalMatrix;
uniform mat4 mvp;
void main()
{
vec3 norm = normalize( NormalMatrix * normals );
vec3 tang = normalize( NormalMatrix * tangents);
vec3 bitang = normalize( NormalMatrix * bitangents);
mat3 toObjectLocal =mat3(
tang.x, bitang.x, norm.x,
tang.y, bitang.y, norm.y,
tang.z, bitang.z, norm.z );
// Transform light direction and view direction to tangent space
vec3 pos = vec3( ModelViewMatrix * vec4(coord3d,1.0));
LightDir = normalize( toObjectLocal * (LightPosition.xyz - pos));
ViewDir = toObjectLocal * normalize(-pos);
gl_Position = mvp * vec4(coord3d,1.0);
f_texcoord = texcoord;
}
FragmentShader.fsh
#version 120
varying vec3 LightDir;
varying vec2 f_texcoord;
varying vec3 ViewDir;
uniform vec4 LightPosition;
uniform vec3 LightIntensity;
uniform vec3 Ar; // Ambient reflectivity
uniform vec3 Sr; // Specular reflectivity
uniform float Shininess; // Specular shininess factor
uniform sampler2D mytexture,mytexture2;
vec3 phongModel( vec3 norm, vec3 diffR ) {
vec3 r = normalize(reflect( -normalize(LightDir), normalize(norm) ));
vec3 ambient = LightIntensity * Ar;
float sDotN = max( dot(normalize(LightDir), normalize(norm)), 0.0 );
vec3 diffuse = LightIntensity * diffR * sDotN;
vec3 spec = vec3(0.0);
if( sDotN > 0.0 )
spec = LightIntensity * Sr *
pow( max( dot(r,normalize(ViewDir)), 0.0 ), Shininess );
return ambient + diffuse + spec;
}
void main() {
// Lookup the normal from the normal map
vec2 flipped_texcoord = vec2(f_texcoord.x, 1.0 - f_texcoord.y);
vec3 normal = 2.0 * texture2D(mytexture2, flipped_texcoord ).rgb - 1.0;
normal = normalize(normal);
vec4 texColor = texture2D( mytexture, flipped_texcoord );
gl_FragColor = vec4( phongModel(normal.xyz, texColor.rgb), 1.0 );
}
我得到的结果是:
答案 0 :(得分:1)
好的,所以我终于发现了这个错误......它不在着色器中,所以着色器还可以!我遇到的问题是我没有正确地将法线贴图图像传递给着色器。