154
edits
Changes
→Fragment Shader
</syntaxhighlight>
==== Fragment Shader ====
<syntaxhighlight lang="cpp">
#define FRAGMENT_SHADER_FILE L"fragmentShader.hlsl"
#define FRAGMENT_SHADER_ENTRY_POINT "fragmentShader"
</syntaxhighlight>
<syntaxhighlight lang="cpp">
// Fragment Shader Support
static ID3DXConstantTable* uniformFS; // for fragment shader
</syntaxhighlight>
<syntaxhighlight lang="cpp">
IDirect3DPixelShader9* APIBase::fragmentShader = nullptr; // fragment shader
ID3DXConstantTable* APIBase::uniformFS = nullptr; // for fragment shader
</syntaxhighlight>
<syntaxhighlight lang="cpp">
LPD3DXBUFFER compiledCodeFS = nullptr;
</syntaxhighlight>
:: [http://msdn.microsoft.com/en-us/library/windows/desktop/bb174450%28v=vs.85%29.aspx IDirect3DDevice9::SetPixelShader()]
<syntaxhighlight lang="cpp">
Colour colour(red, green, blue);
:: [http://msdn.microsoft.com/en-us/library/windows/desktop/bb205775%28v=vs.85%29.aspx ID3DXConstantTable::SetFloatArray()]
:: [http://msdn.microsoft.com/en-us/library/windows/desktop/bb205776%28v=vs.85%29.aspx ID3DXConstantTable::SetInt()]
<syntaxhighlight lang="cpp">
uniformFS->SetBool(d3dd, "lighting", b);
uniformFS->SetFloat (d3dd, "material.power", (FLOAT)r.power);
</syntaxhighlight>
<syntaxhighlight lang="cpp">
}
}
</syntaxhighlight>
<syntaxhighlight lang="cpp">
uniformFS->SetFloatArray(d3dd, "material.ambient", (FLOAT*)&ambient[i], 4);
</syntaxhighlight>
:: [http://msdn.microsoft.com/en-us/library/windows/desktop/bb205774%28v=vs.85%29.aspx ID3DXConstantTable::SetFloat()]
<syntaxhighlight lang="cpp">
uniformFS->SetInt(d3dd, typ, type);
uniformFS->SetFloat(d3dd, ran, range);
</syntaxhighlight>
<syntaxhighlight lang="cpp">
uniformFS->SetFloatArray(d3dd, pos, (FLOAT*)&p, 3);
uniformFS->SetBool(d3dd, constantLightOn, true);
</syntaxhighlight>
<syntaxhighlight lang="cpp">
uniformFS->SetBool(d3dd, constantLightOn, false);
</syntaxhighlight>
<syntaxhighlight lang="cpp">
char str[] = "texOn";
uniformFS->SetBool(d3dd, str, true);
</syntaxhighlight>
<syntaxhighlight lang="cpp">
char str[] = "texOn";
</syntaxhighlight>
* Device
:vertexShader.hlsl - Constant Memory<syntaxhighlight lang="cpp">// Types//// RawVertex holds the original data for a vertex in the stream//struct RawVertex { float3 position : POSITION; // position in local space float3 normal : NORMAL; // normal in local space float2 texCoord : TEXCOORD0; // texture coordinates}; // TransformedVertex holds the transformed data for the vertex//struct TransformedVertex { float4 position : POSITION; // position in homogeneous clip space float2 texCoord : TEXCOORD; // texture coordinates float3 worldPos : TEXCOORD1; // position in world space float3 worldNor : TEXCOORD2; // lighting normal in world space float3 toViewer : TEXCOORD3; // direction to viewer in world space}; // Uniform Data (constant for the stream of vertices)//// Geometryfloat4x4 viewProjection; // view * projection transformationfloat4x4 world; // world transformationfloat3 viewPoint; // camera viewpoint for specular calcs// Lit Vertexbool litVertex; // omit lighting calculations - already lit </syntaxhighlight>: vertexShader.hlsl - vertexShader()<syntaxhighlight lang="cpp">// TransformedVertex holds the transformed data for the vertex//struct TransformedVertex { float4 position : POSITION; // position in homogeneous clip space float2 texCoord : TEXCOORD; // texture coordinates float3 worldPos : TEXCOORD1; // position in world space float3 worldNor : TEXCOORD2; // lighting normal in world space float3 toViewer : TEXCOORD3; // direction to viewer in world space}; // Uniform Data (constant for the stream of vertices)//// Geometryfloat4x4 viewProjection; // view * projection transformationfloat4x4 world; // world transformationfloat3 viewPoint; // camera viewpoint for specular calcs// Lit Vertexbool litVertex; // omit lighting calculations - already lit // vertexShader receives a raw data for a vertex and transforms that data//TransformedVertex vertexShader(RawVertex raw) { TransformedVertex transformed; // result returned by this function float4 worldPosition; // world position of the vertex float3 worldNormal; // vertex normal in world space // Transform the vertex to homogeneous clip coordinates // // A more efficient algorithm would accept the world*view*projection // tranformation as one uniform matrix and avoid the 2-stage product // This will require a bit of restructuring of the application code. // worldPosition = mul(float4(raw.position, 1.0), world); // local to world transformed.position = mul(worldPosition, viewProjection); //... to clip transformed.worldPos = worldPosition.xyz; // Transform the vertex normal to world space. Only the rotation-scaling // part of the world transformation is used. Since the world // transformation may contain scaling, the result of this multiplication // needs to be normalized. // worldNormal = mul(raw.normal, (float3x3)world); worldNormal = normalize(worldNormal); transformed.worldNor = worldNormal; // Determine the direction from the camera's viewpoint to this vertex for // subsequent lighting calculations // transformed.toViewer = normalize(viewPoint - worldPosition.xyz); // pass the texture coordinates along unaltered // transformed.texCoord = raw.texCoord; return transformed;}</syntaxhighlight>: fragmentShader.hlsl - Constant Memory
<syntaxhighlight lang="cpp">
#define MLIGHTS 4
</syntaxhighlight>
<syntaxhighlight lang="cpp">
// The fragment shader receives raw fragment data and returns a pixel colour