Why won’t my texture show up?

Okay, I’m using Java and JoGL. I’m trying to load and display a texture, but the texture isn’t showing up at all. I’m not getting any errors at all, so I don’t know what the problem could be.


import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import com.sun.opengl.util.*;
import com.sun.opengl.util.j2d.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import java.nio.*;
import javax.imageio.*;
import javax.swing.*;


public class chartest extends JFrame implements GLEventListener, KeyListener
{
    private int texture;

    public void display(GLAutoDrawable gLDrawable)
    {
      final GL gl = gLDrawable.getGL();
      gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
      gl.glLoadIdentity();


      gl.glColor3f(1.0f, 1.0f, 1.0f);
      gl.glBindTexture(GL.GL_TEXTURE_2D, texture);

      gl.glBegin(GL.GL_QUADS);              // Draw A Quad
        gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-12.0f, -19.0f,  -15.0f);
        gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 12.0f, -19.0f,  -15.0f);
        gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 12.0f,  19.0f,  -15.0f);
        gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-12.0f,  19.0f,  -15.0f);
      gl.glEnd();                       // Done Drawing The Quad
      gl.glFlush();
    }

    public void displayChanged(GLAutoDrawable g, boolean b, boolean b2){}

    public void init(GLAutoDrawable gLDrawable)
    {
      final GL gl = gLDrawable.getGL();
      gl.glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
      gl.glShadeModel(GL.GL_FLAT);
      gl.glEnable(GL.GL_TEXTURE_2D);

      texture = genTexture(gl);
      gl.glBindTexture(GL.GL_TEXTURE_2D, texture);
      URL url = this.getClass().getResource("test.png");

      try
      {
        BufferedImage img = ImageIO.read(url);
        makeRGBTexture(gl, new GLU(), img, GL.GL_TEXTURE_2D, false);
      }

      catch(Exception e)
      {
        e.printStackTrace();
      }

      gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MIN_FILTER,GL.GL_LINEAR); 
      gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MAG_FILTER,GL.GL_LINEAR); 
    }

    public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height)
    {
      final GL gl = gLDrawable.getGL();
      final GLU glu = new GLU();

      if (height <= 0) // avoid a divide by zero error!
        height = 1;
      final float h = (float)width / (float)height;
      gl.glViewport(0, 0, width, height);
      gl.glMatrixMode(GL.GL_PROJECTION);
      gl.glLoadIdentity();
      glu.gluPerspective(45.0f, h, 1.0, 20.0);
      gl.glMatrixMode(GL.GL_MODELVIEW);
      gl.glLoadIdentity();
    }

    public void keyPressed(KeyEvent e)
    {
        if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
        System.exit(0);
    }
    public void keyReleased(KeyEvent e) {}
    public void keyTyped(KeyEvent e) {}

    public static void main(String[] args) {

        chartest c = new chartest();

        GLCanvas canvas = new GLCanvas(new GLCapabilities());
        canvas.addGLEventListener(c);
        c.add(canvas);
        c.setSize(640, 480);
        canvas.addKeyListener(c);
        c.addWindowListener(new WindowAdapter()
        {
          public void windowClosing(WindowEvent e)
          {
            System.exit(0);
          }
        });
        c.setVisible(true);
        canvas.requestFocus();
    }



    //-----------------------
    private void makeRGBTexture(GL gl, GLU glu, BufferedImage img, int target, boolean mipmapped)
    {
      ByteBuffer dest = null;
      switch (img.getType())
      {
        case BufferedImage.TYPE_3BYTE_BGR:
        case BufferedImage.TYPE_CUSTOM:
        {
          System.out.println("Custom");
          byte[] data = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
          dest = ByteBuffer.allocateDirect(99999);
          dest.order(ByteOrder.nativeOrder());
          dest.put(data, 0, data.length);
          break;
        }
        case BufferedImage.TYPE_INT_RGB:
        {
          int[] data = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
          dest = ByteBuffer.allocateDirect(data.length * BufferUtil.SIZEOF_INT);
          dest.order(ByteOrder.nativeOrder());
          dest.asIntBuffer().put(data, 0, data.length);
          break;
        }
        default:
          throw new RuntimeException("Unsupported image type " + img.getType());
      }

      if (mipmapped)
      {
        glu.gluBuild2DMipmaps(target, GL.GL_RGB8, img.getWidth(), img.getHeight(), GL.GL_RGB, GL.GL_UNSIGNED_BYTE, dest);
      }
      else
      {
        gl.glTexImage2D(target, 0, GL.GL_RGB, img.getWidth(), img.getHeight(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, dest);
      }
    }

    private int genTexture(GL gl)
    {
      final int[] tmp = new int[1];
      gl.glGenTextures(1, tmp, 0);
      return tmp[0];
    }
}

I’m no expert but one thing immediately apparent to me is you don’t have a gluPerspective line in there. I didn’t run your code or anything though.

glu.gluPerspective(45.0f, h, 1.0, 20.0);

It’s in the init method.

No it is only in the reshape method

I got it to work by going here: http://forums.sun.com/thread.jspa?threadID=5197213

If anyone else has similar issues and you’re testing it on a computer with OpenGL before 2.0:
Make sure that your textures are of a power of two. Mine weren’t and I was wondering for really long why it worked on my computer and why it didn’t work on my colleague’s :wink: