HRESULT InitVB()
{
// Initialize three Vertices for rendering a triangle
CUSTOMVERTEX Vertices[] =
{
{ 150.0f, 50.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color
{ 250.0f, 250.0f, 0.5f, 1.0f, 0xff00ff00, },
{ 50.0f, 250.0f, 0.5f, 1.0f, 0xff00ffff, }
};
// Create the vertex buffer. Here we are allocating enough memory
// (from the default pool) to hold all our 3 custom Vertices. We also
// specify the FVF, so the vertex buffer knows what data it contains.
if( FAILED( g_pd3dDevice->CreateVertexBuffer( 3 * sizeof( CUSTOMVERTEX ),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
{
return E_FAIL;
}
// Now we fill the vertex buffer. To do this, we need to Lock() the VB to
// gain access to the Vertices. This mechanism is required becuase vertex
// buffers may be in device memory.
VOID* pVertices;
if( FAILED( g_pVB->Lock( 0, sizeof( Vertices ), ( void** )&pVertices, 0 ) ) )
return E_FAIL;
memcpy( pVertices, Vertices, sizeof( Vertices ) );
g_pVB->Unlock();
return S_OK;
}
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
// Clear the backbuffer to a blue color
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 0, 255 ), 1.0f, 0 );
// Begin the scene
if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{
// Draw the triangles in the vertex buffer. This is broken into a few
// steps. We are passing the Vertices down a "stream", so first we need
// to specify the source of that stream, which is our vertex buffer. Then
// we need to let D3D know what vertex shader to use. Full, custom vertex
// shaders are an advanced topic, but in most cases the vertex shader is
// just the FVF, so that D3D knows what type of Vertices we are dealing
// with. Finally, we call DrawPrimitive() which does the actual rendering
// of our geometry (in this case, just one triangle).
g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof( CUSTOMVERTEX ) );
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 );
// End the scene
g_pd3dDevice->EndScene();
}
// Present the backbuffer contents to the display
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}
vertice - beginner question
I want to render square. I found in DirectX SDK tutoarial how to render triangel by using three Vertices and render them using this command g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 );
Guess I need 6 vertices in CUSTOMVERTEX struct, and i need to change g_pd3dDevice->DrawPrimitive command but i don't know how :(.
Another question..
I need to write color in this fashion 0xffff0000 but when I use photoshop color mixer I get color in this fashion 85c4fe. Should I just write 0xff before color number?
Quote:Original post by dontooIf you add more vertices to the array, you should be able to just change the last number from 1 to 2 in the DrawPrimitive() call - that's the number of triangles to draw.
I want to render square. I found in DirectX SDK tutoarial how to render triangel by using three Vertices and render them using this command g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 );
Guess I need 6 vertices in CUSTOMVERTEX struct, and i need to change g_pd3dDevice->DrawPrimitive command but i don't know how :(.
Quote:Original post by dontooYes, the first 2 characters are the alpha value; 0xff means "opaque", which is probably what you want. Note that changing this won't have any effect unless you've enabled alpha blending.
Another question..
I need to write color in this fashion 0xffff0000 but when I use photoshop color mixer I get color in this fashion 85c4fe. Should I just write 0xff before color number?
*** Source Snippet Removed ***
This is the code that I wrote. It is same as in SDK tutorial, and I changed only Vertices array by adding three more vertices, and i changed D3DPT_TRIANGLELIST, 0, 2 from 1 to 2. When i wnat to start program, my program stops working. I changed nothing but this.
// Initialize three Vertices for rendering a triangle CUSTOMVERTEX Vertices[] = { { 150.0f, 50.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color { 250.0f, 250.0f, 0.5f, 1.0f, 0xff00ff00, }, { 50.0f, 250.0f, 0.5f, 1.0f, 0xff00ffff, }, { 250.0f, 50.0f, 0.5f, 1.0f, 0xff00ffff,}, { 150.0f, 50.0f, 0.5f, 1.0f, 0xffff0000, }, { 250.0f, 250.0f, 0.5f, 1.0f, 0xff00ff00, } };//-----------------------------------------------------------------------------// Name: Render()// Desc: Draws the scene//----------------------------------------------------------------------------- g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 2 );
Quote:Original post by dontooWhat does "stops working" mean? Crashes? What does your debugger tell you? What do the debug runtimes tell you?
When i wnat to start program, my program stops working.
If that's all you changed, then you missed something. You need to increase the buffer size in your call to CreateVertexBuffer (i.e., the first argument).
Quote:Original post by aryxAh - well spotted, I misread that code as sizeof(Vertices) rather than 3 * sizeof( CUSTOMVERTEX ) (Which would also work)
If that's all you changed, then you missed something. You need to increase the buffer size in your call to CreateVertexBuffer (i.e., the first argument).
Quote:Original post by Evil SteveQuote:Original post by aryxAh - well spotted, I misread that code as sizeof(Vertices) rather than 3 * sizeof( CUSTOMVERTEX ) (Which would also work)
If that's all you changed, then you missed something. You need to increase the buffer size in your call to CreateVertexBuffer (i.e., the first argument).
Yeah, it's kind of subtle, especially since the tutorial code uses sizeof(Vertices) everywhere else! I think inconsistency is one of those traits that should NOT exist in tutorials.
// Create the vertex buffer. Here we are allocating enough memory // (from the default pool) to hold all our 3 custom Vertices. We also // specify the FVF, so the vertex buffer knows what data it contains. if( FAILED( g_pd3dDevice->CreateVertexBuffer( 6 * sizeof( CUSTOMVERTEX ), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
I changed from 3 to 6 and now it doesn't crash, but it doesn't render new triangle ( new 3 vertices I wrote ). It renders just one triangle. Guess I need to change something else?
That last 3 vertices are in anti-clockwise order, so they're considered to be "back facing". Either swap the last 2 vertices in the array, or disable culling with:
pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
Quote:Original post by dontoo
*** Source Snippet Removed ***
I changed from 3 to 6 and now it doesn't crash, but it doesn't render new triangle ( new 3 vertices I wrote ). It renders just one triangle. Guess I need to change something else?
Your code probably has backface culling enabled, which means that any triangles that "face away" from your view will be discarded. You need to reorder the verticies you added so this doesn't happen. Notice that if you trace your verticies you go in a clockwise direction. Change the ordering so that it becomes counter-clockwise.
[edit]
Ninja'd! *heads off to meeting with supervisor*
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement