通过 MappedByteBuffer 将原始 VBO 数据加载到 OpenGL 中(不起作用)

发布于 2024-11-23 16:17:57 字数 866 浏览 5 评论 0原文

我尝试通过 Google 在 GDC 2011 上所做的演讲中介绍的方法加载原始、未压缩的 VBO 数据。此方法使用 MappedByteBuffer 在后续调用 glBufferData 时快速加载数据。不幸的是,对我来说,它不起作用。我能够找到一个解决办法(它在下面的代码中被注释掉),但我希望在没有那个黑客的情况下让它工作。这是我的代码示例:

FileInputStream fis = new FileInputStream(new File(location));
FileChannel fc = fis.getChannel();
MappedByteBuffer mbb = fc.map(MapMode.READ_ONLY, 0, fc.size());

// Hackery because passing mbb to glBufferData isn't working. 
//FloatBuffer fb = mbb.asFloatBuffer();
//float triangles[] = new float[fb.capacity()];
//for(int i = 0; i < triangles.length; i++) {
//    triangles[i] = fb.get(i);
//}
//fb = FloatBuffer.wrap(triangles);

bufferInfo = new int[3];
bufferInfo[0] = newBufferID();
int size = (int)fc.size();

GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, bufferInfo[0]);
GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, size, mbb, GLES20.GL_STATIC_DRAW);

I've attempted to load raw, uncompressed VBO data via the method presented in a talk Google did at GDC 2011. This method uses a MappedByteBuffer to quickly load the data in a subsequent call to glBufferData. Unfortunately, for me, it's just not working. I was able to find a hacky work around for it (its commented out in my code below), but I would like to get this working without that hack. Here is a sample of my code:

FileInputStream fis = new FileInputStream(new File(location));
FileChannel fc = fis.getChannel();
MappedByteBuffer mbb = fc.map(MapMode.READ_ONLY, 0, fc.size());

// Hackery because passing mbb to glBufferData isn't working. 
//FloatBuffer fb = mbb.asFloatBuffer();
//float triangles[] = new float[fb.capacity()];
//for(int i = 0; i < triangles.length; i++) {
//    triangles[i] = fb.get(i);
//}
//fb = FloatBuffer.wrap(triangles);

bufferInfo = new int[3];
bufferInfo[0] = newBufferID();
int size = (int)fc.size();

GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, bufferInfo[0]);
GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, size, mbb, GLES20.GL_STATIC_DRAW);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ㄟ。诗瑗 2024-11-30 16:17:57

距离您的帖子已经过去两年了,但这是 Stack Overflow 上关于 MappedByteBuffer 和将数据加载到 VBO 的唯一条目,我想我找到了解决方案。

在我看来,问题不在于这段代码,而在于您尝试加载的数据。就我而言,当我将存储的顶点数据的字节序更改为小字节序时,它会有所帮助。

这就像 Google 在提供的示例中所做的那样:点是使用 sphere.c 生成的,因此如果我运行他们的程序,我会得到小端字节序的值,这就是他们的演示起作用的原因。当我尝试使用 DataOutputStream 存储 Java 中的值时,我得到了大端顺序。

Two years passed since your post, but this is the only entry on Stack Overflow regarding MappedByteBuffer and loading data to VBO and I think I found the solution.

In my opinion problem is not with this code, but with the data you're trying to load. In my case it helped when I've changed endianness of stored vertex data to little endian.

This is just like Google does it in provided example: point are generated using sphere.c, so if I run their program I get values in little endian and that's why their demo works. When I tried to store values from Java using DataOutputStream I got big endian order.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文