GL\Buffer\FloatBuffer comes with the PHP-GLFW extension to more efficiently
handle large arrays of
GLfloat values (
float in php).
Don't confuse these objects with actual GPU buffers.
FloatBuffer stores an array of real
GLfloat values internally, which saves a lot of memory
and makes interaction with the GL API a lot easier. An instance of this object can be used similarly to an array but
comes with obvious limitations. The internal implementation is similar to c++ std::vector, this means you do not have to
specify the exact size / num of elements when constructing the object.
Again the object can be used similarly to an array, but it's not a hash map,
so all keys have to be of type
int. They also have to be in order and explicitly created.
use GL\Buffer\FloatBuffer; $buffer = new FloatBuffer; // values can be pushed into the array using subscript $buffer = 42.69; $buffer = 3.14; // values can be accessed like an array echo $buffer; // 42.69 // values can be modifed like an array // just make sure the used index actually exists, // it will NOT be created implicitly! $buffer = 1.23; // dumping the buffer will display the first 127 value var_dump($buffer);
The buffer object resizes itself logarithmically, so if you are planning to insert 10 million elements,
be sure to call the
reserve method on the object first. As you might guess, it will reserve the
given space, ensuring that the internal buffers are not unnecessarily resized multiple times.
Construct from PHP Array¶
For ease of use, the
FloatBuffer constructor accepts a PHP array which will be pushed
value by value into the buffer. The constructor does a sanity check, so you will receive an error when
your array contains invalid values.
FloatBuffer class exposes the following methods to userland.
pushes a value into the buffer, this is exactly the same as when you would write
$buffer = 3.14.
$valueThe value to be pushed into the buffer.
pushes an array of values into the buffer. This works the same as when you pass inital data to the constructor.
$valuesThe values to be pushed into the buffer.
Pushes the values of a Vec2 object into the buffer
Pushes the values of a Vec3 object into the buffer
Pushes the values of a Vec4 object into the buffer
Pushes the values of a Mat4 (16) object into the buffer
Fills the buffer with
$count amount of values. The second argument is the value that is filled in.
int $countThe number of elements to fill.
float $valueThat value that will be filled in.
Fill will never shrink a buffer down, meaing when you aleady pushed 10 values in, and then decide to fill 4 values, the other 6 values will remain at the end.
Reserves the $size amount of space. Values are still undefined.
$sizeThe number of elements to reserve space for.
Will clear the Buffer, internally this will release all allocated data. Meaning any reserved space will be freed.
Returns the actual size / number of elements of the buffer.
intbuffer size in element count (not bytes)
Returns the current reserved space of the buffer.
intbuffer space in element count (not bytes)