Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3803422
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:21:38+00:00 2026-05-19T14:21:38+00:00

I am trying to load a texture for a cube and I have trouble

  • 0

I am trying to load a texture for a cube and I have trouble with the dimensions I use. The texture has the power of two (256×256). When it should use 256 as width and height it throws an exception:

java.lang.IndexOutOfBoundsException: Required 262144 remaining bytes in buffer, only had 68998
    at com.jogamp.common.nio.Buffers.rangeCheckBytes(Buffers.java:828)

The code:

private void initTexture(GL2ES2 gl) {
try {
    BufferedImage bufferedImage = ImageIO.read(new URI("http://192.168.0.39/images/box.gif").toURL());
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ImageIO.write(bufferedImage, "gif", byteArrayOutputStream);
    byte[] imageData = byteArrayOutputStream.toByteArray();
    imageBuffer = ByteBuffer.wrap(imageData);
} catch (Exception e) {
    e.printStackTrace();
}
imageBuffer.rewind();
gl.glGenTextures(1, textureIds, 0);
gl.glBindTexture(GL2ES2.GL_TEXTURE_2D, textureIds[0]);
gl.glTexImage2D(GL2ES2.GL_TEXTURE_2D, 0, GL2ES2.GL_RGBA, 256, 256, 0, GL2ES2.GL_RGBA, GL2ES2.GL_UNSIGNED_BYTE, imageBuffer);
gl.glTexParameteri(GL2ES2.GL_TEXTURE_2D, GL2ES2.GL_TEXTURE_MAG_FILTER, GL2ES2.GL_LINEAR);
gl.glTexParameteri(GL2ES2.GL_TEXTURE_2D, GL2ES2.GL_TEXTURE_MIN_FILTER, GL2ES2.GL_LINEAR_MIPMAP_NEAREST);
gl.glGenerateMipmap(GL2ES2.GL_TEXTURE_2D);
gl.glBindTexture(GL2ES2.GL_TEXTURE_2D, 0);
}

When I change the parameter width/height to 128 the exception disappears but the cubes show wrong colors:

enter image description here

As bestsss mentioned, the reason might be some raw format. The problem: I can’t fix this. I tried multiple images and formats. Created them with gimp (working on ubuntu) but the exception is always the same. So I guess the reason for that is that I read the image in a wrong way. Some ideas?

Update

My solution (which uses JOGL classes TextureIO and Texture):

Texture texture;

private void initTexture(GL2ES2 gl) {
    try {
        texture = TextureIO.newTexture(new URI("http://192.168.0.39/images/box.gif").toURL(),true,null);
        texture.setTexParameterf(GL2ES2.GL_TEXTURE_MIN_FILTER, GL2ES2.GL_LINEAR_MIPMAP_LINEAR);
        texture.setTexParameterf(GL2ES2.GL_TEXTURE_MAG_FILTER, GL2ES2.GL_LINEAR);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void display(GL2ES2 gl) {
    // code snipped
    if (texture != null) {
        texture.enable();
        texture.bind();
    }
    // code snipped
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T14:21:39+00:00Added an answer on May 19, 2026 at 2:21 pm

    Zero clue about the API however. I can bet the expected format is some raw one NOT gif since 262144 =2^18 (or 256*256*4). RGB+Alpha are 4bytes.

    edit: again,
    gl.glTexImage2D(GL2ES2.GL_TEXTURE_2D, 0, GL2ES2.GL_RGBA, 256, 256, 0, GL2ES2.GL_RGBA, GL2ES2.GL_UNSIGNED_BYTE, imageBuffer);

    just guessting but look at the constants: GL2ES2.GL_RGBA, GL2ES2.GL_RGBA, GL2ES2.GL_UNSIGNED_BYTE – all support RGBA format for the bytes in the byte buffer,see what other contestants are available, the way I believe using NIO would have point only with direct buffers containing the raster in the format specified by the constants. (i.e. no other formats for image storage/transmission like jpeg/bif/png will help)

    So read the documentation again, look for tutorial, examples and proceed (the way you load the image is not very good either)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to load an image file and use it as a texture for
I have a DXT1 texture loaded in a ByteBuffer and I'm trying to load
I'm trying to load a texture with RGBA values but the alpha values just
I'm trying to load an image into Java to create a texture in JoGL.
hi i'm trying to load a texture (.bmp image) ... with debugging i see
A bit of back story: I am currently trying to load a texture with
I am trying to load an image as a texture for openGL using the
I am trying to load images so I can use them as textures. I
Im trying to load a flash movie using swfobject which i have been successful
I have been trying to load a html file in a webview in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.