Bump mapping с GLSL
Я пытаюсь реализовать рельефное отображение, но я не знаю, в чем проблема, шейдеры вроде бы в порядке. Я почти уверен, что нормали, касательные и битовые видения хорошо вычислены, и проблема в шейдере, но если кто-то хочет посмотреть другие части кода, то это:
( 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 );
}
Я получаю следующие результаты:
1 ответ
Решение
Итак, я наконец нашел ошибку... Это было не в шейдерах, так что шейдеры в порядке! Проблема в том, что я неправильно передавал изображение карты нормалей в шейдер.