BufferUtilities - Fish-In-A-Suit/Conquest GitHub Wiki

This class provides the functionality for creating various buffer objects. Buffer objects are required primarily by the vbo code --> to send data to the vbo, you first take an array of data, then call the method which corresponds with the data type of array elements to store the array elements in a corresponding buffer type and then send that to a vbo.

Class layout

Imports

  • org.lwjgl.BufferUtils
  • java.nio.FloatBuffer
  • java.nio.IntBuffer

Public methods

  • static FloatBuffer storeDataInFloatBuffer(float[] array)
  • static IntBuffer storeDataInIntArray(int[] array)

Explanation

FloatBuffer storeDataInFloatBuffer(float[] array)

This method takes in an array of floats as a parameter. Then, it creates an empty buffer of type FloatBuffer named result by assigning it the same number of elements as the length of the input array: FloatBuffer result = BufferUtils.createFloatBuffer(array.length); To accomplish this, the following method is used:

  • BufferUtils.createFloatBuffer(int length), where length is the length of the input array.

Then, the newly created buffer is populated with the elements of the input array using the relative bulk put method for FloatBuffer type: public final FloatBuffer put(float[] src) ... result.put(array);

Then, after having been populated with data, the floatbuffer has to be prepared to be read from. flip() will set the limit of the buffer to the current position and reset the position to zero, which has to be done if we wish future buffer operations to perform as they should.

A nice little test that can be done to check how the above works is to use the toString() method on a FloatBuffer object, which prints out the description of the buffer. Use it like this:

	public static FloatBuffer storeDataInFloatBuffer(float[] array) {
		FloatBuffer result = BufferUtils.createFloatBuffer(array.length);
		System.out.println("The state of the floatbuffer prior to populating it with data: " + result.toString());
		
		result.put(array);
		System.out.println("The state of the floatbuffer after populating it with data: " + result.toString());

		result.flip();
		System.out.println("The state of the floatbuffer after flipping it: " + result.toString());

		return result;
	}
	

After calling this method and providing a valid array as parameter, the following gets printed to console:

The state of the floatbuffer prior to populating it with data: java.nio.DirectFloatBufferU[pos=0 lim=12 cap=12]
The state of the floatbuffer after populating it with data: java.nio.DirectFloatBufferU[pos=12 lim=12 cap=12]
The state of the floatbuffer after flipping it: java.nio.DirectFloatBufferU[pos=0 lim=12 cap=12]

As you can see, the lim and cap are equal to 12 all thorughout (this has to do with the length of the inpu array). You can imagine lim and cap as marks on a ruler going from 0 to 11, incrementing by 1 each step (0, 1, 2 etc). Think of pos as a marker, which slides up and down this rules during the lifetime of a buffer object. Just after a buffer is created, it's marker will be at 0 (at the start of the ruler), meaning that it's ready to face various operations to be performed on it. When populating a buffer with data from the input array, the marked slides along the ruler for each element of the array that is stored at some index of a buffer. At the end, when all of the contents of the array are stored inside the buffer, the marker will be at the end of the ruler (pos will be the same as lim and cap). Now, if we perform any operations on this buffer, the operation will be started at the current position of pos (marker), which is currently at the end of the buffer. This isn't what we want in most cases, so the buffer has to be flipped. Calling the flip() method on a buffer resets it's pos value to 0, meaning that any further operations will be successful, since they will be started at the beginning of a buffer.

IntBuffer sotreDataInIntBuffer(int[] array)

similar to storeDataInFloatBuffer(float[] array)