-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
131 lines (131 loc) · 5.38 KB
/
test.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/// <reference path="engine.ts"/>
function testRasterize() {
var p1 = new Vector4(300, 0, 0);
var p2 = new Vector4(100, 300, 0);
var p3 = new Vector4(400, 600, 0);
var v1 = new Vertex3D(p1, new Vector4(255, 0, 0));
var v2 = new Vertex3D(p2, new Vector4(0, 255, 0));
var v3 = new Vertex3D(p3, new Vector4(0, 0, 255));
var screen = new Screen3D(1024, 768);
screen.rasterizer.rasterizeTriangle(v1, v2, v3);
var transRight = new Vector4(400, 0, 0);
v1 = new Vertex3D(p1.add(transRight), new Vector4(255, 0, 0));
v2 = new Vertex3D(p2.add(transRight), new Vector4(0, 255, 0));
v3 = new Vertex3D(p3.add(transRight), new Vector4(0, 0, 255));
screen.rasterizer.rasterizeTriangle(v1, v2, v3);
}
function testScreen3d() {
var p1 = new Vector4(0, 1, 0);
var p2 = new Vector4(-1, 0, 0);
var p3 = new Vector4(0, -1, 0);
var screen = new Screen3D(1024, 768);
var v1 = new Vertex3D(p1.transform(screen.ndc2screen), new Vector4(255, 0, 0));
var v2 = new Vertex3D(p2.transform(screen.ndc2screen), new Vector4(0, 255, 0));
var v3 = new Vertex3D(p3.transform(screen.ndc2screen), new Vector4(0, 0, 255));
screen.rasterizer.rasterizeTriangle(v1, v2, v3);
}
function testPerspective() {
var p1 = new Vector4(0, 30, -50);
var p2 = new Vector4(-30, -30, -50);
var p3 = new Vector4(30, -30, -50);
var screen = new Screen3D(1024, 768);
var camera = new Camera3D(screen, 90 * 3.14 / 180, 5 / 4, 0.01, 1000);
var v1 = new Vertex3D(p1.transform(camera.view2Clipping).perspectiveDivide().transform(screen.ndc2screen), new Vector4(255, 0, 0));
var v2 = new Vertex3D(p2.transform(camera.view2Clipping).perspectiveDivide().transform(screen.ndc2screen), new Vector4(0, 255, 0));
var v3 = new Vertex3D(p3.transform(camera.view2Clipping).perspectiveDivide().transform(screen.ndc2screen), new Vector4(0, 0, 255));
screen.rasterizer.rasterizeTriangle(v1, v2, v3);
}
function testLookAt() {
var p1 = new Vector4(0, 30, 0);
var p2 = new Vector4(-30, 0, 0);
var p3 = new Vector4(0, -30, 0);
var screen = new Screen3D(1024, 768);
var camera = new Camera3D(screen, 90 * 3.14 / 180, 5 / 4, 0.01, 1000);
camera.lookAt(new Vector4(0, 0, -100), new Vector4(0, 0, 0), new Vector4(0, 1, 0, 0));
var v1 = new Vertex3D(p1.transform(camera.world2View).transform(camera.view2Clipping).perspectiveDivide().transform(screen.ndc2screen), new Vector4(255, 0, 0));
var v2 = new Vertex3D(p2.transform(camera.world2View).transform(camera.view2Clipping).perspectiveDivide().transform(screen.ndc2screen), new Vector4(0, 255, 0));
var v3 = new Vertex3D(p3.transform(camera.world2View).transform(camera.view2Clipping).perspectiveDivide().transform(screen.ndc2screen), new Vector4(0, 0, 255));
screen.rasterizer.rasterizeTriangle(v1, v2, v3);
}
function testPerspective2() {
var screen = new Screen3D(1024 / 2, 768 / 2);
var camera = new Camera3D(screen, 90 * 3.14 / 180, 5 / 4, 10, 1000);
console.log(camera.view2Clipping);
var p2 = new Vector4(0, 0, -500);
var p2p = new Vector4(0, 0, -500);
p2 = new Vector4(0, 0, -0.01);
p2p = p2.transform(camera.view2Clipping);
console.log("========");
console.log(p2);
console.log(p2p);
console.log(p2p.perspectiveDivide());
p2 = new Vector4(0, 0, -10);
p2p = p2.transform(camera.view2Clipping);
console.log("========");
console.log(p2);
console.log(p2p);
console.log(p2p.perspectiveDivide());
p2 = new Vector4(0, 0, -20);
p2p = p2.transform(camera.view2Clipping);
console.log("========");
console.log(p2);
console.log(p2p);
console.log(p2p.perspectiveDivide());
p2 = new Vector4(0, 0, -50);
p2p = p2.transform(camera.view2Clipping);
console.log("========");
console.log(p2);
console.log(p2p);
console.log(p2p.perspectiveDivide());
p2 = new Vector4(0, 0, -300);
p2p = p2.transform(camera.view2Clipping);
console.log("========");
console.log(p2);
console.log(p2p);
console.log(p2p.perspectiveDivide());
p2 = new Vector4(0, 0, -500);
p2p = p2.transform(camera.view2Clipping);
console.log("========");
console.log(p2);
console.log(p2p);
console.log(p2p.perspectiveDivide());
p2 = new Vector4(0, 0, -900);
p2p = p2.transform(camera.view2Clipping);
console.log("========");
console.log(p2);
console.log(p2p);
console.log(p2p.perspectiveDivide());
p2 = new Vector4(0, 0, -1000);
p2p = p2.transform(camera.view2Clipping);
console.log("========");
console.log(p2);
console.log(p2p);
console.log(p2p.perspectiveDivide());
}
function main() {
var screen = new Screen3D(1024, 768);
var camera = new Camera3D(screen, 90 * 3.14 / 180, 5 / 4, 0.001, 1000);
camera.lookAt(new Vector4(0, 0, 50), new Vector4(0, 0, 0), new Vector4(0, 1, 0, 0));
var scene = new Scene3D(camera, screen);
var cube = new Cube3D();
cube.matrix.scale(20, 10, 10);
scene.addObject(cube);
cube = new Cube3D();
cube.renderMode = RenderMode.Color;
cube.matrix.scale(10, 10, 10);
cube.matrix.translate(50, 0, 0);
scene.addObject(cube);
cube = new Cube3D();
cube.renderMode = RenderMode.Wireframe;
cube.matrix.scale(10, 10, 10);
cube.matrix.translate(-50, 0, 0);
scene.addObject(cube);
scene.start();
}
//testRasterize();
// testScreen3d();
//testLookAt();
//testPerspective();
//testPerspective2();
main();
//# sourceMappingURL=test.js.map