Texture Loading Problem

Well I am quite nex to JOGL, did some OpenGL for C++ before, but I now porting my current project and run into a few problems loading textures.

Loading a simple texture is no problem but when I load different textures it really gets screwed up. Probably has something to do with glGenTextures method. But I have seen that glGenTextures(int,int) is not implemented in JOGL and only glGenTextures(int,[]int). Don’t know if makes any difference but … well it doesn’t work.
I already tried to make:
public static int[] g_Textures and make this my reference but still I can’t seam to load more than 1 texture.

I want in the end to be able to let every class load it’s own textures, by calling there init method, so it would be nice if someone could help me out here a bit …

Thanx !

the original C function is glGenTextures(int, int*), not (int, int). Therefore the equivilent in Java is an array of ints instead of a pointer. As with the C version, the array has to be created beforehand.

‘Can’t load more than one texture’ is pretty vauge. So in an attempt to pry more information out of you:

  1. Does the first texture load and display correctly?
  2. Can you correctly generate valid (non-zero) multiple texture ids with glGenTextures()?
  3. Can you sucessfully upload image data to multiple texture ids?
  4. Where the hell does it actually go wrong? :stuck_out_tongue:

Oh, and there was a handy dandy texture loader set of classes posted in this forum a little while back. Saved me a bunch of time, you might want to hunt for it :slight_smile:

thanx for the help … I know I didn’t explain it all that well so I’ll try to do it better now …

  1. Yip the first textures loads just fine … my landscape texture …
  2. never tried but I just took one of the nehe codes for this so I presume this shouldn’t be a problem.
  3. never tried either
  4. goes wrong when I want to display my texture on something. He doesn’t give any faults, but either doesn’t show the texture I bind or uses another one.

I’ll now give some parts of my code so you get a better idea …

First of all use the TextureReader from Pepijn Van Eeckhoudt and his common gl classes.

the loading part is as follows:


/**
       * @param gl
       * @param i
       * @param string
       */
      public static void loadLinearGLTextures(
            GL gl,
            int textureID,
            String textureName)
            throws IOException {

            gl.glGenTextures(1, new int[] { Renderer.g_TexturesID[textureID] });

            TextureReader.Texture texture = TextureReader.readTexture(textureName);

            //Create Linear Filtered Texture
            gl.glBindTexture(GL.GL_TEXTURE_2D, Renderer.g_TexturesID[textureID]);
            gl.glTexParameteri(
                  GL.GL_TEXTURE_2D,
                  GL.GL_TEXTURE_MAG_FILTER,
                  GL.GL_LINEAR);
            gl.glTexParameteri(
                  GL.GL_TEXTURE_2D,
                  GL.GL_TEXTURE_MIN_FILTER,
                  GL.GL_LINEAR);

            gl.glTexImage2D(
                  GL.GL_TEXTURE_2D,
                  0,
                  3,
                  texture.getWidth(),
                  texture.getHeight(),
                  0,
                  GL.GL_RGB,
                  GL.GL_UNSIGNED_BYTE,
                  texture.getPixels());

      }

then in my Renderer class:


/** Array of all the textures references */
      public static int[] g_TexturesID = new int[TextureNames.MAXIMUM_TEXTURES];

and finally from my different classes I call this from init method:


public void init(GLDrawable glDrawable) {
            GL gl = glDrawable.getGL();
            try {
                  TextureLoader.loadLinearGLTextures(
                        gl,
                        Renderer.g_TexturesID[TextureNames.SIMPLE_FONT_TEXTURE],
                        "Data/Font/fonts.png");
            } catch (IOException e) {
                  throw new RuntimeException(e);
            }

of coarse is SIMPLE_FONT_TEXTURE a unique int value for the texture.

I already tried lots of different ways and this is my last one, but if someone has something comparable. Then I mean loading textures from different classes orso, he may always send the source codes to me at kozen@pandora.be

Hope this helps already something …

Thanx already

Lemme see… you’re using glGenTextures in a fruity way. I have no idea what SIMPLE_FONT_TEXTURE is, but you’re working backwards.

Docs:
The glGenTextures function returns n texture names in the textures* parameter.

So in other words, you call glGenTextures with an empty array and it gets filled for you with the new unique ids to use as your new textures. However you’re making a new int array… and throwing it away. :o You’re also filling it with your own made up texture id (which does nothing).

You need to keep hold of the array, and after calling the function retreive the generated id sitting in index 0. This then becomes your texture id. You probably then want to put this into your global array of ids at the correct point. Quick guess:


 public static void loadLinearGLTextures( 
  GL gl, 
  int textureID, // your texture index
  String textureName) 
  throws IOException { 
 
 int[] temp = new int[1]; // create blank
  gl.glGenTextures(1, temp); // fill with proper gl texture id
   Renderer.g_TexturesID[TextureNames.SIMPLE_FONT_TEXTURE] = temp[0]; // store gl texture id in correct slot of global array.
 
// rest of method looks ok..

Thanx for the great help already … !

It’s been more than 8 months since I programmed OpenGL in C, and forgot to fact that you also give arguments to be filled instead of being the return object.

But well … actually it still doens’t work so there will be more problems than only this.

The TextureNames class has only unique ID’s and names to use in the g_TextureID array. So SIMPLE_FONT_TEXTURE is the unique ID for my font texture, and if I then want to bind my texture I do like this:


gl.glBindTexture(
                  GL.GL_TEXTURE_2D,
                  Renderer.g_TexturesID[TextureNames.SIMPLE_FONT_TEXTURE]);

and I changed my loadLinearGLTextures with yours, and now to call this method I do:


public void init(GLDrawable glDrawable) {
            GL gl = glDrawable.getGL();
            try {
                  TextureLoader.loadLinearGLTextures(
                        gl,
                        TextureNames.SIMPLE_FONT_TEXTURE,
                        "Data/Fonts/font.png");
            } catch (IOException e) {
                  throw new RuntimeException(e);
            }

            buildFont(gl);
      }

so withouit the Renderer.g_TexturesID for the textureID parameter this time …

this should work now right … but he still only shows the landscape texture.

deeply ashamed … it works now, but with all the testing from before … I somewhere turned off texturing: gl.glDisable(GL.GL_TEXTURE_2D); … removed it and now the texturing works …

thank you for the help once more !