summaryrefslogtreecommitdiffstats
path: root/blockgame/main.js
blob: d79788901ed3921b8ad23e843738538f2ef4bb73 (plain) (blame)
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
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");
	const gl = canvas.getContext("webgl")
	
	if(gl == null) {
	    alert("Unable to initialize WebGL.");
	    return;
	}
	
	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');
	
	// 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;