-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotex.html
134 lines (102 loc) · 3.42 KB
/
rotex.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
<!DOCTYPE html>
<html>
<head>
<title>Rotex</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);
// shape
const palette = dx.Texture(
[
[255, 255, 0, 255],
[255, 0, 0, 255],
[ 0, 255, 0, 255],
[ 0, 0, 255, 255]
],
1, 4
);
// colors
function tC(i) {
return (2*i + 1) / (2*4);
}
const y = tC(0), r = tC(1), g = tC(2), b = tC(3);
//vertices
const p = 10;
//ac.panWorld(6);
/*const corners = [
[[0*p, -10*p], [g,g], []],
[[5*p, 0*p], [b,b], []],
[[6*p, 1*p], [b,b], []],
[[7*p, 3*p], [b,b], []],
[[8*p, 10*p], [g,g], []],
[[8*p, 15*p], [r,r], []],
[[6*p, 17*p], [y,y], []],
[[8*p, 19*p], [r,y], []],
[[8*p, 20*p], [y,y], []],
[[0*p, 20*p], [y,y], []]
];*/
const corners = [
[0*p, -10*p],
[5*p, 0*p],
[6*p, 1*p],
[7*p, 3*p],
[8*p, 10*p],
[8*p, 15*p],
[6*p, 17*p],
[8*p, 19*p],
[8*p, 20*p],
[0*p, 20*p]
].map(v => [v, [1 - (v[1]+10*p)/(30*p)], []]);
const shape = Shape.RoTex(corners, 256, 0, 360, true, {texture: palette});
/*const texture = new Image();
texture.src = "mx.jpg";
texture.onload = () => shape.texture = dx.Texture(texture);*/
// drawing
const deg = Math.PI / 180;
const {viewMatrix, perspectiveMatrix, projectionMatrix} = ac;
var objMat = mat4.create();
const lightMatrix = mat4.create();
mat4.apply(lightMatrix, lightMatrix, mat4.rotateY, -45*deg);
mat4.apply(lightMatrix, lightMatrix, mat4.rotateX, 45*deg);
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(shape, stdShader, {projectionMatrix: pjMat, lightMatrix: lMat});
}
ac.setRender(function (deltaAnimationTime) {
dx.clear();
draw();
});
ac.updateProjection();
ac.render(); //ac.startAnimation();
</script>
</body>
</html>