Reading obj data from file

Started by
16 comments, last by DarkDemon 16 years ago
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][0]-1][0],vertices[j][facets[j][0]-1][1],vertices[j][facets[j][0]-1][2]);
glColor3f(0.0,1.0,0.0);
glVertex3f(vertices[j][facets[j][1]-1][0],vertices[j][facets[j][1]-1][1],vertices[j][facets[j][1]-1][2]);
glColor3f(0.0,0.0,1.0); 
glVertex3f(vertices[j][facets[j][2]-1][0],vertices[j][facets[j][2]-1][1],vertices[j][facets[j][2]-1][2]);
glColor3f(5.0,1.0,1.0);
glVertex3f(vertices[j][facets[j][3]-1][0],vertices[j][facets[j][3]-1][1],vertices[j][facets[j][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]
Advertisement
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.
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
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.
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][0]-1][0],vertices[j][facets[j][0]-1][1],vertices[j][facets[j][0]-1][2]);
glColor3f(0.0,1.0,0.0);
glVertex3f(vertices[j][facets[j][1]-1][0],vertices[j][facets[j][1]-1][1],vertices[j][facets[j][1]-1][2]);
glColor3f(0.0,0.0,1.0);
glVertex3f(vertices[j][facets[j][2]-1][0],vertices[j][facets[j][2]-1][1],vertices[j][facets[j][2]-1][2]);
glColor3f(5.0,1.0,1.0);
glVertex3f(vertices[j][facets[j][3]-1][0],vertices[j][facets[j][3]-1][1],vertices[j][facets[j][3]-1][2]);
}
glEnd();
}

glFlush();
glutSwapBuffers();
}

[Edited by - DarkDemon on April 23, 2008 7:32:38 PM]
In the interest of making things even simpler, what happens if you have only a single box? And how about a box with only three sides, and culling turned off?
I can render a single box fine. It seems picky in what it wants to render. When I try and render a box with only four sides {no middle section} it'll only render one side.
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.
Could you post your complete .obj file again please, for that last image? You can put it in [ source ] [ / source ] tags for space.
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.

628.602833 28.421965 11.00412528.602833 20.612246 11.00412520.796803 20.612247 11.00412520.796804 28.421965 11.00412528.602835 28.421963 -0.02937628.602831 20.612244 -0.02937620.796801 20.612247 -0.02937620.796803 28.421965 -0.0293761 2 3 45 8 7 61 5 6 22 6 7 33 7 8 45 1 4 829.836802 -20.651819 11.05853029.836802 -28.461538 11.05853022.030771 -28.461536 11.05853022.030773 -20.651819 11.05853029.836803 -20.651821 0.02502929.836800 -28.461540 0.02502922.030769 -28.461536 0.02502922.030771 -20.651819 0.0250291 2 3 45 8 7 61 5 6 22 6 7 33 7 8 45 1 4 813.652043 10.997733 18.75112513.652043 -11.527220 18.751125-13.638737 -11.527218 18.751125-13.638731 10.997738 18.75112513.652050 10.997728 20.75112513.652035 -11.527226 20.751125-13.638741 -11.527216 20.751125-13.638735 10.997734 20.7511251 2 3 45 8 7 61 5 6 22 6 7 33 7 8 45 1 4 813.639189 -9.954520 0.52355713.639189 -11.445096 0.523557-13.651591 -11.445096 0.523557-13.651586 -9.954519 0.52355713.639195 -9.954520 18.98184213.639180 -11.445097 18.981842-13.651595 -11.445096 18.981842-13.651589 -9.954520 18.9818421 2 3 45 8 7 61 5 6 22 6 7 33 7 8 45 1 4 813.644388 10.961957 0.53394113.644388 9.471381 0.533941-13.646392 9.471381 0.533941-13.646385 10.961958 0.53394113.644395 10.961957 18.97261613.644380 9.471380 18.972616-13.646396 9.471381 18.972616-13.646390 10.961957 18.9726161 2 3 45 8 7 61 5 6 22 6 7 33 7 8 45 1 4 813.645390 10.023563 0.55307013.645390 -9.967404 0.553070-13.645391 -9.967402 0.553070-13.645385 10.023568 0.55307013.645396 10.023559 2.55307013.645381 -9.967410 2.553070-13.645394 -9.967401 2.553070-13.645389 10.023564 2.5530701 2 3 45 8 7 61 5 6 22 6 7 33 7 8 45 1 4 8 


This is the original .obj
http://pastebin.com/m5ee13ff9
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

This topic is closed to new replies.

Advertisement