Cannot get tangent processing right with rotating meshes

A place for users of OGRE to discuss ideas and experiences of utilitising OGRE in their games / demos / applications.
Post Reply
Lord Horazont
Gnoblar
Posts: 1
Joined: Thu Oct 02, 2014 7:28 pm

Cannot get tangent processing right with rotating meshes

Post by Lord Horazont » Thu Oct 02, 2014 10:10 pm

So I have this uv sphere exported from blender using the .mesh exporter by MindCalamity.

The issue I’m experiencing is the following: When rendering the scene with the shaders shown below, the tangents are not correctly calculated (while the normals are). The tangents values are moving along the sphere surface, although they should stay static in world space (as the sphere surface itself remains static; it only rotates). For normals, we compensate that by multiplying the normal with the normal matrix, for which I found inverse_transpose_world_matrix to be the correct one (and theoretically, that’s also exactly what it should be: the inversion of the rotation applied to the mesh).

Also, for tracking the issue down, I have these shaders:

Code: Select all

#version 130
// vertex shader
uniform mat4 u_mvp;
uniform mat4 u_normal_matrix;
uniform mat4 u_m;

in vec3 vertex;
in vec3 normal;
in vec2 uv0;
in vec3 tangent;

out vec2 v_uv;
out vec3 v_pos;
out vec3 v_normal;
out vec3 v_tangent;

void main(void)
{
    gl_Position = u_mvp * vec4(vertex, 1.0f);
    v_uv = uv0;
    v_pos = vec3(u_m * vec4(vertex, 1.0f));

    mat3 normal_matrix = inverse(transpose(mat3(u_m)));
//  mat3 normal_matrix = u_normal_matrix; -- does not work either, same effect as with the matrix above.
    v_normal = normal_matrix * normal;
    v_tangent = normal_matrix * tangent;
}

Code: Select all

#version 130
// fragment shader
in vec3 v_pos;
in vec2 v_uv;
in vec3 v_normal;
in vec3 v_tangent;

out vec4 ocolour;

void main(void)
{
    vec3 n_normal = normalize(v_normal);
    vec3 n_tangent = normalize(v_tangent);

    ocolour = vec4(n_tangent / 2.f + 0.5f, 1.f);
}
As well as this material

Code: Select all

// Material generated by blender2ogre 0.6.0 and heavily modified by me :)

vertex_program foo_vert glsl
{
   source foo.vert

   default_params {
       param_named_auto u_mvp worldviewproj_matrix
       param_named_auto u_m world_matrix
       param_named_auto u_normal_matrix inverse_transpose_world_matrix
   }
}

fragment_program foo_frag glsl
{
   source foo.frag

   default_params {
   }
}

material Material
{
    receive_shadows on

    technique
    {
        pass Material
        {
            ambient 0.8000000715255737 0.7702552676200867 0.7470895051956177 1.0
            diffuse 0.640000066757203 0.6162042232782277 0.597671613062495 1.0
            specular 0.5 0.5 0.5 1.0 12.5
            emissive 0.0 0.0 0.0 1.0

            alpha_to_coverage off
            colour_write on
            cull_hardware clockwise
            depth_check on
            depth_func less_equal
            depth_write on
            illumination_stage
            light_clip_planes off
            light_scissor off
            lighting on
            normalise_normals off
            polygon_mode solid
            scene_blend one zero
            scene_blend_op add
            shading gouraud
            transparent_sorting on

            vertex_program_ref foo_vert
            {
            }

            fragment_program_ref foo_frag
            {
            }

        }
    }
}
The sphere is being rotated using

Code: Select all

sphere->rotate(Ogre::Vector3::UNIT_X, Ogre::Degree(10)*evt.timeSinceLastFrame, Ogre::SceneNode::TS_PARENT);
sphere->rotate(Ogre::Vector3::UNIT_Y, Ogre::Degree(15)*evt.timeSinceLastFrame, Ogre::SceneNode::TS_PARENT);
inside the frameRenderingQueued handler.

So I guess I have some flaw in my theory here. All the examples I found with bumpmapping (that’s what I’m aiming for) in ogre don’t take into account the world matrix at all, which is definitely wrong if I’m not entirely wasted and mind-messed from thinking through this issue for a whole day now.

Does anyone have a clue what I’m doing wrong?

best regards,
horazont

p.s.: So this is also my first post in this forum. I hope I picked the correct subforum; I haven’t been on forums for quite some time, so please bear with me if I’m breaking some unspoken netiquette which evolved during the last few years. I tried to find solutions using $favorite_searchengine and the forum search, to no avail.
0 x

Post Reply