2023-01-19 03:43:02 +00:00
|
|
|
import { gl, ext } from './Renderer.js'
|
2023-01-18 14:30:41 +00:00
|
|
|
|
2023-01-19 03:43:02 +00:00
|
|
|
export class Mesh {
|
2023-01-18 14:30:41 +00:00
|
|
|
|
|
|
|
static #vaos = []
|
|
|
|
static #vbos = []
|
|
|
|
|
2023-01-19 03:43:02 +00:00
|
|
|
#vertexCount
|
|
|
|
#count
|
|
|
|
#id
|
2023-01-20 02:21:17 +00:00
|
|
|
#indicies
|
2023-01-18 14:30:41 +00:00
|
|
|
|
|
|
|
constructor(vertexCount) {
|
2023-01-19 03:43:02 +00:00
|
|
|
this.#id = ext.createVertexArrayOES()
|
|
|
|
ext.bindVertexArrayOES(this.#id)
|
|
|
|
Mesh.#vaos.push(this.#id)
|
|
|
|
this.#vertexCount = vertexCount
|
|
|
|
this.#count = 0
|
2023-01-20 02:21:17 +00:00
|
|
|
this.#indicies = 0
|
2023-01-18 14:30:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
store(data, dim) {
|
|
|
|
let id = gl.createBuffer()
|
|
|
|
Mesh.#vbos.push(id)
|
|
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, id)
|
|
|
|
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(data), gl.STATIC_DRAW)
|
2023-01-20 02:21:17 +00:00
|
|
|
gl.vertexAttribPointer(this.#count, dim, gl.FLOAT, gl.FALSE, 0, 0)
|
2023-01-18 14:30:41 +00:00
|
|
|
gl.enableVertexAttribArray(this.#count)
|
|
|
|
this.#count++;
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2023-01-20 02:21:17 +00:00
|
|
|
indicies(data) {
|
|
|
|
let id = gl.createBuffer()
|
|
|
|
Mesh.#vbos.push(id)
|
|
|
|
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, id)
|
|
|
|
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(data), gl.STATIC_DRAW)
|
|
|
|
this.#indicies = data.length
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2023-01-18 14:30:41 +00:00
|
|
|
finish() {
|
|
|
|
this.unbind()
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
bind() {
|
2023-01-19 03:43:02 +00:00
|
|
|
ext.bindVertexArrayOES(this.#id)
|
2023-01-20 02:21:17 +00:00
|
|
|
for(let i = 0; i < this.#count; i++) {
|
|
|
|
gl.enableVertexAttribArray(i)
|
|
|
|
}
|
2023-01-18 14:30:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unbind() {
|
2023-01-19 03:43:02 +00:00
|
|
|
ext.bindVertexArrayOES(null)
|
2023-01-20 02:21:17 +00:00
|
|
|
for(let i = 0; i < this.#count; i++) {
|
|
|
|
gl.disableVertexAttribArray(i)
|
|
|
|
}
|
2023-01-19 03:43:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
draw() {
|
2023-01-20 02:21:17 +00:00
|
|
|
if(this.#indicies > 0) {
|
|
|
|
gl.drawElements(gl.TRIANGLES, this.#indicies, gl.UNSIGNED_SHORT, 0)
|
|
|
|
} else {
|
|
|
|
gl.drawArrays(gl.TRIANGLES, 0, this.#vertexCount)
|
|
|
|
}
|
2023-01-18 14:30:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|