【问题标题】:Bump mapping with GLSL使用 GLSL 进行凹凸映射
【发布时间】:2013-12-04 00:25:33
【问题描述】:

我正在尝试实现凹凸贴图,但我不知道问题出在哪里,着色器似乎没问题。我很确定法线、切线和双切线计算得很好,问题出在着色器中,但如果有人想在这里查看代码的其他部分,那就是:

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

我得到的结果是这样的:

https://dl.dropboxusercontent.com/u/1015014/Captura%20de%20pantalla%202013-12-04%20a%20la%28s%29%2001.16.08.png

【问题讨论】:

  • 您的 TBN 矩阵构造错误。应该是mat3 (tang,bitang,norm)。我可能会建议使用 norm 以外的其他词,因为这通常意味着长度 :) 将“norm”简写为“normal”很诱人,但“norm”已经具有其自身的数学含义。跨度>
  • 感谢您的回答。我已经改变了你所说的,但我仍然得到相同的结果......正如我所说,我很确定法线是正确的,因为当我只在对象上使用具有恒定颜色的灯光时,它显示完美,我想 uvs 没问题,因为它正确地映射了纹理......还有其他想法吗?再次感谢!

标签: opengl glsl shader fragment-shader bump-mapping


【解决方案1】:

好的,所以我终于找到了这个错误......它不在着色器中,所以着色器没问题!我遇到的问题是我没有正确地将法线贴图图像传递给着色器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-09
    • 2012-09-30
    • 1970-01-01
    • 2012-02-29
    • 2016-07-30
    相关资源
    最近更新 更多