【问题标题】:How to pass float array to glVertexAttribPointer如何将浮点数组传递给 glVertexAttribPointer
【发布时间】:2011-05-28 07:38:29
【问题描述】:

我正在 Android 上开发 OpenGL ES 2.0 着色器...

我有一个浮点数组,其中包含顶点位置以及顶点的其他属性。 位置和其他属性可能会随着时间而改变。

如何将这个修改后的数组传递给 glVertexAttribPointer, 这样我就可以用更新的值绘制场景

当我试图通过它时,我得到了

GLES20类型中的方法glVertexAttribPointer(int, int, int, boolean, int, Buffer)不适用于参数(int, int, int, boolean, int, float[])

【问题讨论】:

  • 该错误告诉您需要获取您拥有的浮动顶点数组并以某种方式将其转换为缓冲区对象。我不是 openGL 专家,所以我只能回答这个问题。
  • thanx Eric,但我无法修改缓冲区...所以想使用数组 intead

标签: android opengl-es opengl-es-2.0


【解决方案1】:
FloatBuffer yourFloatBuffer;
float[] yourFloatArray;    

FloatBuffer byteBuf = ByteBuffer.allocateDirect(yourFloatArray.length * 4);
    byteBuf.order(ByteOrder.nativeOrder());
    yourFloatBuffer = byteBuf.asFloatBuffer();
    yourFloatBuffer.put(yourFloatArray);
    yourFloatBuffer.position(0);

这应该可行。

【讨论】:

  • 这对 Egor 有效,我知道。但我想稍后在运行时修改浮点数组......使用此代码我无法实现
  • 我认为,每次更改数组时都应该执行此代码。
  • 是的,我们可以做到这一点,但问题是当我将缓冲区传递给着色器时,GPU 可能正在使用它而我无法控制它......这可能就是我遇到异常的原因
【解决方案2】:

是 java.nio.Buffer 吗?因为看来你需要

java.nio.FloatBuffer

并调用 floatBuffer.array()

从中取出 float[] 数组。

但是,显然它在 FloatBuffer.java 中是最终的:

final float[] hb;   

所以看来您需要... 可能扩展 FloatBuffer 并使数组不是最终的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多