Texture Problem (probably gl.glTexImage2D)

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Texture Problem (probably gl.glTexImage2D)

PedDavid
I'm not getting anything when rendering with the texture (black screen with a shader output = texture input and normal colors with shader that outputs simple colors(the one in the git repository)).
My method is:

gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, texture.getWidth(), texture.getHeight(), 0, GL2.GL_RGB, GL2.GL_UNSIGNED_BYTE, null);

I guess that null is completely wrong...

All my loadTexture method:

    public int loadTexture(String fileName){
        Texture texture = new Texture(0);
        try{
            FileInputStream textureInput = new FileInputStream(fileName + ".png");
            texture = TextureIO.newTexture(textureInput, false, null);
        }catch (IOException e){
            System.out.println("Failed loading texture");
            e.printStackTrace();
            System.exit(0);
        }

        gl.glGenTextures(1, tempTextureHolder, 0);
        gl.glBindTexture(GL2.GL_TEXTURE_2D, tempTextureHolder[0]);
        textures.add(tempTextureHolder[0]);

        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
        gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, texture.getWidth(), texture.getHeight(), 0, GL2.GL_RGB, GL2.GL_UNSIGNED_BYTE, null);

        System.out.println("Texture : "+ gl.glIsTexture(tempTextureHolder[0])); //returning true

        return tempTextureHolder[0];
    }

complete code (a real mess): https://github.com/PedDavid/NubDevEngine

Would love some feedback on the code and if someone could point me in the right direction about textures

Edit: The code still hasn't the changes I made to display, textureLoader (that I already showed) and a second vbo with the textures coordinates

display is:        
        shader.start();
        gl.glBindVertexArray(model.getVaoID());
        gl.glEnableVertexAttribArray(0);
        gl.glEnableVertexAttribArray(1);
        gl.glActiveTexture(GL2.GL_TEXTURE0);
        gl.glBindTexture(GL2.GL_TEXTURE_2D, texModel.getTexture().getId());
        gl.glDrawElements(GL_TRIANGLES, model.getVertexCount(), GL_UNSIGNED_INT, 0);
        gl.glDisableVertexAttribArray(0);
        gl.glDisableVertexAttribArray(1);
        gl.glBindVertexArray(0);
        shader.stop();
Reply | Threaded
Open this post in threaded view
|

Re: Texture Problem (probably gl.glTexImage2D)

gouessej
Administrator
Hi

Don't reinvent the wheel except for pedagogical purposes (in order to learn).

There are some examples in jogl-demos, have you ever looked at them? Your texture object already has an id, I don't understand why you call glGenTextures, please read the documentation of the API.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Texture Problem (probably gl.glTexImage2D)

PedDavid
This post was updated on .
This isn't a project and I'm trying to learn so reinventing the wheel could be good (if done right, currently is getting frustrating)

I've seen a lot of examples, but a lot of them use buffereddata for the image (what I tought was more "primitive"). When you say jogl-demos you're talking about this for example right?

https://github.com/sgothel/jogl-demos/blob/master/src/demos/texture/TestTexture.java

And when you say my texture already has an ID you're refering to the texture object right? I was thinking in using int to ID each texture so I don't have to "move the textures around" but having the texture as a field and just passing it as parameter would probably be easier.

I've read some documentation but for a beginner is pretty overwhelming...

Edit:

So, if I use the glGenTextures anyway (to give a int ID to the texture) but only create the texture after (don't know how to be fair) I wouldn't be giving it 2 ID's like I am now right?
Reply | Threaded
Open this post in threaded view
|

Re: Texture Problem (probably gl.glTexImage2D)

gouessej
Administrator
You mix the low level API with some calls to high level methods and helpers. When you use TextureIO or AWTTextureIO to create a texture, it creates the identifier for you and glTexImage2D isn't necessary too. Texture.getTextureObject(GL) generates the identifier if necessary and returns it.

OpenGL isn't the kind of thing that you can learn in a few days. I advise you to look at the OpenGL red book and its examples have been ported to JOGL (in jogl-demos too).

In my humble opinion, just follow the example that you have found, call enable(), bind() and disable().
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Texture Problem (probably gl.glTexImage2D)

PedDavid
Well, got a test working with this code:

      textures = new int[1];
      gl.glGenTextures(1, textures, 0);
      gl.glBindTexture(GL.GL_TEXTURE_2D, textures[0]);
      try {
         BufferedImage image = ImageIO.read(getClass().getClassLoader().getResource("resource/crate.png"));
         DataBufferByte dbb = (DataBufferByte)image.getRaster().getDataBuffer();
         byte[] data = dbb.getData();
         ByteBuffer pixels = BufferUtil.newByteBuffer(data.length);
         pixels.put(data);
         pixels.flip();
         gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, 256, 256, 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, pixels);
      } catch(Throwable t) {
         t.printStackTrace();
      }

