# Using normal maps to output world-space normals

Im using deffered shading and here is my g-pass Vertex shader code:

vec3 T = normalize(vec3(transform * vec4(tangent, 0.0)));
vec3 N = normalize(vec3(transform * vec4(normal, 0.0)));
vec3 B = cross(T, N);
vs_out.TBN = mat3(T, B, N);


And my Fragment shader code...

gNormal.xyz = normalize(fs_in.TBN * (texture(normal, fs_in.uv).rgb * 2.0 - 1.0));


Usually, you would times your light direction and/or view direction with TBN. However, my g buffers are taking up more bandwidth than they should, and i would prefer to get world space normals from the normal map. How should i go about doing so?

EDIT: Apparently, this should output world space... but it isnt. So i guess something here is wrong.

Tangent generation function:

void CalculateTangents() {
tangents.clear();
for (int i = 0; i < vertices.size(); i += 3) {
glm::vec3 edge1 = vertices[i + 1] - vertices[i];
glm::vec3 edge2 = vertices[i + 2] - vertices[i];
glm::vec2 deltaUV1 = uvs[i + 1] - uvs[i];
glm::vec2 deltaUV2 = uvs[i + 2] - uvs[i];

GLfloat f = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV2.x * deltaUV1.y);
glm::vec3 tangent;
tangent.x = f * (deltaUV2.y * edge1.x - deltaUV1.y * edge2.x);
tangent.y = f * (deltaUV2.y * edge1.y - deltaUV1.y * edge2.y);
tangent.z = f * (deltaUV2.y * edge1.z - deltaUV1.y * edge2.z);
tangent = glm::normalize(tangent);

tangents.push_back(tangent);
tangents.push_back(tangent);
tangents.push_back(tangent);
}
} There is a point light "attached" to the camera (which clearly isnt working) and the reflection is wrong.

## 1 Answer

http://www.thetenthplanet.de/archives/1180

I suggest that you use this form instead. Instead of doing a complete transform of your data it works the other way around if that makes any sense. So that way it's much easier to tell what's going on and you can more easily work with the normals themselves.

Otherwise all you can do is know you have a bug...somewhere. And not necessarily in the code but it could be in your data as well.

This tutorial will help you show actual normals using geometry shader.

http://mbsoftworks.sk/index.php?page=tutorials&series=1&tutorial=17