calculate tangents - bump mapping

glBeatriz's icon

Hi everybody

Maybe this question relates maths rather than jitter... I am following the bump mapping example of the Orange book, and in order to transform the vertex position, normal and light position into tangent space, I need to calculate tangent on every vertex. There seems to be no "gl_Tangent" or built-in variable which allows the application to pass these values to the shader. Also, jit.gl.gridshape does not allow the message "auto_tangents 1"...

So, how do I calculate tangents? See .zip file for the patch, the initial vertices are generated by a jit.gl.gridshape into the shader.

Any clues?
Thanx!

902.bump_mapping.zip
zip
Rob Ramirez's icon

jit.gl.mesh will automatically calculate tangents for you.
set @auto_tangents 1 on the gl.mesh.

gl.mesh will pass the tangent as a vertex attribute to your shader, but you need to do a few things.
add the following lines to your jxs param declarations and bind declarations

add this to your vertex program, same place as the varrying decs:attribute vec3 tangent;

now just remove the line "vec3 tangent = gl_MultiTexCoord6.xyz;" from your original code, and you should be good to go.

glBeatriz's icon

Thanks for your post robtherich
However the patch is not working yet, it shows the right lighting calculations and the right color, but the bumps appear chaotic, and change at every frame (kind of nice but it is not what I want). I checked the shader code and it looks right...maybe jit.gl.mesh defines the tangents in an inconsistent way?

Another question is, why do you write "state", instead of "value" when defining the parameter tangent?

see attached patch

903.bump_mapping_2.zip
zip
Rob Ramirez's icon

well you can try and calculate them yourself.
the first three planes of the matrixoutput matrix are the xyz position values.

you can create your own tangent matrix, and supply it to gl.mesh as a custom vertex attribute matrix.

this post here provides a great example for using vertex attribute matrices:
https://cycling74.com/forums/another-vertex_attribute-shader-example

fwiw, the tangents calculated by gl.mesh have worked fine for me for a normal-map shader.

"state" is simply the property you us when declaring an attribute (as opposed to a uniform).
if that's not clear, read back up on the difference between glsl attributes and uniforms.

glBeatriz's icon

Many thanks! that was clear :)