Game Development Reference
In-Depth Information
// Pixel Shader
//
—————————————————————————————————————————————————————————————
float4 GameCode4_PSMain( PS_INPUT Input ) : SV_TARGET
{
float4 vOutputColor;
if (g_bHasTexture)
vOutputColor =
g_txDiffuse.Sample( g_samLinear, Input.vTexcoord ) * Input.vDiffuse;
else
vOutputColor = Input.vDiffuse;
return vOutputColor;
}
The pixel shader is much simpler than the vertex shader, since all it has to do is mix
a texture sample with the diffuse color sent in from the vertex shader.
Since this pixel can process a texture, there are globals that store a
Texture2D
struc-
ture and a
SamplerState
, which map to the
ID3D11ShaderResourceView
and
ID3D11SamplerState
resources you learned about in the texturing section earlier.
The
VS_INPUT
structure defines the data that is output from the vertex shader,
which will be the diffuse color calculated by the vertex shader and the texture coor-
dinate at the pixel location. The call to
Sample
grabs the texel from the texture,
based on the value of
Input.vTexcoord
. This value is multiplied by
Input.vDiffuse
to blend the light, object color, and texture together. If no texture
is defined,
vOutputColor
is simply set to the
Input.vDiffuse
value.
Just as you saw with the vertex shader, there is a C++ class designed to set up the
pixel shader and communicate data to it.
class GameCode4_Hlsl_PixelShader
{
public:
GameCode4_Hlsl_PixelShader(std::string textureResource);
~GameCode4_Hlsl_PixelShader();
HRESULT OnRestore(Scene *pScene);
HRESULT SetupRender(Scene *pScene, const SceneNode *pNode);
HRESULT SetTexture(std::string textureName);
HRESULT SetTexture(ID3D11ShaderResourceView* const *pDiffuseRV,
ID3D11SamplerState * const *ppSamplers);
void EnableLights(bool enableLights) { m_enableLights = enableLights; }