summaryrefslogtreecommitdiffstats
path: root/blockgame/main.js
diff options
context:
space:
mode:
Diffstat (limited to 'blockgame/main.js')
-rw-r--r--blockgame/main.js90
1 files changed, 85 insertions, 5 deletions
diff --git a/blockgame/main.js b/blockgame/main.js
index d7bf3ea..d797889 100644
--- a/blockgame/main.js
+++ b/blockgame/main.js
@@ -1,4 +1,20 @@
-import * as shaders from "./shaders.js"
+import * as _shaders from "./shaders.js"
+import * as _mesh from "./mesh.js"
+import * as _camera from "./camera.js"
+
+/*function drawScene(gl, shader, camera, mesh) {
+ gl.clearColor(0.0, 0.0, 0.0, 1.0);
+ gl.clearDepth(1.0);
+ gl.enable(gl.DEPTH_TEST);
+ gl.depthFunc(gl.LEQUAL);
+
+ gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
+
+ gl.useProgram(shader.program);
+ camera.use(gl, shader);
+
+ gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
+}
function main() {
const canvas = document.querySelector("#glCanvas");
@@ -9,10 +25,74 @@ function main() {
return;
}
- const shaderProgram = shaders.init(gl);
+ const shader = _shaders.init(gl);
+ const mesh = new _mesh.Mesh(gl, 2);
+ const camera = new _camera.Camera(gl, 45, 0.1, 1000.0);
+ camera.setpos(vec3.create(0, 0, -6.0));
+
+ mesh.add_positions(gl,
+ [-1.0, 1.0,
+ 1.0, 1.0,
+ -1.0, -1.0,
+ 1.0, -1.0]);
+ mesh.addattrib(gl, shader, mesh.posbuffer, 2, gl.FLOAT, false, 0, 0);
+
+ drawScene(gl, shader, camera, mesh);
+
+ console.log(mesh);
+ console.log(camera);
+ console.log(shader);
+}*/
+
+main();
+
+//
+// Start here
+//
+function main() {
+ const canvas = document.querySelector('#glCanvas');
+ const gl = canvas.getContext('webgl');
- gl.clearColor(0.0, 0.0, 0.0, 1.0);
- gl.clear(gl.COLOR_BUFFER_BIT);
+ // If we don't have a GL context, give up now
+
+ if (!gl) {
+ alert('Unable to initialize WebGL. Your browser or machine may not support it.');
+ return;
+ }
+
+ // Vertex shader program
+ const shader = _shaders.init(gl);
+ const mesh = new _mesh.Mesh(gl, 2);
+ const camera = new _camera.Camera(gl);
+
+ const positions = [
+ 1.0, 1.0,
+ -1.0, 1.0,
+ 1.0, -1.0,
+ -1.0, -1.0,
+ ];
+ mesh.add_positions(gl, positions);
+
+ drawScene(gl, shader, camera, mesh);
+}
+
+function drawScene(gl, shader, camera, mesh) {
+ gl.clearColor(0.0, 0.0, 0.0, 1.0); // Clear to black, fully opaque
+ gl.clearDepth(1.0); // Clear everything
+ gl.enable(gl.DEPTH_TEST); // Enable depth testing
+ gl.depthFunc(gl.LEQUAL); // Near things obscure far things
+
+ gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
+ mesh.addattrib(gl, shader, mesh.posbuffer, 2, gl.FLOAT, false, 0, 0);
+
+ gl.useProgram(shader.program);
+ camera.use(gl, shader);
+
+ {
+ const offset = 0;
+ const vertexCount = 4;
+ gl.drawArrays(gl.TRIANGLE_STRIP, offset, vertexCount);
+ }
}
-window.onload = main;
+//window.onload = main;