Sign in to follow this  
DarkDemon

Reading obj data from file

Recommended Posts

Hey there. I'm reading multiple quadrilaterals into the program from file. I've tested that its populating both vertices and facet arrays correctly. I've made both these arrays three dimensional so I that I know which object they belong too. When I come to render this data I get the following. http://imageflock.com/img/1208958210.jpg Its clear that its not rendering everything, though two sides of the box and cube look correct just the cube is in the wrong position.
void drawScene()
{
int i,j;

for(j=0; j<objects; j++){
glBegin(GL_QUADS);
for (i=0;i<6;i++){
glColor3f(1.0,0.0,0.0); 
glVertex3f(vertices[j][facets[j][i][0]-1][0],vertices[j][facets[j][i][0]-1][1],vertices[j][facets[j][i][0]-1][2]);
glColor3f(0.0,1.0,0.0);
glVertex3f(vertices[j][facets[j][i][1]-1][0],vertices[j][facets[j][i][1]-1][1],vertices[j][facets[j][i][1]-1][2]);
glColor3f(0.0,0.0,1.0); 
glVertex3f(vertices[j][facets[j][i][2]-1][0],vertices[j][facets[j][i][2]-1][1],vertices[j][facets[j][i][2]-1][2]);
glColor3f(5.0,1.0,1.0);
glVertex3f(vertices[j][facets[j][i][3]-1][0],vertices[j][facets[j][i][3]-1][1],vertices[j][facets[j][i][3]-1][2]);
}
glEnd();
}

glFlush();
glutSwapBuffers();
}


Data I'm reading from file; The first line tells me how many objects which is reading in correctly.

6
28.602833 28.421965 11.004125
28.602833 20.612246 11.004125
20.796803 20.612247 11.004125
20.796804 28.421965 11.004125
28.602835 28.421963 -0.029376
28.602831 20.612244 -0.029376
20.796801 20.612247 -0.029376
20.796803 28.421965 -0.029376
1 2 3 4
5 8 7 6
1 5 6 2
2 6 7 3
3 7 8 4
5 1 4 8
29.836802 -20.651819 11.058530
29.836802 -28.461538 11.058530
22.030771 -28.461536 11.058530
22.030773 -20.651819 11.058530
29.836803 -20.651821 0.025029
29.836800 -28.461540 0.025029
22.030769 -28.461536 0.025029
22.030771 -20.651819 0.025029
9 10 11 12
13 16 15 14
9 13 14 10
10 14 15 11
11 15 16 12
13 9 12 16
13.652043 10.997733 18.751125
13.652043 -11.527220 18.751125
-13.638737 -11.527218 18.751125
-13.638731 10.997738 18.751125
13.652050 10.997728 20.751125
13.652035 -11.527226 20.751125
-13.638741 -11.527216 20.751125
-13.638735 10.997734 20.751125
17 18 19 20
21 24 23 22
17 21 22 18
18 22 23 19
19 23 24 20
21 17 20 24
13.639189 -9.954520 0.523557
13.639189 -11.445096 0.523557
-13.651591 -11.445096 0.523557
-13.651586 -9.954519 0.523557
13.639195 -9.954520 18.981842
13.639180 -11.445097 18.981842
-13.651595 -11.445096 18.981842
-13.651589 -9.954520 18.981842
25 26 27 28
29 32 31 30
25 29 30 26
26 30 31 27
27 31 32 28
29 25 28 32
13.644388 10.961957 0.533941
13.644388 9.471381 0.533941
-13.646392 9.471381 0.533941
-13.646385 10.961958 0.533941
13.644395 10.961957 18.972616
13.644380 9.471380 18.972616
-13.646396 9.471381 18.972616
-13.646390 10.961957 18.972616
33 34 35 36
37 40 39 38
33 37 38 34
34 38 39 35
35 39 40 36
37 33 36 40
13.645390 10.023563 0.553070
13.645390 -9.967404 0.553070
-13.645391 -9.967402 0.553070
-13.645385 10.023568 0.553070
13.645396 10.023559 2.553070
13.645381 -9.967410 2.553070
-13.645394 -9.967401 2.553070
-13.645389 10.023564 2.553070
41 42 43 44
45 48 47 46
41 45 46 42
42 46 47 43
43 47 48 44
45 41 44 48

Dont suppose you have any ideas why its not fully rendering, and placing some items in the wrong position? Thanks for any help. Adam [Edited by - DarkDemon on April 24, 2008 10:40:08 AM]

Share this post


Link to post
Share on other sites
Objects in wrong position:
What is height in that file? Is it y? If it isn't (some programs like 3D Studio Max use z) you need to exchange the values.

Objects "not fully rendering":
The screenshot looks like there's a winding problem. Try to change the winding of your quads/triangles by swapping the 2nd and 4th vertex: instead of order 1, 2, 3, 4 you'll need to try 1, 4, 3, 2.

Did you make sure that the array is filled correctly?

As side node:
Put the glBegin(...) call outside the for(j=0; j<objects; j++) loop for better performance.

