Jump to content
  • Advertisement
Sign in to follow this  

sscanf issues when reading obj file

This topic is 2850 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

Hey guys,

I'm having a really bizarre issue.

I wrote some code to parse/read an OBJ file and I've figured out how to make it work, but I don't know why it works like this.

Here's a couple of lines in the OBJ file:

1. v 0.500000 -0.500000 -0.500000
2. vt 0.375000 0.000000

When I parse through the first line, the following code will work:
sscanf(buffer, "v %lf %lf %lf", &x, &y, &z);

This will NOT (why??):
sscanf(buffer, "v %f %f %f", &x, &y, &z);

Then...with the second line the following works:
sscanf(buffer, "vt %f %f", &u, &v);

But this will NOT:
sscanf(buffer, "vt %lf %lf", &u, &v);

I cannot figure out why there is such a discrepency? I just have to read a float, but in the first example, I *need* to make it a double or sscanf returns 0. And in the second example, it needs me to make it a float or it returns 0. Totally confused.....

Could someone help shed light on this?

I'm developing on VS2010, building for Win32.



Share this post

Link to post
Share on other sites
I copied-and-pasted your code and it worked as expected - buffer defined as char*, got proper reads when x,y,z,u,v defined as floats for " %f " and defined as double for " %lf ". Just for fun, I added "\n" and "\r\n" to each string and still got proper reads when the variable types match the format spec.

Have you checked to ensure your buffer contains the lines as expected with no other extraneous characters (printable or not)?

Share this post

Link to post
Share on other sites
Yes, I checked the buffer chars on debug and it's coming in as expected. This one has me completely stumped....

I'm running VS2010 on a x64 machine --would that have an issue? (I wouldn't think so as the active target is Win32)....

Share this post

Link to post
Share on other sites
Oh my gosh guys I feel like such a dummie......

I had optimized my code by going in and changing all the double declarations to float, but had missed the class definition for my vertices/vectors:

class Vector3

float x, y, z; //I had this as double instead of float....

Vector3(float nx, float ny, float nz);


//The final code block

case 'v':
case ' ':
//Create new vertex
this->vertices[current_vertex] = new Vector3();
sscanf(buffer, "v %f %f %f", &this->vertices[current_vertex]->x, &this->vertices[current_vertex]->y, &this->vertices[current_vertex]->z);
case 'n':
//Create new vertex normal
this->normals[current_normal] = new Vector3();
sscanf(buffer, "vn %f %f %f", &this->normals[current_normal]->x, &this->normals[current_normal]->y, &this->normals[current_normal]->z);
case 't':
//Create new UV
this->uvs[current_uv] = new Uv();
sscanf(buffer, "vt %f %f", &this->uvs[current_uv]->u, &this->uvs[current_uv]->v);

Argh. Now it all works as expected. I had been staring at this for too long and I just spotted it this morning.

Sorry...my bad!


Share this post

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!