Texture loading with imageio

I tried to load my textures with imageio as i do in java2d, but they aren’t rendered. I only have a white square with :frowning:

GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
        GL11.glBegin(GL11.GL_QUADS);
              GL11.glTexCoord2f(0.0f, 0.0f);
              GL11.glVertex3f(-1.0f, -1.0f, 0.0f);
              GL11.glTexCoord2f(0.0f, 1.0f);
              GL11.glVertex3f(-1.0f, 1.0f, 0.0f);
              GL11.glTexCoord2f(1.0f, 1.0f);
              GL11.glVertex3f(1.0f, 1.0f, 0.0f);
              GL11.glTexCoord2f(1.0f, 0.0f);
              GL11.glVertex3f(1.0f, -1.0f, 0.0f);
        GL11.glEnd();

here is my texture loading code.


private int loadTexture(String path) {
        File file = new File(path);
        BufferedImage source = null;
        try {            
            source = ImageIO.read(file);            
        } catch (IOException e) {
            e.printStackTrace();
        }        

        ByteBuffer imageBuffer = ByteBuffer.allocateDirect(4*source.getWidth()*source.getHeight());
        byte buffer[] = (byte[])source.getRaster().getDataElements(0,0,source.getWidth(),source.getHeight(),null);
        imageBuffer.clear();
        imageBuffer.put(buffer);
        imageBuffer.rewind();
        
        IntBuffer adress = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
        GL11.glGenTextures(adress);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D,imageBuffer.get(0));
        GL11.glTexImage2D(GL11.GL_TEXTURE_2D,0,GL11.GL_RGB,source.getWidth(),source.getHeight(),0,GL11.GL_RGB,GL11.GL_UNSIGNED_BYTE,imageBuffer);
        //System.out.println(adress.get(0));
        return adress.get(0);
    }

Any ideas why the texture isn’t rendered?
The texture itself is correct loaded by imageio.

GL11.glBindTexture(GL11.GL_TEXTURE_2D,imageBuffer.get(0));

should be

GL11.glBindTexture(GL11.GL_TEXTURE_2D,adress.get(0));

Cas :slight_smile:

The quad is still just white without any texture displayed :frowning:

Could it be that i have to use another format than BMP?

Maybe it is because you didn’t set the filters also. Here is my texture loading code:


    private final int loadTexture(String path) {
        File file = new File(path);
        BufferedImage tex = null;
        try {
            tex = ImageIO.read(file);
        }
        catch (IOException e) {
            e.printStackTrace();
        }

        // Put Image In Memory
        ByteBuffer scratch = ByteBuffer.allocateDirect(4 * tex.getWidth() * tex.getHeight());

        byte data[] = (byte[]) tex.getRaster().getDataElements(0, 0, tex.getWidth(), tex.getHeight(), null);
        scratch.clear();
        scratch.put(data);
        scratch.rewind();

        // Create A IntBuffer For Image Address In Memory
        IntBuffer buf = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
        GL11.glGenTextures(buf); // Create Texture In OpenGL

        GL11.glBindTexture(GL11.GL_TEXTURE_2D, buf.get(0));
        // Typical Texture Generation Using Data From The Image

        // Linear Filtering
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
        // Linear Filtering
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
        // Generate The Texture
        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, tex.getWidth(), tex.getHeight(), 0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, scratch);

        return buf.get(0); // Return Image Address In Memory
    }

The only difference is the following:

        // Linear Filtering
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
        // Linear Filtering
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);

no, it is still white :frowning:

here the complete source, maybe it will help hope so

// JSE 1.5 Imports
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.ByteOrder;
import javax.imageio.ImageIO;

// LWJGL Imports
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.OpenGLException;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.glu.GLU;


public class JBE {
    private DisplayMode displayMode;
    private int height, width, texture;
    
    public static void main(String args[]) {
        JBE jbe = new JBE();
        jbe.run();
    }
    
