What I need to use normal maps in ogre?
I am trying to do this with a model in meshviewer but the shader didn't apply my normal map on model.
The model has tangent vectors.
Vertex Program
Code: Select all
//-----------------------------------------------------------------
// Estruturas
//-----------------------------------------------------------------
struct vertexInput {
half4 Position : POSITION;
half2 TexCoord : TEXCOORD0;
half3 Tangent : TEXCOORD1;
half3 Normal : NORMAL;
};
struct vertexOutput {
half4 Position : POSITION;
half2 TexCoord : TEXCOORD0;
float3 EyeVec : TEXCOORD1;
half3 LightVec : TEXCOORD2;
half4 Ambient : COLOR0;
};
//-----------------------------------------------------------------
// Vertex Shader
//-----------------------------------------------------------------
vertexOutput vertexProgram(vertexInput In, uniform half4x4 viewInverse,
uniform half4x4 worldViewProj, uniform half4x4 worldIMat,
uniform half4 lightPosition, uniform half4 lightColor,
uniform half4 materialColor, uniform half4 ambientLightColor,
uniform half4 materialSpecular)
{
half3 N;
half4 E;
half4 H;
half4 L;
half4 ambientColor;
half4 diffuseColor;
half4 specularColor;
half3x3 tangentMatrix;
half3 binormal;
vertexOutput Out;
//Converte a posição do vértice para o espaço de visão
Out.Position = mul(worldViewProj, In.Position);
//Atualiza a coordenada da textura
Out.TexCoord = In.TexCoord;
//Cria a matriz do espaço da textura (Tangent Space)
binormal = cross(In.Tangent, In.Normal);
tangentMatrix[0] = In.Tangent;
tangentMatrix[1] = -binormal;
tangentMatrix[2] = In.Normal;
//Cria os vetores
E = normalize( mul(worldIMat, viewInverse[3]) - In.Position );
N = normalize( In.Normal );
L = normalize( mul(worldIMat, lightPosition) - In.Position );
H = normalize( E + L );
//Atualiza o vetor da luz para o Pixel Shader
Out.LightVec = mul(tangentMatrix, L);
//Atualiza o vetor da câmera para o Pixel Shader
Out.EyeVec = mul(tangentMatrix, E);
//Calcula as cores
ambientColor = materialColor * ambientLightColor;
//Atualiza a cor ambiente
Out.Ambient = ambientColor;
return Out;
}
Code: Select all
//-----------------------------------------------------------------
// Estruturas
//-----------------------------------------------------------------
struct vertexOutput {
half4 Position : POSITION;
half2 TexCoord : TEXCOORD0;
float3 EyeVec : TEXCOORD1;
half3 LightVec : TEXCOORD2;
half4 Ambient : COLOR0;
};
//-----------------------------------------------------------------
// Funções
//-----------------------------------------------------------------
half4 ComputeLighting(half3 normalVector, half3 lightVector, half3 eyeVector,
uniform half4 diffuseColor, uniform half4 specularColor,
uniform half shininess)
{
half3 halfVector = normalize(eyeVector + lightVector);
half4 pixelLighting = lit(dot(lightVector, normalVector), dot(halfVector, normalVector), shininess);
return diffuseColor * pixelLighting.y + specularColor * pixelLighting.z;
}
//-----------------------------------------------------------------
// Pixel Shader (Fragment Program)
//-----------------------------------------------------------------
float4 fragmentProgram(vertexOutput In, uniform half4 materialColor,
uniform half4 materialSpecular, uniform float shininess,
uniform half4 lightColor,
uniform sampler2D normalSampler, uniform sampler2D diffuseSampler) : COLOR
{
half3 normal;
half3 eyeVector;
half3 lightVector;
half4 diffuseTextel;
half4 Out;
//Recupera a cor da textura difusa
diffuseTextel = tex2D(diffuseSampler, In.TexCoord.xy);
//Recupera a normal do normal map
normal = normalize(tex2D(normalSampler, In.TexCoord).xyz * 2.0 - 1.0);
//Normaliza os vetores
eyeVector = normalize(In.EyeVec);
lightVector = normalize(In.LightVec);
//Adiciona a cor ambiente no pixel
Out = diffuseTextel * In.Ambient;
//Adiciona a cor difusa e especular baseada no normal map
Out += lightColor * ComputeLighting(normal, lightVector, eyeVector, materialColor * diffuseTextel,
materialSpecular * diffuseTextel.a, shininess);
return Out;
}
Code: Select all
vertex_program Artmanha/NormalMapVS cg
{
source VS_Ogre_NormalMap.cg
entry_point vertexProgram
profiles vs_1_1
}
fragment_program Artmanha/NormapMapPS cg
{
source PS_Ogre_NormalMap.cg
entry_point fragmentProgram
profiles ps_2_0
}
material blinn1
{
technique
{
pass
{
ambient 1 1 1 1
diffuse 1 1 1 1
specular 0 0 0 20
emissive 0 0 0
vertex_program_ref Artmanha/NormalMapVS
{
param_named_auto viewInverse inverse_view_matrix
param_named_auto worldViewProj worldviewproj_matrix
param_named_auto worldIMat inverse_world_matrix
param_named_auto lightPosition light_position 0
//param_named_auto lightColor light_diffuse_colour 0
param_named materialColor float4 1 1 1 1
param_named_auto ambientLightColor ambient_light_colour 0
//param_named materialSpecular float4 1 1 1 40
}
fragment_program_ref Artmanha/NormapMapPS
{
param_named materialColor float4 1 1 1 1
param_named materialSpecular float4 0 0 0 0
param_named shininess float 80
param_named_auto lightColor light_diffuse_colour 0
}
texture_unit
{
texture 512_Diffuse.png 2d
tex_coord_set 0
}
texture_unit
{
texture 512_Normal_04.png 2d
tex_coord_set 1
}
}
}
}
