3

In java using openGL (ES) we can directly allocate ByteBuffer like

ByteBuffer bf ;
bf.allocateDirect();

But we can not do that in case of FloatBuffer it is not aviliable , why is that ?

I was wondering if it is because of :

Byte is accessible in hardware level (as OpenGL works just above hardware unlike delvik ) and registers in hardware (hardware of GPU ) are in bytes , even floating points numbers should be stored in 4 byte register which may not be available so we cannot allocate directly , rather we should tell the buffer to allocate the memory for a block of given size and after that put the data in those blocks and treat it again as FloatBuffer.

Suma
  • 33,181
  • 16
  • 123
  • 191
erluxman
  • 18,155
  • 20
  • 92
  • 126

1 Answers1

3

OpenGL es is written in c. In c floats, integers etc are not fixed size like java. A float point number in java is 32 bits. Now lets examine how java uses opengl es. When you send vertices to the graphics pipeline using java you actually call c functions that do the dirty work for you. This is called ndk and you find more info here : https://developer.android.com/tools/sdk/ndk/index.html. C is translated to assembly code thus each float can have different byte size on each phone depending on cpu architecture. You use nio buffers ( more here : https://docs.oracle.com/javase/7/docs/api/java/nio/Buffer.html ) to assure that your float array sizes are BASED on your phone's cpu architecture(native order) and not on jvm fixed primitive sizes. Lastly, imagine you have a vertices array of java floats (32bit fixed size). Your cpu floats are 64 bit. If you call an opengl es function from java your program will end up crashing. Hope i helped.

KostasRim
  • 2,053
  • 1
  • 16
  • 32
  • OpenGL uses its own types for floats, ints, etc, which have well defined sizes. There is no platform dependence in the size of these types. A `GLfloat` is guaranteed to be 32 bits on every platform. – Reto Koradi Dec 02 '14 at 15:59
  • Yes a glfloat is guaranteed to be 32 bits, but when you send vertices in opengl es they are not GLfloats but floats which means that are platform dependent and you have to use nio buffers. Correct me if i am wrong, i am not experienced as you. – KostasRim Dec 02 '14 at 20:35