webgl-tools

view sanegl.js @ 7:b215a8d38818

fixed a bug in sanelg. can't remember what it was
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 18 Aug 2011 00:08:40 +0300
parents 56ae66e32998
children
line source
1 /*
2 SaneGL - a small library to bring back sanity to WebGL
3 Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 const GL_MODELVIEW = 0;
19 const GL_PROJECTION = 1;
20 const GL_TEXTURE = 2;
22 const GL_POINTS = 1;
23 const GL_LINES = 2;
24 const GL_TRIANGLES = 3;
25 const GL_QUADS = 4;
27 var gl_ident_val = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
28 var gl_mmode = GL_MODELVIEW;
29 var gl_mat = new Array(
30 [new Float32Array(gl_ident_val)],
31 [new Float32Array(gl_ident_val)],
32 [new Float32Array(gl_ident_val)]);
34 const GL_MAX_VERTS = 512;
36 var gl_prim = 0;
37 var gl_vbuf = null, gl_nbuf = null, gl_cbuf = null, gl_tbuf = null, gl_abuf = null;
38 var gl_vertex, gl_normal, gl_color, gl_texcoord, gl_attrib;
39 var gl_nverts, gl_vert_calls;
40 var gl_curr_normal = new Float32Array(3);
41 var gl_curr_color = new Float32Array(4);
42 var gl_curr_texcoord = new Float32Array(2);
43 var gl_curr_attrib = new Float32Array(4);
44 var gl_vloc, gl_nloc, gl_cloc, gl_tloc, gl_aloc;
45 var gl_use_normal, gl_use_color, gl_use_texcoord, gl_use_attrib;
47 function glMatrixMode(mode)
48 {
49 gl_mmode = mode;
50 }
52 function glPushMatrix()
53 {
54 var mtop = gl_mat[gl_mmode].length - 1;
56 gl_mat[gl_mmode].push(new Float32Array(16));
57 m4_copy(gl_mat[gl_mmode][mtop + 1], gl_mat[gl_mmode][mtop]);
58 }
60 function glPopMatrix()
61 {
62 if(gl_mat[gl_mmode].length <= 1) {
63 alert('glPopMatrix underflow');
64 }
65 gl_mat[gl_mmode].pop();
66 }
68 function glLoadIdentity()
69 {
70 var mtop = gl_mat[gl_mmode].length - 1;
71 m4_identity(gl_mat[gl_mmode][mtop]);
72 }
74 function glLoadMatrixf(mat)
75 {
76 var mtop = gl_mat[gl_mmode].length - 1;
77 m4_copy(gl_mat[gl_mmode][mtop], mat);
78 }
80 function glMultMatrixf(mat)
81 {
82 var mtop = gl_mat[gl_mmode].length - 1;
83 m4_mul(gl_mat[gl_mmode][mtop], gl_mat[gl_mmode][mtop], mat);
84 }
86 function glTranslatef(x, y, z)
87 {
88 var xform = new Float32Array(16);
89 m4_translation(xform, x, y, z);
90 glMultMatrixf(xform);
91 }
93 function glRotatef(angle, x, y, z)
94 {
95 var angle_rads = Math.PI * angle / 180.0;
96 var xform = new Float32Array(16);
97 m4_rotation(xform, angle_rads, x, y, z);
98 glMultMatrixf(xform);
99 }
101 function glScalef(x, y, z)
102 {
103 var xform = new Float32Array(16);
104 m4_scale(xform, x, y, z);
105 glMultMatrixf(xform);
106 }
108 function glOrtho(left, right, bottom, top, near, far)
109 {
110 var dx = right - left;
111 var dy = top - bottom;
112 var dz = far - near;
114 var tx = -(right + left) / dx;
115 var ty = -(top + bottom) / dy;
116 var tz = -(far + near) / dz;
118 var sx = 2.0 / dx;
119 var sy = 2.0 / dy;
120 var sz = -2.0 / dz;
122 var xform = new Float32Array(16);
123 xform[0] = sx;
124 xform[5] = sy;
125 xform[10] = sz;
126 xform[12] = tx;
127 xform[13] = ty;
128 xform[14] = tz;
129 xform[15] = 1.0;
130 xform[1] = xform[2] = xform[3] = xform[4] = xform[6] = xform[7] = xform[8] = xform[9] = 0.0;
132 glMultMatrixf(xform);
133 }
135 function glFrustum(left, right, bottom, top, near, far)
136 {
137 var dx = right - left;
138 var dy = top - bottom;
139 var dz = far - near;
141 var a = (right + left) / dx;
142 var b = (top + bottom) / dy;
143 var c = -(far + near) / dz;
144 var d = -2.0 * far * near / dz;
146 var xform = new Float32Array(16);
147 xform[0] = 2.0 * near / dx;
148 xform[5] = 2.0 * near / dy;
149 xform[8] = a;
150 xform[9] = b;
151 xform[10] = c;
152 xform[11] = -1.0;
153 xform[14] = d;
154 xform[1] = xform[2] = xform[3] = xform[4] = xform[6] = xform[7] = xform[12] = xform[13] = xform[15] = 0.0;
156 glMultMatrixf(xform);
157 }
159 function gluPerspective(vfov, aspect, near, far)
160 {
161 var x = near * Math.tan(vfov / 2.0);
162 glFrustum(-aspect * x, aspect * x, -x, x, near, far);
163 }
165 function gl_apply_xform(prog)
166 {
167 var mvtop = gl_mat[GL_MODELVIEW].length - 1;
168 var ptop = gl_mat[GL_PROJECTION].length - 1;
169 var ttop = gl_mat[GL_TEXTURE].length - 1;
171 var loc = gl.getUniformLocation(prog, "mvmat");
172 if(loc != -1) {
173 gl.uniformMatrix4fv(loc, false, gl_mat[GL_MODELVIEW][mvtop]);
174 }
176 loc = gl.getUniformLocation(prog, "projmat");
177 if(loc != -1) {
178 gl.uniformMatrix4fv(loc, false, gl_mat[GL_PROJECTION][ptop]);
179 }
181 loc = gl.getUniformLocation(prog, "texmat");
182 if(loc != -1) {
183 gl.uniformMatrix4fv(loc, false, gl_mat[GL_TEXTURE][ttop]);
184 }
186 loc = gl.getUniformLocation(prog, "normmat");
187 if(loc != -1) {
188 var normmat = new Float32Array(9);
189 normmat[0] = gl_mat[GL_MODELVIEW][mvtop][0];
190 normmat[1] = gl_mat[GL_MODELVIEW][mvtop][1];
191 normmat[2] = gl_mat[GL_MODELVIEW][mvtop][2];
192 normmat[3] = gl_mat[GL_MODELVIEW][mvtop][4];
193 normmat[4] = gl_mat[GL_MODELVIEW][mvtop][5];
194 normmat[5] = gl_mat[GL_MODELVIEW][mvtop][6];
195 normmat[6] = gl_mat[GL_MODELVIEW][mvtop][8];
196 normmat[7] = gl_mat[GL_MODELVIEW][mvtop][9];
197 normmat[8] = gl_mat[GL_MODELVIEW][mvtop][10];
198 gl.uniformMatrix3fv(loc, false, normmat);
199 }
200 }
202 function glBegin(prim)
203 {
204 gl_vertex = new Float32Array(GL_MAX_VERTS * 4);
205 gl_normal = new Float32Array(GL_MAX_VERTS * 3);
206 gl_color = new Float32Array(GL_MAX_VERTS * 4);
207 gl_texcoord = new Float32Array(GL_MAX_VERTS * 4);
208 gl_attrib = new Float32Array(GL_MAX_VERTS * 4);
210 if(gl_vbuf == null) {
211 gl_vbuf = gl.createBuffer();
212 gl.bindBuffer(gl.ARRAY_BUFFER, gl_vbuf);
213 gl.bufferData(gl.ARRAY_BUFFER, GL_MAX_VERTS * 4 * Float32Array.BYTES_PER_ELEMENT, gl.STREAM_DRAW);
214 logmsg("vertex buffer size: " + gl.getBufferParameter(gl.ARRAY_BUFFER, gl.BUFFER_SIZE) + "\n");
216 gl_nbuf = gl.createBuffer();
217 gl.bindBuffer(gl.ARRAY_BUFFER, gl_nbuf);
218 gl.bufferData(gl.ARRAY_BUFFER, GL_MAX_VERTS * 3 * Float32Array.BYTES_PER_ELEMENT, gl.STREAM_DRAW);
219 logmsg("normal buffer size: " + gl.getBufferParameter(gl.ARRAY_BUFFER, gl.BUFFER_SIZE) + "\n");
221 gl_cbuf = gl.createBuffer();
222 gl.bindBuffer(gl.ARRAY_BUFFER, gl_cbuf);
223 gl.bufferData(gl.ARRAY_BUFFER, GL_MAX_VERTS * 4 * Float32Array.BYTES_PER_ELEMENT, gl.STREAM_DRAW);
224 logmsg("color buffer size: " + gl.getBufferParameter(gl.ARRAY_BUFFER, gl.BUFFER_SIZE) + "\n");
226 gl_tbuf = gl.createBuffer();
227 gl.bindBuffer(gl.ARRAY_BUFFER, gl_tbuf);
228 gl.bufferData(gl.ARRAY_BUFFER, GL_MAX_VERTS * 4 * Float32Array.BYTES_PER_ELEMENT, gl.STREAM_DRAW);
229 logmsg("texcoord buffer size: " + gl.getBufferParameter(gl.ARRAY_BUFFER, gl.BUFFER_SIZE) + "\n");
231 gl_abuf = gl.createBuffer();
232 gl.bindBuffer(gl.ARRAY_BUFFER, gl_abuf);
233 gl.bufferData(gl.ARRAY_BUFFER, GL_MAX_VERTS * 4 * Float32Array.BYTES_PER_ELEMENT, gl.STREAM_DRAW);
234 logmsg("custom attrib buffer size: " + gl.getBufferParameter(gl.ARRAY_BUFFER, gl.BUFFER_SIZE) + "\n");
235 }
237 gl_prim = prim;
238 gl_nverts = gl_vert_calls = 0;
240 gl_prog = gl.getParameter(gl.CURRENT_PROGRAM);
241 if(gl_prog != null) {
242 gl_apply_xform(gl_prog);
244 gl_vloc = gl.getAttribLocation(gl_prog, "attr_vertex");
245 gl_nloc = gl.getAttribLocation(gl_prog, "attr_normal");
246 gl_cloc = gl.getAttribLocation(gl_prog, "attr_color");
247 gl_tloc = gl.getAttribLocation(gl_prog, "attr_texcoord");
248 } else {
249 logmsg("no program currently bound\n");
250 gl_vloc = gl_nloc = gl_cloc = gl_tloc = -1;
251 }
253 if(gl_vloc == -1) {
254 logmsg("attr_vertex not found in currently bound program: " + prog + "\n");
255 }
257 gl_use_normal = gl_nloc != -1;
258 gl_use_color = gl_cloc != -1;
259 gl_use_texcoord = gl_tloc != -1;
261 gl_aloc = -1;
262 }
264 function glEnd()
265 {
266 if(gl_nverts > 0) {
267 gl_draw_immediate();
268 }
269 }
271 function gl_draw_immediate()
272 {
273 var gles_prim;
275 if(gl_vloc == -1) {
276 return;
277 }
279 switch(gl_prim) {
280 case GL_POINTS:
281 gles_prim = gl.POINTS;
282 break;
283 case GL_LINES:
284 gles_prim = gl.LINES;
285 break;
286 case GL_TRIANGLES:
287 case GL_QUADS:
288 gles_prim = gl.TRIANGLES;
289 break;
290 default:
291 }
293 gl.bindBuffer(gl.ARRAY_BUFFER, gl_vbuf);
294 gl.bufferSubData(gl.ARRAY_BUFFER, 0, gl_vertex);
295 gl.vertexAttribPointer(gl_vloc, 4, gl.FLOAT, false, 0, 0);
296 gl.enableVertexAttribArray(gl_vloc);
298 if(gl_use_normal) {
299 gl.bindBuffer(gl.ARRAY_BUFFER, gl_nbuf);
300 gl.bufferSubData(gl.ARRAY_BUFFER, 0, gl_normal);
301 gl.vertexAttribPointer(gl_nloc, 3, gl.FLOAT, false, 0, 0);
302 gl.enableVertexAttribArray(gl_nloc);
303 }
305 if(gl_use_color) {
306 gl.bindBuffer(gl.ARRAY_BUFFER, gl_cbuf);
307 gl.bufferSubData(gl.ARRAY_BUFFER, 0, gl_color);
308 gl.vertexAttribPointer(gl_cloc, 4, gl.FLOAT, true, 0, 0);
309 gl.enableVertexAttribArray(gl_cloc);
310 }
312 if(gl_use_texcoord) {
313 gl.bindBuffer(gl.ARRAY_BUFFER, gl_tbuf);
314 gl.bufferSubData(gl.ARRAY_BUFFER, 0, gl_texcoord);
315 gl.vertexAttribPointer(gl_tloc, 4, gl.FLOAT, false, 0, 0);
316 gl.enableVertexAttribArray(gl_tloc);
317 }
319 if(gl_aloc != -1) {
320 gl.bindBuffer(gl.ARRAY_BUFFER, gl_abuf);
321 gl.bufferSubData(gl.ARRAY_BUFFER, 0, gl_attrib);
322 gl.vertexAttribPointer(gl_aloc, 4, gl.FLOAT, false, 0, 0);
323 gl.enableVertexAttribArray(gl_aloc);
324 }
326 gl.drawArrays(gles_prim, 0, gl_nverts);
328 gl.disableVertexAttribArray(gl_vloc);
329 if(gl_use_normal) {
330 gl.disableVertexAttribArray(gl_nloc);
331 }
332 if(gl_use_color) {
333 gl.disableVertexAttribArray(gl_cloc);
334 }
335 if(gl_use_texcoord) {
336 gl.disableVertexAttribArray(gl_tloc);
337 }
338 if(gl_aloc != -1) {
339 gl.disableVertexAttribArray(gl_aloc);
340 }
341 }
344 function glVertex2f(x, y)
345 {
346 glVertex3f(x, y, 0.0);
347 }
349 function glVertex3f(x, y, z)
350 {
351 var i = 0;
352 var vidx = gl_nverts * 4;
353 var nidx = gl_nverts * 3;
355 if(gl_prim == GL_QUADS && gl_vert_calls % 4 == 3) {
356 var v = vidx - 12;
357 var n = nidx - 9;
359 for(i=0; i<4; i++) {
360 if(gl_aloc != -1) {
361 gl_attrib[vidx] = gl_attrib[v];
362 }
363 if(gl_use_color) {
364 gl_color[vidx] = gl_color[v];
365 }
366 if(gl_use_texcoord) {
367 gl_texcoord[vidx] = gl_texcoord[v];
368 }
369 gl_vertex[vidx++] = gl_vertex[v++];
371 if(gl_use_normal && i < 3) {
372 gl_normal[nidx++] = gl_normal[n++];
373 }
374 }
375 v += 4;
376 n += 3;
377 for(i=0; i<4; i++) {
378 if(gl_aloc != -1) {
379 gl_attrib[vidx] = gl_attrib[v];
380 }
381 if(gl_use_color) {
382 gl_color[vidx] = gl_color[v];
383 }
384 if(gl_use_texcoord) {
385 gl_texcoord[vidx] = gl_texcoord[v];
386 }
387 gl_vertex[vidx++] = gl_vertex[v++];
389 if(gl_use_normal && i < 3) {
390 gl_normal[nidx++] = gl_normal[n++];
391 }
392 }
394 gl_nverts += 2;
395 }
397 gl_vertex[vidx] = x;
398 gl_vertex[vidx + 1] = y;
399 gl_vertex[vidx + 2] = z;
400 gl_vertex[vidx + 3] = 1.0;
402 if(gl_use_color) {
403 gl_color[vidx] = gl_curr_color[0];
404 gl_color[vidx + 1] = gl_curr_color[1];
405 gl_color[vidx + 2] = gl_curr_color[2];
406 gl_color[vidx + 3] = gl_curr_color[3];
407 }
409 if(gl_use_normal) {
410 gl_normal[nidx] = gl_curr_normal[0];
411 gl_normal[nidx + 1] = gl_curr_normal[1];
412 gl_normal[nidx + 2] = gl_curr_normal[2];
413 }
415 if(gl_use_texcoord) {
416 gl_texcoord[vidx] = gl_curr_texcoord[0];
417 gl_texcoord[vidx + 1] = gl_curr_texcoord[1];
418 gl_texcoord[vidx + 2] = 0.0;
419 gl_texcoord[vidx + 3] = 1.0;
420 }
422 if(gl_aloc != -1) {
423 gl_attrib[vidx] = gl_curr_attrib[0];
424 gl_attrib[vidx + 1] = gl_curr_attrib[1];
425 gl_attrib[vidx + 2] = gl_curr_attrib[2];
426 gl_attrib[vidx + 3] = gl_curr_attrib[3];
427 }
429 gl_vert_calls++;
430 gl_nverts++;
432 var buffer_full;
433 if(gl_prim == GL_QUADS) {
434 /* leave space for 6 more worst-case and don't allow flushes mid-quad */
435 buffer_full = gl_nverts >= GL_MAX_VERTS - 6 && gl_vert_calls % 4 == 0;
436 } else {
437 buffer_full = gl_nverts >= GL_MAX_VERTS - gl_prim;
438 }
440 if(buffer_full) {
441 gl_draw_immediate();
442 glBegin(gl_prim); /* reset everything */
443 }
444 }
446 function glNormal3f(x, y, z)
447 {
448 gl_curr_normal[0] = x;
449 gl_curr_normal[1] = y;
450 gl_curr_normal[3] = z;
451 }
453 function glColor3f(r, g, b)
454 {
455 gl_curr_color[0] = r;
456 gl_curr_color[1] = g;
457 gl_curr_color[2] = b;
458 gl_curr_color[3] = 1.0;
459 }
461 function glColor4f(r, g, b, a)
462 {
463 gl_curr_color[0] = r;
464 gl_curr_color[1] = g;
465 gl_curr_color[2] = b;
466 gl_curr_color[3] = a;
467 }
469 function glTexCoord1f(s)
470 {
471 gl_curr_texcoord[0] = s;
472 gl_curr_texcoord[1] = 0.0;
473 }
475 function glTexCoord2f(s, t)
476 {
477 gl_curr_texcoord[0] = s;
478 gl_curr_texcoord[1] = t;
479 }
481 function glVertexAttrib2f(loc, x, y)
482 {
483 gl_aloc = loc;
484 gl_curr_attrib[0] = x;
485 gl_curr_attrib[1] = y;
486 gl_curr_attrib[2] = 0.0;
487 gl_curr_attrib[3] = 1.0;
488 }
490 function glVertexAttrib3f(loc, x, y, z)
491 {
492 gl_aloc = loc;
493 gl_curr_attrib[0] = x;
494 gl_curr_attrib[1] = y;
495 gl_curr_attrib[2] = z;
496 gl_curr_attrib[3] = 1.0;
497 }
499 function glVertexAttrib4f(loc, x, y, z, w)
500 {
501 gl_aloc = loc;
502 gl_curr_attrib[0] = x;
503 gl_curr_attrib[1] = y;
504 gl_curr_attrib[2] = z;
505 gl_curr_attrib[3] = w;
506 }