Sign in to follow this  
Sepiantum

D3D9 can't draw vertices when order changes?

Recommended Posts

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

[code]
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)));
[/code]

DX9 is able to draw the triangle.

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

[code]
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)));
[/code]

Nothing happens.

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

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

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

[code]
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[i]);

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;
}
[/code]

Do you guys have any idea what's wrong?

Share this post


Link to post
Share on other sites
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 [url="http://msdn.microsoft.com/en-us/library/bb204882%28v=vs.85%29.aspx"]render state[/url]. 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

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