Share this post


Link to post
Share on other sites
Quote:
Original post by Lord_Evil
Objects in wrong position:
What is height in that file? Is it y? If it isn't (some programs like 3D Studio Max use z) you need to exchange the values.

Objects "not fully rendering":
The screenshot looks like there's a winding problem. Try to change the winding of your quads/triangles by swapping the 2nd and 4th vertex: instead of order 1, 2, 3, 4 you'll need to try 1, 4, 3, 2.

Did you make sure that the array is filled correctly?

As side node:
Put the glBegin(...) call outside the for(j=0; j<objects; j++) loop for better performance.



Hi lord_evil, thanks for your reply.

I assume the winding is right I'm using blender to generate the models. I also did checks to ensure both arrays are populated correctly. Thanks for the performance tip I've made that change.

I'm not sure what the size is in blender I've only just started using it.

Share this post


Link to post
Share on other sites
I'm also tried the following data which is two small cubes. Thought I'd try something more simple however this doesnt work.

1.265759 -1.711365 0.059438
1.265759 -3.711365 0.059438
-0.734241 -3.711365 0.059438
-0.734240 -1.711364 0.059438
1.265760 -1.711365 2.059438
1.265759 -3.711365 2.059438
-0.734241 -3.711364 2.059438
-0.734240 -1.711365 2.059438
1 2 3 4
5 8 7 6
1 5 6 2
2 6 7 3
3 7 8 4
5 1 4 8
1.000000 6.962705 0.037720
1.000000 4.962705 0.037720
-1.000000 4.962705 0.037720
-1.000000 6.962706 0.037720
1.000000 6.962705 2.037720
0.999999 4.962705 2.037720
-1.000000 4.962706 2.037720
-1.000000 6.962705 2.037720
9 10 11 12
13 16 15 14
9 13 14 10
10 14 15 11
11 15 16 12
13 9 12 16

Ran some code to loop out the data, everything seems to be fine. But I only see one cube on the screen rendered. The other one isnt in sight. Again I'm using blender to model.

http://imageflock.com/img/1208997336.png

Any suggestions? I assume it has to be with the following code considering reading in is working fine, and both arrays are populating.

void drawScene()
{
int i,j;

for(j=0; j<objects; j++){
glBegin(GL_QUADS);
for (i=0;i<6;i++){
glColor3f(1.0,0.0,0.0);
glVertex3f(vertices[j][facets[j][i][0]-1][0],vertices[j][facets[j][i][0]-1][1],vertices[j][facets[j][i][0]-1][2]);
glColor3f(0.0,1.0,0.0);
glVertex3f(vertices[j][facets[j][i][1]-1][0],vertices[j][facets[j][i][1]-1][1],vertices[j][facets[j][i][1]-1][2]);
glColor3f(0.0,0.0,1.0);
glVertex3f(vertices[j][facets[j][i][2]-1][0],vertices[j][facets[j][i][2]-1][1],vertices[j][facets[j][i][2]-1][2]);
glColor3f(5.0,1.0,1.0);
glVertex3f(vertices[j][facets[j][i][3]-1][0],vertices[j][facets[j][i][3]-1][1],vertices[j][facets[j][i][3]-1][2]);
}
glEnd();
}

glFlush();
glutSwapBuffers();
}

[Edited by - DarkDemon on April 23, 2008 7:32:38 PM]

Share this post


Link to post
Share on other sites
I tested something, I changed all the facet data for each object so they all had the same data for facets


1 2 3 4
5 8 7 6
1 5 6 2
2 6 7 3
3 7 8 4
5 1 4 8


The results are more promising, here you can see opengl on the left and blender on the right, its sadly still not perfect. The the box is nearly right, and the cubes are off.

http://imageflock.com/img/1209037946.png

Anyone any ideas.

Share this post


Link to post
Share on other sites
Heres it again, this is just me testing it with the facet data the same, as when its same it renders more, least the whole middle box as opposed to just two sides. The first line is how many objects to read in.