    private void run() {
        // create the window
        this.createWindow();
        // init the OpenGL
        this.initGL();
        // resize
        this.reSizeGLScene(displayMode.getWidth(),displayMode.getHeight());
        // try to load the texture
        this.texture = this.loadTexture("textures/dots.png");
        // our mainloop        
        while  (true) {
            if (Display.isCloseRequested()) {
                cleanup();
                break;
            }
                
            try {                                
                this.render();
                Display.update();
                
            } catch (OpenGLException e) {
                System.err.println(e);
            }            
        }
    }
    
    private void createWindow() {        
        Display.setTitle("Java Bubble Engine");
        try {
            Display.setFullscreen(false);
            Display.create();
            displayMode = Display.getDisplayMode();
            height = displayMode.getHeight();
            width = displayMode.getWidth();
        } catch (LWJGLException e) {
            System.out.println(e);
        }      
    }
    
    protected void reSizeGLScene(int width, int height) {
        if (height == 0)
            height = 1;
        
        GL11.glViewport(0,0,width,height);
        
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        
        // a nice 45° perspective
        GLU.gluPerspective(45.0f,(float)displayMode.getWidth()/(float)displayMode.getHeight(),0.1f,100.0f);
        // ortho view
        //GL11.glOrtho(0.0f,width,height,0.0f,-1.0f,1.0f);
        
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glLoadIdentity();
    }
    
    private void initGL() {
        GL11.glShadeModel(GL11.GL_SMOOTH);
        GL11.glClearColor(0.0f,0.0f,0.0f,0.0f);            // black background
        GL11.glClearDepth(1.0f);                              // Depth Buffer Setup
        GL11.glEnable(GL11.GL_DEPTH_TEST);
        GL11.glDepthFunc(GL11.GL_LEQUAL);
        GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT,GL11.GL_NICEST);        
    }
    
    private void cleanup() {
        Display.destroy();
    }
    
    private void render() {
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        GL11.glLoadIdentity();        
        GL11.glTranslatef(0.0f,0.0f,-6.0f);
       
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
        GL11.glBegin(GL11.GL_QUADS);
              GL11.glTexCoord2f(0.0f, 0.0f);
              GL11.glVertex3f(-1.0f, -1.0f, 0.0f);
              GL11.glTexCoord2f(0.0f, 1.0f);
              GL11.glVertex3f(-1.0f, 1.0f, 0.0f);
              GL11.glTexCoord2f(1.0f, 1.0f);
              GL11.glVertex3f(1.0f, 1.0f, 0.0f);
              GL11.glTexCoord2f(1.0f, 0.0f);
              GL11.glVertex3f(1.0f, -1.0f, 0.0f);
        GL11.glEnd();
    }
    
    private int loadTexture(String path) {
        File file = new File(path);
        BufferedImage source = null;
        try {            
            source = ImageIO.read(file);            
        } catch (IOException e) {
            e.printStackTrace();
        }        
        
        ByteBuffer imageBuffer = ByteBuffer.allocateDirect(4*source.getWidth()*source.getHeight());
        byte buffer[] = (byte[])source.getRaster().getDataElements(0,0,source.getWidth(),source.getHeight(),null);
        imageBuffer.clear();
        imageBuffer.put(buffer);
        imageBuffer.rewind();
        
        IntBuffer adress = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
        GL11.glGenTextures(adress);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D,adress.get(0));
        // Linear Filtering
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
        // Linear Filtering
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
        GL11.glTexImage2D(GL11.GL_TEXTURE_2D,0,GL11.GL_RGB,source.getWidth(),source.getHeight(),0,GL11.GL_RGB,GL11.GL_UNSIGNED_BYTE,imageBuffer);
        //System.out.println(adress.get(0));
        return adress.get(0);
    }
}

GL11.glEnable(GL11.GL_TEXTURE_2D);

You forgot to enable texture mapping.

yea it workz, as i tried to enable gl_texture_2d for some time the result was also a white squad. but now the texture is shown. All errors are gone.
big thx @all