PDA

View Full Version : Creating textures from data



zed
02-21-2011, 11:57 PM
Heres the (concise) version of what Im doing


var b = new ArrayBuffer(32*32*4);
var v1 = new Uint8Array(b);
for ( var i=0; i<32*32*4; i++ )
v1[i] = i%255;

gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 32, 32, 0, gl.RGBA, gl.UNSIGNED_BYTE, v1);

but nothings showing up when I draw the texture its just black, no errors

Im not 100% sure if Im doing the arraybuffer thing correct,
heres the spec
http://www.khronos.org/registry/typedar ... cs/latest/ (http://www.khronos.org/registry/typedarray/specs/latest/)

Anyone have any ideas whats going wrong,
cheers zed

SteveBaker
02-22-2011, 05:29 PM
I did this (based on your code - but Vulcan-mind-melded with some of mine):



var b = new ArrayBuffer(32*32*4);
var v1 = new Uint8Array(b);

for ( var i=0; i<32*32*4; i++ )
v1[i] = i%255;
var texture = gl.createTexture () ;
gl.bindTexture ( gl.TEXTURE_2D, texture ) ;
gl.texParameteri ( gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER,
gl.LINEAR_MIPMAP_LINEAR ) ;
gl.texParameteri ( gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR ) ;
gl.texParameteri ( gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT ) ;
gl.texParameteri ( gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT ) ;
gl.texImage2D (gl.TEXTURE_2D, 0, gl.RGBA, 32, 32, 0, gl.RGBA,
gl.UNSIGNED_BYTE, v1);
gl.generateMipmap ( gl.TEXTURE_2D ) ;


...and it worked OK inside my application. I'd start with that. If it doesn't work - then there is something wrong elsewhere in your application. If it does work and then pull out lines one at a time until it breaks - then you'll know what critical part you were missing!

-- Steve

zed
02-22-2011, 06:05 PM
cheers again steve, seems like we're the only ppl on this forum :)

Im pretty sure thats pretty much what Ive got already, Ill check fully later though.
Well at least I know thats the correct way to set up the ArrayBuffer

zed
02-22-2011, 06:27 PM
hmm embarrassing, I was doing
image.onload = function() { updateTexturePixels( data ); }

of course the function wasnt getting called, since the image wasnt being loaded from somewhere

SteveBaker
02-24-2011, 02:46 PM
Yeah - I could easily see how that would happen! :-)

zed
02-24-2011, 04:03 PM
One of those imfamous copy/paste bugs. Still its not gonna stop me from copying + pasting code, cause I rarely make them.
This would of been easy to find if there was a decent debugger for javascript that lets u step easily through code. Sure theres inbuilt debuggers in the browsers (chrome is a bit better than firebug IMHO) but compared to a C++ IDE theyre a pain, I find its actually easier to chuck a few log( "blah" ); through the code to debug.
RANT - though if javascript had strong typed variables etc I would make 10x fewer mistakes, coding in javascript's driving me nuts. :(

SteveBaker
02-24-2011, 10:21 PM
Yeah...I agree. I hate JavaScript with a passion! It's horrible to not spot something as stupid as a typo in a variable name until the line of code is actually executed! Don't we have computers that can check that stuff for us?! :-)