6
28.602833 28.421965 11.004125
28.602833 20.612246 11.004125
20.796803 20.612247 11.004125
20.796804 28.421965 11.004125
28.602835 28.421963 -0.029376
28.602831 20.612244 -0.029376
20.796801 20.612247 -0.029376
20.796803 28.421965 -0.029376
1 2 3 4
5 8 7 6
1 5 6 2
2 6 7 3
3 7 8 4
5 1 4 8
29.836802 -20.651819 11.058530
29.836802 -28.461538 11.058530
22.030771 -28.461536 11.058530
22.030773 -20.651819 11.058530
29.836803 -20.651821 0.025029
29.836800 -28.461540 0.025029
22.030769 -28.461536 0.025029
22.030771 -20.651819 0.025029
1 2 3 4
5 8 7 6
1 5 6 2
2 6 7 3
3 7 8 4
5 1 4 8
13.652043 10.997733 18.751125
13.652043 -11.527220 18.751125
-13.638737 -11.527218 18.751125
-13.638731 10.997738 18.751125
13.652050 10.997728 20.751125
13.652035 -11.527226 20.751125
-13.638741 -11.527216 20.751125
-13.638735 10.997734 20.751125
1 2 3 4
5 8 7 6
1 5 6 2
2 6 7 3
3 7 8 4
5 1 4 8
13.639189 -9.954520 0.523557
13.639189 -11.445096 0.523557
-13.651591 -11.445096 0.523557
-13.651586 -9.954519 0.523557
13.639195 -9.954520 18.981842
13.639180 -11.445097 18.981842
-13.651595 -11.445096 18.981842
-13.651589 -9.954520 18.981842
1 2 3 4
5 8 7 6
1 5 6 2
2 6 7 3
3 7 8 4
5 1 4 8
13.644388 10.961957 0.533941
13.644388 9.471381 0.533941
-13.646392 9.471381 0.533941
-13.646385 10.961958 0.533941
13.644395 10.961957 18.972616
13.644380 9.471380 18.972616
-13.646396 9.471381 18.972616
-13.646390 10.961957 18.972616
1 2 3 4
5 8 7 6
1 5 6 2
2 6 7 3
3 7 8 4
5 1 4 8
13.645390 10.023563 0.553070
13.645390 -9.967404 0.553070
-13.645391 -9.967402 0.553070
-13.645385 10.023568 0.553070
13.645396 10.023559 2.553070
13.645381 -9.967410 2.553070
-13.645394 -9.967401 2.553070
-13.645389 10.023564 2.553070
1 2 3 4
5 8 7 6
1 5 6 2
2 6 7 3
3 7 8 4
5 1 4 8



This is the original .obj
http://pastebin.com/m5ee13ff9

Share this post


Link to post
Share on other sites
From your rendering, I feel that I see the inside of the little cube... Could it be that there is a probleme in the orientation of your vertices ?

edit : Also, I guess that you swapped the Y and Z axis, thus all the object are aligned vertically

Share this post


Link to post
Share on other sites
I read the nehe tutorial, and your data, and I definitively think you swapped X and Y axis.


// Your code
glVertex3f(
vertices[j][facets[j][i][0]-1][0],
vertices[j][facets[j][i][0]-1][1],
vertices[j][facets[j][i][0]-1][2]
);

// fixed
glVertex3f(
vertices[j][facets[j][i][0]-1][0],
vertices[j][facets[j][i][0]-1][2],
vertices[j][facets[j][i][0]-1][1]
);



Also, about the winding, just try to build your quad in reverse order, just to see what happens

Share this post


Link to post
Share on other sites
Damnit it was an oversight on my part I was working late on this. Thanks so much for you help.

Screenshot

http://imageflock.com/img/1209045095.png

It also looks as if its been drawn inside out

Share this post


Link to post
Share on other sites
The little boxes are ok now

Comparing how vertices are placed in the first little cube (first object, vertices 1 to 8) and the first big sub box (third object, vertices 17 to 24), I see that
on the little box, vertices 1 to 4 are on the top, and vertices 17 to 20 are on the bottom.

So, try by cutting the four lines defining vertices 21 to 24 and paste before vertices 17 to 20 (in other word : swap the coordinates of vertices 17-20 and 21-24)

And so on for the other parts.

Hope this help

tip : I used paper and pen to draw the vertices and spot what could be wrong.

Share this post


Link to post
Share on other sites
Actually, everything is now in the right place which was the main problem there is still an issue with winding though, I noticed on this simple recentangle doesnt look as if theres a top,
backface culling is enabled too.

http://imageflock.com/img/1209052609.png

Depending on the angle you can see some faces just not others.

1
3.956007 0.462443 0.199838
3.956007 -1.537557 0.199838
-3.956007 -1.537556 0.199838
-3.956006 0.462444 0.199838
3.956009 0.462443 2.199838
3.956004 -1.537557 2.199838
-3.956008 -1.537556 2.199838
-3.956007 0.462443 2.199838
1 2 3 4
5 8 7 6
1 5 6 2
2 6 7 3
3 7 8 4
5 1 4 8


 
glBegin(GL_QUADS);
for(j=0; j<objects; j++){
for (i=0;i<6;i++){
glColor3f(1.0,0.0,0.0); glVertex3f(vertices[j][facets[j][i][0]-1][0],vertices[j][facets[j][i][0]-1][2],vertices[j][facets[j][i][0]-1][1]);
glColor3f(1.0,2.0,0.0); glVertex3f(vertices[j][facets[j][i][1]-1][0],vertices[j][facets[j][i][1]-1][2],vertices[j][facets[j][i][1]-1][1]);
glColor3f(1.0,2.2,3.0); glVertex3f(vertices[j][facets[j][i][2]-1][0],vertices[j][facets[j][i][2]-1][2],vertices[j][facets[j][i][2]-1][1]);
glColor3f(1.0,0.0,4.0);
glVertex3f(vertices[j][facets[j][i][3]-1][0],vertices[j][facets[j][i][3]-1][2],vertices[j][facets[j][i][3]-1][1]);
}
}




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