• Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

# Getting camera rotation from unit vector

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1Shawn619  Members   -  Reputation: 201

Like
0Likes
Like

Posted 29 July 2012 - 04:36 PM

To start off, i am NOT talking about gluLookAt() to specify the camera rotation, so dont suggest i use it. I'm glRotatef to alter camera rotation.

So far i'v been using angle xrot,yrot,zrot (in degrees) to specify the camera rotation.
ei:
```glRotatef(xrot,1.0,0.0,0.0);
glRotatef(yrot,0.0,1.0,0.0);
glRotatef(zrot,0.0,0.0,1.0);
```

My problem, say i have a unit vector (0.3433, 0.4243, 0.242) and i want to find the appropriate xrot,yrot,zrot to get the camera rotation according the given vector?

I was thinking to solve, say the xrot, i could take do ->angleBetween( (0.3433,0,0), (1,0,0) ) would give the xrot, and so on for the yrot,zrot.

### #2Bacterius  Crossbones+   -  Reputation: 3558

Like
1Likes
Like

Posted 30 July 2012 - 04:24 AM

A single unit vector isn't enough to obtain a three-dimensional camera basis, it'll only give you two angles (azimuth and inclination) which can be extracted via spherical coordinates, but not the third (rotation around the view vector).

It's fairly easy to understand why intuitively: if you are looking in a given direction, you can't tell whether your head is tilted left, right, or you are standing on your hands with your head upside down - the direction vector remains the same in all cases. You need more information.

From a mathematical point of view, a normalized 3D vector only has two degrees of freedom, since z = sqrt(x^2 + y^2), so the third component effectively adds no information. It should then be obvious that it is impossible to uniquely translate this vector to a representation which includes three degrees of freedom, i.e. your three angles.

"The best comment is a deleted comment."
website · blog

[maintenance in progress]

### #3Shawn619  Members   -  Reputation: 201

Like
0Likes
Like

Posted 30 July 2012 - 06:23 AM

But if you think about the about an eye being the camera, your eye points in a direction just as the camera, so assuming an up-vector of always (0,1,0), there is no reason why you shouldn't be able to come up with the camera rotation angle from any nonzero vector (x,y,z)

### #4clb  Members   -  Reputation: 1594

Like
0Likes
Like

Posted 30 July 2012 - 06:46 AM

To convert an orthogonal coordinate frame (identified by front and up directions) to a Euler rotation triplet, first construct an orthonormal matrix (a 'rotation matrix') that represents that coordinate frame, then decompose the matrix into a sequence of three rotation matrices about the cardinal axis sequence you prefer.

For reference, see e.g. MathGeoLib's float3x3 -> Euler conversion routines. (Code here). The derivations for the extractions are straightforward matrix algebra, but to make them robust requires a bit of thought. David Eberly's pdf is good reference for this.
Me+PC=clb.demon.fi | C++ Math and Geometry library: MathGeoLib, test it live! | C++ Game Networking: kNet | 2D Bin Packing: RectangleBinPack | Use gcc/clang/emcc from VS: vs-tool | Resume+Portfolio | gfxapi, test it live!

### #5Bacterius  Crossbones+   -  Reputation: 3558

Like
0Likes
Like

Posted 30 July 2012 - 06:53 AM

But if you think about the about an eye being the camera, your eye points in a direction just as the camera, so assuming an up-vector of always (0,1,0), there is no reason why you shouldn't be able to come up with the camera rotation angle from any nonzero vector (x,y,z)

Absolutely, if you assume a constant up vector then you can compute your orthonormal basis as follows, where

Or in pseudocode:
```up = (0, 1, 0);
basisX = normalize(cross(v, up)); // "lateral"
basisY = normalize(cross(v, basisX)); // "vertical"
basisZ = v; // "forward"
```

(Note this is assuming a Y-up, Z-forward coordinate system, which is the one I'm most familiar with but is unfortunately not the one used by OpenGL, so shuffle the axes as needed)

I believe you can then extract each angle by dotting each basis vector with its corresponding axis, but I could be wrong (probably am, it's late).

"The best comment is a deleted comment."
website · blog

[maintenance in progress]

### #6l0calh05t  Members   -  Reputation: 362

Like
1Likes
Like

Posted 30 July 2012 - 07:01 AM

unless your vector is pointing directly upwards or downwards (v=(0,+-1,0))

Edited by l0calh05t, 30 July 2012 - 07:02 AM.

### #7Bacterius  Crossbones+   -  Reputation: 3558

Like
0Likes
Like

Posted 30 July 2012 - 07:05 AM

unless your vector is pointing directly upwards or downwards (v=(0,y,0))

Good point, the orthonormal basis is undefined in this case. Note the vector is normalized so it's going to be .

Edited by Bacterius, 30 July 2012 - 07:05 AM.

"The best comment is a deleted comment."
website · blog

[maintenance in progress]

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS