-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw_gl.js
288 lines (246 loc) · 7.58 KB
/
draw_gl.js
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
let gl = null;
const MAX_SPRITES = 128;
const NUM_INDICIES = MAX_SPRITES * 6;
const VERTEX_SIZE = 4 * 4; // 4 * f32
const BUFFER_SIZE = MAX_SPRITES * 6 * VERTEX_SIZE;
let vertex_buffer = undefined;
let index_buffer = undefined;
let texture = undefined;
let programInfo = undefined;
const player_uv = {
u0: 0/7, u1: 1/7,
v0: 0.0, v1: 1.0
};
const enemy_uv = {
u0: 1/7, u1: 2/7,
v0: 0.0, v1: 1.0
};
const target_uv = {
u0: 2/7, u1: 3/7,
v0: 0.0, v1: 1.0
};
const poop_uv = {
u0: 3/7, u1: 4/7,
v0: 0.0, v1: 1.0
};
const icon_uv = {
"Launch": {
u0: 4/7, u1: 5/7,
v0: 0.0, v1: 1.0
},
"Paused": {
u0: 5/7, u1: 6/7,
v0: 0.0, v1: 1.0
},
"Restart": {
u0: 6/7, u1: 1.0,
v0: 0.0, v1: 1.0
}
};
function initializeGL() {
gl = WebGLDebugUtils.makeDebugContext(canvas.getContext('webgl', {
alpha : false
}));
// Generate VBO and VAO
// vertex_array = gl.createVertexArray();
vertex_buffer = gl.createBuffer();
// gl.bindVertexArray(vertex_array);
// gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer);
// Generate Index Buffer
let indicies = [];
let offset = 0;
for (let i = 0; i < NUM_INDICIES; i++) {
indicies.push(offset + 0);
indicies.push(offset + 1);
indicies.push(offset + 2);
indicies.push(offset + 2);
indicies.push(offset + 3);
indicies.push(offset + 0);
offset += 4;
}
index_buffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, index_buffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indicies), gl.STATIC_DRAW);
// Shader Program
const vsSource = `
attribute vec4 vertex_xyuv;
varying highp vec2 vTextureCoord;
void main(void) {
vec4 position = vec4(0, 0, 0, 1);
position.x = vertex_xyuv.x / ${WORLD_WIDTH/2}.0 - 1.0;
position.y = -vertex_xyuv.y / ${WORLD_HEIGHT/2}.0 + 1.0;
gl_Position = position;
vTextureCoord = vertex_xyuv.zw;
}
`;
const fsSource = `
varying highp vec2 vTextureCoord;
uniform sampler2D uSampler;
void main(void) {
gl_FragColor = texture2D(uSampler, vTextureCoord);
// gl_FragColor = vec4(0.0, vTextureCoord.x, vTextureCoord.y, 1.0);
}
`;
// Create Texture
{
texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
// Create temporary texture, while svg texture loads
const level = 0;
const internalFormat = gl.RGBA;
const width = 1;
const height = 1;
const border = 0;
const srcFormat = gl.RGBA;
const srcType = gl.UNSIGNED_BYTE;
const pixel = new Uint8Array([255, 255, 255, 255]); // opaque white
gl.texImage2D(gl.TEXTURE_2D, level, internalFormat,
width, height, border, srcFormat, srcType,
pixel);
const img = new Image();
img.onload = function() {
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, level, internalFormat,
srcFormat, srcType, img);
// WebGL1 has different requirements for power of 2 images
// vs non power of 2 images so check if the image is a
// power of 2 in both dimensions.
const isPowerOf2 = value => ((value & (value - 1)) == 0);
if (isPowerOf2(img.width) && isPowerOf2(img.height)) {
// Yes, it's a power of 2. Generate mips.
gl.generateMipmap(gl.TEXTURE_2D);
} else {
// No, it's not a power of 2. Turn of mips and set
// wrapping to clamp to edge
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
}
};
img.src = './spritesheet.svg';
}
const shaderProgram = initShaderProgram(vsSource, fsSource);
programInfo = {
program: shaderProgram,
attribLocations: {
vertexPosition: gl.getAttribLocation(shaderProgram, 'vertex_xyuv'),
},
uniformLocations: {
projectionMatrix: gl.getUniformLocation(shaderProgram, 'uProjectionMatrix'),
modelViewMatrix:
gl.getUniformLocation(shaderProgram, 'uModelViewMatrix'),
uSampler: gl.getUniformLocation(shaderProgram, 'uSampler'),
},
};
}
function initShaderProgram(vsSource, fsSource) {
function loadShader(type, source) {
const shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
// Check for build errors
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
console.error(
'Shader compilation error:'
+ gl.getShaderInfoLog(shader) + '\n'
+ source);
gl.deleteShader(shader);
return null;
}
return shader;
}
const vertexShader = loadShader(gl.VERTEX_SHADER, vsSource);
const fragmentShader = loadShader( gl.FRAGMENT_SHADER, fsSource);
// Create the shader program
const shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
// Check for link errors
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
console.error(
'Shader program linking error:'
+ gl.getProgramInfoLog(shaderProgram));
return null;
}
return shaderProgram;
}
function drawGL() {
// Clear screen
gl.clearColor(0.1, 0.1, 0.1, 1.0);
gl.clearDepth(1.0);
gl.enable(gl.DEPTH_TEST);
gl.enable(gl.BLEND);
gl.depthFunc(gl.LEQUAL);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
// Orthographic projection
const projectionMatrix = mat4.create();
mat4.ortho(projectionMatrix,
0, WORLD_WIDTH, WORLD_HEIGHT, 0, 0.1, 100);
// Vertex attributes
{
const numComponents = 4;
const type = gl.FLOAT;
const normalize = false;
const stride = 0;
const offset = 0;
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer);
gl.vertexAttribPointer(
programInfo.attribLocations.vertexPosition,
numComponents,
type,
normalize,
stride,
offset);
gl.enableVertexAttribArray(
programInfo.attribLocations.vertexPosition);
}
const modelViewMatrix = mat4.create();
mat4.translate(modelViewMatrix,
modelViewMatrix,
[5.0, 5.0, -6.0]);
// Push sprites for each entity
let positions = [];
let numSprites = 0;
function pushSprite(x, y, uv, size) {
size = size || 1.0;
numSprites++;
positions.push(
x , y , uv.u0, uv.v0,
x , y + size, uv.u0, uv.v1,
x + size, y + size, uv.u1, uv.v1,
x + size, y , uv.u1, uv.v0);
}
pushSprite(player.x, player.y, (game_state != 'Restart') ? player_uv : poop_uv);
for (let enemy of enemies) {
pushSprite(enemy.x, enemy.y, enemy_uv);
}
for (let particle of particles) {
let scale = 0.75 * 1/(Math.max(1, particle.vy - 1.2*PARTICLE_SPEED));
pushSprite(particle.x, particle.y, target_uv, scale);
}
pushSprite(target.x, target.y, target_uv);
if (game_state !== "Play") {
let uv = icon_uv[game_state];
pushSprite(12.5, 7.5, uv, 15);
}
let data = new Float32Array(positions);
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer);
gl.bufferData(gl.ARRAY_BUFFER, data, gl.DYNAMIC_DRAW);
gl.useProgram(programInfo.program);
gl.uniformMatrix4fv(
programInfo.uniformLocations.projectionMatrix,
false,
projectionMatrix);
gl.uniformMatrix4fv(
programInfo.uniformLocations.modelViewMatrix,
false,
modelViewMatrix);
// Texture
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.uniform1i(programInfo.uniformLocations.uSampler, 0); // Tell shader to use texture 0
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, index_buffer);
gl.drawElements(gl.TRIANGLES, numSprites * 6, gl.UNSIGNED_SHORT, 0);
}