I’m working on a project using OpenGL 3.3 with a NVertex class defined as:
class NVertex
{
float x, y, z;
float FillR,FillG,FillB;
float WireframeR,WireframeG,WireframeB;
float Nx, Ny, Nz; //normals
public:
NVertex();
NVertex(float x, float y, float z, float r=1.f, float g=1.f, float b=1.f);
float GetX() const;
float GetY() const;
float GetZ() const;
void Set(float _x, float _y, float _z, float _Nx, float _Ny, float _Nz, float _FillR=1.f, float _FillG=1.f, float _FillB=1.f,float _WireR=0.f, float _WireG=0.f, float _WireB=1.f);
void SetFillColor(float _r, float _g, float _b);
void SetWireframeColor(float _r, float _g, float _b);
void SetPos(float _x, float _y, float _z);
void SetNormal(float _x, float _y, float _z);
};
Elsewhere in the program, I generate the appropriate buffers.
void MyNGLWidget::GenBuffers()
{
//NFault is a class containing a Mesh class containing the vertexes and indexes.
const NFault* fault = Fault.GetFault();
glGenBuffers(1, &Environment.VBO);
glBindBuffer(GL_ARRAY_BUFFER, Environment.VBO);
glBufferData(GL_ARRAY_BUFFER, fault->GetMesh().Size(), fault->GetMesh().GetVertexes(), GL_STATIC_DRAW);
glEnableVertexAttribArray(0); //VERTEX COORDINATES
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(NVertex), BUFFER_OFFSET(0));
glEnableVertexAttribArray(1); //VERTEX COLORS
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(NVertex), BUFFER_OFFSET(12));
glEnableVertexAttribArray(2); //MESH COLORS
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(NVertex), BUFFER_OFFSET(24));
glEnableVertexAttribArray(3); //NORMALS
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(NVertex), BUFFER_OFFSET(36));
glGenBuffers(1,&Environment.VIO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,Environment.VIO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int)*fault->GetMesh().GetNumIndexes(), fault->GetMesh().GetIndexes(),GL_STATIC_DRAW);
}
All this works perfectly, no problem whatsoever (the shaders are compiled elsewhere). Here’s my question, though:
What if I want to change the color of the Mesh (that is, of all the Vertexes contained within it)? Do I simply call
glBindBuffer(GL_ARRAY_BUFFER, Environment.VBO);
glBufferData(GL_ARRAY_BUFFER, fault->GetMesh().Size(), fault->GetMesh().GetVertexes(), GL_STATIC_DRAW);
again (after having altered the mesh colors)? I saw in the doc that this deletes the previous values and puts in new ones. However, do I also have to re-call all the glEnableVertexAttribArray/glVertexAttribPointer calls, or are those maintained?
As well, is there a way of changing only the values of a certain VertexAttribArray? It’s a bit silly copying the exact same coordinates and normals in order to change the colors.
Or would I have to break up all these attributes into different buffers? In fact, what’s the difference between having one giant buffer as I have now, and having different buffers for different attributes of each vertex? Which is best, given that there’s a good chance the colors will have to change?