Vertex Buffer directx

Started by
0 comments, last by streamer 16 years, 1 month ago
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.X = 10+1*i;
		t_vert.Y = 400.0f-1-400*spec[o++];
		t_vert.Z = 0.5f;
		t_vert.RHW = 1.0f;
		t_vert.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;
}
Advertisement
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.

This topic is closed to new replies.

Advertisement