View Full Version : problem loading textures with javascript (beginner)

06-19-2012, 03:59 PM
Hello people, i am just a beginner with webgl, and i have an issue loading textures, hope you can help me!!!

I am using chrome to test and debug the webgl page.

The point is, using the function initTexture with this code :

function initTexture() {
texture[0] = gl.createTexture();
image[0] = new Image();
image[0].onload = function () { createTextureFromImage(image[0], texture[0]) }
image[0].src = "images/image0.jpg";

texture[9] = gl.createTexture();
image[9] = new Image();
image[9].onload = function () { createTextureFromImage(image[9], texture[9]) }
image[9].src = "images/image9.jpg";

i am able to load up to 9 different images and apply them to 9 different polygons, for example, 9 different cubes. But the point is that this code only works with the hardcoded numbers. If i use a code like this :

function initTexture() {
for (var texvalue= 0; texvalue < 10 ; texvalue++) {
texture[texvalue] = gl.createTexture();
image[texvalue] = new Image();
image[texvalue].onload = function () { createTextureFromImage(image[texvalue], texture[texvalue]) }
image[texvalue].src = "images/image" + texvalue.toString() + ".jpg"; }

function does not return any error, but my cubes does not appear on the screen, but the hardcoded way, it does!!!!!

I hope someone can help me, or give me a clue of why this is happening. I am trying to write a more general code, but hardcoding does not help me with this, i want to change the textures any time i want, store them in a javascript array, and use them.

Thank you to all!!