• Advertisement
Sign in to follow this  

D3D9 can't draw vertices when order changes?

This topic is 2451 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

So I'm writing a wrapper class for D3D9, and I have a problem. When I add vertices like this:


triangle->add_vertex(new SimpleColoredVertex(840.0f, 400.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 0)));
triangle->add_vertex(new SimpleColoredVertex(1040.0f, 650.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 0)));
triangle->add_vertex(new SimpleColoredVertex(640.0f, 650.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 0)));


DX9 is able to draw the triangle.

When I switch up the order and populate my vertex buffer like this:


triangle->add_vertex(new SimpleColoredVertex(840.0f, 400.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 0)));
triangle->add_vertex(new SimpleColoredVertex(640.0f, 650.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 0)));
triangle->add_vertex(new SimpleColoredVertex(1040.0f, 650.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 0)));


Nothing happens.

Here is my add_vertex code where vertices is declared as std::vector<void*> vertices:


void VertexBuffer::add_vertex(void* v){
this->vertices.push_back(v);
}


The code for creating and populating the vertex buffer with data is as follows:


bool GraphicsRenderer::createVertexBuffer(VertexBuffer* vbuffer, DWORD usage, D3DPOOL pool, HANDLE* pSharedHandle) const {
if(FAILED(pDevice->CreateVertexBuffer(vbuffer->get_stride()*vbuffer->numverts(), usage, vbuffer->get_fvf(), pool, &vbuffer->vertex_buffer, pSharedHandle))){
return false;
}

//Lock the vertex buffer so that nothing else can access our memory
void* void_ptr = nullptr;
if(FAILED(vbuffer->vertex_buffer->Lock(0, 0, &void_ptr, 0))){
return false;
}

//Copy the data
for(unsigned int i = 0; i < vbuffer->vertices.size(); i++){
BYTE* data = new BYTE[vbuffer->get_stride()];
assert(data != NULL);
data = reinterpret_cast<BYTE*>(vbuffer->vertices);

memcpy(reinterpret_cast<BYTE*>(void_ptr) + i*vbuffer->get_stride(), data, vbuffer->get_stride());

delete data;
}

//Unlock vertex buffer memory
if(FAILED(vbuffer->vertex_buffer->Unlock())){
return false;
}

vbuffer->vertices.clear();

return true;
}


Do you guys have any idea what's wrong?

Share this post


Link to post
Share on other sites
Advertisement
Assuming you're not drawing indexed geometry (e.g. you have an index buffer with your vertex buffer), changing the order of the vertices in your vertex buffer changes the winding order of your geometry. The winding order defines what triangles are "front facing" or "back facing". By default, back faces are culled since they would be occluded by the front face, which improves performance. So when you changed the winding order, your front facing triangle was flipped, and therefore nothing was drawn.

You can of course change the cull mode (e.g. switch to render back/cull front, or cull none), which is a render state. I'm sure if you set the cull mode render state to D3DCULL_NONE, your triangle will be drawn either way you order its vertices.

Share this post


Link to post
Share on other sites
Kthnx. I haven't done any D3D in a while and I need to start picking things back up.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement