Game Development Reference
In-Depth Information
The next method is called to initialize the constant buffer used in the pixel shader
introduced in the previous chapter. This method is called from GameCode4_Hlsl_-
PixelShader::SetupRender() for each SceneNode :
void LightManager::CalcLighting(ConstantBuffer_Lighting* pLighting,
SceneNode *pNode)
{
int count = GetLightCount(pNode);
if (count)
{
pLighting->m_vLightAmbient = *GetLightAmbient(pNode);
memcpy(pLighting->m_vLightDir, GetLightDirection(pNode),
sizeof( Vec4 ) * count );
memcpy(pLighting->m_vLightDiffuse, GetLightDiffuse(pNode),
sizeof( Vec4 ) * count);
pLighting->m_nNumLights = count;
}
}
All this method does is copy precalculated values into a structure that will be sent
into the shader. This simple lighting model is vertex based but calculated per pixel
because of the code in the pixel shader. Many more interesting models are possible,
especially those that operate as a separate render pass after the objects have been
drawn. With this basic introduction into how lights can be added into scenes, how
they can be managed with a light manager class, and how they communicate with
shaders, you can now explore some great lighting experiments.
Rendering the Sky
The sky in computer games is usually a very simple object, such as a cube or faceted
dome. The trick to making the sky look like it is infinitely far away is to keep its
position coordinated with the camera. The following class implements a cube-
shaped sky. The textures that are placed on the cube are created to give the players
the illusion they are looking at a dome-shaped object. You ' ve already seen how the
depth stencil gets set to make the effect work, but that
s not the whole job.
As you look out the window of a moving car, it seems that the sky isn
'
t moving rela-
tive to anything else. It is moving, of course, but it moves so slowly that you can
'
'
t
perceive it. In computer games, this effect is simulated by having the sky literally
move as the camera moves but still keep its orientation. Here
'
s the class to make that
work.
class D3DSkyNode11 : public SceneNode
{
 
 
Search WWH ::




Custom Search