I don't quite understand how you do movement in OpenGL without using glPopMatrix and glPushMatrix. I can only get it to work by calling glDrawArray on several VAOs.
In this example below, I can move one triangle by using the shaders to do matrix multiplication. What I don't understand is say I have several points array I.e. points2[], points3[]. How would I go about being able to do translation without having to draw several VAOs? So how do I put everything into one VAO and still be able to say just multiple matrix3[] with points3[]?
float points[] = {
0.0f, 0.0f, 0.0f,
0.0f, 0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
0.0f, 0.0f, 0.0f,
0.5f, 0.0f, 0.0f,
0.5f, 0.5f, 0.0f
};
float matrix[] = {
1.0f, 0.0f, 0.0f, 0.0f, // first column
0.0f, 1.0f, 0.0f, 0.0f, // second column
0.0f, 0.0f, 1.0f, 0.0f, // third column
0.5f, 0.0f, 0.0f, 1.0f // fourth column
};
unsigned int points_vbo = 0;
glGenBuffers (1, &points_vbo);
glBindBuffer (GL_ARRAY_BUFFER, points_vbo);
glBufferData (GL_ARRAY_BUFFER, 18 * sizeof (float), &points, GL_STATIC_DRAW);
unsigned int vao = 0;
glGenVertexArrays (1, &vao);
glBindVertexArray (vao);
glBindBuffer (GL_ARRAY_BUFFER, points_vbo);
glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, (GLubyte*)NULL);
glBindBuffer (GL_ARRAY_BUFFER, colours_vbo);
glVertexAttribPointer (1, 3, GL_FLOAT, GL_FALSE, 0, (GLubyte*)NULL);
glEnableVertexAttribArray (0);
glEnableVertexAttribArray (1);
//Setup shaders
std::string vertex_shader = loadshaders("test_vs.txt");
std::string fragment_shader = loadshaders("test_fs.txt");
unsigned int vs = glCreateShader (GL_VERTEX_SHADER);
const char* str = vertex_shader.c_str ();
glShaderSource (vs, 1, &str, NULL);
glCompileShader (vs);
unsigned int fs = glCreateShader (GL_FRAGMENT_SHADER);
const char* strb = fragment_shader.c_str ();
glShaderSource (fs, 1, &strb, NULL);
glCompileShader (fs);
unsigned int shader_programme = glCreateProgram ();
glAttachShader (shader_programme, fs);
glAttachShader (shader_programme, vs);
glLinkProgram (shader_programme);
int matrix_location = glGetUniformLocation (shader_programme, "matrix");
glUseProgram (shader_programme);
glUniformMatrix4fv (matrix_location, 1, GL_FALSE, matrix);