3 <script src=
"../htmlrunner.js"></script>
6 // http://www.speich.net/computer/moztesting/3d.htm
7 // Created by Simon Speich
10 var MTrans
= new Array(); // transformation matrix
11 var MQube
= new Array(); // position information of qube
12 var I
= new Array(); // entity matrix
13 var Origin
= new Object();
14 var Testing
= new Object();
17 var DisplArea
= new Object();
18 DisplArea
.Width
= 300;
19 DisplArea
.Height
= 300;
21 function DrawLine(From
, To
) {
26 var dx
= Math
.abs(x2
- x1
);
27 var dy
= Math
.abs(y2
- y1
);
37 if (x2
>= x1
) { IncX1
= 1; IncX2
= 1; }
38 else { IncX1
= -1; IncX2
= -1; }
39 if (y2
>= y1
) { IncY1
= 1; IncY2
= 1; }
40 else { IncY1
= -1; IncY2
= -1; }
58 NumPix
= Math
.round(Q
.LastPx
+ NumPix
);
61 for (; i
< NumPix
; i
++) {
74 function CalcCross(V0
, V1
) {
75 var Cross
= new Array();
76 Cross
[0] = V0
[1]*V1
[2] - V0
[2]*V1
[1];
77 Cross
[1] = V0
[2]*V1
[0] - V0
[0]*V1
[2];
78 Cross
[2] = V0
[0]*V1
[1] - V0
[1]*V1
[0];
82 function CalcNormal(V0
, V1
, V2
) {
83 var A
= new Array(); var B
= new Array();
84 for (var i
= 0; i
< 3; i
++) {
89 var Length
= Math
.sqrt(A
[0]*A
[0] + A
[1]*A
[1] + A
[2]*A
[2]);
90 for (var i
= 0; i
< 3; i
++) A
[i
] = A
[i
] / Length
;
95 function CreateP(X
,Y
,Z
) {
99 // mulitplies two matrices
100 function MMulti(M1
, M2
) {
101 var M
= [[],[],[],[]];
106 for (; j
< 4; j
++) M
[i
][j
] = M1
[i
][0] * M2
[0][j
] + M1
[i
][1] * M2
[1][j
] + M1
[i
][2] * M2
[2][j
] + M1
[i
][3] * M2
[3][j
];
111 //multiplies matrix with vector
112 function VMulti(M
, V
) {
113 var Vect
= new Array();
115 for (;i
< 4; i
++) Vect
[i
] = M
[i
][0] * V
[0] + M
[i
][1] * V
[1] + M
[i
][2] * V
[2] + M
[i
][3] * V
[3];
119 function VMulti2(M
, V
) {
120 var Vect
= new Array();
122 for (;i
< 3; i
++) Vect
[i
] = M
[i
][0] * V
[0] + M
[i
][1] * V
[1] + M
[i
][2] * V
[2];
127 function MAdd(M1
, M2
) {
128 var M
= [[],[],[],[]];
133 for (; j
< 4; j
++) M
[i
][j
] = M1
[i
][j
] + M2
[i
][j
];
138 function Translate(M
, Dx
, Dy
, Dz
) {
148 function RotateX(M
, Phi
) {
151 var Cos
= Math
.cos(a
);
152 var Sin
= Math
.sin(a
);
162 function RotateY(M
, Phi
) {
165 var Cos
= Math
.cos(a
);
166 var Sin
= Math
.sin(a
);
176 function RotateZ(M
, Phi
) {
179 var Cos
= Math
.cos(a
);
180 var Sin
= Math
.sin(a
);
190 function DrawQube() {
191 // calc current normals
192 var CurN
= new Array();
195 for (; i
> -1; i
--) CurN
[i
] = VMulti2(MQube
, Q
.Normal
[i
]);
196 if (CurN
[0][2] < 0) {
197 if (!Q
.Line
[0]) { DrawLine(Q
[0], Q
[1]); Q
.Line
[0] = true; };
198 if (!Q
.Line
[1]) { DrawLine(Q
[1], Q
[2]); Q
.Line
[1] = true; };
199 if (!Q
.Line
[2]) { DrawLine(Q
[2], Q
[3]); Q
.Line
[2] = true; };
200 if (!Q
.Line
[3]) { DrawLine(Q
[3], Q
[0]); Q
.Line
[3] = true; };
202 if (CurN
[1][2] < 0) {
203 if (!Q
.Line
[2]) { DrawLine(Q
[3], Q
[2]); Q
.Line
[2] = true; };
204 if (!Q
.Line
[9]) { DrawLine(Q
[2], Q
[6]); Q
.Line
[9] = true; };
205 if (!Q
.Line
[6]) { DrawLine(Q
[6], Q
[7]); Q
.Line
[6] = true; };
206 if (!Q
.Line
[10]) { DrawLine(Q
[7], Q
[3]); Q
.Line
[10] = true; };
208 if (CurN
[2][2] < 0) {
209 if (!Q
.Line
[4]) { DrawLine(Q
[4], Q
[5]); Q
.Line
[4] = true; };
210 if (!Q
.Line
[5]) { DrawLine(Q
[5], Q
[6]); Q
.Line
[5] = true; };
211 if (!Q
.Line
[6]) { DrawLine(Q
[6], Q
[7]); Q
.Line
[6] = true; };
212 if (!Q
.Line
[7]) { DrawLine(Q
[7], Q
[4]); Q
.Line
[7] = true; };
214 if (CurN
[3][2] < 0) {
215 if (!Q
.Line
[4]) { DrawLine(Q
[4], Q
[5]); Q
.Line
[4] = true; };
216 if (!Q
.Line
[8]) { DrawLine(Q
[5], Q
[1]); Q
.Line
[8] = true; };
217 if (!Q
.Line
[0]) { DrawLine(Q
[1], Q
[0]); Q
.Line
[0] = true; };
218 if (!Q
.Line
[11]) { DrawLine(Q
[0], Q
[4]); Q
.Line
[11] = true; };
220 if (CurN
[4][2] < 0) {
221 if (!Q
.Line
[11]) { DrawLine(Q
[4], Q
[0]); Q
.Line
[11] = true; };
222 if (!Q
.Line
[3]) { DrawLine(Q
[0], Q
[3]); Q
.Line
[3] = true; };
223 if (!Q
.Line
[10]) { DrawLine(Q
[3], Q
[7]); Q
.Line
[10] = true; };
224 if (!Q
.Line
[7]) { DrawLine(Q
[7], Q
[4]); Q
.Line
[7] = true; };
226 if (CurN
[5][2] < 0) {
227 if (!Q
.Line
[8]) { DrawLine(Q
[1], Q
[5]); Q
.Line
[8] = true; };
228 if (!Q
.Line
[5]) { DrawLine(Q
[5], Q
[6]); Q
.Line
[5] = true; };
229 if (!Q
.Line
[9]) { DrawLine(Q
[6], Q
[2]); Q
.Line
[9] = true; };
230 if (!Q
.Line
[1]) { DrawLine(Q
[2], Q
[1]); Q
.Line
[1] = true; };
232 Q
.Line
= [false,false,false,false,false,false,false,false,false,false,false,false];
237 if (Testing
.LoopCount
> Testing
.LoopMax
) return;
238 var TestingStr
= String(Testing
.LoopCount
);
239 while (TestingStr
.length
< 3) TestingStr
= "0" + TestingStr
;
240 MTrans
= Translate(I
, -Q
[8].V
[0], -Q
[8].V
[1], -Q
[8].V
[2]);
241 MTrans
= RotateX(MTrans
, 1);
242 MTrans
= RotateY(MTrans
, 3);
243 MTrans
= RotateZ(MTrans
, 5);
244 MTrans
= Translate(MTrans
, Q
[8].V
[0], Q
[8].V
[1], Q
[8].V
[2]);
245 MQube
= MMulti(MTrans
, MQube
);
247 for (; i
> -1; i
--) {
248 Q
[i
].V
= VMulti(MTrans
, Q
[i
].V
);
255 function Init(CubeSize
) {
257 Origin
.V
= [150,150,20,1];
258 Testing
.LoopCount
= 0;
259 Testing
.LoopMax
= 50;
263 Testing
.TimeTemp
= 0;
264 Testing
.TimeTotal
= 0;
265 Testing
.Init
= false;
267 // transformation matrix
275 // position information of qube
292 Q
[0] = new CreateP(-CubeSize
,-CubeSize
, CubeSize
);
293 Q
[1] = new CreateP(-CubeSize
, CubeSize
, CubeSize
);
294 Q
[2] = new CreateP( CubeSize
, CubeSize
, CubeSize
);
295 Q
[3] = new CreateP( CubeSize
,-CubeSize
, CubeSize
);
296 Q
[4] = new CreateP(-CubeSize
,-CubeSize
,-CubeSize
);
297 Q
[5] = new CreateP(-CubeSize
, CubeSize
,-CubeSize
);
298 Q
[6] = new CreateP( CubeSize
, CubeSize
,-CubeSize
);
299 Q
[7] = new CreateP( CubeSize
,-CubeSize
,-CubeSize
);
302 Q
[8] = new CreateP(0, 0, 0);
304 // anti-clockwise edge check
305 Q
.Edge
= [[0,1,2],[3,2,6],[7,6,5],[4,5,1],[4,0,3],[1,5,6]];
307 // calculate squad normals
308 Q
.Normal
= new Array();
309 for (var i
= 0; i
< Q
.Edge
.length
; i
++) Q
.Normal
[i
] = CalcNormal(Q
[Q
.Edge
[i
][0]].V
, Q
[Q
.Edge
[i
][1]].V
, Q
[Q
.Edge
[i
][2]].V
);
312 Q
.Line
= [false,false,false,false,false,false,false,false,false,false,false,false];
314 // create line pixels
315 Q
.NumPx
= 9 * 2 * CubeSize
;
316 for (var i
= 0; i
< Q
.NumPx
; i
++) new CreateP(0,0,0);
318 MTrans
= Translate(MTrans
, Origin
.V
[0], Origin
.V
[1], Origin
.V
[2]);
319 MQube
= MMulti(MTrans
, MQube
);
323 Q
[i
].V
= VMulti(MTrans
, Q
[i
].V
);
330 window
.onload = function(){ startTest("dromaeo-3d-cube", '979cd0f1');
332 test("Rotate 3D Cube", function(){