1

In this question the opposite is attempted of the Buffer conversion. Here I have a multi-dimensional array and would like to wrap a buffer around it. I have tried the following without any success:

import java.nio.FloatBuffer;

public class Main
{
    private static final int dataSize = 500;
    private static final float[][] data = new float[dataSize][dataSize];
    public static void main(String[] args) {
        for (int i = 0; i < 500; i++) {
            for (int j = 0; j <500; j++) {
                data[i][j] = i - j; /* to test negative numbers */
            }
        }
        FloatBuffer dataBuf = FloatBuffer.allocate(dataSize * dataSize);
        for(int i = 0; i< data.length; ++i){
            dataBuf.put(data[i], 0, dataSize);
        }
    }
}

The example compiles just fine, Should that sufficiently hold the contents of the array? Is this usage sufficient to handle openGL Textures inside LibGDX?

        IntBuffer numberTexture = IntBuffer.allocate(1);
        dataBuf.position(0);
        Gdx.gl30.glGenTextures(1, numberTexture);
        Gdx.gl30.glBindTexture(GL30.GL_TEXTURE_2D, numberTexture.get());
        Gdx.gl30.glTexParameteri(GL30.GL_TEXTURE_2D, GL30.GL_TEXTURE_MIN_FILTER, GL30.GL_NEAREST);
        Gdx.gl30.glTexParameteri(GL30.GL_TEXTURE_2D, GL30.GL_TEXTURE_MAG_FILTER, GL30.GL_LINEAR);
        Gdx.gl30.glTexImage2D(
            GL30.GL_TEXTURE_2D, 0, GL30.GL_RGBA32F,
            width, height, 0, GL30.GL_RGBA, GL30.GL_FLOAT, dataBuf
        );
        Gdx.gl30.glGenerateMipmap(GL30.GL_TEXTURE_2D);

1 Answer 1

0

Some adaptations are needed for the above code because

  1. It doesn't resemble the textures format
  2. It's not a direct buffer
import java.nio.FloatBuffer;

public class Main
{
    private static final int dataSize = 500;
    private static final float[][][] data = new float[dataSize][dataSize][4]; /* for RGBA */
    public static void main(String[] args) {
        for (int i = 0; i < 500; i++) {
            for (int j = 0; j <500; j++) {
                data[i][j][0] = i - j; /* to test negative numbers */
                data[i][j][1] = i - j;
                data[i][j][2] = i - j;
                data[i][j][3] = i - j;
            }
        }
                                        /* Float Byte size * RGBA * width * height */
        FloatBuffer dataBuf = ByteBuffer.allocateDirect( 4 * 4 * dataSize * dataSize)
             .asFloatBuffer();
        for(int i = 0; i< data.length; ++i){
            for (int j = 0; j < data[i].length; j++) {
                dataBuf.put(data[i][j],0, 4);
            }
        }
        dataBuf.position(0); /* reset the caret position to the beginning of the array */
    }
}

After the changes, the data array can be succesffully used as a texture buffer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.