~zjm/Moon3D

ref: 0fe31db4998f636b8f65ca790b75c0585ba47d86 Moon3D/test/project.test.c -rw-r--r-- 2.9 KiB
0fe31db4Zack Michener refactor camera/projection to avoid globals 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "tests.h"
#include <math.h>

void ProjectionTests(void)
{
	DoTest(&TestPerspectiveProjection);
	DoTest(&TestViewmapPolygon);
	DoTest(&TestCameraPosition);
}

void TestPerspectiveProjection(void)
{
	Projection *projection = NewPerspectiveProjection(90, 1, 1, 2);
	Camera *camera = NewCamera(V(0, 0, 0));
	Transform t = GetProjectionTransform(camera, projection);

	Transform ct = CameraTransform(camera);
	AssertTrue(IsIdentity(ct));

	Vertex p = V(0, 1, -1);
	Vertex q = Homogenize(ProjectPoint(p, t));
	AssertTrue(VertexEqual(q, V(0, 1, 1)));

	p = V(0, -1, -1);
	q = Homogenize(ProjectPoint(p, t));
	AssertTrue(VertexEqual(q, V(0, -1, 1)));

	p = V(0, 2, -2);
	q = Homogenize(ProjectPoint(p, t));
	AssertTrue(VertexEqual(q, V(0, 1, -1)));

	p = V(0, -2, -2);
	q = Homogenize(ProjectPoint(p, t));
	AssertTrue(VertexEqual(q, V(0, -1, -1)));
}

void TestViewmapPolygon(void)
{
	InitDeviceTransform(400, 240);
	Polygon *p = NewPolygon();
	AddVertex(p, NewVector(-0.921176, -1.000000, 1.000000));
	AddVertex(p, NewVector(1.000000, -1.000000, 0.559785));
	AddVertex(p, NewVector(1.000000, 1.000000, 0.559785));
	AddVertex(p, NewVector(-0.921176, 1.000000, 1.000000));

	ViewmapPolygon(p);
	for (int i = 0; i < NumVertices(p); i++) {
		Vertex *v = PolygonVertex(p, i);
		AssertTrue(v->x >= 0);
		AssertTrue(v->x < 400);
		AssertTrue(v->y >= 0);
		AssertTrue(v->y < 240);
	}
	FreePolygon(p);

	p = NewPolygon();
	AddVertex(p, NewVector(0.757628, -1.000000, 1.000000));
	AddVertex(p, NewVector(0.757628, 1.000000, 1.000000));
	AddVertex(p, NewVector(-1.000000, 1.000000, 0.574397));
	AddVertex(p, NewVector(-1.000000, -1.000000, 0.574397));

	ViewmapPolygon(p);
	for (int i = 0; i < NumVertices(p); i++) {
		Vertex *v = PolygonVertex(p, i);
		AssertTrue(v->x >= 0);
		AssertTrue(v->x < 400);
		AssertTrue(v->y >= 0);
		AssertTrue(v->y < 240);
	}
	FreePolygon(p);


	p = NewPolygon();
	AddVertex(p, NewVector(0.90350350893137943, 1.7320508075688776, 1.1582046473758254));
	AddVertex(p, NewVector(-2.0841785003064004, 1.7320508075688776, 0.14396012337173758));
	AddVertex(p, NewVector(-2.0841785003064004, -1.7320508075688776, 0.14396012337173758));
	AddVertex(p, NewVector(0.90350350893137943, -1.7320508075688776, 1.1582046473758254));
	ClipPolygon(&p);
	HomogenizePolygon(p);
	ViewmapPolygon(p);
	for (int i = 0; i < NumVertices(p); i++) {
		Vertex *v = PolygonVertex(p, i);
		AssertTrue(v->x >= 0);
		AssertTrue(v->x < 400);
		AssertTrue(v->y >= 0);
		AssertTrue(v->y < 240);
	}
	FreePolygon(p);
}

void TestCameraPosition(void)
{
	Camera *camera = NewCamera(V(10, 20, -3));
	Vector p = camera->position;
	AssertEqual(p.x, 10);
	AssertEqual(p.y, 20);
	AssertEqual(p.z, -3);

	MoveCamera(V(1, 1, 1), camera);
	p = camera->position;
	AssertEqual(p.x, 11);
	AssertEqual(p.y, 21);
	AssertEqual(p.z, -2);

	RotateCamera(0, 130, 0, camera);
	p = camera->position;
	AssertEqual(p.x, 11);
	AssertEqual(p.y, 21);
	AssertEqual(p.z, -2);
}