Jump to content

  • Log In with Google      Sign In   
  • Create Account


problem with load obj file


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
12 replies to this topic

#1 Dannyli   Members   -  Reputation: 135

Like
0Likes
Like

Posted 18 June 2013 - 03:46 PM

I try to load the obj file to the opengl but the system crash, I tested My code in console system:

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <ostream>
#include <algorithm>
#include <Windows.h>
using namespace std;

class vertex
{public:
float f1;
float f2;
float f3;
vertex(float F1,float F2, float F3):f1(F1),f2(F2),f3(F3){};
};//the vertex

class face{
public:
	int a;
	int b;
	int c;
	int d;
	bool four;
	face(int x,int y,int z):a(x),b(y),c(z){four =false;};
	face(int x,int y,int z,int k):a(x),b(y),c(z),d(k){four =true;};
};

vector<vertex>v;
vector<face>f;

void loadvertex(char*filename){

	ifstream infile(filename);
	ofstream outfile("out.txt");
	float f1,f2,f3;
	char buffer;
	if(!infile.is_open())
	{return;
	}
	while(infile>>buffer>>f1>>f2>>f3){
	if(buffer=='v')
	{outfile<<f1<<" "<<f2<<" "<<f3<<endl;
	cout<<f1<<" "<<f2<<" "<<f3<<endl;
	v.push_back(vertex(f1,f2,f3));}
	
	}

	infile.close();
	outfile.close();

}


void loadface(char*filename){
	ifstream infile(filename);
	ofstream outfile("out.txt");
	int f1,f2,f3;
	char buffer;
	if(!infile.is_open())
	{return;
	}
	while(infile>>buffer>>f1>>f2>>f3){
	if(buffer=='f'){

	outfile<<f1<<" "<<f2<<" "<<f3<<endl;
	cout<<f1<<" "<<f2<<" "<<f3<<endl;
	f.push_back( face(f1,f2,f3));
	}
	}

	infile.close();
	outfile.close();
}
void Loadobj(char*filename)
{
string line;
ifstream objfile(filename);
if(objfile.is_open())
{

while(!objfile.eof())
{ getline(objfile,line);
  if(line.c_str()[0]=='v')
 {  float tmpx,tmpy,tmpz;
   sscanf(line.c_str(),"v %f %f %f",&tmpx,&tmpy,&tmpz);
   v.push_back(vertex(tmpx,tmpy,tmpz));
}
  if(line.c_str()[0]=='f')
  {  int a,b,c,d;
   sscanf(line.c_str(),"f %d//%d %d//%d %d//%d",&a,&b,&c,&b,&d,&b);
   f.push_back(face(a,c,d));
}
}
}

}
int main(){
	Loadobj("cube.txt");
	for(int i=0;i<f.size();i++)
	{ 
		float F1=v[f[i].a-1].f1;
		float F2=v[f[i].a-1].f2;
		float F3=v[f[i].a-1].f3;
		cout<<F1<<"--"<<F2<<"--"<<F3<<endl;
	
	}
	system("pause");
	return 0;
}

the obj file:

 

# Blender3D v249 OBJ File: 
# www.blender3d.org
v 1.000000 1.000000 -1.000000
v 1.000000 -1.000000 -1.000000
v -1.000000 -1.000000 -1.000000
v -1.000000 1.000000 -1.000000
v 1.000000 0.999999 1.000000
v 0.999999 -1.000001 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 1.000000 1.000000
vn 0.000000 1.000000 0.000000
vn -1.000000 0.000000 -0.000000
vn -0.000000 -1.000000 -0.000000
vn 1.000000 0.000000 -0.000000
vn 1.000000 -0.000001 0.000000
vn 0.000000 0.000000 1.000000
vn 0.000000 0.000000 -1.000000
usemtl Material
s off
f 5//1 1//1 4//1
f 5//1 4//1 8//1
f 3//2 7//2 8//2
f 3//2 8//2 4//2
f 2//3 6//3 3//3
f 6//3 7//3 3//3
f 1//4 5//4 2//4
f 5//5 6//5 2//5
f 5//6 8//6 6//6
// the error shows that: ...
if (longone)
//                                    *(long UNALIGNED *)pointer = (unsigned long)number;
  //                             else...

Edited by Dannyli, 18 June 2013 - 04:41 PM.


Sponsor:

#2 Dannyli   Members   -  Reputation: 135

Like
0Likes
Like

Posted 18 June 2013 - 04:42 PM

And I also get warning that I can't use "sscanf"

how to  fix this problem??



#3 mhagain   Crossbones+   -  Reputation: 7866

Like
0Likes
Like

Posted 18 June 2013 - 04:55 PM

Did you run it in your debugger?  On a crash, the debugger will break at the line that caused the crash, which will help you fix it.

 

Since you're #include-ing windows.h you're probably on Windows, so you really should use Visual Studio's debugger - it's the best in the business and a valuable tool for this kind of situation.


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#4 dpadam450   Members   -  Reputation: 921

Like
0Likes
Like

Posted 18 June 2013 - 05:27 PM

