Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
671 khor 1
#include "CGLA/Vec3f.h"
2
#include "CGLA/Quaternion.h"
3
 
4
using namespace std;
5
using namespace CGLA;
6
 
7
/* CGLA example no. 1:
8
	 Computing a normal from the three vertices
9
	 of a triangle */
10
 
11
void example1()
12
{
13
	// Define triangle vertices
14
	Vec3f p0(10,10,10);
15
	Vec3f p1(20,10,10);
16
	Vec3f p2(10,20,10);
17
 
18
	// Compute normal
19
	Vec3f n = normalize(cross(p1-p0,p2-p0));
20
 
21
	// Vectors can be printed using normal facilities
22
	cout << n << endl;
23
}
24
 
25
/* CGLA example no. 2:
26
	 Using spherical coordinates.
27
*/
28
void example2()
29
{
30
	// Create a null vector
31
	Vec3f p;
32
 	p.set_spherical(0.955317f, 3.1415926f/4.0f , 1.0f);
33
	cout << " p = " << p << ", p.length() = " << p.length() << endl;
34
}
35
 
36
void example3()
37
{
38
	// Construct a quaternion that rotates the x-axis into the 
39
	// y-axis.
40
	Quaternion q;
41
	q.make_rot(Vec3f(1,0,0), Vec3f(0,1,0));
42
 
43
	// Create a 4 by 4 Matrix
44
	Mat4x4f m = translation_Mat4x4f(Vec3f(1,2,3));
45
 
46
	// Multiply the quaternion rotation onto m.
47
	m *= q.get_Mat4x4f();
48
 
49
	// Create a vector (1,1,1)
50
	Vec3f p(1);
51
 
52
	// Multiply p onto m as a point. That is we assign 1 to p's w coordinate
53
	// and then throw away the w coordinate after the transformation.
54
	Vec3f p2 = m.mul_3D_point(p);
55
 
56
	cout << p2 << endl;
57
}
58
 
59
 
60
void example4()
61
{
62
	Vec3f p1(0.8660f,.4f,-.3f);
63
	Vec3f p2(-0.8660f,.4f,-.3f);
64
	Vec3f p3(0.0f,.7f,-.7141f);
65
 
66
	Vec3f n1 = cross(p1,p2);
67
	Vec3f n2 = cross(p2,p3);
68
	Vec3f n3 = cross(p3,p1);
69
	Vec3f n  = normalize(n1+n2+n3);
70
	cout << n1 << n1.length() << endl
71
			 << n2 << n2.length() << endl
72
			 << n3 << n3.length() << endl
73
			 << dot(n,Vec3f(0,0,1)) << endl;
74
}
75
 
76
int main()
77
{
78
// 	example1();
79
// 	example2();
80
// 	example3();
81
	example4();
82
}