3

How do you migrate Flexible Vertex Format Constants (or FVF codes) from DirectX 9 to Direct3D 11?

Old code:

#include <d3dx9.h>

struct Vertex {

public:
    Vertex() : p(D3DXVECTOR3(0.0f, 0.0f, 0.0f)), n(D3DXVECTOR3(0.0f, 0.0f, 0.0f)), tu(0.0f), tv(0.0f) {}
    Vertex(D3DXVECTOR3 p, D3DXVECTOR3 n, float tu, float tv) : p(p), n(n), tu(tu), tv(tv) {}

    // Position of the vertex (in world space)
    D3DXVECTOR3 p;
    // Normal of this vertex
    D3DXVECTOR3 n;
    // Texture UV coordinates
    float tu, tv;
};

// D3DFVF_XYZ: Vertex format includes the position of an untransformed vertex.
// D3DFVF_NORMAL: Vertex format includes a vertex normal vector.
// D3DFVF_TEX1: Number of texture coordinate sets for this vertex.
#define VERTEX_FVF ( D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1 )
#define VERTEX_FVF_SIZE D3DXGetFVFVertexSize( VERTEX_FVF )

New code without D3DX:

#include <DirectXMath.h>
using namespace DirectX;

struct Vertex {

public:
    Vertex() : p(XMFLOAT3(0.0f, 0.0f, 0.0f)), n(XMFLOAT3(0.0f, 0.0f, 0.0f)), tu(0.0f), tv(0.0f) {}
    Vertex(XMFLOAT3 p, XMFLOAT3 n, float tu, float tv) : p(p), n(n), tu(tu), tv(tv) {}

    // Position of the vertex (in world space)
    XMFLOAT3 p;
    // Normal of this vertex
    XMFLOAT3 n;
    // Texture UV coordinates
    float tu, tv;
};

// ??

P.S.: Can someone with enough reputation points create and add a directx9 tag.

Matthias
  • 1,074
  • 9
  • 25

1 Answers1

2

Vertex formats are specified in D3D11 using the input layout. An example input layout description for your vertex might look like:

D3D11_INPUT_ELEMENT_DESC aInputDescs[] =
{
    { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, UINT(offsetof(Vertex, p)), D3D11_INPUT_PER_VERTEX_DATA, 0 },
    { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, UINT(offsetof(Vertex, n)), D3D11_INPUT_PER_VERTEX_DATA, 0 },
    { "UV", 0, DXGI_FORMAT_R32G32_FLOAT, 0, UINT(offsetof(Vertex, tu)), D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
Nathan Reed
  • 25,002
  • 2
  • 68
  • 107
  • Is there a tangible D3D11 book you advice (like the red, blue, orange books for OpenGL)? Or is it just better to stick to Microsoft's webpages? – Matthias Oct 15 '16 at 11:15
  • 1
    @Matthias I learned from the online API docs and other online resources. Maybe someone else can recommend a good book; I'm sure there are some out there. – Nathan Reed Oct 15 '16 at 14:01