Subversion Repositories gelsvn

Rev

Rev 171 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 171 Rev 178
Line 1... Line 1...
1
#include "Vec3f.h"
1
#include "Vec3f.h"
2
#include "Quaternion.h"
2
#include "Quaternion.h"
3
 
3
 
4
namespace CGLA 
4
namespace CGLA 
5
{
5
{
6
 
-
 
7
	void Quaternion::make_rot(const Mat4x4f& m)
-
 
8
	{
-
 
9
		float trace = m[0][0] + m[1][1] + m[2][2]  + 1.0f;
-
 
10
 
-
 
11
		//If the trace of the matrix is greater than zero, then
-
 
12
		//perform an "instant" calculation.
-
 
13
		if ( trace > TINY ) 
-
 
14
		{
-
 
15
			float S = sqrt(trace) * 2.0f;
-
 
16
			qv = Vec3f(m[2][1] - m[1][2], m[0][2] - m[2][0], m[1][0] - m[0][1] );
-
 
17
			qv /= S;
-
 
18
			qw = 0.25f * S;
-
 
19
		}
-
 
20
		else
-
 
21
		{
-
 
22
			//If the trace of the matrix is equal to zero (or negative...) then identify
-
 
23
			//which major diagonal element has the greatest value.
-
 
24
			//Depending on this, calculate the following:
-
 
25
 
-
 
26
			if ( m[0][0] > m[1][1] && m[0][0] > m[2][2] )  {	// Column 0: 
-
 
27
				float S  = sqrt( 1.0 + m[0][0] - m[1][1] - m[2][2] ) * 2.0f;
-
 
28
				qv[0] = 0.25f * S;
-
 
29
				qv[1] = (m[1][0] + m[0][1] ) / S;
-
 
30
				qv[2] = (m[0][2] + m[2][0] ) / S;
-
 
31
				qw = (m[2][1] - m[1][3] ) / S;
-
 
32
			} else if ( m[1][1] > m[2][2] ) {			// Column 1: 
-
 
33
				float S  = sqrt( 1.0 + m[1][1] - m[0][0] - m[2][2] ) * 2.0f;
-
 
34
				qv[0] = (m[1][0] + m[0][1] ) / S;
-
 
35
				qv[1] = 0.25f * S;
-
 
36
				qv[2] = (m[2][1] + m[1][2] ) / S;
-
 
37
				qw = (m[0][2] - m[2][0] ) / S;
-
 
38
			} else {						// Column 2:
-
 
39
				float S  = sqrt( 1.0 + m[2][2] - m[0][0] - m[1][1] ) * 2.0f;
-
 
40
				qv[0] = (m[0][2] + m[2][0] ) / S;
-
 
41
				qv[1] = (m[2][1] + m[1][2] ) / S;
-
 
42
				qv[2] = 0.25f * S;
-
 
43
				qw = (m[1][0] - m[0][1] ) / S;
-
 
44
			}
-
 
45
		}
-
 
46
		//The quaternion is then defined as:
-
 
47
		//  Q = | X Y Z W |
-
 
48
	}
-
 
49
 
-
 
50
	void Quaternion::make_rot(float angle, const Vec3f& v)
6
  void Quaternion::make_rot(float angle, const Vec3f& v)
51
  {
7
  {
52
    angle = angle/2;
8
    angle = angle/2;
53
    qv = CGLA::normalize(v);
9
    qv = CGLA::normalize(v);
54
    qv *= sin(angle);
10
    qv *= sin(angle);
55
    qw  = cos(angle);
11
    qw  = cos(angle);