0

I have a piece of code that takes a normal and a position and then creates a matrix. The matrix should orient values in the direction of the normal and offset everything in a certain direction. using namespace glm;

//Initialize a base translation matrix:
mat4 matrix = translate(mat4(),vec3(pos.x,pos.y,pos.z));

vec3 normal = vec3(inputnormal.x,inputnormal.y,inputnormal.z);
const vec3 upvector = vec3(0.0,0.0,1.0);

//Rotate around normal axis a bit (input by artists)
//matrix = rotate(matrix,radians(degrees),normal);

//Build orientation matrix from normal + upvector:
vec3 tangent = normalize(cross( normal, upvector ));
vec3 binormal = normalize(cross(normal, tangent));
mat3 orient = mat3(tangent, binormal, normal);

//Multiply by the orientation matrix
matrix *= mat4(orient); 

The orientation works, the top of the box is correctly pointed in the direction of the normal. But the box rotates around the normal axis for a lot of normals. (even with rad being 0.0)

Here is a video of the problem: the decal in the level editor point into the normal direct, but also rotates along normal axis.

EDIT: Added video and new calculation... https://www.youtube.com/watch?v=Yeq9Fn4f9Gs

So my question is: how do i prevent rotation around the normal axis, OR how do i find the proper angle to undo said rotation in my matrix. (So, Given matrix and arbitrary axis, how do i find angle?)

AnalyticaL
  • 501
  • 2
  • 9

1 Answers1

1

you need 2 non parallel directions instead of just one ... and exploit cross product to obtain the 3th vector.

So one axis is your normal as the other vector use some invariant to something. Which one depends on your environment. For example some possibilities: Up,North,To Sun,To moon,(1,0,0) or (0,1,0) or (0,0,1), To first vertex of face. Wrongly selected vector will cause the matrix will rotate with change of the normal vector.

For more info see:

Community
  • 1
  • 1
Spektre
  • 49,595
  • 11
  • 110
  • 380
  • So if i understand this correctly, i chose the wrong up vector? The orientation function takes 1 normal and 1 up vector and should provide me with a correct matrix. (it does the cross internally to create axis/angle information, then creates a matrix) – AnalyticaL Jun 09 '16 at 14:24
  • @AnalyticaL It does not matter where the vector points (do not need to be just Up) but yes wrongly selected second vector will cause rotation. As your `UP` vector is constant but not related to your face the cross product will return perpendicular vector to booth but rotated by the actual circumstances ... The direction should be chosen so the result align to something like horizon , some point, light source , etc ... – Spektre Jun 09 '16 at 14:41
  • I tried other up vector's, for as far as i understand, the orientation function does what you describe, create axis/angle from normal + up vector and the create a matrix out of it. Am i missing something? if i cross normal with something and use that as input, my box gets misaligned. – AnalyticaL Jun 09 '16 at 18:02
  • @AnalyticaL well without knowing what exactly are you trying to do is hart to tell you which directions to use .... You did not mention to what you want align ... the only info you provide is that you have a box. Also there is a very slight possibility you got problem with your `orientation` function. You can try to use one of the other axises of the original object matrix instead of your `UP` or as a last resort you can try to get the angle between one of the side axises before and after rotation exploiting their dot product and rotate back localy.. – Spektre Jun 10 '16 at 06:15
  • Will test following code tonight: `//Build orientation matrix from normal + upvector: vec3 normal = vec3(inputnormal.x,inputnormal.y,inputnormal.z); const vec3 upvector = vec3(0.0,0.0,1.0); vec3 tangent = normalize(cross( normal, upvector )); vec3 binormal = normalize(cross(normal, tangent)); mat3 orient = mat3(tangent, binormal, normal);` After that i will post a video to show what i mean. Like i said: orientation into normal direction works (part of GLM btw... not my function) But the box also rotates around the normal axis... The 1st effect is desired, the side effect not. – AnalyticaL Jun 10 '16 at 07:22
  • Updated question, added video to illustrate the problem. I don't have an object matrix as that is the thing i am building in this function. what i have is a world-space position and a world-space normal. What i want to create from that is an object matrix that translate to that world-space position, and aligns all vertices of the object to make the object "point" into the normal direction. This all works except somehow it also rotates around the normal. – AnalyticaL Jun 10 '16 at 12:20
  • After updating calculation to the calculation above and then using (0.0,1.0,0.0): This happened: https://www.youtube.com/watch?v=sJUQpl93Jd4 Fixed! Thanks for pointing me in the right direction... (no pun intended) – AnalyticaL Jun 10 '16 at 12:45
  • @AnalyticaL glad to be of help – Spektre Jun 10 '16 at 13:49