Jump to content
  • Advertisement
Sign in to follow this  
chillypacman

OpenGL OpenGL Does Not Like My Box

This topic is 3809 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to load a very basic obj model, it's just a box into OpenGL but it isn't displaying correctly. The OBJ is bare minimum geometry data, all texture coordinates and whatnot have been cut out, it's basically a text file with this:
Quote:
# Max2Obj Version 4.0 Mar 10th, 2001 # # object Box01 to come ... # v -0.500000 -0.500000 0.000000 v 0.500000 -0.500000 0.000000 v -0.500000 0.500000 0.000000 v 0.500000 0.500000 0.000000 v -0.500000 -0.500000 1.000000 v 0.500000 -0.500000 1.000000 v -0.500000 0.500000 1.000000 v 0.500000 0.500000 1.000000 # 8 vertices g Box01 f 1 3 4 f 4 2 1 f 5 6 8 f 8 7 5 f 1 2 6 f 6 5 1 f 2 4 8 f 8 6 2 f 4 3 7 f 7 8 4 f 3 1 5 f 5 7 3 # 12 faces g
I've read in all the vertices *correctly* and am trying to render it through glVertex3f() but it won't work, instead I'm getting this: http://img246.imageshack.us/img246/7697/23387889ne1.jpg Here's the exact drawing code:
void ml::drawModel()
{
	glBegin(GL_TRIANGLES);


glVertex3f(-0.500000, -0.500000, 0.000000);
glVertex3f(0.500000, -0.500000, 0.000000);
glVertex3f(-0.500000, 0.500000, 0.000000);
glVertex3f(0.500000, 0.500000, 0.000000);
glVertex3f(-0.500000, -0.500000, 1.000000);
glVertex3f(0.500000, -0.500000, 1.000000);
glVertex3f(-0.500000, 0.500000, 1.000000);
glVertex3f(0.500000, 0.500000, 1.000000);

	glEnd();
}
Could anyone please help?

Share this post


Link to post
Share on other sites
Advertisement
In order to draw a box, you have to provide 12 triangles to OpenGL (36=3*12 calls of glVertex3f()). Your obj file also clearly shows it is 12 faces.

You also need to load the indexes from the obj file to draw the box properly.

Here is some code snippets:

float vertices = {...}; // 8 vertices
int indices = {1,3,4, 4,2,1, 5,6,8, ... }; // 3*12
...

glBegin(GL_TRIANGLES);
for(int i = 0; i < 3*12; ++i)
{
glVertex3fv(&vertices[indices-1]);
}
glEnd();






A better solution is using Vertex Array, such as glDrawArrays() or glDrawElements(), instead of glVertex3f().

Share this post


Link to post
Share on other sites
Thanks, that helped :)

but now I'm stuck with texture loading :-
I've loaded all the texture coordinates but two things confuse me, one is that it seems the textures have three coordinates while opengl is just u/v coordinates. Second of all there are 12 texture coordinates but only 8 vertices. How am I supposed to split them up between the vertices?

I tried running the first 8 texture coordinates' u and v ignoring the w but it ended up with a relaly bad texture that didn't look like anything.

Quote:
# Max2Obj Version 4.0 Mar 10th, 2001
#
# object Box01 to come ...
#
v -0.500000 -0.500000 -0.500000
v 0.500000 -0.500000 -0.500000
v -0.500000 0.500000 -0.500000
v 0.500000 0.500000 -0.500000
v -0.500000 -0.500000 0.500000
v 0.500000 -0.500000 0.500000
v -0.500000 0.500000 0.500000
v 0.500000 0.500000 0.500000
# 8 vertices

vt 0.000000 0.000000 0.000000
vt 1.000000 0.000000 0.000000
vt 0.000000 1.000000 0.000000
vt 1.000000 1.000000 0.000000
vt 0.000000 0.000000 0.000000
vt 1.000000 0.000000 0.000000
vt 0.000000 1.000000 0.000000
vt 1.000000 1.000000 0.000000
vt 0.000000 0.000000 0.000000
vt 1.000000 0.000000 0.000000
vt 0.000000 1.000000 0.000000
vt 1.000000 1.000000 0.000000
# 12 texture vertices

vn 0.000000 0.000000 -2.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -2.000000
vn 0.000000 0.000000 2.000000
vn 0.000000 0.000000 1.000000
vn 0.000000 0.000000 1.000000
vn 0.000000 0.000000 2.000000
# 8 vertex normals

g Box01
s 2
f 1/10/1 3/12/3 4/11/4
f 4/11/4 2/9/2 1/10/1
s 4
f 5/9/5 6/10/6 8/12/8
f 8/12/8 7/11/7 5/9/5
s 8
f 1/5/1 2/6/2 6/8/6
f 6/8/6 5/7/5 1/5/1
s 16
f 2/1/2 4/2/4 8/4/8
f 8/4/8 6/3/6 2/1/2
s 32
f 4/5/4 3/6/3 7/8/7
f 7/8/7 8/7/8 4/5/4
s 64
f 3/1/3 1/2/1 5/4/5
f 5/4/5 7/3/7 3/1/3
# 12 faces

g

Share this post


