Harold Serrano

View Original

The 11 steps to render in OpenGL ES

The OpenGL API is vast. It has so many function calls that remembering them is an uphill battle. OpenGL is also a state machine. There are so many steps that you need to remember. Rendering in OpenGL is quite simple, but, the steps to do so may be forgotten after a couple of days. It happens to me. I tend to work on my 3D game engine daily and I still forget these steps. I decided to set up a post so I can remember how to render in OpenGL ES.

Here are the 11 steps required to render an object in OpenGL ES. I hope they are helpful to you. Try to use these steps as a cheat-sheet.

Let's start:

Assume characterData contains the data of your character which you want to render:

//Vertex data of character
float characterData[36]={1.0,0.4,0.9,1.0,....};

Step 1. Generate a Vertex Array Object:

//1. Generate a Vertex Array Object. Assume you have a global GLuint myVertexArrayObject declared.
glGenVertexArraysOES(1,&myVertexArrayObject);

Step 2. Bind the Vertex Array Object:

//2. Bind the Vertex Array Object
glBindVertexArrayOES(myVertexArrayObject);

Step 3. Generate a Vertex Buffer Object:

//3. Create a vertex buffer object
GLuint myBuffer;

glGenBuffers(1,&myBuffer);

Step 4. Bind the Vertex Buffer Object:

//4. Bind the vertex buffer
glBindBuffer(GL_ARRAY_BUFFER,myBuffer);

Step 5. Load Data into the buffer:

//5. Load data in the buffer
glBufferData(GL_ARRAY_BUFFER,sizeof(characterData),characterData,GL_STATIC_DRAW);

Step 6. Get Location of Attributes in current active shader:

//6. Get the location of the shader attribute called "position". Assume positionLocation is a global GLuint variable

positionLocation=glGetAttribLocation(programObject, "position");

Step 7. Get Location of Uniform in current active shader:

//7. Get Location of uniform called "modelViewProjectionMatrix". Assume modelViewProjectionUniformLocation is a global GLuint variable.

modelViewProjectionUniformLocation = glGetUniformLocation(programObject,"modelViewProjectionMatrix");

Step 8. Enable the attribute location found in the shader:

//8. Enable the attribute location
glEnableVertexAttribArray(positionLocation);

Step 9. Link buffer data to shader attributes:

//9. Link the buffer data to the shader attribute locations and inform OpenGL about the types of data in bound buffers and any memory offsets needed to access the data

glVertexAttribPointer(positionLocation,3, GL_FLOAT, GL_FALSE, 0, (const GLvoid *)0);

Step 10. Draw using data in currently bound and enabled buffers:

//1. Bind the VAO
glBindVertexArrayOES(myVertexArrayObject);

//2. Start the rendering process
glDrawArrays(GL_TRIANGLES, 0, 36);

//3. Unbind the VAO
glBindVertexArrayOES(0);

Step 11. Delete previously generated buffer:

glDeleteBuffers(1, &myBuffer);

PS. Sign up to my newsletter and get OpenGL development tips.