Sign in to follow this  

sscanf issues when reading obj file

This topic is 2634 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.

Thanks,

Leonard


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
{
public:

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

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

};


//The final code block

//Vertex
case 'v':
switch(buffer[1]){
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);
current_vertex++;
break;
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);
current_normal++;
break;
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);
current_uv++;
break;
}
break;



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!

Leonard

Share this post


Link to post
Share on other sites

This topic is 2634 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this