Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Position of fields (position, normal, etc.) in a D3D9 vertex

I'm trying to read 3D models which were created for a DirectX applications, which are defined in the following way :

  • In the file header, the Flexible Vertex Format (FVF) of the mesh is given (actually, I have any combinations of D3DFVF_{XYZ,DIFFUSE,NORMAL,TEX1,TEX2} in the meshes I tested)
  • Then, n vertices are given in a linear pattern, with the fields presents according to the FVF.

However, I do not know the order of these fields. The logic would be that it is defined somewhere in DirectX documentation, but I was unable to find it. For example, which of these two structures is correct with FVF = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_NORMAL (C syntax, but this problem applies to every language) ?

// This one ?
struct vertex1
{
    D3DVERTEX pos;
    DWORD color;
    D3DVERTEX normal;
};

// Or this one ?
struct vertex2
{
    D3DVERTEX pos;
    D3DVERTEX normal;
    DWORD color;
};

I would like a general answer to this question with all the possible fields (for example, XYZ before DIFFUSE before NORMAL before TEX1 before TEX2). A pointer to the right page of the documentation would be fine too as I was not able to find it :) .

like image 297
Pierre Bourdon Avatar asked Oct 03 '09 20:10

Pierre Bourdon


2 Answers

I ran into the same thing myself.

I think the order of the bits is the required order. From d3d9types.h:

#define D3DFVF_RESERVED0        0x001
#define D3DFVF_POSITION_MASK    0x400E
#define D3DFVF_XYZ              0x002
#define D3DFVF_XYZRHW           0x004
#define D3DFVF_XYZB1            0x006
#define D3DFVF_XYZB2            0x008
#define D3DFVF_XYZB3            0x00a
#define D3DFVF_XYZB4            0x00c
#define D3DFVF_XYZB5            0x00e
#define D3DFVF_XYZW             0x4002

#define D3DFVF_NORMAL           0x010
#define D3DFVF_PSIZE            0x020
#define D3DFVF_DIFFUSE          0x040
#define D3DFVF_SPECULAR         0x080

#define D3DFVF_TEXCOUNT_MASK    0xf00
#define D3DFVF_TEXCOUNT_SHIFT   8
#define D3DFVF_TEX0             0x000
#define D3DFVF_TEX1             0x100
#define D3DFVF_TEX2             0x200
#define D3DFVF_TEX3             0x300
#define D3DFVF_TEX4             0x400
#define D3DFVF_TEX5             0x500
#define D3DFVF_TEX6             0x600
#define D3DFVF_TEX7             0x700
#define D3DFVF_TEX8             0x800

I'm pretty sure that the order you are looking for is:

POSITION,NORMAL,PSIZE,DIFFUSE,SPECULAR,TEX0[,TEXn...]

I wasn't able to find a definitive answer in the documentation either.

like image 193
doug65536 Avatar answered Oct 26 '22 22:10

doug65536


here you are

FVF (OP says the information on this page is incorrect. I dont know, didnt check if FVF positioning is correct)

Generator

like image 25
Andrew Keith Avatar answered Oct 26 '22 23:10

Andrew Keith