我正在使用ogre3d meshy来查看模型。我编写了一个可以使用法线贴图,反射,高光贴图的着色器。但是法线贴图不能正常工作,如图所示。
正如您所见,反射正在工作,而且世界地图正在发挥作用,但我不需要看到法线贴图和凹凸贴图。但
我试图禁用所有内容并输出法线贴图的结果及其纹理和采样正确,镜面反射和漫反射贴图相同。
/ * 环境立方反射 法线贴图 TetxureMapping Scaling * /
#version 120
#define lowp
#define mediump
#define highp
attribute vec4 vertex;
attribute vec3 normal;
attribute vec4 uv0;
attribute vec4 uv1;
attribute vec4 tangent;
//attribute vec3 binormal;
uniform mat4 normalMatrix;
uniform mat4 modelViewProjectionMatrix;
uniform mat4 modelView;
uniform vec3 camera_world_position;
uniform mat4 textureMatrix0;
varying vec2 texCoord[2];
varying vec3 viewWorld;
varying mat3 world2Tangent;
void main()
{
gl_Position = modelViewProjectionMatrix * vertex;
// transform the uv coordinates
texCoord[0] = (textureMatrix0 * uv0).xy;
texCoord[1] = uv1.xy;
//world.
vec3 vertexWorld = vec3(modelView * vertex);
//transform world to tangent.
//world2Tangent = mat3(normalMatrix) * mat3(tangent, binormal, normal);
// no binormal in ogre?. must reconstruct. Ogre inverts?
vec3 binormal = cross ( normal, tangent.xyz ) * tangent.www;
world2Tangent = mat3(normalMatrix) * mat3(tangent.xyz, binormal, normal);
//Camera Position
//Use Light0 instead of camera position to match phong specular with environment reflection
viewWorld = normalize( - vertexWorld );
}
#version 120
#define lowp
#define mediump
#define highp
uniform sampler2D diffuseColorMap;
uniform sampler2D ambientOcclusionMap;
uniform sampler2D normalMap;
uniform sampler2D specularMap;
uniform samplerCube envMap;
uniform float diffuseFactor;
uniform float reflectionFactor;
uniform float opacity;
varying vec2 texCoord[2];
varying mat3 world2Tangent;
varying vec3 viewWorld;
void main()
{
//unpack current normal in tangent space
vec3 normal = 2.0 * texture2D (normalMap, texCoord[0].st).rgb - 1.0;
// environment reflection in world space
vec3 normalWorld = normalize(world2Tangent * normal);
vec3 refDir = viewWorld - 2.0 * dot(viewWorld,normalWorld) * normalWorld;
vec4 diffuseColor = texture2D( diffuseColorMap, texCoord[0] );
//mix ambient material with reflection
vec4 final = diffuseFactor * ( diffuseColor * texture2D(ambientOcclusionMap, texCoord[0]) ) +
reflectionFactor * ( textureCube(envMap,refDir) * texture2D(specularMap, texCoord[0]) );
final.a = diffuseColor.a * opacity;
gl_FragColor= final;
}