0

[Using HLSL, an *.fx file and DirectX9]

I'm new to using shaders and have got a scenario working but don't understand why. I'm confused as to how the mesh can have one version of the vertex decleration while the shader can define another.

I create a ID3DXMesh sphere with D3DXCreateSphere(...), which comes setup with D3DFVF_XYZ | D3DFVF_NORMAL. I call ID3DXMesh::DrawSubset(0) to render; inside BeginPass/EndPass calls.

I then have a vertex shader with the struct defined:

struct Vertex {
    float4 position:POSITION;
    float2 textureCoords:TEXCOORD0;
    float3 lightDir:TEXCOORD1;
    float3 normal:TEXCOORD2;
};

and the shader itself:

Vertex vertexShader( Vertex original, float3 _normal:NORMAL ) {
    Vertex result = (Vertex)0;

    float4x4 worldViewMatrix = mul( worldMatrix, viewMatrix );
    float4x4 worldViewProjectionMatrix = mul( worldViewMatrix, projectionMatrix );

    result.position = mul( original.position, worldViewProjectionMatrix );
    result.normal = -normalize( mul(_normal, worldMatrix) ); 

    result.textureCoords = original.textureCoords;
    result.lightDir = normalize(presetLightDir);

    return result;
}

While I recognize that the lightDir and textureCoords might as well be null before I set them, what data does DirectX actually give in that Vertex original parameter?

Just the position? I notice that the float3 _normal:NORMAL is offered as a separate parameter but is that value also inside original at the start of the shader too?

Jono
  • 3,949
  • 4
  • 28
  • 48

1 Answers1

1

The data that I expect to be set are:

  • result.position

  • _normal

and as original.textureCoords is not set, result.textureCoords is most likely garbage.

The way this works is trough the " type name : semantic " so in your case " float4 position:POSITION; "

Normally the data to send to a vertex shader would just be in a line position>texturecoords>lightDir>etc And the data you send should keep this order.

But by using semantics you can change the order. So the data you send will fill in a float3 POSITION and a float3 NORMAL. (note that you are requesting a float4 which is XYZW where W = 1.0f)

The fact that there also is a "normal" in the Vertex struct doesn't matter as it is marked as TEXTURECOORD2.

I hope this help, although I might not have been very clear.

Stardidi
  • 308
  • 2
  • 10