OpenGL OBJ model loader, problem with quads (c++)

Started by
1 comment, last by David Norberg 11 years, 11 months ago
Hi, I am making a simple model-loader för .OBJ files being rendered with OpenGL and using SFML for handling windows. It works fine with models that only uses polygons with 3 verticies but quads are broken. When polygonmode "GL_LINE" is used everything looks fine but with "GL_FILL" there are big holes. (see screenshots)



else if(line[0] == 'f' && line[1] == ' '){
int v1 = 1, t1 = 1, n1 = 1, v2 = 1, t2 = 1, n2 = 1, v3 = 1, t3 = 1, n3 = 1, v4 = 1, t4 = 1, n4 = 1;
if(std::count(line.begin(), line.end(), ' ') == 3){//Triangle
if(line.find("/") == std::string::npos)
sscanf(line.c_str(), "f %d %d %d", &v1, &v2, &v3);
else if(line.find("//") != std::string::npos)
sscanf(line.c_str(), "f %d//%d %d//%d %d//%d", &v1, &n1, &v2, &n2, &v3, &n3);
else
sscanf(line.c_str(), "f %d/%d/%d %d/%d/%d %d/%d/%d", &v1, &t1, &n1, &v2, &t2, &n2, &v3, &t3, &n3);
int vertInd[] = {v1, v2, v3, 0};
int normInd[] = {n1, n2, n3, 0};
int texInd[] = {t1, t2, t3, 0};
mFaces.push_back(ModelFace(vertInd, normInd, texInd, false));
}else{//Quad
if(line.find("/") == std::string::npos)
sscanf(line.c_str(), "f %d %d %d %d", &v1, &v2, &v3, &v4);
else if(line.find("//") != std::string::npos)
sscanf(line.c_str(), "f %d//%d %d//%d %d//%d %d//%d", &v1, &n1, &v2, &n2, &v3, &n3, &v4, &n4);
else
sscanf(line.c_str(), "f %d/%d/%d %d/%d/%d %d/%d/%d %d/%d/%d", &v1, &t1, &n1, &v2, &t2, &n2, &v3, &t3, &n3, &v4, &t4, &n4);

int vertInd[] = {v1, v2, v3, v4};
int normInd[] = {n1, n2, n3, n4};
int texInd[] = {t1, t2, t3, t4};
mFaces.push_back(ModelFace(vertInd, normInd, texInd, true));
}
}

This code is for loading the indicies for the faces.




glNewList(mIndex, GL_COMPILE);
for(size_t i = 0; i < mFaces.size(); ++i){
int verts = 0;
if(mFaces.isQuad()){
verts = 4;
glBegin(GL_QUADS);
}else{
verts = 3;
glBegin(GL_TRIANGLES);
}

int* vertId = mFaces.getVertInd();
int* normId = mFaces.getNormInd();
int* texId = mFaces.getTexInd();
for(int n = 0; n < verts; ++n){
if(mHasNormals){
sf::Vector3f norm = mVertexNormals[normId[n] - 1];
glNormal3f(norm.x, norm.y, norm.z);
}
if(mHasTexture){
sf::Vector2f tex = mTexCoords[texId[n] - 1];
glTexCoord2f(tex.x, tex.y);
}
sf::Vector3f vert = mVerticies[vertId[n] - 1];
glVertex3f(vert.x, vert.y, vert.z);
}
glEnd();
}

glEndList();

This is for compiling the model




Thanks!
Advertisement
Also if someone have a tips on how to improve how I see if it is a QUAD or not, that would be appreciated smile.png


if(std::count(line.begin(), line.end(), ' ') == 3)
Problem solved...didn't expect polygons with 25 vertices. Now it can take any number of vertices on a polygon and the holes are fixed!

This topic is closed to new replies.

Advertisement