webgl-tools

view sanegl.js @ 6:3c42ff114e49

Changed a single sentence in the README file
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 18 Jun 2011 07:21:48 +0300
parents 9eb4c37ce415
children b215a8d38818
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;
38 var gl_vertex, gl_normal, gl_color, gl_texcoord;
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_vloc, gl_nloc, gl_cloc, gl_tloc;
44 var gl_use_normal, gl_use_color, gl_use_texcoord;
46 function glMatrixMode(mode)
47 {
48 gl_mmode = mode;
49 }
51 function glPushMatrix()
52 {
53 var mtop = gl_mat[gl_mmode].length - 1;
55 gl_mat[gl_mmode].push(new Float32Array(16));
56 m4_copy(gl_mat[gl_mmode][mtop + 1], gl_mat[gl_mmode][mtop]);
57 }
59 function glPopMatrix()
60 {
61 if(gl_mat[gl_mmode].length <= 1) {
62 alert('glPopMatrix underflow');
63 }
64 gl_mat[gl_mmode].pop();
65 }
67 function glLoadIdentity()
68 {
69 var mtop = gl_mat[gl_mmode].length - 1;
70 m4_identity(gl_mat[gl_mmode][mtop]);
71 }
73 function glLoadMatrixf(mat)
74 {
75 var mtop = gl_mat[gl_mmode].length - 1;
76 m4_copy(gl_mat[gl_mmode][mtop], mat);
77 }
79 function glMultMatrixf(mat)
80 {
81 var mtop = gl_mat[gl_mmode].length - 1;
82 m4_mul(gl_mat[gl_mmode][mtop], gl_mat[gl_mmode][mtop], mat);
83 }
85 function glTranslatef(x, y, z)
86 {
87 var xform = new Float32Array(16);
88 m4_translation(xform, x, y, z);
89 glMultMatrixf(xform);
90 }
92 function glRotatef(angle, x, y, z)
93 {
94 var angle_rads = Math.PI * angle / 180.0;
95 var xform = new Float32Array(16);
96 m4_rotation(xform, angle_rads, x, y, z);
97 glMultMatrixf(xform);
98 }
100 function glScalef(x, y, z)
101 {
102 var xform = new Float32Array(16);
103 m4_scale(xform, x, y, z);
104 glMultMatrixf(xform);
105 }
107 function glOrtho(left, right, bottom, top, near, far)
108 {
109 var dx = right - left;
110 var dy = top - bottom;
111 var dz = far - near;
113 var tx = -(right + left) / dx;
114 var ty = -(top + bottom) / dy;
115 var tz = -(far + near) / dz;
117 var sx = 2.0 / dx;
118 var sy = 2.0 / dy;
119 var sz = -2.0 / dz;
121 var xform = new Float32Array(16);
122 xform[0] = sx;
123 xform[5] = sy;
124 xform[10] = sz;
125 xform[12] = tx;
126 xform[13] = ty;
127 xform[14] = tz;
128 xform[15] = 1.0;
129 xform[1] = xform[2] = xform[3] = xform[4] = xform[6] = xform[7] = xform[8] = xform[9] = 0.0;
131 glMultMatrixf(xform);
132 }
134 function glFrustum(left, right, bottom, top, near, far)
135 {
136 var dx = right - left;
137 var dy = top - bottom;
138 var dz = far - near;
140 var a = (right + left) / dx;
141 var b = (top + bottom) / dy;
142 var c = -(far + near) / dz;
143 var d = -2.0 * far * near / dz;
145 var xform = new Float32Array(16);
146 xform[0] = 2.0 * near / dx;
147 xform[5] = 2.0 * near / dy;
148 xform[8] = a;
149 xform[9] = b;
150 xform[10] = c;
151 xform[11] = -1.0;
152 xform[14] = d;
153 xform[1] = xform[2] = xform[3] = xform[4] = xform[6] = xform[7] = xform[12] = xform[13] = xform[15] = 0.0;
155 glMultMatrixf(xform);
156 }
158 function gluPerspective(vfov, aspect, near, far)
159 {
160 var x = near * Math.tan(vfov / 2.0);
161 glFrustum(-aspect * x, aspect * x, -x, x, near, far);
162 }
164 function gl_apply_xform(prog)
165 {
166 var mvtop = gl_mat[GL_MODELVIEW].length - 1;
167 var ptop = gl_mat[GL_PROJECTION].length - 1;
168 var ttop = gl_mat[GL_TEXTURE].length - 1;
170 var loc = gl.getUniformLocation(prog, "mvmat");
171 if(loc != -1) {
172 gl.uniformMatrix4fv(loc, false, gl_mat[GL_MODELVIEW][mvtop]);
173 }
175 loc = gl.getUniformLocation(prog, "projmat");
176 if(loc != -1) {
177 gl.uniformMatrix4fv(loc, false, gl_mat[GL_PROJECTION][ptop]);
178 }
180 loc = gl.getUniformLocation(prog, "texmat");
181 if(loc != -1) {
182 gl.uniformMatrix4fv(loc, false, gl_mat[GL_TEXTURE][ttop]);
183 }
185 loc = gl.getUniformLocation(prog, "normmat");
186 if(loc != -1) {
187 var normmat = new Float32Array(16);
188 m4_copy(normmat, gl_mat[GL_MODELVIEW][mvtop]);
189 normmat[3] = normmat[7] = normmat[11] = normmat[12] = normmat[13] = normmat[14] = 0.0;
190 normmat[15] = 1.0;
191 gl.uniformMatrix4fv(loc, false, normmat);
192 }
193 }
195 function glBegin(prim)
196 {
197 gl_vertex = new Float32Array(GL_MAX_VERTS * 4);
198 gl_normal = new Float32Array(GL_MAX_VERTS * 3);
199 gl_color = new Float32Array(GL_MAX_VERTS * 4);
200 gl_texcoord = new Float32Array(GL_MAX_VERTS * 4);
202 if(gl_vbuf == null) {
203 gl_vbuf = gl.createBuffer();
204 gl.bindBuffer(gl.ARRAY_BUFFER, gl_vbuf);
205 gl.bufferData(gl.ARRAY_BUFFER, GL_MAX_VERTS * 4 * Float32Array.BYTES_PER_ELEMENT, gl.STREAM_DRAW);
206 logmsg("vertex buffer size: " + gl.getBufferParameter(gl.ARRAY_BUFFER, gl.BUFFER_SIZE) + "\n");
208 gl_nbuf = gl.createBuffer();
209 gl.bindBuffer(gl.ARRAY_BUFFER, gl_nbuf);
210 gl.bufferData(gl.ARRAY_BUFFER, GL_MAX_VERTS * 3 * Float32Array.BYTES_PER_ELEMENT, gl.STREAM_DRAW);
211 logmsg("normal buffer size: " + gl.getBufferParameter(gl.ARRAY_BUFFER, gl.BUFFER_SIZE) + "\n");
213 gl_cbuf = gl.createBuffer();
214 gl.bindBuffer(gl.ARRAY_BUFFER, gl_cbuf);
215 gl.bufferData(gl.ARRAY_BUFFER, GL_MAX_VERTS * 4 * Float32Array.BYTES_PER_ELEMENT, gl.STREAM_DRAW);
216 logmsg("color buffer size: " + gl.getBufferParameter(gl.ARRAY_BUFFER, gl.BUFFER_SIZE) + "\n");
218 gl_tbuf = gl.createBuffer();
219 gl.bindBuffer(gl.ARRAY_BUFFER, gl_tbuf);
220 gl.bufferData(gl.ARRAY_BUFFER, GL_MAX_VERTS * 4 * Float32Array.BYTES_PER_ELEMENT, gl.STREAM_DRAW);
221 logmsg("texcoord buffer size: " + gl.getBufferParameter(gl.ARRAY_BUFFER, gl.BUFFER_SIZE) + "\n");
222 }
224 gl_prim = prim;
225 gl_nverts = gl_vert_calls = 0;
227 gl_prog = gl.getParameter(gl.CURRENT_PROGRAM);
228 if(gl_prog != null) {
229 gl_apply_xform(gl_prog);
231 gl_vloc = gl.getAttribLocation(gl_prog, "attr_vertex");
232 gl_nloc = gl.getAttribLocation(gl_prog, "attr_normal");
233 gl_cloc = gl.getAttribLocation(gl_prog, "attr_color");
234 gl_tloc = gl.getAttribLocation(gl_prog, "attr_texcoord");
235 } else {
236 logmsg("no program currently bound\n");
237 gl_vloc = gl_nloc = gl_cloc = gl_tloc = -1;
238 }
240 if(gl_vloc == -1) {
241 logmsg("attr_vertex not found in currently bound program: " + prog + "\n");
242 }
244 gl_use_normal = gl_nloc != -1;
245 gl_use_color = gl_cloc != -1;
246 gl_use_texcoord = gl_tloc != -1;
247 }
249 function glEnd()
250 {
251 if(gl_nverts > 0) {
252 gl_draw_immediate();
253 }
254 }
256 function gl_draw_immediate()
257 {
258 var gles_prim;
260 if(gl_vloc == -1) {
261 return;
262 }
264 switch(gl_prim) {
265 case GL_POINTS:
266 gles_prim = gl.POINTS;
267 break;
268 case GL_LINES:
269 gles_prim = gl.LINES;
270 break;
271 case GL_TRIANGLES:
272 case GL_QUADS:
273 gles_prim = gl.TRIANGLES;
274 break;
275 default:
276 }
278 gl.bindBuffer(gl.ARRAY_BUFFER, gl_vbuf);
279 gl.bufferSubData(gl.ARRAY_BUFFER, 0, gl_vertex);
280 gl.vertexAttribPointer(gl_vloc, 4, gl.FLOAT, false, 0, 0);
281 gl.enableVertexAttribArray(gl_vloc);
283 if(gl_use_normal) {
284 gl.bindBuffer(gl.ARRAY_BUFFER, gl_nbuf);
285 gl.bufferSubData(gl.ARRAY_BUFFER, 0, gl_normal);
286 gl.vertexAttribPointer(gl_nloc, 3, gl.FLOAT, false, 0, 0);
287 gl.enableVertexAttribArray(gl_nloc);
288 }
290 if(gl_use_color) {
291 gl.bindBuffer(gl.ARRAY_BUFFER, gl_cbuf);
292 gl.bufferSubData(gl.ARRAY_BUFFER, 0, gl_color);
293 gl.vertexAttribPointer(gl_cloc, 4, gl.FLOAT, true, 0, 0);
294 gl.enableVertexAttribArray(gl_cloc);
295 }
297 if(gl_use_texcoord) {
298 gl.bindBuffer(gl.ARRAY_BUFFER, gl_tbuf);
299 gl.bufferSubData(gl.ARRAY_BUFFER, 0, gl_texcoord);
300 gl.vertexAttribPointer(gl_tloc, 4, gl.FLOAT, false, 0, 0);
301 gl.enableVertexAttribArray(gl_tloc);
302 }
304 gl.drawArrays(gles_prim, 0, gl_nverts);
306 gl.disableVertexAttribArray(gl_vloc);
307 if(gl_use_normal) {
308 gl.disableVertexAttribArray(gl_nloc);
309 }
310 if(gl_use_color) {
311 gl.disableVertexAttribArray(gl_cloc);
312 }
313 if(gl_use_texcoord) {
314 gl.disableVertexAttribArray(gl_tloc);
315 }
316 }
319 function glVertex2f(x, y)
320 {
321 glVertex3f(x, y, 0.0);
322 }
324 function glVertex3f(x, y, z)
325 {
326 var i = 0;
327 var vidx = gl_nverts * 4;
328 var nidx = gl_nverts * 3;
330 if(gl_prim == GL_QUADS && gl_vert_calls % 4 == 3) {
331 var v = vidx - 12;
332 var n = nidx - 9;
334 for(i=0; i<4; i++) {
335 if(gl_use_color) {
336 gl_color[vidx] = gl_color[v];
337 }
338 if(gl_use_texcoord) {
339 gl_texcoord[vidx] = gl_texcoord[v];
340 }
341 gl_vertex[vidx++] = gl_vertex[v++];
343 if(gl_use_normal && i < 3) {
344 gl_normal[nidx++] = gl_normal[n++];
345 }
346 }
347 v += 4;
348 n += 3;
349 for(i=0; i<4; i++) {
350 if(gl_use_color) {
351 gl_color[vidx] = gl_color[v];
352 }
353 if(gl_use_texcoord) {
354 gl_texcoord[vidx] = gl_texcoord[v];
355 }
356 gl_vertex[vidx++] = gl_vertex[v++];
358 if(gl_use_normal && i < 3) {
359 gl_normal[nidx++] = gl_normal[n++];
360 }
361 }
363 gl_nverts += 2;
364 }
366 gl_vertex[vidx] = x;
367 gl_vertex[vidx + 1] = y;
368 gl_vertex[vidx + 2] = z;
369 gl_vertex[vidx + 3] = 1.0;
371 if(gl_use_color) {
372 gl_color[vidx] = gl_curr_color[0];
373 gl_color[vidx + 1] = gl_curr_color[1];
374 gl_color[vidx + 2] = gl_curr_color[2];
375 gl_color[vidx + 3] = gl_curr_color[3];
376 }
378 if(gl_use_normal) {
379 gl_normal[nidx] = gl_curr_normal[0];
380 gl_normal[nidx + 1] = gl_curr_normal[1];
381 gl_normal[nidx + 2] = gl_curr_normal[2];
382 }
384 if(gl_use_texcoord) {
385 gl_texcoord[vidx] = gl_curr_texcoord[0];
386 gl_texcoord[vidx + 1] = gl_curr_texcoord[1];
387 gl_texcoord[vidx + 2] = gl_texcoord[vidx + 3] = 0.0;
388 }
390 gl_vert_calls++;
391 gl_nverts++;
393 var buffer_full;
394 if(gl_prim == GL_QUADS) {
395 /* leave space for 6 more worst-case and don't allow flushes mid-quad */
396 buffer_full = gl_nverts >= GL_MAX_VERTS - 6 && gl_vert_calls % 4 == 0;
397 } else {
398 buffer_full = gl_nverts >= GL_MAX_VERTS - gl_prim;
399 }
401 if(buffer_full) {
402 gl_draw_immediate();
403 glBegin(gl_prim); /* reset everything */
404 }
405 }
407 function glNormal3f(x, y, z)
408 {
409 gl_curr_normal[0] = x;
410 gl_curr_normal[1] = y;
411 gl_curr_normal[3] = z;
412 }
414 function glColor3f(r, g, b)
415 {
416 gl_curr_color[0] = r;
417 gl_curr_color[1] = g;
418 gl_curr_color[2] = b;
419 gl_curr_color[3] = 1.0;
420 }
422 function glColor4f(r, g, b, a)
423 {
424 gl_curr_color[0] = r;
425 gl_curr_color[1] = g;
426 gl_curr_color[2] = b;
427 gl_curr_color[3] = a;
428 }
430 function glTexCoord1f(s)
431 {
432 gl_curr_texcoord[0] = s;
433 gl_curr_texcoord[1] = 0.0;
434 }
436 function glTexCoord2f(s, t)
437 {
438 gl_curr_texcoord[0] = s;
439 gl_curr_texcoord[1] = t;
440 }