Sign in to follow this  
Lednakashim

Reading in a RAW 3d Model

Recommended Posts

I'm having some trouble reading in a raw model with c++. The file structure is something like this;
-1.069700 -3.846240 -18.545400 -1.120690 -3.918460 -17.960800 -1.750160 -3.754230 -17.835300
-1.750160 -3.754230 -17.835300 -1.729650 -3.701600 -18.392900 -1.069700 -3.846240 -18.545400
I had been hoping to do something like this:

std::vector<GLfloat> mIndexToRawDraw;
	float v=0;
	while (!feof(f))
	{
		fscanf(f,"%v",&v);
		mIndexToRawDraw.push_back(v);
	}
I am unsure how to advance the fscanf function, and more specifically how to advance it only until a space is encountered.

Share this post


Link to post
Share on other sites
Ok, first off, what file format is your data stored in? Are you storing it as ASCII or binary?

I didn't see any code there for opening and reading a file so I'll asume that you've either:

a) already written that code
or
b) don't know that you need to do this first.

In any case I'll go over how to do this from scratch. Hopefully you'll learn something.

Note, we are using functions included in the header stdio.h, so make sure to include this at the top of your source file. Also note that if you properly read through the reference documentation you could probably figure it out yourself. C++ : Reference : C Library : cstdio (stdio.h)

This example assumes that your file has its data stored in ASCII format.


FILE * file = NULL;

/* stream buffer */

char buffer[256];
float vertex_x1, vertex_y1, vertex_z1;
float vertex_x2, vertex_y2, vertex_z3;
float vertex_x3, vertex_y2, vertex_z3;

/* open the file 'myfile.txt' in read mode 'r' */
file = fopen("myfile.txt", "r");
if (!file)
return false;

while (!feof(file))
{
/* read data from file into our buffer */
fgets(buffer, sizeof(buffer), file);

/* Scan our string, 'buffer', for a floating point value. */
/* If there are any float values in the sequence which we defined in sscanf, snatch those values up and store them in the variables respectively listed. */
sscanf(buffer, "%f %f %f %f %f %f %f %f %f", &vertex_x1, &vertex_y1, &vertex_z1, &vertex_x2, &vertex_y2, &vertex_z2, &vertex_x3, &vertex_y3, &vertex_z3);

/* now the data for these vertices has been read, store this data however you need, most likely in your vertex array or however you are using these values. */
}

fclose(file);


fgets()
Quote:

Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or a the End-of-File is reached, whichever comes first.
A newline character makes fgets stop reading, but it is considered a valid character and therefore it is included in the string copied to str.
A null character is automatically appended in str after the characters read to signal the end of the C string.


I highly recommend for your betterment that you read through those IO function descriptions from the link I posted above. :) Hope this helps.

Share this post


Link to post
Share on other sites

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