Jump to content

  • Log In with Google      Sign In   
  • Create Account


Obj Importer


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 TITAN 3D   Members   -  Reputation: 105

Like
0Likes
Like

Posted 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]

Sponsor:

#2 Ashaman73   Crossbones+   -  Reputation: 5915

Like
1Likes
Like

Posted 20 June 2012 - 05:00 AM

Object files containes indexed data. That is, all vertices, normals, uv are written independently from each other, then, when the faces are defined, the indicies point to the correct data.

Example (might be buggy):


v 0 0 0
v 0 1 0
v 1 0 0

n 0 0 1

f 1/1 2/1 3/1

The face, consisting of 3 vertex/normal pairs (could be 4 if a quad is defined), refers three different vertices with the same normal.

#3 TITAN 3D   Members   -  Reputation: 105

Like
0Likes
Like

Posted 20 June 2012 - 05:28 AM

Thank you Ashaman73 that was very helpful Posted Image




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS