glTexImage2D not loading all textures (JOGL)

Hi! I’m currently trying to port a C# application to Java, and in this app is used OpenGL.
In the C# app was used OpenTK, while in the Java port I chose JOGL.

The problem comes when the program has to load textures. Java has no unsigned byte types, and the texture has to be loaded from a byte[] array.
This is the piece of code which is causing troubles:


            for (int i = 0; i < model.model.mdlData[0].material.material.length; i++) {
                BMD0.Model.ModelData.Material.MatDef mat = (BMD0.Model.ModelData.Material.MatDef) model.model.mdlData[0].material.material[i];
                ImageData tmp_tex = Nsbtx.getTexture(tex, mat.texID, mat.palID).getImageData();
                gl.glBindTexture(GL2.GL_TEXTURE_2D, texturesID.get(i));
                ByteBuffer tmp_tex_data = ByteBuffer.allocate(tmp_tex.height * tmp_tex.width * 4);
                PaletteData pal = tmp_tex.palette;
                for (int h = 0; h < tmp_tex.height; h++) {
                    for (int w = 0; w < tmp_tex.width; w++) {
                        tmp_tex_data.put((byte) (pal.getRGB(tmp_tex.getPixel(w, h)).red & 0xff));
                        tmp_tex_data.put((byte) (pal.getRGB(tmp_tex.getPixel(w, h)).green & 0xff));
                        tmp_tex_data.put((byte) (pal.getRGB(tmp_tex.getPixel(w, h)).blue & 0xff));
                        tmp_tex_data.put((byte) (tmp_tex.getAlpha(w, h) & 0xff));
                    }
                }
                tmp_tex_data.flip();
                gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGBA, tmp_tex.width, tmp_tex.height, 0, GL2.GL_RGBA, GL2.GL_UNSIGNED_BYTE, tmp_tex_data);
                texturesGL.put(i, texturesID.get(i));
            }

The texture is loaded from an SWT’s ImageData class, and tranformed into a RGBA array. In the C# version textures are loaded flawlessly, in the Java one some texture are loaded, some other no (or the colors are not correct). In fact, I’ve debugged the application and found that when I convert RGBA integers to byte the signs are not correct and so the number…

Here are a screenshot showing messed up values during debug and a screenshot showing difference in texture rendering respectively between Java and C#::
[ATTACH=CONFIG]1237[/ATTACH]
[ATTACH=CONFIG]1238[/ATTACH]

How can I solve this? I’m not an expert, this is one of my first program with opengl, I would like to understand how this process works…

EDIT: Here is the link showing the full image of the debugging (this one has been compressed):
h ttp://i.imgur.com/Q5Jc0Gn.png

I’m sorry, I think the error isn’t glTexImage not loading…

The problem is multiple textures! I manually tried to change numbers when calling glBindTexture in the display() method, and in fact it loads textures (although wrong obviously, given I used a fixed number)…

So my question has changed: how can I handle correctly multiple textures? I saw on the internet many examples using JOGL Texture object, but I don’t want to use it… How can I achieve this using only “pure” opengl?

glActiveTexture() and glBindTexture(). If you’re using shaders, use glUniform1i() to associate texture units with sampler uniforms.

I’m not using shaders, but something changed when I introduced glActiveTexture() (it’s still not working though…)

But I think I don’t need to use glActiveTexture(), because from what I understood it has to be used when applying multiple texture to a SINGLE object.

This isn’t my case: I have multiple object, everyone with a different texture (selected from 4 available).

Here is C# code:


                for (int i = 0; i < model.model.mdlData[0].polygon.header.num_objs; i++)
                {
                    sBMD0.Model.ModelData.Polygon.Display poly = model.model.mdlData[0].polygon.display[i];
                    if (poly.materialID >= model.model.mdlData[0].material.material.Length)
                        poly.materialID = 0;
                    sBMD0.Model.ModelData.Material.MatDef mat = (sBMD0.Model.ModelData.Material.MatDef)model.model.mdlData[0].material.material[poly.materialID];

                    sBTX0.Texture.TextInfo texInfo = (sBTX0.Texture.TextInfo)tex.texture.texInfo.infoBlock.infoData[mat.texID];

                    GL.BindTexture(TextureTarget.Texture2D, poly.materialAssoc);
                    GL.MatrixMode(MatrixMode.Texture);
                    GL.LoadIdentity();
                    ....
                }

So in Java:



            for (int i = 0; i < model.model.mdlData[0].polygon.header.num_objs; i++) {
                BMD0.Model.ModelData.Polygon.Display poly = model.model.mdlData[0].polygon.display[i];
                if (poly.materialID >= model.model.mdlData[0].material.material.length)
                    poly.materialID = 0;
                BMD0.Model.ModelData.Material.MatDef mat = (BMD0.Model.ModelData.Material.MatDef) model.model.mdlData[0].material.material[poly.materialID];
                BTX0.Texture.TextInfo texInfo = (BTX0.Texture.TextInfo) tex.texture.texInfo.infoBlock.infoData[mat.texID];

                gl.glBindTexture(GL2.GL_TEXTURE_2D, poly.materialAssoc);
                gl.glEnable(GL2.GL_TEXTURE_2D);
                gl.glMatrixMode(GL2.GL_TEXTURE);
                gl.glLoadIdentity();
                gl.glPushMatrix();
                ....
           }

It seems glBindTexture doesn’t work…

Hey I’ve solved!!!

It was such a stupid error!!!

When loading textures, I forgot glTexParameteri!


            for (int i = 0; i < model.model.mdlData[0].material.material.length; i++) {
                BMD0.Model.ModelData.Material.MatDef mat = (BMD0.Model.ModelData.Material.MatDef) model.model.mdlData[0].material.material[i];
                ImageData tmp_tex = Nsbtx.getTexture(tex, mat.texID, mat.palID).getImageData();
                gl.glBindTexture(GL2.GL_TEXTURE_2D, texturesID.get(i));
                gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_NEAREST);
                gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_NEAREST);
                gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S, GL2.GL_REPEAT);
                gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T, GL2.GL_REPEAT);
                byte[] texture = new byte[tmp_tex.height * tmp_tex.width * 4];
                PaletteData pal = tmp_tex.palette;
                int index = 0;
                for (int h = 0; h < tmp_tex.height; h++) {
                    for (int w = 0; w < tmp_tex.width; w++) {
                        texture[index] = (byte) pal.getRGB(tmp_tex.getPixel(w, h)).red;
                        texture[index + 1] = (byte) pal.getRGB(tmp_tex.getPixel(w, h)).green;
                        texture[index + 2] = (byte) pal.getRGB(tmp_tex.getPixel(w, h)).blue;
                        texture[index + 3] = (byte) tmp_tex.getAlpha(w, h);
                        index += 4;
                    }
                }
                gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGBA, tmp_tex.width, tmp_tex.height, 0, GL2.GL_RGBA, GL2.GL_UNSIGNED_BYTE, Buffers.newDirectByteBuffer(texture));
                texturesGL.put(i, texturesID.get(i));
            }