Creating a Vertex Buffer

Initialises shaders, buffers and other state required to begin rendering with OpenGL ES

These steps will create a vertex buffer and load some hardcoded data into it.

What are Buffer Objects?:

To operate on any data, OpenGL first needs to be able to access it. The GPU maintains a separate pool of memory which is independent of the CPU. While on many embedded systems these are in the same physical memory, the distinction exists so that memory can be used and allocated without needing to worry about synchronising with any other processors in the device.

Data therefore needs to be uploaded into buffers, which are reserved sections of memory for the GPU to use. By creating a buffer and giving it some data, the GPU will know how to render a triangle.

First the vertex data containing the positions of each point of the triangle and their corresponding texture co-ordinates needs to be defined.
What are Vertices?:

When rendering a polygon or model to screen, OpenGL ES has to be told where to draw the object and more fundamentally what shape it is. The data used to do this is referred to as vertices, which are points in 3D space usually collected into groups of three to render as triangles. Any advanced 3D shape in OpenGL ES is constructed from a series of these vertices, with each vertex representing one corner of a polygon.

The values which are going to be loaded into the vertex buffer are shown below. This data consists of three floats for the position co-ordinates and two floats for the texture co-ordinates.

The texture co-ordinates are the points on the texture image which are sampled to determine what colour the pixels will be at each vertex. More importantly, they are used as anchor points to interpolate between the vertices and find the texture coordinates of all the pixels inside the triangle. In this way they determine how the texture image maps onto the triangle.

 	GLfloat vertexData[] = { -0.4f, -0.4f, 0.0f, 0.0f, 0.0f, 0.4f, -0.4f, 0.0f, 1.0f, 0.0f, 0.0f, 0.4f, 0.0f, 0.5f, 1.0f };

Next generate the buffer object.

 	glGenBuffers(1, &_vertexBuffer);

This function binds the buffer as a vertex buffer so it can be filled with data. OpenGL ES functions which specify the target binding point, GL_ARRAY_BUFFER, will now affect the object _vertexBuffer.

 	glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);

The buffer's size and usage need to be set, and the data needs to be loaded into the buffer.

Note the last argument - GL_STATIC_DRAW. This tells the driver that the intention is to read from the buffer on the GPU, and the data will not be modified until it is finished with.

 	glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);

Check for any GL Errors after a GL call.

 	if (!testGLError(_surfaceData, "glBufferData"))
 		return false;
 	return true;