Texture problems in nehe lesson

Hi,
I got the code bellow and it is not loading the jpg, png an bmp textures. What kind of problem could be happen?

The lines of code responsible by textures loading are marked below in red rolor:
Obs: some methods are deleteted because the maximum caracteres are exceeded

/*

  • Lesson06.java
  • Created on July 16, 2003, 11:30 AM
    /
    package texturas;
    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.media.opengl.;
import javax.media.opengl.glu.
;
import com.sun.opengl.util.*;

/** Port of the NeHe OpenGL Tutorial (Lesson 6)

static class Renderer
implements GLEventListener,
KeyListener
{
private float xrot; // X Rotation ( NEW )
private float yrot; // Y Rotation ( NEW )
private float zrot; // Z Rotation ( NEW )
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();									// Reset The View
  gl.glTranslatef(0.0f,0.0f,-5.0f);

  gl.glRotatef(xrot,1.0f,0.0f,0.0f);
  gl.glRotatef(yrot,0.0f,1.0f,0.0f);
  gl.glRotatef(zrot,0.0f,0.0f,1.0f);

  [font=Verdana]gl.glBindTexture(GL.GL_TEXTURE_2D, texture);

  gl.glBegin(GL.GL_QUADS);
    // Front Face
    gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f,  1.0f);
    gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f,  1.0f);
    gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f,  1.0f,  1.0f);
    gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f,  1.0f,  1.0f);
    // Back Face
    gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f);
    gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f,  1.0f, -1.0f);
    gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f,  1.0f, -1.0f);
    gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f);
    // Top Face
    gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f,  1.0f, -1.0f);
    gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f,  1.0f,  1.0f);
    gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f,  1.0f,  1.0f);
    gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f,  1.0f, -1.0f);
    // Bottom Face
    gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f);
    gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f);
    gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f,  1.0f);
    gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f,  1.0f);
    // Right face
    gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f);
    gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f,  1.0f, -1.0f);
    gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f,  1.0f,  1.0f);
    gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f,  1.0f);
    // Left Face
    gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f);
    gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f,  1.0f);
    gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f,  1.0f,  1.0f);
    gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f,  1.0f, -1.0f);
  gl.glEnd();
  xrot+=0.3f;
  yrot+=0.2f;
  zrot+=0.4f;
}
[/font]     

public void init(GLAutoDrawable gLDrawable)
{
  final GL gl = gLDrawable.getGL();
  gl.glShadeModel(GL.GL_SMOOTH);              // Enable Smooth Shading
  gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);    // Black Background
  gl.glClearDepth(1.0f);                      // Depth Buffer Setup
  gl.glEnable(GL.GL_DEPTH_TEST);							// Enables Depth Testing
  gl.glDepthFunc(GL.GL_LEQUAL);								// The Type Of Depth Testing To Do
  gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);	// Really Nice Perspective Calculations
  gl.glEnable(GL.GL_TEXTURE_2D);
  gLDrawable.addKeyListener(this);
  texture = genTexture(gl);
  gl.glBindTexture(GL.GL_TEXTURE_2D, texture);
  BufferedImage img = readBMPImage("c:/NeHe.jpg");
  makeRGBTexture(gl, new GLU(), img, GL.GL_TEXTURE_2D, false);
	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)
  {
    animator.stop();
    System.exit(0);
  }
}




[font=Verdana]private BufferedImage readBMPImage(String resourceName)
{
  try
  {
    URL url = getResource(resourceName);
    if (url == null)
    {
      throw new RuntimeException("Error reading resource " + resourceName);
    }
    BufferedImage img = ImageIO.read(url);
    java.awt.geom.AffineTransform tx = java.awt.geom.AffineTransform.getScaleInstance(1, -1); 
    tx.translate(0, -img.getHeight(null)); 
    AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); 
    img = op.filter(img, null); 
    return img;
  }
  catch (IOException e)
  {
    throw new RuntimeException(e);
  }
}
 
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:
    {
      byte[] data = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
      dest = ByteBuffer.allocateDirect(data.length);
      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);
  }
}[/font]
private int genTexture(GL gl)
{
  final int[] tmp = new int[1];
  gl.glGenTextures(1, tmp, 0);
  return tmp[0];
}

}

public final static URL getResource(final String filename)
{
// Try to load resource from jar
URL url = ClassLoader.getSystemResource(filename);
// If not found in jar, then load from disk
if (url == null)
{
try
{
url = new URL(“file”, “localhost”, filename);
}
catch (Exception urlException){} // ignore
}
return url;
}

/** Program’s main entry point

  • @param args command line arguments.
    */
    public static void main(String[] args)
    {
    Frame frame = new Frame(“Lesson 6: Texture Mapping”);
    GLCanvas canvas = new GLCanvas();
    canvas.addGLEventListener(new Renderer());
    frame.add(canvas);
    frame.setSize(640, 480);
    animator = new Animator(canvas);
    frame.addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    animator.stop();
    System.exit(0);
    }
    });
    frame.show();
    animator.start();
    canvas.requestFocus();
    }
    }

the exception returned is bellow:

Exception in thread “AWT-EventQueue-0” java.lang.IndexOutOfBoundsException: Required 196608 remaining bytes in buffer, only had 0
at com.sun.gluegen.runtime.BufferFactory.rangeCheckBytes(BufferFactory.java:274)
at com.sun.opengl.impl.GLImpl.glTexImage2D(GLImpl.java:21147)
at texturas.Lesson06$Renderer.makeRGBTexture(Lesson06.java:241)
at texturas.Lesson06$Renderer.init(Lesson06.java:125)
at com.sun.opengl.impl.GLDrawableHelper.init(GLDrawableHelper.java:72)
at javax.media.opengl.GLCanvas$InitAction.run(GLCanvas.java:271)
at com.sun.opengl.impl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:189)
at javax.media.opengl.GLCanvas.maybeDoSingleThreadedWorkaround(GLCanvas.java:265)
at javax.media.opengl.GLCanvas.display(GLCanvas.java:130)
at javax.media.opengl.GLCanvas.paint(GLCanvas.java:142)
at sun.awt.RepaintArea.paintComponent(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Hi,

Just looking at the exception I suspect a missing rewing on the buffer containing the image data. If I remember well nehe tutorials ports are based on old jogl implementations so you may have to twek the code a little if you’re using the latest releases.

I already atualized the nehe codes… the texture methods did not show problems

turquoise3232 is right; you need to call dest.rewind() before calling glTexImage2D.

They’ve been updated to 1.1.1rc3 in the meantime :wink:

Where can i find those updated tutorials of NeHe?
When I try to download the tutorial at
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=07
This is still based on an old version of JoGL

sorry, no need to answer … :-[ :-[

Cool !!!
God sake “dest.rewind()” ;D and ken russel and everyone registered in javagamming.org