Xi Yang

06-08-2011, 10:21 AM

I need some help about how to correctly transform the normals from texture coordinate to object (or viewer) coordinate.

For instance, I have a fragment shader like this:

// the interpolated normal provided by vertex shader

in vec3 obj_normal;

// the normal mapping sampler

uniform sampler2D sp_bump;

// the "default" normal value from the texture

const vec3 unit_z = vec3(0,0,1);

void main() {

// get the normal from texture

vec3 tex_normal = texture(sp_bump,gl_TexCoord[0].st).rgb;

tex_normal = normalize(normal_tex*2-vec3(1,1,1));

// normal transform

vec3 axis = cross(unit_z,obj_normal);

float cosine = dot(unit_z,obj_normal);

float sine = length(axis);

// I guarantee this matrix was correctly generated, as

// I've tested it both using matrix and using quaternion

mat3 normal_trans = some_way_that_give_the_matrix();

// now, produce the normal to use later

// it should has: obj_normal = normal_trans*unit_z;

// so it should also has this:

vec3 use_normal = normal_trans * tex_normal;

}

However, my transform is not working correctly, especially when the obj_normal is nearly towards camera. What's the problem inside there?

Thanks!

For instance, I have a fragment shader like this:

// the interpolated normal provided by vertex shader

in vec3 obj_normal;

// the normal mapping sampler

uniform sampler2D sp_bump;

// the "default" normal value from the texture

const vec3 unit_z = vec3(0,0,1);

void main() {

// get the normal from texture

vec3 tex_normal = texture(sp_bump,gl_TexCoord[0].st).rgb;

tex_normal = normalize(normal_tex*2-vec3(1,1,1));

// normal transform

vec3 axis = cross(unit_z,obj_normal);

float cosine = dot(unit_z,obj_normal);

float sine = length(axis);

// I guarantee this matrix was correctly generated, as

// I've tested it both using matrix and using quaternion

mat3 normal_trans = some_way_that_give_the_matrix();

// now, produce the normal to use later

// it should has: obj_normal = normal_trans*unit_z;

// so it should also has this:

vec3 use_normal = normal_trans * tex_normal;

}

However, my transform is not working correctly, especially when the obj_normal is nearly towards camera. What's the problem inside there?

Thanks!