Sign in to follow this  
hksduhksdu

VertexBuffer and IndexedBuffer question

Recommended Posts

Hello, If I have a VertexBuffer and IndexedBuffer, I simply use CreateVertexBuffer and CreateIndexedBuffer functions in DX9 API, then SetStream() and whatsoever to DrawIndexedPrimitive(). As far as I understand, it works in the way that DX will find the indices of vertices from the IB, and draw the vertex by VB; however, what if I have texture UV for the vertex? In circumstances, one vertex may be used for more than once, and same vertex may have different texture UV when it is being used in different triangles. Then how do I solve this problem? Thanks in advance, Andy

Share this post


Link to post
Share on other sites
I don't know about DirectX, but in OpenGL you can specify and upload texcoords for each texture unit. So if you work with 2-4 texture units, you can upload a different set of texture coordinates.

The way I do it is have one set of texture coordinates, then for each texture unit I scale the texture matrix for however much I want the textures to stretch, ect...

Share this post


Link to post
Share on other sites
I think he's referring to the basic problem of a cube. Each side has vertices which share the same position, but have differing normals and UV coordinates.

In cases like this, you MUST create a second vertex. For a cube, you'll need 4 vertices for each cube face (note, you don't need 6 vertices for each face, the two triangles of the same same can share 2 vertices).

Most meshes have UVs and normals that flow nicely, and can share vertices. When you need a sharp change in lighting or texture you need to create additional vertices.

Share this post


Link to post
Share on other sites
Sorry for confusing you, here is the example. I have 8 vertices:

Cube:
(0,0,0)
(0,1,0)
(1,0,0)
(0,0,1)
(1,1,0)
(0,1,1)
(1,0,1)
(1,1,1)

My vertex structure is as follow:

struct MyVertex{
float x, y, z;
float tu, tv;
}

As you can see, tu and tv are the texture coordinate of texture mapping. Now, I have to use a list of index to create a cube because a cube has 6 faces, which should have 24 indices. Then my cube vertices information should be this instead of above:

Cube:
(0,0,0,1.0,0.0) -- (x,y,z,tu,tv)
(1,0,0,0.0,0.0)
...

Because of my texture mapping, I must put the tu and tv with every vertex. Now here is the question:

There are 6 vertices but 24 indices, that means there are 18 indices are using same vertices. But with front face, I wanna map pic1.bmp, and left face with pic2.bmp. But because vertices are using same tu and tv, when I map pic1.bmp with vertices(1, 2, 3, 4), and pic2.bmp with vertices(8, 1, 4, 5), you can see 1, and 4 are being used twice, but the tu and tv in 1 & 4 should be different, then how do I solve this problem?

Hopefully this is clear enough.

Thanks,

Andy

Share this post


Link to post
Share on other sites
Quote:
Original post by Namethatnobodyelsetook
For a cube, you'll need 4 vertices for each cube face (note, you don't need 6 vertices for each face, the two triangles of the same same can share 2 vertices).


So, your cube will need 4 vertices * 6 cubefaces = 24 vertices.
You cube will be 3 indices per poly * 2 polys per cube face * 6 cube faces = 36 indices.


0--1
| /|
|/ |
2--3

First face, 4 vertices, 6 indices (0,1,2, 1,3,2)

4--5
/ /
6--7
. ..
.. .
....

Second face, 4 vertices, 6 indices (4,5,6, 5,7,6)

Note that vertices 0 and 1 are in the same physical position as vertices 6, and 7, however they are seperate vertices as they need unique UVs and normals.

Share this post


Link to post
Share on other sites
Yes yes, this is what I am meaning, is it possible to draw with IndexBuffer? Because the TU and TV are different in the VB when drawing with different IB, I cannot simply overwrite the previous VB data because there maybe duplicated Vertex in one IB list with different TU and TV. Any idea?

Thanks,

Andy

Share this post


Link to post
Share on other sites
Quote:
Original post by hksduhksdu
Because the TU and TV are different in the VB when drawing with different IB

