问题描述
我一直在用我的Android code FloatBuffers一段时间(从一些opengles教程中复制),但我不明白到底是什么,这种结构是,为什么它是必要的。
I've been using FloatBuffers in my Android code for a while (copied it from some opengles tutorial), but I cannot understand exactly what this construct is and why it is needed.
例如该code(或类似)我在许多许多人的code和Android的教程,请参阅:
For example this code (or similar) I see in many many peoples' code and android tutorials:
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
这看起来太过冗长,凌乱的东西而据我可以告诉是float数组大约只是一个花哨的包装。
This seems awfully verbose and messy for something which as far as I can tell is just a fancy wrapper around of an array of floats.
问题:
-
有什么理由这种类型的类(ByteBuffer的,FloatBuffer),而不是任何其他类型的集合或简单的花车阵?
What is the justification for this type of class (ByteBuffer, FloatBuffer), as opposed to any other kind of collection or simple array of floats?
什么是背后将其转换成一个FloatBuffer之前创建的ByteBuffer的想法?
What's the idea behind creating a ByteBuffer before converting it into a FloatBuffer?
推荐答案
主要的原因是性能:的ByteBuffers和其他NIO类使加速操作时避免需要将数据复制到与本机code接口(通常一个临时缓冲器)。
The main reason is performance: ByteBuffers and the other NIO classes enable accelerated operations when interfacing with native code (typically by avoiding the need to copy data into a temporary buffer).
这是pretty的重要的,如果你是做了很多的OpenGL渲染调用,例如。
This is pretty important if you are doing a lot of OpenGL rendering calls for example.
原因首先创建一个ByteBuffer的是,你要使用的allocateDirect调用创建的直接的字节的缓冲区,这得益于加速操作。然后,从这个共享同一内存中创建一个FloatBuffer。该FloatBuffer本身不具有allocateDirect方法,出于某种原因,这就是为什么你必须通过ByteBuffer中去。
The reason for creating a ByteBuffer first is that you want to use the allocateDirect call to create a direct byte buffer, which benefits from the accelerated operations. You then create a FloatBuffer from this that shares the same memory. The FloatBuffer doesn't itself have an allocateDirect method for some reason, which is why you have to go via ByteBuffer.
这篇关于的FloatBuffer而不是浮动为什么[]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!