-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebgl.js
More file actions
174 lines (140 loc) · 3.83 KB
/
webgl.js
File metadata and controls
174 lines (140 loc) · 3.83 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* WebGL boilerplate code
*/
var gl;
/**
* Gets the shader object from the scripts
*/
function getShader(gl, id) {
var shaderScript = document.getElementById(id);
if (!shaderScript) {
alert(id);
return null;
}
// Fill str with the souce of the shader
var str = "";
var k = shaderScript.firstChild;
while (k) {
// ????
if (k.nodeType == 3) {
str += k.textContent;
}
k = k.nextSibling;
}
var shader;
if (shaderScript.type == "x-shader/x-fragment") {
shader = gl.createShader(gl.FRAGMENT_SHADER);
} else if (shaderScript.type == "x-shader/x-vertex") {
shader = gl.createShader(gl.VERTEX_SHADER);
} else {
return null;
}
gl.shaderSource(shader, str);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
var shaderProgram;
function initShaders() {
var fragmentShader = getShader(gl, "shader-fs");
var vertexShader = getShader(gl, "shader-vs");
shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert("Could not initialize shaders");
}
gl.useProgram(shaderProgram);
// Set program's variables to be vertex shader's variables
shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
shaderProgram.vertexColorAttribute = gl.getAttribLocation(shaderProgram, "aVertexColor");
gl.enableVertexAttribArray(shaderProgram.vertexColorAttribute);
shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
}
var mvMatrix = mat4.create();
var mvMatrixStack = [];
var pMatrix = mat4.create(); // perspective
function mvPushMatrix() {
mvMatrixStack.push(mat4.clone(mvMatrix));
}
function mvPopMatrix() {
if (mvMatrixStack.length == 0) {
throw "Invalid popMatrix!";
}
mvMatrix = mvMatrixStack.pop();
}
function degToRad(degrees) {
return degrees * Math.PI / 180;
}
function $g(id) {
return document.getElementById(id);
}
function setMatrixUniforms() {
gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix);
gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix);
}
/**
* Convenience function to get the cursors position
*/
function getCursorPosition(e) {
var x, y;
if ( event.offsetX == null ) { // Firefox
x = event.originalEvent.layerX;
y = event.originalEvent.layerY;
}
else { // Other browsers
x = event.offsetX;
y = event.offsetY;
}
return [x, y];
}
/**
* Event handler when the mouse is moved over the canvas
*/
function canvasMouseMove(e) {
blockus.setMousePosition(getCursorPosition(e));
}
/**
* Event handler when the mouse is pressed down
*/
function canvasMouseDown(e) {
blockus.mouseDown();
}
/**
* Event handler when a mouse click occurs
*/
function canvasClick(e) {
blockus.mouseClicked();
}
function tick() {
requestAnimFrame(tick);
blockus.update();
blockus.draw();
}
var blockus;
function webGLStart() {
var canvas = $g("canvas");
gl = WebGLUtils.setupWebGL(canvas);
if (!gl) {
return;
}
canvas.addEventListener("mousemove", canvasMouseMove, false);
canvas.addEventListener("mousedown", canvasMouseDown, false);
canvas.addEventListener("mouseup", canvasClick, false);
initShaders();
var gridSize = 20; // 20x20 board
var pieces = initializePieces(gl, shaderProgram, gridSize);
blockus = new Blockus(gl, shaderProgram, gridSize, pieces);
gl.viewportWidth = canvas.width;
gl.viewportHeight = canvas.height;
gl.clearColor(1.0, 1.0, 1.0, 1.0);
gl.enable(gl.DEPTH_TEST);
document.addEventListener("keyup", blockus.onKeyReleased, false);
tick();
}