-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
116 lines (95 loc) · 2.49 KB
/
index.js
File metadata and controls
116 lines (95 loc) · 2.49 KB
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
let canvas;
let context;
let TARGET_FPS = 30;
let frame_start;
let pos_x = 0;
let pos_y = 0;
let pos_z = 2;
let scale = 1;
let rotation = 50;
// A cube model
let cube = new Model([
new Vector3(-0.5, 0.5, 0.5),
new Vector3(0.5, 0.5, 0.5),
new Vector3(-0.5, -0.5, 0.5),
new Vector3(0.5, -0.5, 0.5),
new Vector3(-0.5, 0.5, -0.5),
new Vector3(0.5, 0.5, -0.5),
new Vector3(-0.5, -0.5, -0.5),
new Vector3(0.5, -0.5, -0.5),
], [
new Color(0, 0, 1),
new Color(0, 1, 0),
new Color(0.5, 0, 0.5),
new Color(1, 0.5, 1),
new Color(1, 0, 1),
new Color(0.5, 0, 0),
new Color(1, 0, 0),
new Color(1, 1, 0)
], [
0,1,3,
0,2,3,
1,5,7,
1,3,7,
4,5,6,
5,6,7,
0,2,4,
2,4,6,
2,3,6,
3,6,7,
0,1,4,
1,4,5
])
class Global {
static activeModel = cube;
}
/**
* Initialize the graphics pipeline
*/
function init() {
document.getElementById("objFile").addEventListener("change", function() {
OBJLoader.load(this.files[0]);
})
canvas = document.getElementById('canvas');
ctx = canvas.getContext('2d');
canvas.width = 200;// window.innerWidth;
canvas.height = 200;//window.innerHeight;
Renderer.init(canvas, ctx);
}
/**
* Main frame loop
*/
async function loop() {
// set the projection matrix in the shaders
Shader.projectionMatrix = Matrix4.createProjectionMatrix(
canvas.width / canvas.height,
60,
0.1,
100
);
let rot = 0;
while (true) {
pos_x = document.getElementById("pos_x").value / 5;
pos_y = document.getElementById("pos_y").value / 5;
pos_z = document.getElementById("pos_z").value;
scale = document.getElementById("scale").value / 100;
rotation = document.getElementById("rotation").value;
frame_start = Date.now();
// Transform the cube in the shader
Shader.transformationMatrix = Matrix4.createTransformationMatrix(
new Vector3(pos_x, pos_y, -pos_z),
new Vector3(scale, scale, scale),
new Vector3(0, rot, 0)
)
// Draw to the draw buffer the given model
Renderer.draw(Global.activeModel);
// Swap the draw and display buffers
Renderer.swapBuffers();
// Try to hit TARGET_FPS fps
let dt = Date.now() - frame_start;
rot += (rotation / 180) * Math.PI * (dt / 1000);
await new Promise(resolve => setTimeout(resolve, Math.max(0, (1 / TARGET_FPS) - dt)))
}
}
init()
loop()