Click here to Skip to main content
16,004,833 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am a beginner using Direct3D C++ 2010. Trying to build Tutorial2 which shows 3 vertices for a triangle.
Works fine.
Now I need to understand how to display many triangles.
Based on suggestions from Jack Dingler,
I changed the vertices to 2 triangles as below.
Also changed the buffer parameter 3 * to 6*.
Currently it displays only the first triangle.
Which call below has the number of triangles to display?
Please help.
thanks
ayyangar
6/21
C++
CUSTOMVERTEX vertices[] =
  {
    { 250.0f,  50.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color
      { 250.0f, 250.0f, 0.5f, 1.0f, 0xffff0000, },
      { 50.0f, 250.0f, 0.5f, 1.0f, 0xfffffffff, },
      { 50.0f, 250.0f, 0.5f, 1.0f, 0xfffffffff, },
      { 50.0f, 50.0f, 0.5f, 1.0f, 0xfffffffff, },
      { 250.0f,  50.0f, 0.5f, 1.0f, 0xffff0000, },
  };

if( FAILED( g_pd3dDevice->CreateVertexBuffer( 6 * sizeof( CUSTOMVERTEX ),
                                               0, D3DFVF_CUSTOMVERTEX,
                                               D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
 {
     return E_FAIL;
 }

//part of the code from Render is given below.

VOID Render()

g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof( CUSTOMVERTEX ) );
   g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
   g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 1, 1 );
Posted
Updated 21-Jun-12 9:17am
v3
Comments
JackDingler 20-Jun-12 13:11pm    
What doesn't work? Does the CreateVertexBuffer call fail?

If so, what error does it return?

From MSDN:

Return value

Type: HRESULT

If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be one of the following: D3DERR_INVALIDCALL, D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY.

1 solution

Are you calling code to draw triangles or quads?

If you're calling a function to draw a triangle, it will only use sets of 3 vertices. If you want to use a triangle drawing function to draw a polygon, you'll need to divide it into a mesh of triangles.

To draw a four sided polygon, you'll need 2 triangles.

Keep in mind that triangles have a facing order.

The order that you provide vertices in, determines which face is the front and which is the back.
 
Share this answer
 
v2
Comments
ayyangar 20-Jun-12 14:27pm    
thanks. now i understand that i need to draw 2 triangles. how do i setup the code. i made 6 vertices all together. now it crashes, it would be better if you send me the code to setup the vertices
CUSTOMVERTEX vertices[] =
{
{ 250.0f, 50.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color
{ 250.0f, 250.0f, 0.5f, 1.0f, 0xffff0000, },
{ 50.0f, 250.0f, 0.5f, 1.0f, 0xfffffffff, },
{ 50.0f, 250.0f, 0.5f, 1.0f, 0xfffffffff, },
{ 50.0f, 50.0f, 0.5f, 1.0f, 0xfffffffff, },
{ 250.0f, 50.0f, 0.5f, 1.0f, 0xffff0000, },
};
JackDingler 20-Jun-12 14:51pm    
At what line does it crash? What function is being called when it crashes?
ayyangar 20-Jun-12 15:09pm    
It crashed because of CreateVertexBuffer(3* . it does not crash with value 6
however, it only displays the first triangle.
JackDingler 20-Jun-12 15:18pm    
Please use the "Improve Question" button to provide information on what function you're passing the buffer to, to do the drawing with.

The CreateVertexBuffer function doesn't actually do the drawing. the value of 6 looks correct. Somewhere else, you make a call to display the triangle and you're telling it that it should draw just one triangle. We need to identify that call, and let it know that you're drawing two triangles.
ayyangar 20-Jun-12 17:28pm    
Where do i find the "Improve Question" button? Is it a part of the Visual Studio ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900