# Best practices with Vertices in Open GL

What is the best practice in regards to storing vertex data in Open GL? I.e:

struct VertexColored {
public:
GLfloat position[];
GLfloat normal[];

byte colours[];
}

class Terrian  {
private:
GLuint vbo_vertices;
GLuint vbo_normals;
GLuint vbo_colors;
GLuint ibo_elements;

VertexColored vertices[];
}


or having them stored seperatly in the required class like:

class Terrian  {
private:
GLfloat vertices[];
GLfloat normals[];
GLfloat colors[];

GLuint vbo_vertices;
GLuint vbo_normals;
GLuint vbo_colors;
GLuint ibo_elements;
}

• i keep hearing that having your per-vertex values intertwined is important for performance, would be great if somebody could explain why exactly – dreta Jul 14 '12 at 12:36
• Second example has wrong code: GLfloat[] ... -> GLfloat ...[] – kravemir Jul 14 '12 at 12:42
• Yup sorry, I just whipped it up to give an example, I come from a C# background where you declare arrays like so: GLfloat[] array; – Darestium Jul 15 '12 at 1:07

There isn't any best practice. Everything varies on application which is being developed.

For game renderer there is no need to store vertices in both RAM and VRAM, that means you need to keep only vbo id. Exception is terrain where you need to have access to positions because of height player placement/collisions.

Do you need to have all vertex data together? If you want to have multiple colors for same vertices like different colors for same player models then putting everything together can cause little obstacle in development.

Do you access data outside from class? If no, you will only end up with more code like vertices->position[i] instead of positions[i]. In game/model designer you need to access them from outside, but you aren't accessing all data at same time when you are modeling then you access positions and when you are coloring then you access colors.

Only thing i would put together is xyz (rgb) values into one structure for nicer code:

Vector3 {
GLfloat x,y,z;
};

Color3 {
GLbyte r,g,b;
};


Edit: I'd like to add, that some people are using unions for easier glVertex*3v, glVertex*4v, glUniform*v... calling where array is passed instead of separated variables:

union Vector3f {
GLfloat v[3];
struct {
GLfloat x,y,z;
};
};

union Color3b {
GLbyte c[3];
struct {
GLbyte r,g,b;
};
};

• If you want different colors for same model, you just change textures, or have everything in grayscale and multiply by color (or color texture map) in pixel shader. But dont keep cpu data if you dont need to. But sometimes you want to keep them, for example for raycasting, that depends, but keep just vertices. – Notabene Jul 15 '12 at 3:55