• Advertisement
Sign in to follow this  

Obj Importer

This topic is 2042 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement