Only the latest loaded texture remains visible...

OK, here’s the deal. After we have fought a battle of thousand courses we finally got those snicky textures on our screen. Now, happy as we are, we noticed that we only can use the texture that has been loaded most recently.

So, basically, as soon as we load an other texture (we even tried to simply make a copy of the first byte[] so we know we got nice numbers in there), the previous seems to got forgotten and only a white square appears. The element drawn after we call binding for the other (the most recently loaded) texture, however, works perfectly.

We suspect we missed something important. The approach we use is pretty much this.


int[] fuckTures = new int[2];
this.gl.glGenTextures (2, fuckTures);
this.gl.glBindTexture (GL.GL_TEXTURE_2D, fuckTures[0]);
this.gl.glTexImage2D (GL.GL_TEXTURE_2D, 0, GL.GL_RGB, image.getWidth (), image.getHeight (), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, dataToDraw1);
this.gl.glBindTexture (GL.GL_TEXTURE_2D, fuckTures[1]);
this.gl.glTexImage2D (GL.GL_TEXTURE_2D, 0, GL.GL_RGB, image.getWidth (), image.getHeight (), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, dataToDraw2);

As we said, we ensure that the dataToDraw1 and dataToDraw2 have the same values (but are, of course, two different objects).

Here’s a more specific tricky part for us. When we’ve skipped the second call to glBindTexture (hence simply executed


[i]
glBindTexture (..., id=1)
glTexImage2D (..., byte[] b1)
glTexImage2D (..., byte[] b2)
[/i]

we still, to our grate surprise, get the second texture to be drawn (as we call on it by the ID=2 in display) while the first texture (the only texture we actually glBind-ed in init) gets somehow forgotten! What did we do stupidly this time? ???

glBindTexture() works like a ‘select’. All modifications
and rendering work with the current bound (selected)
texture. If you need to work on another texture, you must
bind (select) the new texture.

.rex

This is exactly what we do. Both in init (right before each glTextImage2D) and display (before drawing the actual squares). The issue is that the firstly loaded texture seems not to be there in the memory (or at least refuses to come out on the screen). This is the first problem.

The second problem is an entirely different question. The code we presented in the second part shows something even more strange (i guess that’s why you assumed we call binding too few times). The thing is that after we “selected” the first ID and load both textures (which is incorrect, i know) it still seems that the second texture is remembered as ID=2 (even if we didn’t actually bounded it with that ID) while the first one gets forgotten.

Hehe, i’ve found the error myself (well, actually somebody else pointed it out for me). In case any poor soul gets into the same problems - here’s what did my day sparkle.

For some reason, on my system, i needed to put:


this.gl.glTexParameteri (GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT);
this.gl.glTexParameteri (GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT);
this.gl.glTexParameteri (GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
this.gl.glTexParameteri (GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
this.gl.glTexEnvf (GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_DECAL);

RIGHT BETWEEN the call to glBindTexture and glTexImage2D. I can’t explain why and francly i’ve head it with those stupid textures but the fact remains that once i switched to this, it worked out fine.

If you execute the above code, texture 1 will contain the data from b2 and texture 2 will contain nothing. So it not that surprising that you never see the data from b1 on screen.