# Vector Rotation

## Recommended Posts

Hi, In the process of bump mapping, I came upon this problem: Let: -a = (x1,y1,z1) -b = (0,0,1) -c = (x2,y2,z2) Then: f(b) = a The question is: f(c) = ? In practice, I know a, b, and c as givens, and all vectors are unit vectors. Thanks, -Geometrian

##### Share on other sites
The solution is not unique; imagine if you will the rotation as being the combination of two rotations, one is the rotation about the b vector, and the other is a rotation that takes the vector b to the vector a. the first rotation can be chosen to have any angle, because rotating b about the vector b won't modify it, but will result in a different matrix which will change the outcome of applying the rotation to the c vector.

Where has this problem arose from? I've done a bit of bumpmapping before and I can't understand why you need this.

##### Share on other sites
Well, given an arbitrary mesh, say, a sphere, it will have normals at right angles to the surface facing in all directions.

The normal map is a full-color image with slightly perturbed normals. These normals, encoded as RBG, are mostly blue, i.e., along the z-axis. The normals from the normal map need to be distorted around the object, so that they are in object space.

Currently, the object is loaded, and all the normals on the object are nearly along the z-axis, which looks weird for obvious reasons.

##### Share on other sites
ah i see now, you need to construct a tangent space for a given point on the sphere which requires a choice of orientation due to the said non-uniqueness of the solution.

Basicly, you have a basis at each point in space that defines the plane at that point, so you obviously have the normal, but to calculate the other two axi, you need to choose one, to find the other;

I do this with the normal, and then having a vector that points around the sphere in the way you would imagine a texture would span horizontally, then the other vector found with cross product, and you form the matrix to transform normal from normal map.

So with normal from sphere, i discard the y component, rotate it 90 degrees in xz plane, normalise, then take cross product;use those 3 axi to form the matrix for transforming the normal from normal map (may need a change of coordinate system since you implied the normal map majored on the z axis)

##### Share on other sites
There is a chapter in the "Cg Tutorial" about bump mapping. The book is now available for free at: http://developer.nvidia.com/object/cg_tutorial_home.html. It is very good, and explains well how tangent space fits into bump-mapping.

When i first did bump mapping, some time ago, i learned how to calculate the tangent space (i.e. the tangent and bitangent vectors, which together with the normal, forms a 3D-space called the tangent space) primarily from this: http://jerome.jouvie.free.fr/OpenGl/Lessons/Lesson8.php

(note, as far as i remember there were a few mistakes in this note. Also, it calculates the tangent, bitangent and normal vectors from scratch per triangle. It is also possible to calculate the tangent and bitangent per vertex relative to the supplied vertex-normal, which is probably what you want.)

Also you could take a look at this: http://www.terathon.com/code/tangent.html

Quote:
 The normals from the normal map need to be distorted around the object, so that they are in object space.

The usual way of doing it, is not to transform the normals from the normal-map into object-space, but rather to transform all vectors you use (vertex-normal, light-vectors etc) into tangent space. (the normals in the normal-map are already in tangent space). (see the Cg Tutorial). You can, however, transform the normals into object space if you want.

Edit: removed typo

[Edited by - ZaiPpA on October 25, 2008 3:05:19 PM]

##### Share on other sites
Hi,

I just need the tangent and bitangent to the normals now (they need to be calculated in the shader, as you realized). Is there a glsl example of that?

Thanks,
-G

##### Share on other sites
You can't calculate the tangent and bitangent in a shader. A vertex-shader only knows information about the vertex on which it is run. But you need information about all three vertices of a triangle in order to calculate the tangent and bitangent vectors, not just one of them. This can't be done in the vertex-shader. (it can probably be done in a geometry-shader, tho)

So you must calculate the tangent and bitangent vectors in your C++ program, for each vertex. When you draw your mesh, you send these vectors to your GLSL shader as vertex attributes, for each vertex, similarly to how you do for normals (or for each triangle, if you use per-triangle vectors instead of per-vertex vectors)

(well, you only need to send one of them actually, since the other one can be calculated as a cross product in the shader)

##### Share on other sites
Yes, but wasn't there a way to do it in a vertex shader using the texture coordinates?

##### Share on other sites
Well... You need the texture coordinates from the other vertices of the triangle as well, not just from one vertex.

(it is possible that there exists some other method to make the tangent-space vectors. Actually i'm sure that there exists some other methods which only works in spacial cases, but the normal way to calculate them (which works for meshes in general) requires info about more than one vertex. And i guess this is what you're after?)

I suppose so.