Archived

This topic is now archived and is closed to further replies.

changing vertices

This topic is 5876 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 there. i have made a rotating and translating cube in direct3D. but now i would like to get it to change shape in the renderloop in realtime. how do i do that when the vertex struct is already set in the Init3D() function?. because the vertices is already set when the program enters the Render() loop. how do i change it IN the Render() loop?. thanks in advance.

Share this post


Link to post
Share on other sites
Basically, you have a FrameUpdate() ( or something of the like) that's called constantly on idle-time (no windows msgs to process).

This FrameUpdate should do 2 things :
1 - Modify any application states, perform any required calculations (AI, Visibility determination) through calling the relevant functions
2 - Renders the scene through a call to Render()

Since Render() renders from an already-initialized vertex buffer, FrameUpdate() would have to modify the buffer every specific time (you can control that using a simple timer).
then Render() is going to render the updated BUffer.

Modifying vertex buffer data is simple, and can be done in more than one way :

1) Use a dynamic vertex buffer (Create with D3DUSAGE_DYNAMIC and D3DPOOL_DEFAULT) :
This buffer is ideal for refilling every time you need to update the buffer.
You just lock it with the D3DLOCK_DISCARD flag which returns a new pointer (thus saving some time, but that means you lose all the data that was stored in it), then you use the pointer to fill the buffer with some vertices.
You then unlock, render.

2) Overwrite an ordinary buffer :
Lock the buffer, overwrite its contents, unlock it, render it

3) Recreate a buffer (used for resize) :
Destroy, Create with new size, Lock, fill, unlock, render.

Although instead of resizing, you can use a dynamic (or ordinary) buffer and just split your vertices into groups (where each group can fit into the buffer) and then render them using multiple DrawPrimitive() calls


Sorry for any mistakes here (There might be)
GOod Luck!

Edited by - Coder on November 14, 2001 7:42:16 AM

Share this post


Link to post
Share on other sites
please!. whats wrong, it didnt show nuttin, black screen . whats wrong.


HRESULT InitGeometry()
{

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






VOID Render()
{


CUSTOMVERTEX g_Vertices[] =
{
{ 0.0f,0.0f, 0.0f,0xffffffff }, // x,y,z,color
{ 1.0f,0.0f, 0.0f,0xffffffff },
{ 1.0f,1.0f, 0.0f,0xffffffff },
{ 0.0f,1.0f, 0.0f,0xffffffff },
{ 0.0f,0.0f, 0.0f,0xffffffff },


{ 0.0f,0.0f, -1.0f,0xffffffff }, // x,y,z,color
{ 1.0f,0.0f, -1.0f,0xffffffff },
{ 1.0f,1.0f, -1.0f,0xffffffff },
{ 0.0f,1.0f, -1.0f,0xffffffff },
{ 0.0f,0.0f, -1.0f,0xffffffff },

};



if( FAILED( g_pVB->Lock( 0, sizeof(g_Vertices), (BYTE**)&pVertices, 0 ) ) )
memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
g_pVB->Unlock();


g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );

g_pd3dDevice->BeginScene();
D3DXMATRIX Worldrotate;
D3DXMATRIX Worldtranslate;
D3DXMATRIX matWorld

if(raknare>30)raknare=0;
raknare=raknare+0.04;

D3DXMatrixTranslation(&Worldtranslate, raknare/4, -raknare, raknare);
D3DXMatrixRotationYawPitchRoll(&Worldrotate, 0, 0, raknare);
D3DXMatrixMultiply(&matWorld, &Worldrotate, &Worldtranslate);

g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );

D3DXMATRIX matView;
D3DXMatrixLookAtLH( &matView, &D3DXVECTOR3( 0.0f, 3.0f,-5.0f ),
&D3DXVECTOR3( 0.0f, 0.0f, 0.0f ),
&D3DXVECTOR3( 0.0f, 1.0f, 0.0f ) );
g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );

// Render the vertex buffer contents
g_pd3dDevice->SetStreamSource( 0, g_pVB, sizeof(CUSTOMVERTEX) );
g_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
g_pd3dDevice->DrawPrimitive( D3DPT_LINESTRIP, 0, 8);

g_pd3dDevice->EndScene();
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}



Edited by - Funkodysse on November 14, 2001 9:37:10 AM

Edited by - Funkodysse on November 14, 2001 9:38:14 AM

Share this post


Link to post
Share on other sites
1 - You''ve defined 10 vertices which make 9 lines, yet you render 8 (this is definitely not the reason everything''s black)

2 - These vertices :
{ 0.0f,0.0f, 0.0f,0xffffffff }, // x,y,z,color
{ 1.0f,0.0f, 0.0f,0xffffffff },
{ 1.0f,1.0f, 0.0f,0xffffffff },
seem to be defined in counter clock-wise direction. What''s the backface culling setting?

Share this post


Link to post
Share on other sites
this is in the Init3D funkction

// Turn off culling, so we see the front and back of the triangle
g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );

// Turn off D3D lighting, since we are providing our own vertex colors
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );



can u figure it out?

Share this post


Link to post
Share on other sites
quote:

if( FAILED( g_pVB->Lock( 0, sizeof(g_Vertices), (BYTE**)&pVertices, 0 ) ) )
memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
g_pVB->Unlock();



Is this a typo or what? looks like you are only copying vertices if the vb FAILS to lock. Maybe you had a return EFAIL here, or you just forgot to put it in?

Share this post


Link to post
Share on other sites
quote:
2 - These vertices :
{ 0.0f,0.0f, 0.0f,0xffffffff }, // x,y,z,color
{ 1.0f,0.0f, 0.0f,0xffffffff },
{ 1.0f,1.0f, 0.0f,0xffffffff },
seem to be defined in counter clock-wise direction. What''s the backface culling setting?


I believe I''ve made a mistake. Backface culling is applied for faces (i.e Triangles) I don''t think backface culling is applied to lines

Share this post


Link to post
Share on other sites