Advertisement Jump to content
  • Advertisement


  • Content Count

  • Joined

  • Last visited

Community Reputation

105 Neutral

About AlamgirNasir

  • Rank
  1. AlamgirNasir

    Obj Importer

    Thank you Ashaman73 that was very helpful
  2. AlamgirNasir

    Obj Importer

    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.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('v'&&!='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('v'&&'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('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]
  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!