summaryrefslogtreecommitdiff
path: root/public/gl/core
diff options
context:
space:
mode:
Diffstat (limited to 'public/gl/core')
-rw-r--r--public/gl/core/Camera.js6
-rw-r--r--public/gl/core/Mesh.js25
-rw-r--r--public/gl/core/Renderer.js8
3 files changed, 29 insertions, 10 deletions
diff --git a/public/gl/core/Camera.js b/public/gl/core/Camera.js
index 353b5e9..b9b7686 100644
--- a/public/gl/core/Camera.js
+++ b/public/gl/core/Camera.js
@@ -26,15 +26,15 @@ export class Camera {
d[0] = u.x;
d[1] = v.x;
d[2] = w.x;
- d[3] = 1
+ d[3] = 0
d[4] = u.y;
d[5] = v.y;
d[6] = w.y;
- d[7] = 1
+ d[7] = 0
d[8] = u.z;
d[9] = v.z;
d[10] = w.z;
- d[11] = 1
+ d[11] = 0
d[12] = -u.dot(this.position)
d[13] = -v.dot(this.position)
d[14] = -w.dot(this.position)
diff --git a/public/gl/core/Mesh.js b/public/gl/core/Mesh.js
index b35c13c..0d80205 100644
--- a/public/gl/core/Mesh.js
+++ b/public/gl/core/Mesh.js
@@ -8,6 +8,7 @@ export class Mesh {
#vertexCount
#count
#id
+ #indicies
constructor(vertexCount) {
this.#id = ext.createVertexArrayOES()
@@ -15,6 +16,7 @@ export class Mesh {
Mesh.#vaos.push(this.#id)
this.#vertexCount = vertexCount
this.#count = 0
+ this.#indicies = 0
}
store(data, dim) {
@@ -22,12 +24,21 @@ export class Mesh {
Mesh.#vbos.push(id)
gl.bindBuffer(gl.ARRAY_BUFFER, id)
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(data), gl.STATIC_DRAW)
- gl.vertexAttribPointer(this.#count, dim, gl.FLOAT, false, Float32Array.BYTES_PER_ELEMENT * dim, 0)
+ gl.vertexAttribPointer(this.#count, dim, gl.FLOAT, gl.FALSE, 0, 0)
gl.enableVertexAttribArray(this.#count)
this.#count++;
return this
}
+ 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
+ }
+
finish() {
this.unbind()
return this
@@ -35,14 +46,24 @@ export class Mesh {
bind() {
ext.bindVertexArrayOES(this.#id)
+ for(let i = 0; i < this.#count; i++) {
+ gl.enableVertexAttribArray(i)
+ }
}
unbind() {
ext.bindVertexArrayOES(null)
+ for(let i = 0; i < this.#count; i++) {
+ gl.disableVertexAttribArray(i)
+ }
}
draw() {
- gl.drawArrays(gl.TRIANGLES, 0, this.#vertexCount)
+ if(this.#indicies > 0) {
+ gl.drawElements(gl.TRIANGLES, this.#indicies, gl.UNSIGNED_SHORT, 0)
+ } else {
+ gl.drawArrays(gl.TRIANGLES, 0, this.#vertexCount)
+ }
}
} \ No newline at end of file
diff --git a/public/gl/core/Renderer.js b/public/gl/core/Renderer.js
index 38a7974..76dfb82 100644
--- a/public/gl/core/Renderer.js
+++ b/public/gl/core/Renderer.js
@@ -43,9 +43,7 @@ export class Renderer {
const material = scene.materials[material_id]
gl.enable(gl.DEPTH_TEST);
gl.enable(gl.CULL_FACE);
- gl.cullFace(gl.BACK);
- gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
- gl.clearColor(0, 0, 0, 1);
+ gl.cullFace(gl.FRONT);
material.bind()
material.shader.loadMat4("proj", this.proj())
material.shader.loadMat4("view", camera.view())
@@ -64,14 +62,14 @@ export class Renderer {
const proj = new Mat4()
const d = proj.data
- const tanHalfFovy = Math.tan((this.FOV * (Math.PI/180))/ 2.0);
+ const tanHalfFovy = Math.tan((this.FOV * (Math.PI/180)) / 2.0);
const aspect = canvas.width / canvas.height
d[0] = 1.0 / (aspect * tanHalfFovy)
d[5] = 1.0 / tanHalfFovy
d[10] = this.FAR / (this.FAR - this.NEAR)
d[11] = 1.0
- d[14] = -(this.FAR * this.NEAR) / (this.FAR)
+ d[14] = -(this.FAR * this.NEAR) / (this.FAR - this.NEAR)
return proj
}