-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathObjectND.js
134 lines (111 loc) · 4.23 KB
/
ObjectND.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
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
class ObjectND{
constructor(
N, vertices,
edges, edgesWithCulling,
faces, facesWithCulling,
camerasND, parent,
renderingMode){
this.N = N;
this.originalVertices = vertices.map(x=>x.clone());
this.currentVertices = this.originalVertices;
this.edgesList = edges;
this.edgesWithCullingList = edgesWithCulling;
this.facesList = faces;
this.facesWithCullingList = facesWithCulling;
this.parent = parent;
this.requiresUpdate = false;
this.updateVertexMatrix(camerasND, false);
this.initializeOpenGL();
this.useCulling = false;
this.renderingMode = -1;
this.setRenderingMode(renderingMode)
}
onDelete(){
this.parent.gl.deleteBuffer(this.VAO);
this.parent.gl.deleteBuffer(this.VBO);
this.parent.gl.deleteBuffer(this.EBO);
}
initializeOpenGL(){
let gl = this.parent.gl;
this.vertices = math.flatten(math.matrix(this.currentVertices))._data;
this.indices = math.flatten(this.edgesList.value)
this.indicesFaces = math.flatten(this.facesList.value)
// Create VAO, VBO and EBO
this.VAO = gl.createVertexArray();
gl.bindVertexArray(this.VAO);
this.VBO = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.VBO);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.vertices), gl.DYNAMIC_DRAW);
let positionAttLocation = gl.getAttribLocation(this.parent.shaderProgram, 'position')
gl.enableVertexAttribArray(positionAttLocation);
gl.vertexAttribPointer(positionAttLocation, 3, gl.FLOAT, gl.FALSE, 3 * Float32Array.BYTES_PER_ELEMENT, 0);
this.EBO = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.EBO);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(this.indices), gl.STATIC_DRAW, 0);
}
getRenderingMode(){ return this.renderingMode; }
setRenderingMode(newRenderingMode){
if(this.renderingMode == newRenderingMode){return;}
this.renderingMode = newRenderingMode;
this.updateEBO();
}
getUseCulling(){ return this.useCulling; }
setUseCulling(newUseCulling){
if(this.useCulling == newUseCulling){return;}
this.useCulling = newUseCulling;
this.updateEBO();
}
updateEBO(){
return;
let gl = this.parent.gl;
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.EBO);
if(this.renderingMode == OBJECTND_RENDERINGMODE_LINES){
if(this.useCulling){
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint32Array(math.flatten(math.matrix(this.edgesWithCullingList.value))._data), gl.STATIC_DRAW, 0);
}else{
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(this.indices), gl.STATIC_DRAW, 0);
}
} else if(this.renderingMode == OBJECTND_RENDERINGMODE_FACES){
if(this.useCulling){
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint32Array(math.flatten(math.matrix(this.facesWithCullingList.value))._data), gl.STATIC_DRAW, 0);
}else{
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint32Array(this.indicesFaces), gl.STATIC_DRAW, 0);
}
}
}
render() {
let gl = this.parent.gl;
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
gl.bindVertexArray(this.VAO);
if(this.renderingMode == OBJECTND_RENDERINGMODE_LINES){
if (this.useCulling) {
gl.drawElements(gl.LINES, this.edgesWithCullingList.value.length*2, gl.UNSIGNED_SHORT, 0);
} else {
gl.drawElements(gl.LINES, this.indices.length, gl.UNSIGNED_SHORT, 0);
}
} else if(this.renderingMode == OBJECTND_RENDERINGMODE_FACES){
if (this.useCulling) {
gl.drawElements(gl.TRIANGLES, this.facesWithCullingList.value.length*3, gl.UNSIGNED_SHORT, 0);
} else {
gl.drawElements(gl.TRIANGLES, this.facesList.value.length*3, gl.UNSIGNED_SHORT, 0);
}
} else {
console.log("Invalid renderingMode!")
}
gl.bindVertexArray(null);
}
updateVertexMatrix(camerasND, doUpdateVBO){
this.currentVertices = this.originalVertices;
console.log('cameras', camerasND.length, camerasND)
for (let c of camerasND){
this.currentVertices = projectPointsLosingDimension(this.currentVertices, c.viewProjectionModelMatrix, c.usePerspective);
}
if(doUpdateVBO){
updateVBO();
}
}
updateVBO(){
gl.bindBuffer(gl.ARRAY_BUFFER, this.VBO);
gl.bufferData(gl.ARRAY_BUFFER, this.currentVertices.map(x=>math.flatten(math.transpose(x))._data), gl.DYNAMIC_DRAW, 0);
}
}