Can I avoid sending vertex data every frame?

For sake of simplicity say I have a cube made of 12 triangles and I want it to rotate. The method I am using atm is:


float triangles[108] = {...values...}; // 12 triangles * 3 vertices * 3 floats per vertex
.
.
while (somecondition)
{

    int matrix[9] = get_matrix();

    GLuint buffer;

    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat), 108, triangles, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glUniformMatrix3fv(uniform_id, 1, GL_FALSE, matrix);
    glDrawArrays(GL_TRIANGLES, 0, 36);
    glDisableVertexAttribArray(0);
}

I realize I am sending the same 108 floats to opengl every frame, when all I really want is to update the uniform matrix.
How do I do this?

Move all of your code except for the glUniformMatrix3fv call outside of the loop.

Buffers you create with glGenBuffers are permanent until you release them with glDeleteBuffers. The data you transfer with glBufferData is also persistent until you overwrite it with another glBufferData or glBufferSubData call.

glDrawArrays should also remain inside the loop. The new code will look like:

GLuint buffer; 
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat), 108, triangles, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

while (somecondition)
{
    int matrix[9] = get_matrix();
 
    glUniformMatrix3fv(uniform_id, 1, GL_FALSE, matrix);
    glDrawArrays(GL_TRIANGLES, 0, 36);
}

glDisableVertexAttribArray(0);