I was using the book “Game Programming in C++: Creating 3D Games (Game Design)” to learn how to program a game with C++, SDL and Opengl. I was trying to make a simple cube, but beforehand I searched in the internet about it and find out that you need to use 24 vertices to make a cube, but then I went to see in the book how they did it and I stumbled upon this.
The Cube.gpmesh they use looks like this.
{
"version":1,
"vertexformat":"PosNormTex",
"shader":"BasicMesh",
"textures":[
"Assets/Cube.png"
],
"specularPower":100.0,
"vertices":[
[-0.5,-0.5,-0.5,0,0,-1,0,0],
[0.5,-0.5,-0.5,0,0,-1,1,0],
[-0.5,0.5,-0.5,0,0,-1,0,-1],
[0.5,0.5,-0.5,0,0,-1,1,-1],
[-0.5,0.5,0.5,0,1,0,0,-1],
[0.5,0.5,0.5,0,1,0,1,-1],
[-0.5,-0.5,0.5,0,0,1,0,0],
[0.5,-0.5,0.5,0,0,1,1,0],
[-0.5,0.5,-0.5,0,0,-1,0,-1],
[0.5,-0.5,-0.5,0,0,-1,1,0],
[-0.5,0.5,-0.5,0,1,0,0,-1],
[0.5,0.5,-0.5,0,1,0,1,-1],
[-0.5,0.5,0.5,0,1,0,0,-1],
[-0.5,0.5,0.5,0,0,1,0,-1],
[0.5,0.5,0.5,0,0,1,1,-1],
[-0.5,-0.5,0.5,0,0,1,0,0],
[-0.5,-0.5,0.5,0,-1,0,0,0],
[0.5,-0.5,0.5,0,-1,0,1,0],
[-0.5,-0.5,-0.5,0,-1,0,0,0],
[0.5,-0.5,-0.5,0,-1,0,1,0],
[0.5,-0.5,-0.5,1,0,0,1,0],
[0.5,-0.5,0.5,1,0,0,1,0],
[0.5,0.5,-0.5,1,0,0,1,-1],
[0.5,0.5,0.5,1,0,0,1,-1],
[-0.5,-0.5,0.5,-1,0,0,0,0],
[-0.5,-0.5,-0.5,-1,0,0,0,0],
[-0.5,0.5,0.5,-1,0,0,0,-1],
[-0.5,0.5,-0.5,-1,0,0,0,-1]
],
"indices":[
[2,1,0],
[3,9,8],
[4,11,10],
[5,11,12],
[6,14,13],
[7,14,15],
[18,17,16],
[19,17,18],
[22,21,20],
[23,21,22],
[26,25,24],
[27,25,26]
]
}
As you can see, it uses 28 vertices, not 24. There is 4 extra vertices.
And the code to the Mesh.cpp
VertexArray::Layout layout = VertexArray::PosNormTex;
size_t vertSize = 8;
...
// Load in the vertices
const rapidjson::Value& vertsJson = doc["vertices"];
if (!vertsJson.IsArray() || vertsJson.Size() < 1)
{
SDL_Log("Mesh %s has no vertices", fileName.c_str());
return false;
}
std::vector<Vertex> vertices;
vertices.reserve(vertsJson.Size() * vertSize);
mRadius = 0.0f;
for (rapidjson::SizeType i = 0; i < vertsJson.Size(); i++)
{
// For now, just assume we have 8 elements
const rapidjson::Value& vert = vertsJson[i];
if (!vert.IsArray())
{
SDL_Log("Unexpected vertex format for %s", fileName.c_str());
return false;
}
Vector3 pos(vert[0].GetDouble(), vert[1].GetDouble(), vert[2].GetDouble());
mRadius = Math::Max(mRadius, pos.LengthSq());
mBox.UpdateMinMax(pos);
if (layout == VertexArray::PosNormTex)
{
Vertex v;
// Add the floats
for (rapidjson::SizeType j = 0; j < vert.Size(); j++)
{
v.f = static_cast<float>(vert[j].GetDouble());
vertices.emplace_back(v);
}
}
else
{
...
}
}
As you can see in the “Vector3 pos(…)” line, it takes the first 3 elements from each row to make the position of the vertice, as is confirmed in VertextArray.cpp were they say that the first 3 elements of the VertexArray are for position, another 3 are used for Normals and the last 2 are used for Texture, that checks up, but the array still has a size of 28…