Hello Guest

Creating Textures

  • 3 Replies
Creating Textures
« on: March 08, 2007, 01:51:39 »

Hello. I'm encountering some errors trying to create a 512 x 512 texture and filling it with an empty byte buffer. Java throws an error saying that it expects a texture resolution of 1024 x 1024.

java.lang.IllegalArgumentException: Number of remaining buffer elements is 262144, must be at least 1048576

Any idea what it could be?


HEre's the code

bb = createTextureBB(320);
int dimension = (int) Math.sqrt(bb.limit());
myTextureHandle = makeTexture(bb, dimension, dimension);

public static ByteBuffer createTextureBB(int size) {
      int power = 1;
      int powSize = 2;
      while (size > powSize){
      powSize = (int) (Math.pow((double) 2,(double) power));
      size = powSize;
      System.out.println("size "+size);
      byte[] bytearray = new byte[(powSize*powSize)];
      for (int i = 0; i< (powSize*powSize); i++){
         bytearray =00000000;
       ByteBuffer bb = ByteBuffer.allocateDirect(bytearray.length * SIZE_BYTE).order(ByteOrder.nativeOrder());
       return bb;

public static int makeTexture(ByteBuffer pixels, int w, int h)
      // get a new empty texture
      int textureHandle = allocateTexture();
      // 'select' the new texture by it's handle
      // set texture parameters:
      // how to wrap texture
      GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
      GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
      // how to scale up texture
      // Create the texture from pixels
      GL11.glTexImage2D(GL11.GL_TEXTURE_2D,   // type of texture we're creating
            0,                      // level-of-detail: use 0
            GL11.GL_RGBA,           // texture pixel format
            w, h,                   // width and height of texture image (powers of 2)
            0,                      // widtt of the border (either 0 or 1, use 0)
            GL11.GL_RGBA,           // image pixel format
            GL11.GL_UNSIGNED_BYTE,  // image pixel data type
            pixels                  // image pixel data
      return textureHandle;

Re: Creating Textures
« Reply #1 on: March 08, 2007, 14:19:12 »
Your byte array (bytearray[]) needs to be a size of powSize*powSize*bytesPerPixelForTexture (4 bytes for GL11.GL_RGBA). Also your ByteBuffer should be the size of bytearray, not the size of bytearray*SIZE_BYTE(whatever that is ???)

Hope that helps ;D

EDIT: this also means that you won't be able to get your dimension by taking the square root anymore (Math.sqrt(bb.limit()))
« Last Edit: March 08, 2007, 14:22:15 by Fool Running »
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Re: Creating Textures
« Reply #2 on: March 08, 2007, 17:38:44 »

thanks, that helps quite considerably ... java is no longer throwing those errors.

however, when i tried to update the texture (with frames from a movie captured as an array of bytes using the function glTexSubImage2D), the texture is completely skewed (both horizontally and vertically). i checked to see if my texture coordinates were correct by loading in a still image and that image displayed correctly. it occurred to me think think that the pixel array (byte buffer) is not being correctly generated. and when i checked the length of the byte buffer, it looks like it's getting more than the right amount of bytes (320 x 240 x 4) ... it's actually 311040 (324 x 240 x 4). could that be the cause?


funny, when i added 4 pixels to the width of the texture in glTexSubImage2D, it works just fine. how to others typically handle this situation?



Re: Creating Textures
« Reply #3 on: March 08, 2007, 18:21:53 »
could that be the cause?
It looks like (from the screenshot) that the movie is actually that resolution (324x240) instead of what you think it should be (320x240).
Its possible that OpenGL is trying to align the bytes on some boundary (like 8 bytes) which might be causing that problem... You might try doing this before you call glTexSubImage2D():
Code: [Select]
GL11.glPixelStorei(GL11.GL_PACK_ROW_LENGTH, 0);
GL11.glPixelStorei(GL11.GL_PACK_ALIGNMENT, 1);
GL11.glPixelStorei(GL11.GL_PACK_SKIP_ROWS, 0);
GL11.glPixelStorei(GL11.GL_PACK_SKIP_PIXELS, 0);
Code: [Select]
GL11.glPixelStorei(GL11.GL_UNPACK_ROW_LENGTH, 0);
GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
GL11.glPixelStorei(GL11.GL_UNPACK_SKIP_ROWS, 0);
GL11.glPixelStorei(GL11.GL_UNPACK_SKIP_PIXELS, 0);
(I don't remember which of those you need in that case :-[)
Also, is there a reason you have an alpha channel in the movie? It seems like an alpha channel would be worthless in a movie (maybe I'm wrong ;D)
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D