-
Notifications
You must be signed in to change notification settings - Fork 0
/
cubes.html
154 lines (120 loc) · 4.94 KB
/
cubes.html
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
143
144
145
146
147
148
149
150
151
152
153
154
<!DOCTYPE html>
<html>
<head>
<title>Cubes</title>
<meta charset="utf-8">
<style>
* { padding: 0; margin: 0; box-sizing: border-box; }
body { background-color: #000000; }
#renderer {
width: 100%; height: 100%;
position: absolute; top: 0px; bottom: 0px; left: 0px; right: 0px;
border: 5px solid black;
border-radius: 7px;
background-color: #ffffff;
}
</style>
</head>
<body>
<canvas id="renderer"></canvas>
<script src="lib/gl-matrix.js"></script>
<script src="lib/gl-matrix-extension.js"></script>
<script src="lib/dx.js"></script>
<script src="lib/firstPersonContext.js"></script>
<script src="lib/stdShader.js"></script>
<script src="lib/shape.js"></script>
<script>
// init
const canvas = document.getElementById("renderer");
const gl = canvas.getContext("webgl");
const dx = DX(gl);
const ac = AnimationContext(gl, {keyboardView: true, mouseView: true});
stdShader.init(dx);
Shape.init(dx);
// go
ac.translate([0, -500, -1000]);
ac.tilt(9);
ac.updateProjection();
// shape
var s = 100;
var s0 = 0.25, s1 = 0.5, s2 = 1.0, s3 = 1.5;
let y = [0,0], r = [1,0], g = [0,1], b = [1,1];
// let y = [0,0], r = [0,0], g = [0,0], b = [0,0];
const c0 = [y, r, g];
const c0r = [y, g, r];
const c1 = [y, g, b];
const c1r = [y, b, g];
/*const c0 = [b, b, b];
const c0r = [b, b, b];
const c1 = [b, b, b];
const c1r = [b, b, b];*/
var shape0 = Shape.VerTex(
[
[-s, -s, s], [ s, -s, s], [ s, s, s], [-s, s, s],
[-s, -s, -s], [ s, -s, -s], [ s, s, -s], [-s, s, -s],
], [
[[0, 1, 2], c0], [[0, 2, 3], c1], // front
[[4, 6, 5], c0r], [[4, 7, 6], c1r], // back
[[0, 5, 1], c0r], [[0, 4, 5], c1r], // bottom
[[3, 2, 6], c0], [[3, 6, 7], c1], // top
[[0, 7, 4], c0r], [[0, 3, 7], c1r], // left
[[1, 6, 2], c0r], [[1, 5, 6], c1r], // right
],
{texture: dx.Texture(
[
[255, 255, 0, 255], [255, 0, 0, 255], // yellow, red
[ 0, 255, 0, 255], [ 0, 0, 255, 255] // green, blue
],
2, 2
)}
);
// drawing
const deg = Math.PI / 180;
const {viewMatrix, perspectiveMatrix, projectionMatrix} = ac;
const lightMatrix = mat4.create();
mat4.apply(lightMatrix, lightMatrix, mat4.rotateY, -45*deg);
mat4.apply(lightMatrix, lightMatrix, mat4.rotateX, 45*deg);
var objMat = mat4.create();
const pjMat = mat4.create();
const lMat = mat4.create();
function draw() {
mat4.multiply(pjMat, projectionMatrix, objMat);
mat4.multiply(lMat, lightMatrix, mat4.getRotationMatrix(lMat, objMat));
dx.draw(shape0, stdShader, {projectionMatrix: pjMat, lightMatrix: lMat});
}
var cubeRotation = 0.0;
ac.setRender(function (deltaAnimationTime) {
cubeRotation += deltaAnimationTime / 1000; // in seconds
dx.clear();
// origin
mat4.fromScaling(objMat, [0.03, 0.03, 0.03]);
draw();
mat4.fromTranslation(objMat, [0,0,750]); // amount to translate
mat4.rotate(objMat, objMat, cubeRotation, [0, 0, -1]);
mat4.rotate(objMat, objMat, 0.7 * cubeRotation, [0, -1, 0]);
mat4.rotate(objMat, objMat, 0.2 * cubeRotation, [1, 0, 0]);
mat4.scale(objMat, objMat, [s0,s0,s0]);
draw();
mat4.fromTranslation(objMat, [0,0,250]); // amount to translate
mat4.rotate(objMat, objMat, cubeRotation, [0, 0, -1]);
mat4.rotate(objMat, objMat, 0.7 * cubeRotation, [0, 1, 0]);
mat4.rotate(objMat, objMat, 0.2 * cubeRotation, [-1, 0, 0]);
mat4.scale(objMat, objMat, [s1,s1,s1]);
draw();
mat4.fromTranslation(objMat, [0,0,-250]); // amount to translate
mat4.rotate(objMat, objMat, cubeRotation, [0, 0, -1]);
mat4.rotate(objMat, objMat, 0.7 * cubeRotation, [0, -1, 0]);
mat4.rotate(objMat, objMat, 0.2 * cubeRotation, [1, 0, 0]);
// mat4.scale(objMat, objMat, [s2,s2,s2]);
draw();
mat4.fromTranslation(objMat, [0,0,-750]); // amount to translate
mat4.rotate(objMat, objMat, cubeRotation, [0, 0, -1]);
mat4.rotate(objMat, objMat, 0.7 * cubeRotation, [0, 1, 0]);
mat4.rotate(objMat, objMat, 0.2 * cubeRotation, [-1, 0, 0]);
mat4.scale(objMat, objMat, [s3,s3,s3]);
draw();
});
ac.startAnimation();
</script>
</body>
</html>