Search code examples
c++directxdirectx-9

Direct3D 9.0 Interleaved non-fvf Vertex Buffer


I am trying to interleave vertex data into an STL container of std vector template. I have successfully done this with OpenGL already, the source code of my mesh interface is available online for my open source project... https://github.com/RobertBColton/enigma-dev/blob/master/ENIGMAsystem/SHELL/Graphics_Systems/OpenGL3/GL3model.cpp

Now I want to do the same method using DirectX and have already managed to do so partially by logical xor'ing my FVF DWORD together with what components of each vertex I have. However this is what the mesh currently renders as when it contains only vertices and normals:

enter image description here

Now DX makes this very difficult for me to do the same thing I was doing in OGL, I have checked, double checked, and checked over and over again and my byte alignment is perfect inside the buffer. What I am trying to do can be better explained by the OpenGL Wiki... /wiki/Vertex_Specification_Best_Practices#Formatting_VBO_Data

Now I do not want to implement a shader to accomplish this either. If anyone could simply offer me any pointers on what I am trying to do or point me in the direction of an example or tutorial of doing this in Direct3D 9 it would really help me out as this does not seem to be very common practice in DX.


Solution

  • After closer inspection of what you are trying to do here, I believe this is what you want:

    D3DVERTEXELEMENT9 custom_vertex [] =
    {
      { 0,  0, D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
      { 0, 12, D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL,   0 },
      { 0, 24, D3DDECLTYPE_FLOAT2,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
      { 0, 32, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR,    0 },
      D3DDECL_END()
    };
    

    The format of this data structure is pretty similar to OpenGL's Vertex Pointer system, you have to pass in the offset, the size and number of components (e.g. D3DDECLTYPE_FLOAT3). You also include the purpose of the field (e.g. D3DDECLUSAGE_COLOR). Unlike OpenGL, D3D can calculate the stride for you simply through use of the D3DDECL_END() macro.

    IDirect3DVertexDeclaration9* vertex_declaration;
    d3dDevice->CreateVertexDeclaration (custom_vertex, &vertex_declaration);
    

    Now you should be good to go and create and fill your vertex buffer using this declaration. You can add/remove the elements as need be by using a different D3DVERTEXELEMENT9 array.

    Do remember to use a DWORD (D3D) or 4 ubyte (OpenGL) colors though. There is no reason to use floating-point colors a good 90% of the time. They just waste storage and memory bandwidth.