English 中文(简体)
为什么是浮点数而不是浮点数?
原标题:Why FloatBuffer instead of float[]?

我用我Android代码中的浮控缓冲器已经使用了一段时间了(从一些开阔角度的教义中读取),但我无法确切理解这个构造是什么,为什么需要它。

例如,我在许多民族中看到这种守则(或类似的守则)以及机器人教义:

float[] vertices = ...some array...

ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4); 
vbb.order(ByteOrder.nativeOrder());    // use the device hardware s native byte order
FloatBuffer fb = vbb.asFloatBuffer();  // create a floating point buffer from the ByteBuffer
fb.put(vertices);    // add the coordinates to the FloatBuffer
fb.position(0);      // set the buffer to read the first coordinate

这似乎非常多变和乱七八糟 对于某事,据我所知,这只是一个花哨包装 围绕一系列浮标。

问题:

  • 这种类别(ByteBuffer、Floatbuffer)相对于任何其他种类的集合或简单的浮标阵列而言,其理由何在?

  • 将字节布弗转换成花节布弗之前 创建字节布弗有什么想法?

最佳回答

其主要原因是业绩:ByteButffers和其他NIO类别在与本地代码接口时能够加速操作(通常通过避免将数据复制到临时缓冲器)。

如果您在做许多 OpenGL 发送电话等动作, 这一点非常重要 。

首先创建位元缓冲的原因是, 您想要使用分配 Direct 调用创建 < em > 直接 < / em > 字节缓冲, 缓冲会从加速操作中受益。 然后您会从此创建一个浮控缓冲, 共享相同的内存 。 FloatButffer 本身没有分配 Direct 方法, 出于某种原因, 这就是为什么您必须从 ByteButffer 中访问 。

问题回答

我们在爪哇对Android进行编码,但OpenGL ES的基本实施实际上以C形式写成。 在我们将数据传送到 OpenGL之前,我们需要将数据转换成一个它会理解的形式。 Java 和本地系统可能不会以同样的顺序存储它们的字节, 所以我们使用一套特殊的缓冲类, 创建一个足以保存我们数据的ByteBuffer大字节, 并告诉它用本地字节顺序存储数据。 然后我们将其转换成一个浮点数据, 以便我们用它来保存浮点数据。 最后, 我们把阵列复制到缓冲中 。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签