How to pass these vertices to Direct3D to avoid back culling?

Started by
12 comments, last by FrEEzE2046 13 years, 5 months ago
If I have a set of vertices which constitute of a pyramid,
Which order should I pass them to dx in order to avoid back culling?
when light casts on them?

They are
        10, 10, 10       ..        .. 5,0,15---------- 15,0,15    | /            \| 5,0,5-------------15,0,5


struct JVERTEX {		FLOAT x, y, z;	//	FLOAT rhw;		FLOAT nx, ny, nz;		D3DXCOLOR color;		FLOAT tu, tv;   // Texture coordinates};// Custom flexible vertex format (FVF) describing the custom vertex structure#define D3DFVF_JVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_TEX1)void drawPyramid (float x, float y, float z, float height, float width){	JVERTEX v[] = 	{		{10, 10, 10,				-ROOT2D2, ROOT2D2, 0, D3DXCOLOR(255,0,255,255), 0, 0, },////??		 	};	

Thanks in advance



Jack

[Edited by - lucky6969b on October 21, 2010 6:51:18 AM]
Advertisement
D3D will cull triangles with vertices specified in counter-clockwise order by default. In D3D9, you can change that with SetRenderState(D3DRS_CULLMODE, mode).
Quote:Original post by Evil Steve
D3D will cull triangles with vertices specified in counter-clockwise order by default. In D3D9, you can change that with SetRenderState(D3DRS_CULLMODE, mode).


Hello Steve,
Thanks for your prompt response,
So will this just barely work?

JVERTEX v[] = 	{		{10, 10, 10,				-ROOT2D2, ROOT2D2, 0, D3DXCOLOR(255,0,255,255), 0, 0, },                {5,10,15,  ... normal2                {5,0,5,  ...normal3                {15,0,15 ... normal4                {15,0,5   ... normal5         };          r = g_pDevice->CreateVertexBuffer(sizeof(JVERTEX)*5,0, D3DFVF_JVERTEX, D3DPOOL_DEFAULT, &g_pVB);

cos I am not sure which way is counter-clockwise
Thanks
Jack

[Edited by - lucky6969b on October 21, 2010 7:51:26 AM]
Assuming this is a 3D pyramid with no base, you want:

// Front:(10, 10, 10)(15,0,5)(5,0,5)// Right:(10, 10, 10)(15,0,15)(15,0,5)// Left(10, 10, 10)(5,0,5)(5,0,15)// Back:(10, 10, 10)(5,0,15)(15,0,15)

Quote:Original post by Evil Steve
Assuming this is a 3D pyramid with no base, you want:

*** Source Snippet Removed ***


Thanks Steve.
Let me think about it.
Really appreciated...
But should I create 5 seperate vertex buffers for each face.
That seems to be easier.
Jack
The winding of a triangle determines the direction of the cross product, which lets the API decide whether or not to cull the triangle.

Picture a Clock, the direction the clock is facing is the direction of it's normal. If the vertices are listed in clockwise order, than this triangle will be drawn by default. A Triangle with vertices at <noon, 4, 8> will be drawn, while a Triangle with vertices reversed <noon, 8, 4> will not.
Quote:Original post by lucky6969b
But should I create 5 seperate vertex buffers for each face.
That seems to be easier.
The easiest and best way would be to use an index buffer.

Then your vertices would be:
{(10, 10, 10), (5, 0, 5), (15, 0, 5), (5, 0, 15), (15, 0, 15)}

And your indices would be:
{
0, 2, 1, // Front
0, 4, 2, // Right
0, 1, 3, // Left
0, 3, 4, // Back
}

Then you can render with a single call to:
DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 5, 0, 4);
Thanks both of you
Burnt_Fyr and Evil Steve
for your help
Hi Evil Steve,
Here I am again. Sorry, after looking into how the vertices are setup,
I don't understand why 0,2,1 isn't clockwise but anti-clockwise
10 10 10         5,0,15  5,0,5


The order should be 10,10,10 => 5,0,5 => 5,0,15
How come?
Thanks
Jack
Quote:Original post by lucky6969b
Hi Evil Steve,
Here I am again. Sorry, after looking into how the vertices are setup,
I don't understand why 0,2,1 isn't clockwise but anti-clockwise
10 10 10         5,0,15  5,0,5


The order should be 10,10,10 => 5,0,5 => 5,0,15
How come?
Thanks
Jack


Winding is relative and coordinate systems are arbitrary. If your camera origin is (0,0,-50) and the look vector is (0,0,1), then in a left handed cordinate system those vertices appear counterclockwise. The coordinate system being left handed due to DirectX programmers typically using D3DXPerspectiveFovLH to set their projection matrix.

This topic is closed to new replies.

Advertisement