PDA

View Full Version : Convert normalmap to object space



_NoName_
07-27-2016, 12:26 PM
Hello, I had a look at lightning and phong shaders so I also have to do with normal maps in tangend-space.
I want to use this normals without using tangend-vectors.
I tryed to use a rotation Matrix but My Result dose not make me realy happy.

Uploading a screenshot has failed...

The light position is at exactly the center of the world by the coords : 0 35 0, this works without the normal maps well but if I add the normal map the specular part of lightning is not right anyomre and it seams like the lightposition has changed but it did not...

The folowing Codes are my vertex and fragment shader.

This is my vertex shader :

#version 440

layout(location = 0) in vec4 vertexPosition;
layout(location = 3) in vec4 vertexColor;
layout(location = 2) in vec4 vertexNormal;
layout(location = 8) in vec2 vertexTexCoord;

out vec4 Color;
out vec4 Normal;
out vec4 Position;
out vec2 TexCoord;

out mat3 normalMapMatrix;

uniform mat4 modelMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelViewMatrix;
uniform mat4 normalMatrix;


uniform vec3 ambientLight;
uniform vec3 diffuseLight;
uniform vec3 specularLight;
uniform vec3 lightPosition;
uniform float shininess;
uniform vec3 eyePosition;

vec3 surfaceToLight;
vec3 surfaceToCam;
vec3 reflectionVector;
float cosAngle;


void SetNormalMapMatrix()
{
float xAngle = dot(Normal.yz, vec2(1, 0));
float zAngle = dot(Normal.xy, vec2(0, 1));

mat3 xMatrix = mat3(1, 0, 0,
0, cos(xAngle), -sin(xAngle),
0, sin(xAngle), cos(xAngle));
mat3 zMatrix = mat3(cos(zAngle), -sin(zAngle), 0,
sin(zAngle), cos(zAngle), 0,
0, 0, 1);
xMatrix = transpose(xMatrix);
zMatrix = transpose(zMatrix);

normalMapMatrix = xMatrix*zMatrix;
}


void main()
{
gl_Position = projectionMatrix*viewMatrix*modelMatrix*vertexPosi tion;
Color = vertexColor;
Normal = normalize(normalMatrix*vertexNormal);
Position = modelViewMatrix*vertexPosition;
TexCoord = vertexTexCoord;

SetNormalMapMatrix();
}

//END

This is my fragment shader :

#version 440

in vec4 Color;
in vec4 Normal;
in vec4 Position;
in vec2 TexCoord;

in mat3 normalMapMatrix;

out vec4 fragmentColor;

vec3 viewVector;

uniform sampler2D Tex1;

uniform vec3 ambientLight;
uniform vec3 diffuseLight;
uniform vec3 specularLight;
uniform vec3 lightPosition;
uniform float shininess;
uniform vec3 eyePosition;

vec3 surfaceToLight;
vec3 surfaceToCam;
vec3 reflectionVector;

vec3 diffuseColor;
vec3 specularColor;

float cosAngle;

void Phong(vec3 position, vec3 normal)
{
normal = normalize(normal);
surfaceToLight = normalize(lightPosition.xyz-position);
surfaceToCam = normalize(eyePosition.xyz-position);
reflectionVector = normalize(reflect(-surfaceToLight, normal));
cosAngle = max(dot(reflectionVector, surfaceToCam), 0);

float specularCoefficient = pow(cosAngle, 3);
float diffuseCoefficient = max(dot(normalize(normal), normalize(surfaceToLight)), 0);

diffuseColor = diffuseColor + diffuseCoefficient*diffuseLight;
specularColor = specularColor+ specularCoefficient*specularLight;
}

void main()
{


vec4 texColor = texture(Tex1, TexCoord);

vec3 t = vec3(texColor.x-0.5, texColor.y-0.5, texColor.z-0.5);
vec3 b = normalize(cross(t, Normal.xyz));
mat3 tbn = mat3(t, b, Normal.xyz);

Phong(Position.xyz, normalMapMatrix*-t.xyz);
fragmentColor = vec4((diffuseColor+ambientLight)*1+specularColor, 1);
}

//END


Do anyone know why this dose not work ??