• Create Account

drawing wavefront .obj data?

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.

8 replies to this topic

#1Ussyless  Members

142
Like
0Likes
Like

Posted 24 January 2011 - 08:44 PM

Hi, so i have a working script that loads the .obj data (triangulated) from a file into 3 separate vectors (x, y, z) , it all loads correctly and in the right order, however
when i try to draw them, they dont turn out right, not really sure how to explain it
ok, im drawing the data, assuming that it is set in sets of 3, for triangles, but when i use a line strip, i can tell that its just not correct

here is a snippet of code im using to draw it (in opengl)

[b]	void mod::draw(int x2, int y2, int z2)
{
glBegin(GL_TRIANGLES); //tried polygons too, but it doesnt help
unsigned int i;
for(i=0; i<x.size();i++)
{
glVertex3f(x2+x[i],y2+y[i],z2+z[i]);
}
glEnd();
}[/b]

i've attached some pictures to show how it's messing up, in the pictures, im trying to just draw a cube with 4 faces on each side

thanks, any help/tips on how to draw the .obj file correctly would be much appreciated

#2karwosts  Members

840
Like
0Likes
Like

Posted 24 January 2011 - 08:46 PM

Maybe you need to offset your indices by 1? Obj's first element is 1, not 0, so you have to subtract one if you want to index the vertices in an array.

Also you should probably post your whole function, I have no idea what your arrays of data contain.
My Projects:
Portfolio Map for Android - Free Visual Portfolio Tracker
Electron Flux for Android - Free Puzzle/Logic Game

#3Ussyless  Members

142
Like
0Likes
Like

Posted 24 January 2011 - 09:13 PM

Maybe you need to offset your indices by 1? Obj's first element is 1, not 0, so you have to subtract one if you want to index the vertices in an array.

Also you should probably post your whole function, I have no idea what your arrays of data contain.

well, dont really want to post the script im using to load the files, needless to say i have three vectors (x,y,z) which, after being loaded, contain exactly what is in the .obj file
class mod {
public:
string debg;
vector<float> x;
vector<float> y;
vector<float> z;

void draw(int,int,int);
};
thats how the class is created, and i've just double checked to make sure everything loaded properly, and it is
not sure what you mean by changing my indicies though? do you mean read from [i-1] in the drawing script or something?

#4LegendInStone  Members

117
Like
0Likes
Like

Posted 24 January 2011 - 09:20 PM

Maybe you need to offset your indices by 1? Obj's first element is 1, not 0, so you have to subtract one if you want to index the vertices in an array.

Also you should probably post your whole function, I have no idea what your arrays of data contain.

well, dont really want to post the script im using to load the files, needless to say i have three vectors (x,y,z) which, after being loaded, contain exactly what is in the .obj file
class mod {
public:
string debg;
vector<float> x;
vector<float> y;
vector<float> z;

void draw(int,int,int);
};
thats how the class is created, and i've just double checked to make sure everything loaded properly, and it is
not sure what you mean by changing my indicies though? do you mean read from [i-1] in the drawing script or something?

I believe he means, if in the object file you have

f 1 2 3

then you're indices for the triangle would be 0 1 2

#5karwosts  Members

840
Like
0Likes
Like

Posted 24 January 2011 - 09:21 PM

I'm not sure exactly what you think is in the obj file, but 3 vectors is not sufficient information to reproduce a model.

Obj's have vertices and faces, you have to read each face and draw the vertices represented in the face.

so you might have a obj like this (a simple 2d square):

v 0.0 0.0
v 0.0 1.0
v 1.0 1.0
v 1.0 0.0

f 1/4/3
f 3/2/1

Now assuming you store the vertices in a tightly packed float array, and indices in a tightly packed int array:

float vertices[0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0]
int indices[1,4,3,3,2,1];

You'll have to write code that says : draw a triangle out of vertices (1,4,3) and vertices (3,2,1). It's not enough to just iterate through the list of vertices and draw them in order.
My Projects:
Portfolio Map for Android - Free Visual Portfolio Tracker
Electron Flux for Android - Free Puzzle/Logic Game

#6Ussyless  Members

142
Like
0Likes
Like

Posted 24 January 2011 - 09:26 PM

oh, you mean the lines with the f tags? i've done nothing with those at all, couldnt find any decent documentation on them, do i need to use them in order to read it correctly?
im going to try something that might make it work, if the F lines are needed

#7Ussyless  Members

142
Like
0Likes
Like

Posted 24 January 2011 - 09:28 PM

I'm not sure exactly what you think is in the obj file, but 3 vectors is not sufficient information to reproduce a model.

Obj's have vertices and faces, you have to read each face and draw the vertices represented in the face.

so you might have a obj like this (a simple 2d square):

v 0.0 0.0
v 0.0 1.0
v 1.0 1.0
v 1.0 0.0

f 1/4/3
f 3/2/1

Now assuming you store the vertices in a tightly packed float array, and indices in a tightly packed int array:

float vertices[0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0]
int indices[1,4,3,3,2,1];

You'll have to write code that says : draw a triangle out of vertices (1,4,3) and vertices (3,2,1). It's not enough to just iterate through the list of vertices and draw them in order.

oh ok thanks alot! in my file its listed like
f 1/1/1 2/2/1 3/3/1
but i guess that means something like vertex/texture/normals
ok thanks, ill try fix my script with this information

#8karwosts  Members

840
Like
0Likes
Like

Posted 24 January 2011 - 09:38 PM

Yes you're right, those are vertex / texcoord / normal indices.

Its : v0/t0/n0 v1/t1/n1 v2/t2/n2

Where v0, v1, and v2 are the vertex indices.

There's some info here:

http://en.wikipedia....front_.obj_file
My Projects:
Portfolio Map for Android - Free Visual Portfolio Tracker
Electron Flux for Android - Free Puzzle/Logic Game

#9Ussyless  Members

142
Like
1Likes
Like

Posted 25 January 2011 - 02:59 AM

thanks guys, everything is working good now ( even fixed the lighting a bit )