forked from kalinalinkalina/OpenGL.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
glCoordinateTransformation.js
142 lines (115 loc) · 3.59 KB
/
glCoordinateTransformation.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
132
133
134
135
136
137
138
139
140
141
142
var GL_MODELVIEW = 0; //stack is 32
var GL_PROJECTION = 1; //stack is 2
var GL_TEXTURE = 2; //stack is 2
var GL_COLOR = 4; //stack is 2
MatrixStackObject=function(){
this.matrixStackIndex = [0,0,0,0];
this.matrixStacks = [new Array(32), new Array(2), new Array(2), new Array(2)];
this.matrixMode = 0;
//init
var IdentityMat4 = new Mat4();
IdentityMat4.loadIdentity();
console.log(IdentityMat4);
for(var i = 0;i<4;i++)
this.matrixStacks[i][0]=(IdentityMat4.clone());
this.getActiveStack = function(){
return this.matrixStacks[this.matrixMode];
}
this.getActiveIndex = function(){
return this.matrixStackIndex[this.matrixMode];
}
this.increaseActiveIndex = function(){
this.matrixStackIndex[this.matrixMode] += 1;
}
this.decreaseActiveIndex = function(){
//Don't decrease if it's already at zero
if (this.matrixStackIndex[this.matrixMode] == 0) return;
this.matrixStackIndex[this.matrixMode] -= 1;
}
this.setActiveMatrix = function(matrix){
this.getActiveStack()[this.getActiveIndex()] = matrix;
}
this.getActiveMatrix = function(){
return this.getActiveStack()[this.getActiveIndex()];
}
this.getModelView = function(){
return this.matrixStacks[0][this.matrixStackIndex[0]];
}
this.getProjection = function(){
return this.matrixStacks[1][this.matrixStackIndex[1]];
}
this.getTexture = function(){
return this.matrixStacks[2][this.matrixStackIndex[2]];
}
this.getColor = function(){
return this.matrixStacks[3][this.matrixStackIndex[3]];
}
}
matrixStacks = new MatrixStackObject();
function glMatrixMode(mode){
matrixStacks.matrixMode = mode;
}
function glPushMatrix(){
var copyMatrix = matrixStacks.getActiveMatrix();
matrixStacks.increaseActiveIndex();
matrixStacks.setActiveMatrix(copyMatrix);
//matrixStackIndex[matrixMode]++;
//copyMatrix( matrixModeStack[matrixMode][MatrixStackIndex[matrixMode]-1],
// matrixModeStack[matrixMode][MatrixStackIndex[matrixMode]] );
}
function glPopMatrix(){
matrixStacks.decreaseActiveIndex();
//matrixStackIndex[matrixMode]--;
}
function glTranslatef(x,y,z){
var matrix =new Mat4();
matrix.getFromArray([1,0,0,x, 0,1,0,y, 0,0,1,z, 0,0,0,1]);
console.log(matrixStacks.getActiveMatrix());
glMultMatrix(matrix);
//matrixModeStack[matrixMode][matrixStackIndex[matrixMode]] = matrixProduct(matrixModeStack[matrixMode][matrixStackIndex[matrixMode]], matrix);
}
function glRotatef(angle,x,y,z){
var axis = new Vec3();
axis.getFromArray([x,y,z]);
axis.normalize();
var rtMatrix = new Mat4();
var degree = angle*Math.PI/180;
var c = Math.cos(degree);
var ac = 1 - c;
var s = Math.sin(degree);
rtMatrix.m00 = axis.x * axis.x * ac + c;
rtMatrix.m01 = axis.x * axis.y * ac - axis.z * s;
rtMatrix.m02 = axis.x * axis.z * ac + axis.y * s;
rtMatrix.m03 = 0;
rtMatrix.m10 = axis.y * axis.x * ac + axis.z * s;
rtMatrix.m11 = axis.y * axis.y * ac + c;
rtMatrix.m12 = axis.y * axis.z * ac - axis.x * s;
rtMatrix.m13 = 0;
rtMatrix.m20 = axis.z * axis.x * ac - axis.y * s;
rtMatrix.m21 = axis.z * axis.y * ac + axis.x * s;
rtMatrix.m22 = axis.z * axis.z * ac + c;
rtMatrix.m23 = 0;
rtMatrix.m30 = 0;
rtMatrix.m31 = 0;
rtMatrix.m32 = 0;
rtMatrix.m33 = 1;
glMultMatrix(rtMatrix);
}
function glScalef(x,y,z){
var scaleMatrix = new Mat4();
scaleMatrix.getFromArray([x,0,0,0,
0,y,0,0,
0,0,z,0,
0,0,0,1]);
glMultMatrix(scaleMatrix);
}
function glLoadIdentity(){
matrixStacks.getActiveMatrix().loadIdentity();
}
function glLoadMatrix(m){
matrixStacks.setActiveMatrix(m);
}
function glMultMatrix(m){
var newMatrix = mat4Product(matrixStacks.getActiveMatrix(),m);
matrixStacks.setActiveMatrix(newMatrix);
}