Texturing problem:

I’ve been staring at this for a day and half now and I can’t see the problem. :frowning: I’m using Kevins texture loader code from the wiki. Output is a window with a shaded spinning triangle - non textured. Here’s a code dump. Can anyone see where the error is?


public class FrameTest extends Frame implements GLEventListener {
      private int width;
      private int height;
      private GLCanvas canvas;
      protected GL gl;
      protected GLU glu;
      private boolean stopRenderer = false;

      private Animator animator;
      private TextureLoader textureLoader;
      
      float RotateX = 0.0f;                                          
      float RotationSpeed = 0.5f;
      Texture texture;
      
      public FrameTest(String title, int width, int height) {
            super(title);

            this.setUndecorated(false);

            initFrame(width, height);
      }

      private void initFrame(int width, int height) {
            this.width = width;
            this.height = height;

            GLCapabilities caps = new GLCapabilities();
            canvas = GLDrawableFactory.getFactory().createGLCanvas(caps);
            canvas.addGLEventListener(this);
            canvas.setNoAutoRedrawMode(true);
            canvas.setFocusable(true);
            canvas.requestFocus();

            animator = new Animator(canvas);

            addWindowListener(new WindowAdapter() {
                  public void windowClosing(WindowEvent e) {
                        animator.stop();
                        System.exit(0);
                  }

                  public void windowActivated(WindowEvent e) {
                        canvas.requestFocus();
                  }
            });

            setLayout(new BorderLayout());
            add(canvas);
            setSize(width, height);
            
            init(canvas);

            show();
            
            getTextureLoader();
            try {
                  texture=textureLoader.getTexture("texture","hobgoblin.png");
            }
            catch (IOException ioe)
            {
                  System.out.println("Failed to load texture.");
            }
            
            animator.start();
      }

      public void dispose() {
            animator.stop();
            stopRenderer = true;
      }

      public void display(GLDrawable drawable) {
            if (stopRenderer) {
                  return;
            }
            if (!isVisible()) {
                  return;
            }

            draw();
      }

      public void draw() {
            gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);      
            gl.glLoadIdentity();
            
            glu.gluLookAt(0, 1.5f, 90,0, 0.5f, 0,0, 1, 0);
            
            gl.glRotatef(RotateX, 0f, 1.0f, 0f);                                    
            RotateX += RotationSpeed;      
            
            texture.bind(gl);

            gl.glBegin(GL.GL_TRIANGLES);
                  gl.glTexCoord2i(0,0);
                  gl.glVertex3f(0,0,0);
                  gl.glTexCoord2i(0,100);
                  gl.glVertex3f(0,30,0);
                  gl.glTexCoord2i(100,100);
                  gl.glVertex3f(30,0,0);
                  
            gl.glEnd();
      }

      public void displayChanged(GLDrawable drawable, boolean modeChanged, boolean deviceChanged) {
      }

      public void init(GLDrawable drawable) {
            gl = canvas.getGL();
            glu =canvas.getGLU();
            
            gl.glEnable(GL.GL_TEXTURE_2D);                                          
            gl.glEnable(GL.GL_DEPTH_TEST);      
            
            gl.glEnable(GL.GL_LIGHT0);                                                
            gl.glEnable(GL.GL_LIGHTING);                                          
            gl.glEnable(GL.GL_COLOR_MATERIAL);
            //gl.glEnable(GL.GL_CULL_FACE);                                          
            //gl.glCullFace(GL.GL_FRONT);

      }

      public void reshape(GLDrawable drawable, int x, int y, int width, int height) {
            this.width = width;
            this.height = height;

            gl.glMatrixMode(GL.GL_PROJECTION);
            gl.glLoadIdentity();

            glu.gluPerspective(45.0f,(float)width/(float)height, .5f ,150.0f);

            gl.glMatrixMode(GL.GL_MODELVIEW);                                          
            gl.glLoadIdentity();
      }

      public TextureLoader getTextureLoader() {
            if (textureLoader == null) {
                  textureLoader = new TextureLoader(canvas); 
            } 

            return textureLoader;
      }
      
      public static void main (String [] args)  {
            FrameTest test=new FrameTest("Test", 500,500);
      }

}

console output:

Loading texture hobgoblin.png to ID: 0

The id is zero every time I run it. Thanks :-/

Remove init(canvas) from initFrame and move texture loading stuff to init.

The problem is that you must not try to init the GLEventListener yourself : it is made by GLCanvas when the OpenGL rendering context is ok. The way you do it, the rendering context is unusable when you load the texture therefore you get an invalid texture id.

 private void initFrame(int width, int height) {
  this.width = width;
  this.height = height;

  GLCapabilities caps = new GLCapabilities();
  canvas = GLDrawableFactory.getFactory().createGLCanvas(caps);
  canvas.addGLEventListener(this);
  canvas.setNoAutoRedrawMode(true);
  canvas.setFocusable(true);
  canvas.requestFocus();

  animator = new Animator(canvas);

  addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent e) {
    animator.stop();
    System.exit(0);
   }

   public void windowActivated(WindowEvent e) {
    canvas.requestFocus();
   }
  });

  setLayout(new BorderLayout());
  add(canvas);
  setSize(width, height);

  show();

  animator.start();
 }
public void init(GLDrawable drawable) {
  gl = canvas.getGL();
  glu =canvas.getGLU();

  gl.glEnable(GL.GL_TEXTURE_2D);
  gl.glEnable(GL.GL_DEPTH_TEST);

  gl.glEnable(GL.GL_LIGHT0);
  gl.glEnable(GL.GL_LIGHTING);
  gl.glEnable(GL.GL_COLOR_MATERIAL);
  //gl.glEnable(GL.GL_CULL_FACE);
  //gl.glCullFace(GL.GL_FRONT);


  textureLoader = new TextureLoader(canvas);

  try {
   texture=textureLoader.getTexture("texture","hobgoblin.png");
  }
  catch (IOException ioe)
  {
   System.out.println("Failed to load texture.");
  }

 }

Thanks for that :slight_smile: .

I found this paragraph from the jogl users guide. I didn’t pickup on the fact your gl was only current in init(), display() and reshape()

[quote]Applications implement the GLEventListener interface to perform OpenGL drawing. When the methods of the GLEventListener are called, the underlying OpenGL context associated with the drawable is already current. The listener fetches the GL object out of the GLDrawable and begins to perform rendering.
[/quote]