Are you using Visual Studio? Run in debug mode and step by step see what it reads and why it is not breaking.  What does infile >> return at end of file? Figure it out. Your while loop codition is awful as well. Read a single char: "v" and if(char == "v"){ read f1,f2,f3}  not reading them all at once



#5 Dannyli   Members   -  Reputation: 135

Like
0Likes
Like

Posted 18 June 2013 - 06:09 PM

I am using visual stdio 2010

the error appears at 

void Loadobj(char*filename)
{
string line;
ifstream objfile(filename);
if(objfile.is_open())
{

while(!objfile.eof())
{ getline(objfile,line);
if(line.c_str()[0]=='v')
{ float tmpx,tmpy,tmpz;
sscanf(line.c_str(),"v %f %f %f",&tmpx,&tmpy,&tmpz);
v.push_back(vertex(tmpx,tmpy,tmpz));
}
if(line.c_str()[0]=='f')
{ int a,b,c,d;
sscanf(line.c_str(),"f %d//%d %d//%d %d//%d",&a,&b,&c,&b,&d,&b);
f.push_back(face(a,c,d));
}
}
}

}

because I only use this function



#6 marcClintDion   Members   -  Reputation: 431

Like
0Likes
Like

Posted 18 June 2013 - 10:28 PM

There is a pretty decent .obj loader at the following site.  I'm able to export indices, tangents and biNormals out of it without any issue.  It does have some intermittent crash problems that are related to some of the Blender export settings, you might be having the same problem.  I just set a preset that exports the model using stable settings.  For instance, if a .obj loader expects edges and you don't set the Blender export plugin to include edges you may have a crash if there is no proper error handling built in to your program.  Conversely, if your program is NOT expecting edges and you do include them, then your parser may fail and once again you might have a crash. Finding the proper Blender export settings for your program may also help you track down your bug.

 

http://www.dhpoware.com/demos/glObjViewer.html


Consider it pure joy, my brothers and sisters, whenever you face trials of many kinds, because you know that the testing of your faith produces perseverance. Let perseverance finish its work so that you may be mature and complete, not lacking anything.


#7 BitMaster   Crossbones+   -  Reputation: 3942

Like
0Likes
Like

Posted 19 June 2013 - 04:44 AM

...


Apart from the fact that I would avoid the scanf family of functions (either use the standard streams or generate a proper parser with AntLR or friends): you are entering undefined behavior with "line.c_str()[0]" as soon as a line you read is empty.

#8 Bacterius   Crossbones+   -  Reputation: 8693

Like
0Likes
Like

Posted 19 June 2013 - 06:15 AM


you are entering undefined behavior with "line.c_str()[0]" as soon as a line you read is empty.

 

It isn't UB, I think. The line may be empty but strlen() will return zero and the character at [0] will be the null character. c_str() returns a valid C null-terminated char array and may not return a null pointer. So it should just skip the line. Still kind of fragile, though, I agree.


The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#9 dpadam450   Members   -  Reputation: 921

Like
0Likes
Like

Posted 19 June 2013 - 09:26 AM

So you should put a breakpoint and step through your code. If you don't know how to do that look it up. You have a bug in your code you have to see whats going on and this is straightforward to solve in VS debuginning line by line.

Also c++ strings have an index operator.
line.c_str()[0]=='v'
line[0] == 'v'

 



#10 EddieV223   Members   -  Reputation: 1406

Like
-3Likes
Like

Posted 19 June 2013 - 11:52 PM

Dude your code is an eye sore.


If this post or signature was helpful and/or constructive please give rep.

 

// C++ Video tutorials

http://www.youtube.com/watch?v=Wo60USYV9Ik

 

// Easy to learn 2D Game Library c++

SFML2.1 Download http://www.sfml-dev.org/download.php

SFML2.1 Tutorials http://www.sfml-dev.org/tutorials/2.1/

 

// SFML 2 book

http://www.amazon.com/gp/product/1849696845/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=1849696845&linkCode=as2&tag=gamer2creator-20

 


#11 swiftcoder   Senior Moderators   -  Reputation: 9883

Like
0Likes
Like

Posted 20 June 2013 - 12:01 AM

I'm not completely sure about that. The string does not have to be zero-terminated unless c_str() was called. At least I think so. It's a bit hot here.

 
According to cplusplus.com: "If pos is equal to the string length, the function returns a reference to a null character ('\0')."

Which seems like a sane definition.


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#12 jbadams   Senior Staff   -  Reputation: 18211

Like
1Likes
Like

Posted 20 June 2013 - 02:51 AM

Dude your code is an eye sore.

Eddie -- criticism is welcome if you're able to offer helpful advice on how a user might improve, or lacking that even if you can point out specific problems to be examined and improved upon.  Posting just to insult someone's code without taking the time to help is neither helpful nor friendly, and even wastes the time of people reading the response.  Please don't respond unless you actually have something to contribute.



#13 metsfan   Members   -  Reputation: 654

Like
0Likes
Like

Posted 20 June 2013 - 11:25 AM

Just a suggestion:  If your goal is not to build a model parser, but just to load a model into memory so you can accomplish your actual goal, consider using: http://assimp.sourceforge.net/


Edited by metsfan, 20 June 2013 - 11:26 AM.





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