Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!

1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Member Since 28 Jun 2011
Offline Last Active Jun 24 2012 12:29 PM

Topics I've Started

Obj Importer

20 June 2012 - 03:30 AM

Hello everyone, I am new to direct 3d and really need some help with vertex normals. I have written a function to import an obj file. The function works fine but the normals are all weird. Why are there less or in some cases greater number of vertex normals then vertices? More importantly how do I deal with them?

Any help will be greatly appreciated.

[source lang="cpp"]int Create_array_size( std::string file,std::string keyword){ int x=0; std::string line; std::ifstream myfile (file); if (myfile.is_open()) { while ( myfile.good() ) { getline(myfile,line); if(line!="") if(line.at(0)=='#'&&line.find_first_of("0123456789-")!=std::string::npos) if(line.find(keyword)!= std::string::npos) { x += atoi(line.substr(2,line.find("vertices")-2).c_str()); } } myfile.close(); } return x;}void Ver_Nor_Fac(){ vertices_no = Create_array_size("truck.obj","vertices"); faces_no = Create_array_size("truck.obj","faces" ); vertices=new SimpleVertex[vertices_no]; indices =new DWORD [faces_no*3 ]; for(int x=0;x<vertices_no;x++) { vertices[x].Norm.x=0.3f; vertices[x].Norm.y=0.5f; vertices[x].Norm.z=0.8f; } int ver=0,fac=0,nor=0,n; std::string line1; std::ifstream myfile ("truck.obj"); if (myfile.is_open()) { while ( myfile.good() ) { getline(myfile,line1); if(line1!="") if(line1.at(0)=='v'&&line1.at(1)!='n') { n=line1.find_first_of(' ',line1.find_first_of("0123456789-")); vertices[ver].Pos.x=atof(line1.substr(line1.find_first_of("0123456789-"),n).c_str())/3; vertices[ver].Pos.y=atof(line1.substr(n,line1.find_first_of(' ',n)).c_str())/3; n=line1.find_first_of(' ',n+1); vertices[ver].Pos.z=atof(line1.substr(n).c_str())/3; ver++; } else if(line1.at(0)=='v'&&line1.at(1)=='n') { if(nor+1>vertices_no) continue; n=line1.find_first_of(' ',line1.find_first_of("0123456789-")); vertices[nor].Norm.x=atof(line1.substr(line1.find_first_of("0123456789-"),n).c_str()); vertices[nor].Norm.y=atof(line1.substr(n,line1.find_first_of(' ',n)).c_str()); n=line1.find_first_of(' ',n+1); vertices[nor].Norm.z=atof(line1.substr(n).c_str()); nor++; } else if(line1.at(0)=='f') { n=line1.find_first_of(' ',line1.find_first_of("0123456789-")); indices[fac]=atoi(line1.substr(line1.find_first_of("0123456789-"),n).c_str())-1; fac++; indices[fac]=atoi(line1.substr(n,line1.find_first_of(' ',n)).c_str())-1; fac++; n=line1.find_first_of(' ',n+1); indices[fac]=atoi(line1.substr(n).c_str())-1; fac++; } } myfile.close(); }}[/source]