I'm having some issues with my .obj parser. My cube looks weird and I have no idea how to fix it.
Here is my code:
std::ifstream file(FileName);
std::string line;
std::vector<float> vp;
std::vector<float> vt;
std::vector<float> vn;
std::vector<Face> faces;
while (std::getline(file, line))
{
//check for vertices
if (line.substr(0, 2) == "v ")
{
ParseIntoFloat(line, vp);
}
//check for texture
else if (line.substr(0, 2) == "vt")
{
ParseIntoFloat2(line, vt);
}//check for Normals
else if (line.substr(0, 2) == "vn")
{
ParseIntoFloat(line, vn);
}
//check for faces
else if (line.substr(0, 2) == "f ")
{
MeshResource::ParseFace(line, 2, faces);
}
}
int currentIndex = 0;
for (int i = 0; i < faces.size(); i++)
{
//---------SOME THOUGHT----------\\
//Set an index for each vertex (3 vertex points, 2 UV, 3 normals) (see end of for loops)
for (int j = 0; j < faces[i].numConfigs; j++)
{
int index = vertices.size() / 8;
int baseIndex_Position = (faces[i].configs[j].i[0] - 1) * 3;
vertices.push_back(vp[baseIndex_Position]); //Which Coords? configs[0].i[0], configs[1].i[0], configs[2].i[0]? How to know?
vertices.push_back(vp[baseIndex_Position + 1]);
vertices.push_back(vp[baseIndex_Position + 2]);
int baseIndex_Texture = (faces[i].configs[j].i[1] - 1) * 2;
vertices.push_back(vt[baseIndex_Texture]);
vertices.push_back(vt[baseIndex_Texture + 1]);
//vertices.push_back(vt[faces[i].configs[k].i[1]-1]); //Same issue; I have to have ONLY two; from where?
int baseindex_Normals = (faces[i].configs[j].i[2] - 1) * 3;
vertices.push_back(vn[baseindex_Normals]);
vertices.push_back(vn[baseindex_Normals + 1]);
vertices.push_back(vn[baseindex_Normals + 2]);
indices.push_back(currentIndex);
currentIndex++;
}
}
SetupMesh();
SetupMesh:
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), &vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
GLint stride = sizeof(GLfloat) * ( 3 + 2 + 3);
GLint offsetToTexture = sizeof(GLfloat) * ( 3);
GLint offsetToNormals = sizeof(GLfloat) * ( 3 + 2);
//GLint offsetToColor = sizeof(GLfloat) * 3;
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0);
/*
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, stride, (void*)offsetToColor);*/
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, stride, (void*)offsetToTexture);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, stride, (void*)offsetToNormals);
glBindVertexArray(0);