drawing wavefront .obj data?

Started by
7 comments, last by Ussyless 13 years, 2 months ago
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)

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,y2+y,z2+z);
}
glEnd();
glLoadIdentity();
}



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
Advertisement
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.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game

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 load(string);
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?

'karwosts' said:

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 load(string);
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
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.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
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

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
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
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
thanks guys, everything is working good now ( even fixed the lighting a bit )
added picture if anyone's interested

This topic is closed to new replies.

Advertisement