-
Notifications
You must be signed in to change notification settings - Fork 4
/
transform.js
89 lines (66 loc) · 2.21 KB
/
transform.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
function MatrixStack() {
this.stack = [];
// 20 deep stack outght to be enough for anyone.
for(var i = 0; i < 20; ++i)
this.stack.push(mat4.create());
this.stackTop = 0; // points to the place in the stack when the next matrix should be pushed to.
this.current = mat4.create();
this.loadIdentity();
}
MatrixStack.prototype.pushMatrix = function(m) {
if (this.stackTop >= this.stack.length) // deepen the stack...
this.stack.push(mat4.create());
if (m) {
//this.stack.push(mat4.create(m));
//this.current = mat4.create(m);
mat4.set(m, this.stack[this.stackTop]);
mat4.set(m, this.current);
} else {
//this.stack.push(mat4.create(this.current));
mat4.set(this.current, this.stack[this.stackTop]);
}
++this.stackTop;
}
MatrixStack.prototype.popMatrix = function() {
if (this.stackTop === 0) {
throw "Invalid popMatrix!";
}
--this.stackTop;
mat4.set(this.stack[this.stackTop], this.current);
return this.current;
}
MatrixStack.prototype.loadIdentity = function() {
mat4.identity(this.current);
}
MatrixStack.prototype.load = function(m) {
mat4.set(m, this.current);
}
MatrixStack.prototype.multMatrix = function(m) {
mat4.multiply(this.current, m);
}
// expects an array
MatrixStack.prototype.translate = function(v) {
mat4.translate(this.current, v);
}
MatrixStack.prototype.scale = function(s) {
mat4.scale(this.current, [s,s,s]);
}
// v is Array
// angle in degrees
MatrixStack.prototype.rotate = function(angle, v) {
mat4.rotate(this.current, radFromDeg(angle), v);
}
MatrixStack.prototype.rotateX = function(angle) {
mat4.rotateX(this.current, radFromDeg(angle));
}
MatrixStack.prototype.rotateY = function(angle) {
mat4.rotateY(this.current, radFromDeg(angle));
}
MatrixStack.prototype.rotateZ = function(angle) {
mat4.rotateZ(this.current, radFromDeg(angle));
}
MatrixStack.prototype.perspective = function(fovy, aspect, znear, zfar) {
mat4.perspective(fovy, aspect, znear, zfar, this.current);
}
var mv = new MatrixStack();
var pr = new MatrixStack();