lucasyyy lucasyyy - 1 year ago 154
C Question

obj model loading texture coords and vertex position not loaded correctly

im loading a 3d .obj model to my game, however this is what it looks like now:3d model

which definitely is not what it suppose to look like. im thinking maybe is my way of connecting the indices are wrong, but I just can't find the problem

this is my code:

char lineHeader[128];
FILE * file = fopen(fileName, "r");

if (file == NULL) {
printf("Impossible to open the file !\n");

vector <vec3> positionVec;
vector <vec2> texCoordVec;
vector <vec3> normalVec;
vector <GLuint> vertexIndices;
vector <GLuint> uvIndices;
vector <GLuint> normalIndices;

while (true) {
char lineHeader[128];
int res = fscanf(file, "%s", lineHeader);

if (res == EOF) {

if (strcmp(lineHeader, "v") == 0) {
glm::vec3 vertex;
fscanf(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z);
else if (strcmp(lineHeader, "vt") == 0) {
glm::vec2 uv;
fscanf(file, "%f %f\n", &uv.x, &uv.y);
else if (strcmp(lineHeader, "vn") == 0) {
glm::vec3 normal;
fscanf(file, "%f %f %f\n", &normal.x, &normal.y, &normal.z);
else if (strcmp(lineHeader, "f") == 0) {
int vertexIndex1, vertexIndex2, vertexIndex3, uvIndex1, uvIndex2, uvIndex3, normalIndex1, normalIndex2, normalIndex3;
fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &vertexIndex1, &uvIndex1, &normalIndex1, &vertexIndex2, &uvIndex2, &normalIndex2, &vertexIndex3, &uvIndex3, &normalIndex3);

vector <vec3> vertexPositions;
vector <vec2> textureCoords;
vector <vec3> normals;

for (unsigned int i = 0; i < vertexIndices.size(); i++) {
vertexIndices[i] = vertexIndices[i] - 1;

for (unsigned int i = 0; i < uvIndices.size(); i++) {
unsigned int index = uvIndices[i];
vec2 uv = texCoordVec[index - 1];

for (unsigned int i = 0; i < normalIndices.size(); i++) {
unsigned int index = normalIndices[i];
vec3 normal = normalVec[index - 1];

GLuint modelVertecesBuffer;
glGenBuffers(1, &modelVertecesBuffer);
glBindBuffer(GL_ARRAY_BUFFER, modelVertecesBuffer);
glBufferData(GL_ARRAY_BUFFER, positionVec.size() * sizeof(glm::vec3), &positionVec[0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

GLuint modelTextureCoordBuffer;
glGenBuffers(1, &modelTextureCoordBuffer);
glBindBuffer(GL_ARRAY_BUFFER, modelTextureCoordBuffer);
glBufferData(GL_ARRAY_BUFFER, texCoordVec.size() * sizeof(vec2), &texCoordVec[0], GL_STATIC_DRAW);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, 0);

GLuint modelNormalBuffer;
glGenBuffers(1, &modelNormalBuffer);
glBindBuffer(GL_ARRAY_BUFFER, modelNormalBuffer);
glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(vec3), &normals[0], GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);

GLuint positionIndicesBuffer;
glGenBuffers(1, &positionIndicesBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, positionIndicesBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, vertexIndices.size() * sizeof(int), &vertexIndices[0], GL_STATIC_DRAW);

Answer Source

There are a few things:

Indices in the OBJ model are 1-based. You consider this when filling the vectors vertexPositions etc. But you don't use these vectors after that. Using the indices from the OBJ directly as an index buffer won't work. You have to subtract one. As genpfault mentioned in the comments, indices can even be negative for relative referencing.

Face vertices in the OBJ may reference different position and normal ids (and texture coordinate ids). This is something that you cannot represent in OpenGL and you would need to duplicate some data. So this approach will only work if the referenced ids are all the same.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download