Skip to main content
gamedev.net
🔒 Locked

File loader

Started by cowsarenotevil Mar 5, 2003 at 4:54 PM 4 replies 500+ views
Original Post
cowsarenotevil
cowsarenotevil
I don''t get errors when I compile this...but it doesn''t work the way I want(in fact it doesn''t work at all...). Here''s the general file format any help is greatly appreciated. It is a wavefront obj exported from milkshape... there are so many different types of this format, I wanted to load this myself. //////////////////////// words that are unimportant v float1 float2 float3 vt float1 float2 vn float1 float2 float3 f float1/float2/float3 float1/float2/float3 float1/float2/float3 //////////////////////// #include <iostream.h> #include <fstream> #include <string.h> #include <vector> using namespace std; #define FILENAME "bessie.obj" struct data { float x, y, z; }; int main() { ifstream infile; infile.open(FILENAME, ios::in); char *character = ""; char *temp = ""; vector data(0); for (int i=0; !infile.eof(); i++) { infile.get(character,255,'' ''); if (strcmp(character,"v") == 0) { // load in whatever "v" is data.resize(i+1); infile >> data.x >> data[i].y >> data[i].z; } else if (strcmp(character,"vt") == 0) { // load in whatever "vt" is data.resize(i+1); infile >> data[i].x >> data[i].y; } else if (strcmp(character,"vn") == 0) { // load in whatever "vn" is data.resize(i+1); infile >> data[i].x >> data[i].y >> data[i].z; } else if (strcmp(character,"f") == 0) { // load in whatever "f" is data.resize(i+3); for (int a=1; a<=3; a++) { infile.get(temp,''/''); data[a+i].x = atof(temp); infile.get(temp,''/''); data[a+i].y = atof(temp); infile.get(temp,'' '' ''\n''); data[a+i].z = atof(temp); } } } infile.close(); for (int j=0; j -~-The Cow of Darkness-~-
-~-The Cow of Darkness-~-
cowsarenotevil
cowsarenotevil
I can''t respond to this... keeps losing the server...agian:


  
#include <iostream.h>
#include <fstream>
#include <string.h>
#include <vector>

using namespace std;

#define FILENAME "bessie.obj"

struct data
{
float x, y, z;
};

int main()
{
ifstream infile;
infile.open(FILENAME, ios::in);

char *character;
char *temp;
vector<data> data(0);

for (int i=0; !infile.eof(); i++)
{
infile.get(character, '' '');
if (strcmp(character,"v") == 0)
{ // load in whatever "v" is

data.resize(i+1);
infile >> data[i].x >> data[i].y >> data[i].z;
} else if (strcmp(character,"vt") == 0)
{ // load in whatever "vt" is

data.resize(i+1);
infile >> data[i].x >> data[i].y;
} else if (strcmp(character,"vn") == 0)
{ // load in whatever "vn" is

data.resize(i+1);
infile >> data[i].x >> data[i].y >> data[i].z;
} else if (strcmp(character,"f") == 0)
{ // load in whatever "f" is

for (int a=1; a<=3; a++)
{
infile.get(temp,''/'');
data[a+i].x = atof(temp);
infile.get(temp,''/'');
data[a+i].y = atof(temp);
infile.get(temp,''/'');
data[a+i].z = atof(temp);
}
}
}

infile.close();

for (int j=0; j<data.capacity(); j++)
{
cout << data[j].x << "," << data[j].y << "," << data[j].z << endl;
}

return 0;
}



-~-The Cow of Darkness-~-
-~-The Cow of Darkness-~-
SirHando
SirHando
I don't know what compiler you are trying to use here, but I can't see how you expect this to work.

Calls to stream get methods require you to supply a buffer for it to put the data into. You are passing it an unitialised char pointer. I assume that this either seg faults or raises an exception os something similar.

I do not know the file format, so I can not really comment on the validity of the parser you have here.

You should at least so something like....

const int BUFFER_SIZE = 255;
char character[BUFFER_SIZE];
char temp[BUFFER_SIZE];

infile.get(&character, BUFFER_SIZE, ' '); // Make sure not to overflow the buffer.

Ciao

[edited by - SirHando on March 6, 2003 8:14:04 AM]
thasmin
thasmin
You have the format wrong. For the faces, it''s integers not floats:

f int/int/int int/int/int int/int/int

Since you''re exporting from Milkshape this isn''t a problem, but there''s 3 ints per face is only when you''re specifying vertex/texcoord/normal. One or two ints is also valid. More than three vertices is also valid and it''s rendered like a triangle fan (I believe). Faces includes vertex 1,2,3, then 1,3,4, then 1,4,5...
cowsarenotevil
cowsarenotevil
I''m extremely bad at file loading, and it really shows... I tried anonymous poster''s code, and it just hung and I got a white box saying I had no resources...

-~-The Cow of Darkness-~-
-~-The Cow of Darkness-~-

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.