So I decided to wait

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();
}
About these ads
This entry was posted in Uncategorized and tagged , , , , , , . Bookmark the permalink.

4 Responses to So I decided to wait

  1. Ram says:

    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!

  2. 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.

  3. Vjeux says:

    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

  4. Pingback: Javascript – jQuery Binary Ajax | Vjeux

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s