Sign in to follow this  

Understanding DrawPrimitive and vertexs.

This topic is 4857 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

Hi all: I supose that this question is a bit for a beginner ( And that's what I am! :P ) but I've realized that I understand some more advanced concepts but this i don't. My question rounds around DrawPrimitive question and defining vertexs. I'm creating a CFace class in order to draw a rectangle (For example, for a skybox). For drawing a rectangle, we need 4 vertexs, isn't it?? (I think it's obvious, but I've got some extrange results when trying. They were anti-clock defined). If this is correct, the function that I don't understand is DrawPrimitive. cpy_pD3DDevice->DrawPrimitive (D3DPT_TRIANGLELIST, 0, 2); I now 0 means the start vertex and 2 the "primitive count". Mainly , that's what i don't understand. I don't know what means. Every primitive count is a vertex defined in my struct FACE_CUSTOMVERTEX cvVertices[] = { ... } ???. If i had 4 vertex how can I read all?. I suposed this two ways: DrawPrimitive (D3DPT_TRIANGLELIST, 0, 2); DrawPrimitive (D3DPT_TRIANGLELIST, 2, 4); // If the primitive count is ciclical, I mean, 4 means 0, because I have 4 vertexs. or: DrawPrimitive (D3DPT_TRIANGLELIST, 0, 2); DrawPrimitive (D3DPT_TRIANGLELIST, 2, 0); // If is not. Really, the problem is that I don't understand how directx knows what vertices are to render, and in what order, because if i drawing a "face" or rectangle this is composed of two triangles... Thx ! :)

Share this post


Link to post
Share on other sites
The way that a Tri-List works is it stores 3 vertices for each triangle. A rectangle is made of 2 triangle, so that is 6 vertices. I think you want to use the Tri-STRIP primitive. It has 3 vertices for the first triangle, then each consecutive vertex makes a new triangle from the last 2 vertices and itself (see figure).


TRI LIST:
1
|\ 4---5
| \ \ |
| \ \ |
3---2 \|
6


TRI STRIP

1----2
| /|
| / |
| / |
|/ |
3----4


I usually try to make Tri-Strips in a z-fashion (to make sure they are not culled. Hope that helps a bit.

Edit: Another little quirk about DirectX is culling. DirectX will not draw triangles that are facing away from the camera. This is done by examining if the vertices of each triangle are in a clockwise (CW) or counter-clockwise (CCW) order. By default it only draw CW (it culls the CCW ones). You can change this by altering the render states. Set it to None and it will do no culling, CW will cull all clockwise ardered vertices, CCW will cull CCW ones.

Share this post


Link to post
Share on other sites
You can tell DX which vertex to use for each triangle via an index buffer and then use DrawIndexedPrimitive. If you do not want to use an index buffer you can tell DX via the first flag how your triangles are made up. You are specifying D3DPT_TRIANGLELIST whioch means that DX treats every sequential group of 3 vertices as a triangle. So you are going to need 6 vertices for your rectangle, 3 for each face. So here is a good reason to use an index buffer, with an index buffer you can just provide 4 vertices and use the index buffer to tell DX which verts make up which triangle.

So if you have vertices 0,1,2 and 3 defined at each corner of the rectangle your index buffer would be 0,1,2,0,2,3 it is saying the first tri uses verts 0,1 and 2 and the second uses verts 0,2 and 3. BTW: define in clockwise order for DX

The parameters to the DrawPrimitive fn are flag, start vertex and primitive count. So if you have 6 vertices for your 2 triangles using a triangle list the call will be:

DrawPrimitive(D3DPT_TRIANGLELIST,0,2)

You may also want to look at my website which describes all this process from scratch and in more detail: Try this page as a start point: primitives

Share this post


Link to post
Share on other sites
Thank you very much, very appreciated:

Only another question...

Let's do it for start with Triangle List. But... What's with Texture Mapping?... I mean,

If i have a texture (128x128) bmp, it's easy to assing to four vertexs:
{ ,,, D3DCOLOR_XRGB(255, 0, 0), 0.0f , 0.0f ,},
{ ,,, D3DCOLOR_XRGB(255, 0, 0), 0.0f , 1.0f ,},
{ ,,, D3DCOLOR_XRGB(255, 0, 0), 1.0f , 1.0f ,},
{ ,,, D3DCOLOR_XRGB(255, 0, 0), 1.0f , 0.0f ,},

But what's happen if i have 6 vertexs? ... I don't have clearly how to assing the texture mapping points. I've assigned the same texture mapping point for "equal" vertexs , but i don't get a good result. In some way..it seems that the texture is a lot zoomed (¿?)... [Edit: It seems to appear only a color, i supose that is the texture zoomed but i don't know]

Some texturing code :) ->

virtual bool SetTexture(LPCTSTR szTextureFilePath){
D3DXCreateTextureFromFile(cpy_pD3DDevice, szTextureFilePath, &cpy_pTexture)
}

if (cpy_pTexture != NULL) {
cpy_pD3DDevice->SetTexture(0, cpy_pTexture);
cpy_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
}

I don't know where's the problem. Thank you very much Sr_Guapo & Trip99 :)

[Edited by - Jacob84 on August 26, 2004 7:06:50 PM]

Share this post


Link to post
Share on other sites

This topic is 4857 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.

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