【问题标题】:Convert multidimensional array to Floatbuffer and then OpenGL Texture to be used in shader in a batch将多维数组转换为 Floatbuffer,然后将 OpenGL 纹理转换为批量用于着色器
【发布时间】:2021-07-20 16:29:39
【问题描述】:

this question 中尝试了相反的缓冲区转换。 这里我有一个多维数组,想在它周围包装一个缓冲区。 我尝试了以下方法但没有成功:

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);
        }
    }
}

该示例编译得很好,是否足以容纳数组的内容? 这种用法是否足以处理 LibGDX 中的 openGL 纹理?

        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);

【问题讨论】:

    标签: java opengl libgdx textures


    【解决方案1】:

    上面的代码需要做一些修改,因为

    1. 它不像纹理格式
    2. 它不是直接缓冲区
    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 */
        }
    }
    
    

    修改后的数据数组可以成功用作纹理缓冲区。

    【讨论】:

      猜你喜欢
      • 2016-08-07
      • 1970-01-01
      • 1970-01-01
      • 2012-06-02
      • 1970-01-01
      • 2013-03-14
      • 2022-01-15
      • 2014-11-25
      • 1970-01-01
      相关资源
      最近更新 更多