The OpenGL tutorial that I was following only used a single model to demonstrate various points. I wanted to draw more than one model. After some research, I came to the conclusion that I needed to create a vertex array object, once for each model, and than I can use it to access buffers and attributes for each model. In order to do that, I am using this game object struct
.
(I'm not including the whole code because it is quite long.)
typedef struct GameObject {
GLuint VertexArray;
GLuint NumVertices;
kmMat4 ModelMatrix;
} GameObject;
For each object I want to draw, I am using loader function, such as the following:
#include "GameObject.h"
static const GLfloat triangle_vertices[] = {
0, 1, 0,
-1, -1, 0,
1, -1, 0
};
static const GLfloat triangle_colors[] = {
0.2, 0.2, 0.2,
0.5, 0.5, 0.5,
0.8, 0.8, 0.8
};
GameObject *newTriangle()
{
GameObject *triangle = malloc(sizeof(GameObject));
GLuint VertexBuffer;
GLuint ColorBuffer;
// 1. Generate VertexArray for object
glGenVertexArrays(1, &(triangle->VertexArray));
// Bind it
glBindVertexArray(triangle->VertexArray);
// Create and fill buffers vertex and color buffers
glGenBuffers(1, &VertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, VertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(triangle_vertices), triangle_vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(
0,
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0
);
glGenBuffers(1, &ColorBuffer);
glBindBuffer(GL_ARRAY_BUFFER, ColorBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(triangle_colors), triangle_colors, GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(
1,
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0
);
// unbind vertex array
glBindVertexArray(0);
triangle->NumVertices = (sizeof(triangle_vertices)/sizeof(GLfloat));
kmMat4Identity(&(triangle->ModelMatrix));
return triangle;
}
And I am using this function to draw each game object:
#include "GameObject.h"
void Draw(GameObject *go, kmMat4 *VP, GLuint MatrixID)
{
kmMat4 MVP;
kmMat4Multiply(&MVP,VP,&go->ModelMatrix);
// Bind vertex array
glBindVertexArray(go->VertexArray);
// set mvp matrix
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, MVP.mat);
// Draw object
glDrawArrays(GL_TRIANGLES, 0, go->NumVertices);
// Unbind VertexArray
glBindVertexArray(0);
}
And this is my main loop:
while (!glfwWindowShouldClose(window))
{
kmMat4 current_translation;
GLfloat translatex = 0;
GLfloat translatez = 0;
double current_time = glfwGetTime();
double delta_time = current_time - last_time;
last_time = current_time;
if (glfwGetKey( window, GLFW_KEY_RIGHT ) == GLFW_PRESS){
translatex += 2 * delta_time;
}
if (glfwGetKey( window, GLFW_KEY_LEFT ) == GLFW_PRESS){
translatex -= 2 * delta_time;
}
if (glfwGetKey( window, GLFW_KEY_UP ) == GLFW_PRESS){
translatez -= 2 * delta_time;
}
if (glfwGetKey( window, GLFW_KEY_DOWN ) == GLFW_PRESS){
translatez += 2 * delta_time;
}
kmMat4Translation(¤t_translation, translatex, 0, translatez);
kmMat4Multiply(&square->ModelMatrix, ¤t_translation, &square->ModelMatrix);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Draw(triangle, &vp, MatrixID);
Draw(square, &vp, MatrixID);
glfwSwapBuffers(window);
glfwPollEvents();
}