troubleshooting Question

JoGL; Texture Mapping problem [ByteBuffer]

Avatar of InteractiveMind
InteractiveMindFlag for United Kingdom of Great Britain and Northern Ireland asked on
Java
3 Comments1 Solution1590 ViewsLast Modified:
Hi.

I have an image, which I'm reading in, to a ByteBuffer, using this method:


public ByteBuffer readData( URL url ) throws Exception
{
      ByteBuffer buf = null;
      BufferedImage img = null;
      
      Image src = new ImageIcon( url ).getImage();
      img = new BufferedImage( src.getWidth(null), src.getHeight(null), BufferedImage.TYPE_INT_RGB );
      img.getGraphics().drawImage( src, 0, 0, null );
      
      int [] data = ( (DataBufferInt ) img.getRaster().getDataBuffer() ).getData();
      buf = ByteBuffer.allocateDirect( data.length
                  * BufferUtils.SIZEOF_INT );
      buf.order( ByteOrder.nativeOrder() );
      buf.asIntBuffer().put( data, 0, data.length );
      
      return buf;
}


When I map the ByteBuffer (as the data) to some quads, that make up a 3D cube (which is rotating), like so:


Texture img = new Texture();   // Custom class -- very simple though
img.width = 256;  // for example
img.height = 256; //
img.data = readData( new File("my_image.jpg").toURL() );  // the data member is obviously a ByteBuffer instance

glu.gluBuild2DMipmaps( GL.GL_TEXTURE_2D, GL.GL_RGB, img.width, img.height, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, img.data );

// Then define the texture coordinates, and render the quads here !


There are no compile-time/run-time errors, a such ... however, the texture being mapped to the cube is not how it is in the file lol. Take a look for yourself:

Here's what the output of my program is currently:
   http://www.myjavaserver.com/~rdarkins/it_looks_like.jpg

Whereas the texture that I'm loading, and attempting to map to the cube is:

   http://www.myjavaserver.com/~rdarkins/crate.jpg

If you look closely enough however, then you can see some very faint detail from the actual proper texture appear on the cube... so it's not a completely random texture...

Can anyone see a problem with my readData() method, perhaps?

By the way, I'm quite confident that this is not a problem with the setup/attributes of the program...

Thanks  : )
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros