Jump to content

  • Log In with Google      Sign In   
  • Create Account


david92n

Member Since 05 May 2012
Offline Last Active May 24 2012 02:27 PM
-----

Topics I've Started

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

05 May 2012 - 08:46 AM

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[i].isQuad()){
	verts = 4;
	glBegin(GL_QUADS);
   }else{
	verts = 3;
	glBegin(GL_TRIANGLES);
   }
  
   int* vertId = mFaces[i].getVertInd();
   int* normId = mFaces[i].getNormInd();
   int* texId = mFaces[i].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!

PARTNERS