What is the other IB? How is some other index buffer affecting your UVs (or any part of your vertices at all)?

For drawing the two faces of the cube, I'd have 8 vertices (exactly as I've numbered them in my previous example), and one index buffer containing (0,1,2,1,3,2,4,5,6,5,7,6). Vertices 6 and 7 exists because of differing UVs/normals from vertices 0 and 1. If the vertex data isn't exactly the same, you need a new vertex.

Each mesh has it's own vertices and indices. An IB for another mesh won't affect the IB or VB of the first mesh. Any vertices of a single mesh that need different UVs or normals need to be seperate vertices, as the above example of two cube faces shows.

Share this post


Link to post
Share on other sites
Thank you very much. In this case, then it will be a big mess. Here is a simple file that I am reading from:

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
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 1.000000
vt 0.000000 2.000000
vt 1.000000 2.000000
vt 0.000000 3.000000
vt 1.000000 3.000000
vt 0.000000 4.000000
vt 1.000000 4.000000
vt 2.000000 0.000000
vt 2.000000 1.000000
vt -1.000000 0.000000
vt -1.000000 1.000000
vn 0.000000 0.000000 1.000000
vn 0.000000 0.000000 1.000000
vn 0.000000 0.000000 1.000000
vn 0.000000 0.000000 1.000000
vn 0.000000 1.000000 0.000000
vn 0.000000 1.000000 0.000000
vn 0.000000 1.000000 0.000000
vn 0.000000 1.000000 0.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 -1.000000 0.000000
vn 0.000000 -1.000000 0.000000
vn 0.000000 -1.000000 0.000000
vn 0.000000 -1.000000 0.000000
vn 1.000000 0.000000 0.000000
vn 1.000000 0.000000 0.000000
vn 1.000000 0.000000 0.000000
vn 1.000000 0.000000 0.000000
vn -1.000000 0.000000 0.000000
vn -1.000000 0.000000 0.000000
vn -1.000000 0.000000 0.000000
vn -1.000000 0.000000 0.000000
f 1/1/1 2/2/2 4/4/3 3/3/4
f 3/3/5 4/4/6 6/6/7 5/5/8
f 5/5/9 6/6/10 8/8/11 7/7/12
f 7/7/13 8/8/14 2/10/15 1/9/16
f 2/2/17 8/11/18 6/12/19 4/4/20
f 7/13/21 1/1/22 3/3/23 5/14/24

In case you don't know what file format this is, it's .obj from Alias WaveFront(Maya). The f is face, v/vt/vn.

On face 2,3, and 6, vertex number 5 are being used. But in face 6, vertex 5 is having different vt. So does that mean that in my VB, I need to generate one more vertex 5 which has vt 14?

Thank you very much,

Andy

Share this post


Link to post
Share on other sites
Quote:
Original post by hksduhksdu
Hello,

If I have a VertexBuffer and IndexedBuffer, I simply use CreateVertexBuffer and CreateIndexedBuffer functions in DX9 API, then SetStream() and whatsoever to DrawIndexedPrimitive(). As far as I understand, it works in the way that DX will find the indices of vertices from the IB, and draw the vertex by VB; however, what if I have texture UV for the vertex?

In circumstances, one vertex may be used for more than once, and same vertex may have different texture UV when it is being used in different triangles. Then how do I solve this problem?

Thanks in advance,

Andy


DX doesn't currently support multiple IB, so you'd have to duplicate vertices.

Share this post


Link to post
Share on other sites
Yes, you'll need a few copies of vertex 5. Each unique triplet must be a seperate vertex for DirectX and OpenGL.

5/5/8 which is 5, 0.000000 2.000000, 0, 1, 0
5/5/9 5, 0.000000 2.000000, 0, 0, -1
5/14/24 5, 0.000000 2.000000, -1, 0, 0

Each of these must become a unique vertex.

If your mesh made out of quads? Direct3D doesn't handle drawing quads, and these must be broken into triangles. Usually modelling is done at a triangle level.

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