Skip to main content
gamedev.net
🔒 Locked

Vertex Buffer Rendering Problem

Started by tscott1213 1 reply 200+ views
Original Post
tscott1213
tscott1213
Hello all, I must be making some newbie mistake here that I can''t see. In the following example I setup the vertext to render a textured quad as a triangle list. The first triangle gets rendered but the second one doesn''t. The problem appears to be with the lower right vertex because if I switch the order of the vertices then the first triangle doesn''t render and the second one does. Thanks for the help. Here is the code: //Lock the VertexBuffer SingleTexFVF *pVertBuffer; m_pD3DVertexBuffer->Lock(0, VERTEX_PER_SPRITE * sizeof(SingleTexFVF), (void **)&pVertBuffer, D3DLOCK_DISCARD); //Setup the first Vertex pVertBuffer[0].x = x; pVertBuffer[0].y = y; pVertBuffer[0].z = z; pVertBuffer[0].u = 0.0f; pVertBuffer[0].v = 0.0f; pVertBuffer[0].nx = x; pVertBuffer[0].ny = y; pVertBuffer[0].nz = -1; //Setup the second & fourth Vertex pVertBuffer[1].x = pVertBuffer[3].x = x + m_dwWidth; pVertBuffer[1].y = pVertBuffer[3].y = y; pVertBuffer[1].z = pVertBuffer[3].z = z; pVertBuffer[1].u = pVertBuffer[3].u = 1.0f; pVertBuffer[1].v = pVertBuffer[3].v = 0.0f; pVertBuffer[1].nx = pVertBuffer[3].nx = x + m_dwWidth; pVertBuffer[1].ny = pVertBuffer[3].ny = y; pVertBuffer[1].nz = pVertBuffer[3].nz = -1; //Setup the third & fifth Vertex pVertBuffer[2].x = pVertBuffer[4].x = x; pVertBuffer[2].y = pVertBuffer[4].y = y - m_dwHeight; pVertBuffer[2].z = pVertBuffer[4].z = z; pVertBuffer[2].u = pVertBuffer[4].u = 0.0f; pVertBuffer[2].v = pVertBuffer[4].v = 1.0f; pVertBuffer[2].nx = pVertBuffer[4].nx = x; pVertBuffer[2].ny = pVertBuffer[4].ny = y - m_dwHeight; pVertBuffer[2].nz = pVertBuffer[4].nz = -1; //Setup the sixth Vertex pVertBuffer[5].x = x + m_dwWidth; pVertBuffer[5].y = y - m_dwHeight; pVertBuffer[5].z = z; pVertBuffer[5].u = 1.0f; pVertBuffer[5].v = 1.0f; pVertBuffer[5].nx = x + m_dwWidth; pVertBuffer[5].ny = y - m_dwHeight; pVertBuffer[5].nz = -1; m_pD3DVertexBuffer->Unlock(); m_pD3DDevice->SetFVF(SINGLETEX_FVF); m_pD3DDevice->SetStreamSource(0, m_pD3DVertexBuffer, 0, sizeof(SingleTexFVF)); m_pD3DDevice->SetTexture(0, m_pD3DTexture); m_pD3DDevice->SetMaterial(m_pD3DMaterial); m_pD3DDevice->DrawPrimitive(0, 2);
IFooBar
IFooBar
The problem is with the order of specifying your vertices. Your two polygons are defined like this:


V1---V2
| /
|p1/
| /
|/
V3

V1
/|
/ |
/ |
/p2 |
/ |
V2----V3


See triangle one, if you move from v1 to v2 you are going in a clockwise order. Now see triangle 2, if you move from v1 to v2 you are going in an anticlockwise order. The order you define your vertices in make a difference to whether they show up on screen or not. By default, d3d uses counter-clockwise backface culling. What this means is this:

Every three vertices that are passed to d3d are passed in the order they are defined in. If CCW culling is on, then if you pass in triangle two as it is, d3d will assume you are looking at a backface and will cull it away. On the other hand if you had CW culling turned on, and you passed in triangle 1, then d3d would assume you are looking at a backface and have it culled away.

By default, CCW culling is turned on in d3d, so Ill assume that it what was on when you tried to render those two triangles. When a triangle is rendered, it's three vertices are passed to d3d in the order that they are defined in. So in your case the first three vertices would be v1, v2 and v3 of triangle 1.

Say the view matrix is pointing straight at the triangle. Now given the three points of the first triangle, and without going into much math, d3d would find the front of the triangle. In this case the front of the triangle is pointing straight at you. Then d3d would check if the viewer is looking at the front face or not. If it is, then the triangle is drawn.

Now for the second triangle, the vertices are defined in a CCW fashion, so once the verts are passed to d3d, the front will be facing away from the viewer (remember CCW faces are considered back faces by default), and it wont show.

Now you have the concepts, try and work on a solution. You can simply turn off culling all together by setting the D3DRS_CULLMODE renderstate to D3DCULL_NONE. But first try and solve it as it is.


| TripleBuffer Software |
| Plug-in Manager :: System Information Class :: C++ Debug Kit :: DirectX Tutorials :: Awesome Books |


[edited by - ifoobar on April 24, 2004 10:15:46 PM]
[size=2]aliak.net
tscott1213
tscott1213
Thank you for taking the time to explain this to me.

This is very clear.

I had thought that the normal for the vertices would determine the front and back of the triangles. I clearly need to read more on the culling and the normals to better understand what is going on.


Thanks again.
Todd

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.