Sign in to follow this  
Ben091986

Vertex Buffer directx

Recommended Posts

Ben091986    108
Hi folks, i have the problem that this code is part of my very basic application. I dont want to create a new vertexbuffer each frame... i would more likly to change the current vertex buffer. i have the reference t_buffer but that does not help me right now. how can i update my vertexbuffer without creating a new one. or if that fails how can i destroy the old one before creating a new one. thanks for your time Ben
// this is the function that puts the 3D models into video RAM
void init_graphics(void)
{

	std::vector<float> spec;
	spec.resize(256,0.0f); //fill vector with infos ...

	
    CUSTOMVERTEX t_vert[512];
	int o = 0;
	for(int i = 0;i<512;i+=2)
	{
		t_vert[i].X = 10+1*i;
		t_vert[i].Y = 400.0f-1-400*spec[o++];
		t_vert[i].Z = 0.5f;
		t_vert[i].RHW = 1.0f;
		t_vert[i].COLOR = D3DCOLOR_XRGB(255, 0, 0);

		t_vert[i+1].X = 10+1*i;
		t_vert[i+1].Y = 400.0f;
		t_vert[i+1].Z = 0.5f;
		t_vert[i+1].RHW = 1.0f;
		t_vert[i+1].COLOR = D3DCOLOR_XRGB(255, 255, 0);
	}
    // create a vertex buffer interface called t_buffer
    //how can i change the current one than rather then creating a new one??
	d3ddev->CreateVertexBuffer(512*sizeof(CUSTOMVERTEX),
                               0,
                               CUSTOMFVF,
                               D3DPOOL_MANAGED,
                               &t_buffer,
                               NULL);
    VOID* pVoid;    // a void pointer

    // lock t_buffer and load the vertices into it
    t_buffer->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, t_vert, sizeof(t_vert));
    t_buffer->Unlock();	
    return;
}

Share this post


Link to post
Share on other sites
streamer    415
So, the answer is that you don't need to create vertex buffer every frame, it will degrade your performance. Instead of that make vertex buffer global, and fill it with vertices when you init your application.
If you need to change vertex buffer dynamically (every frame) then create vertex buffer with D3DUSAGE_WRITEONLY |D3DUSAGE_DYNAMIC flags and then with lock and unlock you can access your vertex buffer.

And with DirectX questions, please post in DirectX forum, you can get more accurate answers there.

Share this post


Link to post
Share on other sites

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