michael@0: // 3D Cube Rotation michael@0: // http://www.speich.net/computer/moztesting/3d.htm michael@0: // Created by Simon Speich michael@0: michael@0: var Q = new Array(); michael@0: var MTrans = new Array(); // transformation matrix michael@0: var MQube = new Array(); // position information of qube michael@0: var I = new Array(); // entity matrix michael@0: var Origin = new Object(); michael@0: var Testing = new Object(); michael@0: var LoopTimer; michael@0: michael@0: var DisplArea = new Object(); michael@0: DisplArea.Width = 300; michael@0: DisplArea.Height = 300; michael@0: michael@0: function DrawLine(From, To) { michael@0: var x1 = From.V[0]; michael@0: var x2 = To.V[0]; michael@0: var y1 = From.V[1]; michael@0: var y2 = To.V[1]; michael@0: var dx = Math.abs(x2 - x1); michael@0: var dy = Math.abs(y2 - y1); michael@0: var x = x1; michael@0: var y = y1; michael@0: var IncX1, IncY1; michael@0: var IncX2, IncY2; michael@0: var Den; michael@0: var Num; michael@0: var NumAdd; michael@0: var NumPix; michael@0: michael@0: if (x2 >= x1) { IncX1 = 1; IncX2 = 1; } michael@0: else { IncX1 = -1; IncX2 = -1; } michael@0: if (y2 >= y1) { IncY1 = 1; IncY2 = 1; } michael@0: else { IncY1 = -1; IncY2 = -1; } michael@0: if (dx >= dy) { michael@0: IncX1 = 0; michael@0: IncY2 = 0; michael@0: Den = dx; michael@0: Num = dx / 2; michael@0: NumAdd = dy; michael@0: NumPix = dx; michael@0: } michael@0: else { michael@0: IncX2 = 0; michael@0: IncY1 = 0; michael@0: Den = dy; michael@0: Num = dy / 2; michael@0: NumAdd = dx; michael@0: NumPix = dy; michael@0: } michael@0: michael@0: NumPix = Math.round(Q.LastPx + NumPix); michael@0: michael@0: var i = Q.LastPx; michael@0: for (; i < NumPix; i++) { michael@0: Num += NumAdd; michael@0: if (Num >= Den) { michael@0: Num -= Den; michael@0: x += IncX1; michael@0: y += IncY1; michael@0: } michael@0: x += IncX2; michael@0: y += IncY2; michael@0: } michael@0: Q.LastPx = NumPix; michael@0: } michael@0: michael@0: function CalcCross(V0, V1) { michael@0: var Cross = new Array(); michael@0: Cross[0] = V0[1]*V1[2] - V0[2]*V1[1]; michael@0: Cross[1] = V0[2]*V1[0] - V0[0]*V1[2]; michael@0: Cross[2] = V0[0]*V1[1] - V0[1]*V1[0]; michael@0: return Cross; michael@0: } michael@0: michael@0: function CalcNormal(V0, V1, V2) { michael@0: var A = new Array(); var B = new Array(); michael@0: for (var i = 0; i < 3; i++) { michael@0: A[i] = V0[i] - V1[i]; michael@0: B[i] = V2[i] - V1[i]; michael@0: } michael@0: A = CalcCross(A, B); michael@0: var Length = Math.sqrt(A[0]*A[0] + A[1]*A[1] + A[2]*A[2]); michael@0: for (var i = 0; i < 3; i++) A[i] = A[i] / Length; michael@0: A[3] = 1; michael@0: return A; michael@0: } michael@0: michael@0: function CreateP(X,Y,Z) { michael@0: this.V = [X,Y,Z,1]; michael@0: } michael@0: michael@0: // multiplies two matrices michael@0: function MMulti(M1, M2) { michael@0: var M = [[],[],[],[]]; michael@0: var i = 0; michael@0: var j = 0; michael@0: for (; i < 4; i++) { michael@0: j = 0; michael@0: 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]; michael@0: } michael@0: return M; michael@0: } michael@0: michael@0: //multiplies matrix with vector michael@0: function VMulti(M, V) { michael@0: var Vect = new Array(); michael@0: var i = 0; michael@0: 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]; michael@0: return Vect; michael@0: } michael@0: michael@0: function VMulti2(M, V) { michael@0: var Vect = new Array(); michael@0: var i = 0; michael@0: for (;i < 3; i++) Vect[i] = M[i][0] * V[0] + M[i][1] * V[1] + M[i][2] * V[2]; michael@0: return Vect; michael@0: } michael@0: michael@0: // add to matrices michael@0: function MAdd(M1, M2) { michael@0: var M = [[],[],[],[]]; michael@0: var i = 0; michael@0: var j = 0; michael@0: for (; i < 4; i++) { michael@0: j = 0; michael@0: for (; j < 4; j++) M[i][j] = M1[i][j] + M2[i][j]; michael@0: } michael@0: return M; michael@0: } michael@0: michael@0: function Translate(M, Dx, Dy, Dz) { michael@0: var T = [ michael@0: [1,0,0,Dx], michael@0: [0,1,0,Dy], michael@0: [0,0,1,Dz], michael@0: [0,0,0,1] michael@0: ]; michael@0: return MMulti(T, M); michael@0: } michael@0: michael@0: function RotateX(M, Phi) { michael@0: var a = Phi; michael@0: a *= Math.PI / 180; michael@0: var Cos = Math.cos(a); michael@0: var Sin = Math.sin(a); michael@0: var R = [ michael@0: [1,0,0,0], michael@0: [0,Cos,-Sin,0], michael@0: [0,Sin,Cos,0], michael@0: [0,0,0,1] michael@0: ]; michael@0: return MMulti(R, M); michael@0: } michael@0: michael@0: function RotateY(M, Phi) { michael@0: var a = Phi; michael@0: a *= Math.PI / 180; michael@0: var Cos = Math.cos(a); michael@0: var Sin = Math.sin(a); michael@0: var R = [ michael@0: [Cos,0,Sin,0], michael@0: [0,1,0,0], michael@0: [-Sin,0,Cos,0], michael@0: [0,0,0,1] michael@0: ]; michael@0: return MMulti(R, M); michael@0: } michael@0: michael@0: function RotateZ(M, Phi) { michael@0: var a = Phi; michael@0: a *= Math.PI / 180; michael@0: var Cos = Math.cos(a); michael@0: var Sin = Math.sin(a); michael@0: var R = [ michael@0: [Cos,-Sin,0,0], michael@0: [Sin,Cos,0,0], michael@0: [0,0,1,0], michael@0: [0,0,0,1] michael@0: ]; michael@0: return MMulti(R, M); michael@0: } michael@0: michael@0: function DrawQube() { michael@0: // calc current normals michael@0: var CurN = new Array(); michael@0: var i = 5; michael@0: Q.LastPx = 0; michael@0: for (; i > -1; i--) CurN[i] = VMulti2(MQube, Q.Normal[i]); michael@0: if (CurN[0][2] < 0) { michael@0: if (!Q.Line[0]) { DrawLine(Q[0], Q[1]); Q.Line[0] = true; }; michael@0: if (!Q.Line[1]) { DrawLine(Q[1], Q[2]); Q.Line[1] = true; }; michael@0: if (!Q.Line[2]) { DrawLine(Q[2], Q[3]); Q.Line[2] = true; }; michael@0: if (!Q.Line[3]) { DrawLine(Q[3], Q[0]); Q.Line[3] = true; }; michael@0: } michael@0: if (CurN[1][2] < 0) { michael@0: if (!Q.Line[2]) { DrawLine(Q[3], Q[2]); Q.Line[2] = true; }; michael@0: if (!Q.Line[9]) { DrawLine(Q[2], Q[6]); Q.Line[9] = true; }; michael@0: if (!Q.Line[6]) { DrawLine(Q[6], Q[7]); Q.Line[6] = true; }; michael@0: if (!Q.Line[10]) { DrawLine(Q[7], Q[3]); Q.Line[10] = true; }; michael@0: } michael@0: if (CurN[2][2] < 0) { michael@0: if (!Q.Line[4]) { DrawLine(Q[4], Q[5]); Q.Line[4] = true; }; michael@0: if (!Q.Line[5]) { DrawLine(Q[5], Q[6]); Q.Line[5] = true; }; michael@0: if (!Q.Line[6]) { DrawLine(Q[6], Q[7]); Q.Line[6] = true; }; michael@0: if (!Q.Line[7]) { DrawLine(Q[7], Q[4]); Q.Line[7] = true; }; michael@0: } michael@0: if (CurN[3][2] < 0) { michael@0: if (!Q.Line[4]) { DrawLine(Q[4], Q[5]); Q.Line[4] = true; }; michael@0: if (!Q.Line[8]) { DrawLine(Q[5], Q[1]); Q.Line[8] = true; }; michael@0: if (!Q.Line[0]) { DrawLine(Q[1], Q[0]); Q.Line[0] = true; }; michael@0: if (!Q.Line[11]) { DrawLine(Q[0], Q[4]); Q.Line[11] = true; }; michael@0: } michael@0: if (CurN[4][2] < 0) { michael@0: if (!Q.Line[11]) { DrawLine(Q[4], Q[0]); Q.Line[11] = true; }; michael@0: if (!Q.Line[3]) { DrawLine(Q[0], Q[3]); Q.Line[3] = true; }; michael@0: if (!Q.Line[10]) { DrawLine(Q[3], Q[7]); Q.Line[10] = true; }; michael@0: if (!Q.Line[7]) { DrawLine(Q[7], Q[4]); Q.Line[7] = true; }; michael@0: } michael@0: if (CurN[5][2] < 0) { michael@0: if (!Q.Line[8]) { DrawLine(Q[1], Q[5]); Q.Line[8] = true; }; michael@0: if (!Q.Line[5]) { DrawLine(Q[5], Q[6]); Q.Line[5] = true; }; michael@0: if (!Q.Line[9]) { DrawLine(Q[6], Q[2]); Q.Line[9] = true; }; michael@0: if (!Q.Line[1]) { DrawLine(Q[2], Q[1]); Q.Line[1] = true; }; michael@0: } michael@0: Q.Line = [false,false,false,false,false,false,false,false,false,false,false,false]; michael@0: Q.LastPx = 0; michael@0: } michael@0: michael@0: function Loop() { michael@0: if (Testing.LoopCount > Testing.LoopMax) return; michael@0: var TestingStr = String(Testing.LoopCount); michael@0: while (TestingStr.length < 3) TestingStr = "0" + TestingStr; michael@0: MTrans = Translate(I, -Q[8].V[0], -Q[8].V[1], -Q[8].V[2]); michael@0: MTrans = RotateX(MTrans, 1); michael@0: MTrans = RotateY(MTrans, 3); michael@0: MTrans = RotateZ(MTrans, 5); michael@0: MTrans = Translate(MTrans, Q[8].V[0], Q[8].V[1], Q[8].V[2]); michael@0: MQube = MMulti(MTrans, MQube); michael@0: var i = 8; michael@0: for (; i > -1; i--) { michael@0: Q[i].V = VMulti(MTrans, Q[i].V); michael@0: } michael@0: DrawQube(); michael@0: Testing.LoopCount++; michael@0: Loop(); michael@0: } michael@0: michael@0: function Init(CubeSize) { michael@0: // init/reset vars michael@0: Origin.V = [150,150,20,1]; michael@0: Testing.LoopCount = 0; michael@0: Testing.LoopMax = 50; michael@0: Testing.TimeMax = 0; michael@0: Testing.TimeAvg = 0; michael@0: Testing.TimeMin = 0; michael@0: Testing.TimeTemp = 0; michael@0: Testing.TimeTotal = 0; michael@0: Testing.Init = false; michael@0: michael@0: // transformation matrix michael@0: MTrans = [ michael@0: [1,0,0,0], michael@0: [0,1,0,0], michael@0: [0,0,1,0], michael@0: [0,0,0,1] michael@0: ]; michael@0: michael@0: // position information of qube michael@0: MQube = [ michael@0: [1,0,0,0], michael@0: [0,1,0,0], michael@0: [0,0,1,0], michael@0: [0,0,0,1] michael@0: ]; michael@0: michael@0: // entity matrix michael@0: I = [ michael@0: [1,0,0,0], michael@0: [0,1,0,0], michael@0: [0,0,1,0], michael@0: [0,0,0,1] michael@0: ]; michael@0: michael@0: // create qube michael@0: Q[0] = new CreateP(-CubeSize,-CubeSize, CubeSize); michael@0: Q[1] = new CreateP(-CubeSize, CubeSize, CubeSize); michael@0: Q[2] = new CreateP( CubeSize, CubeSize, CubeSize); michael@0: Q[3] = new CreateP( CubeSize,-CubeSize, CubeSize); michael@0: Q[4] = new CreateP(-CubeSize,-CubeSize,-CubeSize); michael@0: Q[5] = new CreateP(-CubeSize, CubeSize,-CubeSize); michael@0: Q[6] = new CreateP( CubeSize, CubeSize,-CubeSize); michael@0: Q[7] = new CreateP( CubeSize,-CubeSize,-CubeSize); michael@0: michael@0: // center of gravity michael@0: Q[8] = new CreateP(0, 0, 0); michael@0: michael@0: // anti-clockwise edge check michael@0: Q.Edge = [[0,1,2],[3,2,6],[7,6,5],[4,5,1],[4,0,3],[1,5,6]]; michael@0: michael@0: // calculate squad normals michael@0: Q.Normal = new Array(); michael@0: 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); michael@0: michael@0: // line drawn ? michael@0: Q.Line = [false,false,false,false,false,false,false,false,false,false,false,false]; michael@0: michael@0: // create line pixels michael@0: Q.NumPx = 9 * 2 * CubeSize; michael@0: for (var i = 0; i < Q.NumPx; i++) CreateP(0,0,0); michael@0: michael@0: MTrans = Translate(MTrans, Origin.V[0], Origin.V[1], Origin.V[2]); michael@0: MQube = MMulti(MTrans, MQube); michael@0: michael@0: var i = 0; michael@0: for (; i < 9; i++) { michael@0: Q[i].V = VMulti(MTrans, Q[i].V); michael@0: } michael@0: DrawQube(); michael@0: Testing.Init = true; michael@0: Loop(); michael@0: } michael@0: michael@0: for ( var i = 20; i <= 160; i *= 2 ) { michael@0: Init(i); michael@0: } michael@0: michael@0: var actual = ''; michael@0: for (var i = 0; i < Q.length; ++i) { michael@0: actual += Q[i].V + ';'; michael@0: } michael@0: var expected = "-116.618229186398,212.51135212951073,62.5094191967962,1;127.83701023614447,417.11611179082263,90.41153816299942,1;293.9570894432935,196.58093046570656,252.17789153139591,1;49.501850020750915,-8.02382919560505,224.275772565193,1;6.042910556709444,103.41906953429206,-212.1778915313964,1;250.49814997925202,308.02382919560387,-184.27577256519325,1;416.61822918640064,87.48864787048812,-22.509419196796493,1;172.1629897638581,-117.1161117908236,-50.41153816299975,1;150.0000000000007,149.99999999999952,20,1;"; michael@0: assertEq(actual, expected); michael@0: michael@0: Q = null; michael@0: MTrans = null; michael@0: MQube = null; michael@0: I = null; michael@0: Origin = null; michael@0: Testing = null; michael@0: LoopTime = null; michael@0: DisplArea = null; michael@0: