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

3

6

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 :) .

Gine answered 3/10, 2009 at 20:43 Comment(0)
R
1

here you are

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

Generator

Robomb answered 3/10, 2009 at 20:55 Comment(1)
The first link does not really define the order of the fields in the structure (already read it before asking the question). However, the second link seems to be fairly complete, thanks a lot !Gine
F
2

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.

Flavorous answered 2/5, 2012 at 10:46 Comment(0)
R
1

here you are

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

Generator

Robomb answered 3/10, 2009 at 20:55 Comment(1)
The first link does not really define the order of the fields in the structure (already read it before asking the question). However, the second link seems to be fairly complete, thanks a lot !Gine
L
0

Well you should be defining it as follows.

struct EitherVertex
{
    float x, y, z;
    DWORD col;
    float nx, ny, nz
};

or

struct EitherVertex
{
    D3DXVECTOR3 pos;
    DWORD col;
    D3DXVECTOR3 nrm;
};

(D3DVERTEX refers to an entire vertex struct and not just a 3 element vector)

Of your 2 options a lot depends on how you access those vert elements. If you are using the depreceated FVF then the second of your 2 choice is the more correct.

If however you are using Vertex Declarations then YOU define where in the struct the relevant data is and the ordering does not matter.

Legerdemain answered 3/10, 2009 at 20:53 Comment(2)
The problem is the general order, should DIFFUSE always be before NORMAL ? What about TEX1, TEX2, etc. ? I'm not using vertex declarations because the original file format is not using them. In fact I'm using the data in the file to draw the mesh with OpenGL :) .Gine
The problem the OP is having is that in C/C++, the order of a struct is important when using FVF. The FVF has to be arranged correctly to work.Robomb

© 2022 - 2024 — McMap. All rights reserved.