When constructing my mesh I keep all the possible arrays of normals in another const static GLfloat[6][12] array. The mesh will only consist of square faces of a cube, so there is only six possible normals. When I add a face to the mesh I copy the data from the appropriate sub-array in my static array to an std::vector<GLfloat> and when finished I pass the data from the vector to the vertex buffer using glBufferData.
The static array:
static constexpr GLfloat facenormals[6][12] = {
{
0.0f, 1.0f, 0.0f, // TOP
// Each sub-array has four identical 3-part vectors, I only included one of each.
},
{
0.0f, -1.0f, 0.0f, // BOTTOM
},
{
0.0f, 0.0f, 1.0f, // FRONT
},
{
0.0f, 0.0f, -1.0f, // BACK
},
{
1.0f, 0.0f, 0.0f, // RIGHT
},
{
-1.0f, 0.0f, 0.0f, // LEFT
}
};
Copied into the vector using:
normals.insert(normals.end(), CubeData::facenormals[direction], CubeData::facenormals[direction] + 12);
Passed to vertex buffer:
glBufferData(GL_ARRAY_BUFFER, chunkmesh.normals.size() * sizeof(GLfloat), chunkmesh.getnormals(), GL_STATIC_DRAW);
Like this the vector is just a continous block of memory containing the normals in the order specified by the indexbuffer.
Is it possible and/or sane to instead fill the vector with pointers to the static data in the array and pass the pointers to the vertex buffer, and by doing so shaving the time it takes to copy the data from the mesh building time.
I measured the time it takes to build the mesh, including random generation, using Visual Studios debugging tools, and the copying of data used up over 40% of CPU time, positions, normals, and texturecoordinates.
[6][12]? Shouldn't it befacenormals[3][6]? Then it would beglBufferData(GL_ARRAY_BUFFER, 3*6*sizeof(GLfloat), &facenormals[0][0], GL_STATIC_DRAW);- Rabbid76