-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrenderer.js
78 lines (65 loc) · 1.95 KB
/
renderer.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
import { Matrix4 } from './math';
import GLWrapper from './webgl';
export default class Renderer {
constructor(canvas) {
let ctx = this.ctx = canvas.getContext('webgl');
// Use debugging utils if included
// https://www.khronos.org/webgl/wiki/Debugging
if (false && window.WebGLDebugUtils) {
ctx = WebGLDebugUtils.makeDebugContext(
ctx,
undefined,
(funcName, args) => {
// Log on any undefined args
let valid = true;
for (let i = 0; i < args.length; i++) {
if (args[i] === undefined) {
valid = false;
}
}
if (!valid) {
console.error(`Undefined argument in ${funcName}: ${args}`);
}
}
);
}
this.gl = new GLWrapper(ctx);
this.gl.setSize(canvas.width, canvas.height);
// Temp matrices during render
this._mvMatrix = new Matrix4();
this._mMatrix = new Matrix4();
}
getContext() {
return this.ctx;
}
setSize(width, height) {
this.gl.setSize(width, height);
}
render(scene, camera) {
const { gl } = this;
const pMatrix = camera.getProjectionMatrix();
const vMatrix = camera.getInverseWorldMatrix();
// Clear out the canvas
gl.clear();
const queue = [scene];
while (queue.length) {
const node = queue.shift();
// Get and construct our model and model view matrices
const mMatrix = node.getWorldMatrix();
let mvMatrix = this._mvMatrix;
Matrix4.multiply(mvMatrix.identity(), vMatrix, mMatrix);
// If we have a model, pass it to the GLWrapper
// to draw
if (node.isModel) {
this.gl.draw(node.geometry, node.material, mMatrix, mvMatrix, pMatrix, vMatrix);
}
// If this node has children, push them into the queue
if (node.hasChildren()) {
const children = node.getChildren();
for (let child of children) {
queue.push(child);
}
}
}
}
}