nicolasbol

09-10-2009, 10:33 AM

I'm currently writing an engine for iPHone 3GS and diving in openGL es 2.0 made me realize how weak my matrices kung-ku is.

I've followed the shaders example here : http://www.ozone3d.net/tutorials/bump_mapping_p4.php

[Vertex_Shader]

varying vec3 lightVec;

varying vec3 eyeVec;

varying vec2 texCoord;

attribute vec3 vTangent;

void main(void)

{

gl_Position = ftransform();

texCoord = gl_MultiTexCoord0.xy;

vec3 n = normalize(gl_NormalMatrix * gl_Normal);

vec3 t = normalize(gl_NormalMatrix * vTangent);

vec3 b = cross(n, t);

vec3 vVertex = vec3(gl_ModelViewMatrix * gl_Vertex);

vec3 tmpVec = gl_LightSource[0].position.xyz - vVertex;

lightVec.x = dot(tmpVec, t);

lightVec.y = dot(tmpVec, b);

lightVec.z = dot(tmpVec, n);

tmpVec = -vVertex;

eyeVec.x = dot(tmpVec, t);

eyeVec.y = dot(tmpVec, b);

eyeVec.z = dot(tmpVec, n);

}

I have now a georgous iphone bumpMapping support for iphone 3GS but:

1/ It looks like the shader build a matrix cameraspace->tangentspace, instead of providing light position in model space and perform bumpMapping directly, this seems unefficient.

2/ Before modifying to the much faster gpwiki method ( http://gpwiki.org/index.php/OpenGL:Tutorials:GLSL_Bump_Mapping ), I'm trying to undestand exactly what is going on:

vec3 n = normalize(gl_NormalMatrix * gl_Normal);

vec3 t = normalize(gl_NormalMatrix * vTangent);

vec3 b = cross(n, t);

This is basically a matrix*matrix operation.

gl_NormalMatrix is a modelspace->cameraspace,

gl_Normal and vTangent are two compoennt of the tangent space matrix, which perform a modelspace->tangentspace transformation.

Thing I don't get #1:

How comes doing:

modelspace->cameraspace * modelspace->tangentspace

builds a matrix that allow to do cameraspace--> tangentspace

Thing I don't get #2:

When the light vector is rotated, it's not done via a Matrix*Vector but the opposite: Vector*Matrix.

Any help ?

I've followed the shaders example here : http://www.ozone3d.net/tutorials/bump_mapping_p4.php

[Vertex_Shader]

varying vec3 lightVec;

varying vec3 eyeVec;

varying vec2 texCoord;

attribute vec3 vTangent;

void main(void)

{

gl_Position = ftransform();

texCoord = gl_MultiTexCoord0.xy;

vec3 n = normalize(gl_NormalMatrix * gl_Normal);

vec3 t = normalize(gl_NormalMatrix * vTangent);

vec3 b = cross(n, t);

vec3 vVertex = vec3(gl_ModelViewMatrix * gl_Vertex);

vec3 tmpVec = gl_LightSource[0].position.xyz - vVertex;

lightVec.x = dot(tmpVec, t);

lightVec.y = dot(tmpVec, b);

lightVec.z = dot(tmpVec, n);

tmpVec = -vVertex;

eyeVec.x = dot(tmpVec, t);

eyeVec.y = dot(tmpVec, b);

eyeVec.z = dot(tmpVec, n);

}

I have now a georgous iphone bumpMapping support for iphone 3GS but:

1/ It looks like the shader build a matrix cameraspace->tangentspace, instead of providing light position in model space and perform bumpMapping directly, this seems unefficient.

2/ Before modifying to the much faster gpwiki method ( http://gpwiki.org/index.php/OpenGL:Tutorials:GLSL_Bump_Mapping ), I'm trying to undestand exactly what is going on:

vec3 n = normalize(gl_NormalMatrix * gl_Normal);

vec3 t = normalize(gl_NormalMatrix * vTangent);

vec3 b = cross(n, t);

This is basically a matrix*matrix operation.

gl_NormalMatrix is a modelspace->cameraspace,

gl_Normal and vTangent are two compoennt of the tangent space matrix, which perform a modelspace->tangentspace transformation.

Thing I don't get #1:

How comes doing:

modelspace->cameraspace * modelspace->tangentspace

builds a matrix that allow to do cameraspace--> tangentspace

Thing I don't get #2:

When the light vector is rotated, it's not done via a Matrix*Vector but the opposite: Vector*Matrix.

Any help ?