Until webgl was more advanced, and I’m glad I did. The new Google Chrome Beta and Firefox Beta 7 are both amazingly good for webgl, and they now both support arraybuffers over xhr. Thanks to Alan Chaney for help with this.
function getDisplayable(disp) {
disp.xhr.onreadystatechange = function() {
if (disp.xhr.readyState == 4 && disp.xhr.status == 200
&& disp.ready!=true) {
if (disp.xhr.responseType=="arraybuffer")
{disp.data=disp.xhr.response;}
else if (disp.xhr.mozResponseArrayBuffer != null)
{disp.data=disp.xhr.mozResponseArrayBuffer;}
else if (disp.xhr.responseText != null ) {
var data = new String(disp.xhr.responseText);
var ary = new Array(data.length);
for (var i = 0; i <data.length; i++)
{ary[i] = data.charCodeAt(i) & 0xff;}
var uint8ay = new Uint8Array(ary);
disp.data = uint8ay.buffer;
}
disp.ready=true;
}}
disp.xhr.open("GET", disp.url, true);
if(disp.xhr.hasOwnProperty("responseType"))
{disp.xhr.responseType="arraybuffer";}
else
{disp.xhr.overrideMimeType('text/plain; charset=x-user-defined');}
disp.xhr.send();
}
Hi! Great post.
Can you fill me in on how exactly you got the returned binary data into a webGL buffer?
I can get the buffer returned correctly when using xhr.response in Chrome, but I get errors when I try to use it as a GL buffer.
TNKS!
disp.buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, disp.buffer);
gl.bufferData(gl.ARRAY_BUFFER, disp.data, gl.STATIC_DRAW);
And then to draw
gl.vertexAttribPointer(shader.variables['position'],3,gl.UNSIGNED_BYTE,gl.FALSE,6,0); and so on.
Hey,
Thanks for the tip, I made a jQuery patch in order to use it more easily.
http://blog.vjeux.com/2011/javascript/jquery-binary-ajax.html
Also you may want to take a look at a DataView wrapper I made to read binary files.
http://blog.vjeux.com/2011/javascript/jdataview-read-binary-file.html
Pingback: Javascript – jQuery Binary Ajax | Vjeux