source: http://www.java-gaming.org/index.php?topic=20725.0

But it is rendering with blue/red swapped (calling GL.GL_BGR gets the "true color"). When looking on the internet it seems that usually that happens with bmp instead of png though

Thanks for the tips and I will take a look at the book
Reply | Threaded
Open this post in threaded view
|

Re: Texture Problem (probably gl.glTexImage2D)

gouessej
Administrator
You can use TextureIO.newTextureData() instead of ImageIO, it is generally faster and more cross-platform. Moreover, some kinds of JPEG images aren't supported by ImageIO whereas they are supported by our TextureIO.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Texture Problem (probably gl.glTexImage2D)

PedDavid
This post was updated on .
Just tested it, I don't need to use BGR that way what is actually nice. If it helps cross platform and image support even better. Although I just wrapped the method and it seems to take quite a lot more time to process:

        textures = new int[1];
        gl.glGenTextures(1, textures, 0);
        textureID = textures[0];
        gl.glBindTexture(GL_TEXTURE_2D, textures[0]);
        try {
            long start = System.nanoTime();
            //BufferedImage image = ImageIO.read(getClass().getClassLoader().getResource(textureFileName));
            TextureData image = TextureIO.newTextureData(getGLProfile(), getClass().getClassLoader().getResource(textureFileName)
                    , false, null);
            //DataBufferByte dbb = (DataBufferByte)image.getRaster().getDataBuffer();
            //byte[] data = dbb.getData();
            //ByteBuffer pixels = ByteBuffer.wrap(data);
            gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            gl.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.getWidth(), image.getHeight(),
                    0, GL_BGR, GL_UNSIGNED_BYTE, image.getBuffer());
            gl.glBindTexture(GL_TEXTURE_2D, 0);
            long time = System.nanoTime() - start;
            System.out.println("Average time was " + time);
        } catch(Throwable t) {
            t.printStackTrace();
        }

*the comments are from the previous version with ImageIO

ImageIO timings: Time~=30 000 000
TextureData timings: Time~=60 000 000
Texture (TextureIO.newTexture) timings: Time~=52 000 000

Edit: Tried that getTextureObject() method

        try {
            texture = TextureIO.newTexture(
                    getClass().getClassLoader().getResource(textureFileName), // relative to project root
                    false, textureFileType);
            gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            textureID = texture.getTextureObject(gl);
        } catch (GLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

I only noticed that I didn't even bind a texture before creating this one and it's all working so I guess it isn't needed?

Edit2: Well, just noticed the second method (TextureData) is displaying upside down... Just tried pass extension as ".png" isntead of null and the image is now in the correct orientation, but it is in BGR again...
Reply | Threaded
Open this post in threaded view
|

Re: Texture Problem (probably gl.glTexImage2D)

gouessej
Administrator
If you don't pass the extension properly or if you pass null, TextureIO uses a fallback on ImageIO. I fixed it in JogAmp's Ardor3D Continuation and I'll put the correct mechanism into JOGL.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Texture Problem (probably gl.glTexImage2D)

PedDavid
And I am back! I thought I was rid of these problems but well... I can now load models! What is awesome, I went and tried to load the stanford dragon but I had some problems with the textures. Long story short... 1024*1024 texture is RGB and 256*256 is BGR... Any explanation on that?
Reply | Threaded
Open this post in threaded view
|

Re: Texture Problem (probably gl.glTexImage2D)

gouessej
Administrator
What do you mean exactly? Do you use the same image file for both textures? What do you use to resize it?
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Texture Problem (probably gl.glTexImage2D)

PedDavid
This post was updated on .
So, until now I was using textures from the NeHe tutorials (crate and nehe png 256*256, and glass.png 128*128).

I was going to start experimenting with lights so I got an obj of the stanford dragon and those textures were all messed up so I just went to ms paint and created unicolor textures. White.png(256*256) was all fine, but when I tried other colors it got all messed up too. I then tried paint.net because maybe it was some ms paint random stuff and well, it got better, I now have a unicolor just like I wanted. The problem is, the light.png 256*256 is displaying as BGR and the lightBrown.png (exactly the same image but saved as 1024*1024) displays in RGB...

Edit: Just tested it again, now on my laptop since I'm not at home, created the textures (256*256 and 1024*1024) on gimp, both are BGR (so, just okay like before...).