Wow, so many replies! Thanks!
To clarify what I want to do: The Triangle class will be used only for the ground. For other type of objects I will make seperate classes. And I want to perform the most basic actions on the vertices like move, rotate, scale.(like in blender) This is inspired by the game soldat. This is an example of a map in soldat.
How the drawing is done: In two passes. Somewhere in my main loop I call triangles->draw() and later selection->draw(). The Triangle class manages the vertex buffer for the triangles. It has functions to add/remove triangles and if the buffer is full it automatically creates a new buffer with twice the size and copies the data. The buffer has this structure:
struct Vertex {
float x, y; // position
float u, v; // texturecoords
char r, g, b, a; // color
};
The Triangle class also has the index buffer for drawing the triangles.
This is the draw function for the triangles:
void Triangles::draw() {
glUseProgram(shaderProgram);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferId);
// vertex
glVertexAttribPointer(vertexPositionLocation, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
// texCoord
glVertexAttribPointer(texCoordLocation, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(2 * sizeof(float)));
// color
glVertexAttribPointer(vertexColorLocation, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), (GLvoid*)(4 * sizeof(float)));
glEnableVertexAttribArray(vertexPositionLocation);
glEnableVertexAttribArray(texCoordLocation);
glEnableVertexAttribArray(vertexColorLocation);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferId);
glEnable(GL_TEXTURE_2D);
for(int i = 0; i < triangleCount; ++i) {
if(trianglesData[i].isUsed) {
glBindTexture(GL_TEXTURE_2D, trianglesData[i].texture->id);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, (GLvoid*)(sizeof(Triangle)*i));
}
}
glDisable(GL_TEXTURE_2D);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDisableVertexAttribArray(vertexColorLocation);
glDisableVertexAttribArray(texCoordLocation);
glDisableVertexAttribArray(vertexPositionLocation);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glUseProgram(0);
}
I will also replace the for loop in the future and instead use the spatial tree from Box2D to draw only currently visible triangles.
Now the problem is the draw method of the selection class. Currently I already render white points for the vertices and I reuse the vertex buffer from the Triangle class with a new index buffer. I want to do the same for the edges, however I need additional information. For every edge where at least one vertex is selected I need two booleans to know how to draw the line. e.g. if only one vertex is selected then the line should fade from full white to transparent. I think this information doesn't belong into the Triangle class because I might e.g. use the Triangle class later in the game itself where I don't need selections. So instead I want to create a new buffer with the booleans. What I don't know is how to use them both at once. e.g. can I even do something like this:
glBindBuffer(GL_ARRAY_BUFFER, triangleVertexBufferId);
glVertexAttribPointer(vertexPositionLocation, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glBindBuffer(GL_ARRAY_BUFFER, selectionVertexBufferId);
glVertexAttribPointer(vertexSelectionLocation, 2, GL_FLOAT, GL_FALSE, sizeof(Selection), 0);
...
glDrawElements(GL_LINES, ...);
Wouldn't the result be that only the second buffer is bound when I draw the lines? But I need both.