4 Copyright (C) 2007 Apple Inc. All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
15 THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 <title>SunSpider
3d-cube
</title>
29 <link rel=
"stylesheet" href=
"sunspider.css"></link>
40 // http://www.speich.net/computer/moztesting/3d.htm
41 // Created by Simon Speich
44 var MTrans
= new Array(); // transformation matrix
45 var MQube
= new Array(); // position information of qube
46 var I
= new Array(); // entity matrix
47 var Origin
= new Object();
48 var Testing
= new Object();
51 var DisplArea
= new Object();
52 DisplArea
.Width
= 300;
53 DisplArea
.Height
= 300;
55 function DrawLine(From
, To
) {
60 var dx
= Math
.abs(x2
- x1
);
61 var dy
= Math
.abs(y2
- y1
);
71 if (x2
>= x1
) { IncX1
= 1; IncX2
= 1; }
72 else { IncX1
= -1; IncX2
= -1; }
73 if (y2
>= y1
) { IncY1
= 1; IncY2
= 1; }
74 else { IncY1
= -1; IncY2
= -1; }
92 NumPix
= Math
.round(Q
.LastPx
+ NumPix
);
95 for (; i
< NumPix
; i
++) {
108 function CalcCross(V0
, V1
) {
109 var Cross
= new Array();
110 Cross
[0] = V0
[1]*V1
[2] - V0
[2]*V1
[1];
111 Cross
[1] = V0
[2]*V1
[0] - V0
[0]*V1
[2];
112 Cross
[2] = V0
[0]*V1
[1] - V0
[1]*V1
[0];
116 function CalcNormal(V0
, V1
, V2
) {
117 var A
= new Array(); var B
= new Array();
118 for (var i
= 0; i
< 3; i
++) {
119 A
[i
] = V0
[i
] - V1
[i
];
120 B
[i
] = V2
[i
] - V1
[i
];
123 var Length
= Math
.sqrt(A
[0]*A
[0] + A
[1]*A
[1] + A
[2]*A
[2]);
124 for (var i
= 0; i
< 3; i
++) A
[i
] = A
[i
] / Length
;
129 function CreateP(X
,Y
,Z
) {
133 // multiplies two matrices
134 function MMulti(M1
, M2
) {
135 var M
= [[],[],[],[]];
140 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
];
145 //multiplies matrix with vector
146 function VMulti(M
, V
) {
147 var Vect
= new Array();
149 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];
153 function VMulti2(M
, V
) {
154 var Vect
= new Array();
156 for (;i
< 3; i
++) Vect
[i
] = M
[i
][0] * V
[0] + M
[i
][1] * V
[1] + M
[i
][2] * V
[2];
161 function MAdd(M1
, M2
) {
162 var M
= [[],[],[],[]];
167 for (; j
< 4; j
++) M
[i
][j
] = M1
[i
][j
] + M2
[i
][j
];
172 function Translate(M
, Dx
, Dy
, Dz
) {
182 function RotateX(M
, Phi
) {
185 var Cos
= Math
.cos(a
);
186 var Sin
= Math
.sin(a
);
196 function RotateY(M
, Phi
) {
199 var Cos
= Math
.cos(a
);
200 var Sin
= Math
.sin(a
);
210 function RotateZ(M
, Phi
) {
213 var Cos
= Math
.cos(a
);
214 var Sin
= Math
.sin(a
);
224 function DrawQube() {
225 // calc current normals
226 var CurN
= new Array();
229 for (; i
> -1; i
--) CurN
[i
] = VMulti2(MQube
, Q
.Normal
[i
]);
230 if (CurN
[0][2] < 0) {
231 if (!Q
.Line
[0]) { DrawLine(Q
[0], Q
[1]); Q
.Line
[0] = true; };
232 if (!Q
.Line
[1]) { DrawLine(Q
[1], Q
[2]); Q
.Line
[1] = true; };
233 if (!Q
.Line
[2]) { DrawLine(Q
[2], Q
[3]); Q
.Line
[2] = true; };
234 if (!Q
.Line
[3]) { DrawLine(Q
[3], Q
[0]); Q
.Line
[3] = true; };
236 if (CurN
[1][2] < 0) {
237 if (!Q
.Line
[2]) { DrawLine(Q
[3], Q
[2]); Q
.Line
[2] = true; };
238 if (!Q
.Line
[9]) { DrawLine(Q
[2], Q
[6]); Q
.Line
[9] = true; };
239 if (!Q
.Line
[6]) { DrawLine(Q
[6], Q
[7]); Q
.Line
[6] = true; };
240 if (!Q
.Line
[10]) { DrawLine(Q
[7], Q
[3]); Q
.Line
[10] = true; };
242 if (CurN
[2][2] < 0) {
243 if (!Q
.Line
[4]) { DrawLine(Q
[4], Q
[5]); Q
.Line
[4] = true; };
244 if (!Q
.Line
[5]) { DrawLine(Q
[5], Q
[6]); Q
.Line
[5] = true; };
245 if (!Q
.Line
[6]) { DrawLine(Q
[6], Q
[7]); Q
.Line
[6] = true; };
246 if (!Q
.Line
[7]) { DrawLine(Q
[7], Q
[4]); Q
.Line
[7] = true; };
248 if (CurN
[3][2] < 0) {
249 if (!Q
.Line
[4]) { DrawLine(Q
[4], Q
[5]); Q
.Line
[4] = true; };
250 if (!Q
.Line
[8]) { DrawLine(Q
[5], Q
[1]); Q
.Line
[8] = true; };
251 if (!Q
.Line
[0]) { DrawLine(Q
[1], Q
[0]); Q
.Line
[0] = true; };
252 if (!Q
.Line
[11]) { DrawLine(Q
[0], Q
[4]); Q
.Line
[11] = true; };
254 if (CurN
[4][2] < 0) {
255 if (!Q
.Line
[11]) { DrawLine(Q
[4], Q
[0]); Q
.Line
[11] = true; };
256 if (!Q
.Line
[3]) { DrawLine(Q
[0], Q
[3]); Q
.Line
[3] = true; };
257 if (!Q
.Line
[10]) { DrawLine(Q
[3], Q
[7]); Q
.Line
[10] = true; };
258 if (!Q
.Line
[7]) { DrawLine(Q
[7], Q
[4]); Q
.Line
[7] = true; };
260 if (CurN
[5][2] < 0) {
261 if (!Q
.Line
[8]) { DrawLine(Q
[1], Q
[5]); Q
.Line
[8] = true; };
262 if (!Q
.Line
[5]) { DrawLine(Q
[5], Q
[6]); Q
.Line
[5] = true; };
263 if (!Q
.Line
[9]) { DrawLine(Q
[6], Q
[2]); Q
.Line
[9] = true; };
264 if (!Q
.Line
[1]) { DrawLine(Q
[2], Q
[1]); Q
.Line
[1] = true; };
266 Q
.Line
= [false,false,false,false,false,false,false,false,false,false,false,false];
271 if (Testing
.LoopCount
> Testing
.LoopMax
) return;
272 var TestingStr
= String(Testing
.LoopCount
);
273 while (TestingStr
.length
< 3) TestingStr
= "0" + TestingStr
;
274 MTrans
= Translate(I
, -Q
[8].V
[0], -Q
[8].V
[1], -Q
[8].V
[2]);
275 MTrans
= RotateX(MTrans
, 1);
276 MTrans
= RotateY(MTrans
, 3);
277 MTrans
= RotateZ(MTrans
, 5);
278 MTrans
= Translate(MTrans
, Q
[8].V
[0], Q
[8].V
[1], Q
[8].V
[2]);
279 MQube
= MMulti(MTrans
, MQube
);
281 for (; i
> -1; i
--) {
282 Q
[i
].V
= VMulti(MTrans
, Q
[i
].V
);
289 function Init(CubeSize
) {
291 Origin
.V
= [150,150,20,1];
292 Testing
.LoopCount
= 0;
293 Testing
.LoopMax
= 50;
297 Testing
.TimeTemp
= 0;
298 Testing
.TimeTotal
= 0;
299 Testing
.Init
= false;
301 // transformation matrix
309 // position information of qube
326 Q
[0] = new CreateP(-CubeSize
,-CubeSize
, CubeSize
);
327 Q
[1] = new CreateP(-CubeSize
, CubeSize
, CubeSize
);
328 Q
[2] = new CreateP( CubeSize
, CubeSize
, CubeSize
);
329 Q
[3] = new CreateP( CubeSize
,-CubeSize
, CubeSize
);
330 Q
[4] = new CreateP(-CubeSize
,-CubeSize
,-CubeSize
);
331 Q
[5] = new CreateP(-CubeSize
, CubeSize
,-CubeSize
);
332 Q
[6] = new CreateP( CubeSize
, CubeSize
,-CubeSize
);
333 Q
[7] = new CreateP( CubeSize
,-CubeSize
,-CubeSize
);
336 Q
[8] = new CreateP(0, 0, 0);
338 // anti-clockwise edge check
339 Q
.Edge
= [[0,1,2],[3,2,6],[7,6,5],[4,5,1],[4,0,3],[1,5,6]];
341 // calculate squad normals
342 Q
.Normal
= new Array();
343 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
);
346 Q
.Line
= [false,false,false,false,false,false,false,false,false,false,false,false];
348 // create line pixels
349 Q
.NumPx
= 9 * 2 * CubeSize
;
350 for (var i
= 0; i
< Q
.NumPx
; i
++) CreateP(0,0,0);
352 MTrans
= Translate(MTrans
, Origin
.V
[0], Origin
.V
[1], Origin
.V
[2]);
353 MQube
= MMulti(MTrans
, MQube
);
357 Q
[i
].V
= VMulti(MTrans
, Q
[i
].V
);
364 for ( var i
= 20; i
<= 160; i
*= 2 ) {