Link to post
Share on other sites
Quote:
I've loaded all the texture coordinates but two things confuse me, one is that it seems the textures have three coordinates while opengl is just u/v coordinates.

OpenGL supports 3 dimensional textures, so the third coordinate is perfectly valid. For 2D texturing, you can simply ignore it (you will have to read from the file of course, but you don't have to actually use it for anything).
Quote:
Second of all there are 12 texture coordinates but only 8 vertices. How am I supposed to split them up between the vertices?

No, you have 12 vertices. Position is an attribute of a vertex, just like normal, texture coordinates, color, etc. So you have 8 positions, but 12 vertices.

Quote:
I tried running the first 8 texture coordinates' u and v ignoring the w but it ended up with a relaly bad texture that didn't look like anything.

That is not the correct way to do it.The vertices are actually defined by the "f" entries. In your file, each face has 3 vertices (note: this is not a hard rule; faces can technically be made up of any number of vertices). Look at the first and fifth "f" (face) entries:

f 1/10/1 3/12/3 4/11/4
...
f 1/5/1 2/6/2 6/8/6

1/10/1 is one vertex. 1/5/1 is another vertex completely independent of the first. Yes, they share the same position information, but they are not the same vertex.
I know, you would like to just store the positions in your vertex buffer, and the indices in your index buffer, pass it off to OpenGL and be done with it. Texture coordinates do tend to make this trickier. In order to use one index buffer, you will have to duplicate position information and you will have to reorder your texture coordinates so that the indices match with their corresponding vertex position.

Share this post


Link to post
Share on other sites
I'm not sure I quite follow, for instance if I have a face like this:

f 1/2/3 4/5/6 7/8/9

I render the vertices defined at 1, 4 and 5 in my vertex vector, so lets say I have tehse vertices:

v -0.500000 -0.500000 0.000000
v 0.500000 -0.500000 0.000000
v -0.500000 0.500000 0.000000
v 0.500000 0.500000 0.000000
v -0.500000 -0.500000 1.000000
v 0.500000 -0.500000 1.000000
v -0.500000 0.500000 1.000000
v 0.500000 0.500000 1.000000

I draw the following:

v -0.500000 -0.500000 0.000000
v 0.500000 0.500000 0.000000
v -0.500000 -0.500000 1.000000

in that order. It has worked for me so far even with complex models so I don't think thats the wrong way to do it.

When exporting an obj without texture coordinates f is followed by single numbers without /'s so I assum ehte /'s have something to do with textures. I'm just not sure what, how am I supposed to use them in OpenGL to get an accurate texture map?

Share this post


Link to post
Share on other sites
First, I hope your reading documentation and not guessing. When dealing with data formats u need docs.

http://en.wikipedia.org/wiki/Obj

Second it goes:

f for face. followed by 3 vertices defined: vertex/texcoord/normal

Thats all there is to it. The reason your 8 vertices / 12 texcoords is there is because a cube can be described by 8 vertices, but remember it has 6sides and each side can have a different texture on it so: 6sides*2 texture coords. (2 because the two triangles making a face share 2 vertices.

Share this post


Link to post
Share on other sites
Quote:
f 1/2/3 4/5/6 7/8/9

I render the vertices defined at 1, 4 and 5 in my vertex vector, so lets say I have tehse vertices:

v -0.500000 -0.500000 0.000000
v 0.500000 -0.500000 0.000000
v -0.500000 0.500000 0.000000
v 0.500000 0.500000 0.000000
v -0.500000 -0.500000 1.000000
v 0.500000 -0.500000 1.000000
v -0.500000 0.500000 1.000000
v 0.500000 0.500000 1.000000

I draw the following:

v -0.500000 -0.500000 0.000000
v 0.500000 0.500000 0.000000
v -0.500000 -0.500000 1.000000

in that order. It has worked for me so far even with complex models so I don't think thats the wrong way to do it.

When exporting an obj without texture coordinates f is followed by single numbers without /'s so I assum ehte /'s have something to do with textures. I'm just not sure what, how am I supposed to use them in OpenGL to get an accurate texture map?


You are still confusing the concept of a vertex, and the attributes of that vertex. A vertex is not a position in space. A vertex *has* a position in space. It can also *have* a normal, a texture coordinate, a color, etc. When you only have position as a an attribute, then yes, there happens to be a 1-1 mapping between the vertex and its position. When you mix in other attributes, as you have seen, this mapping breaks down. The vertices are defined by the f entries, so let's take a look:

f 1/2/3 4/5/6 7/8/9
v0 v1 v2

The first vertex, v0, has three attributes. It has a position attribute, which is the position stored in the first element of the position list. It has a texture coordinate which is stored in the second element of the texture coordinate list. It has a normal attribute which is stored in the third element of the normal list. All three of these attributes make up 1 vertex. v1 and v2 are defined in the same manner.

Now, it can happen that there is a 1-1 mapping between a vertex and all of its attributes:


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


In this instance, all attributes have the same index into their corresponding lists. That means you can use a single index buffer and render them in the manner that you are trying to do. However, in your case, there is no such 1-1 mapping. So what you need to do is reorganize the lists so that there *is* a 1-1 mapping. That will require reordering some of the elements, and it will require duplicating some of the position information.

[Edited by - CodeMunkie on January 18, 2008 2:15:00 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!