The Main Source File
In
the main source file, two new objects are added to the global section:
one to pass the light position to the shaders and one to pass the
camera position. The end of the global section with the new variables
in the Lighting demo are shown in Listing 1. In the InitializeDemo() function, these effect variables are obtained so we can use them to actually pass data to the shaders. The InitializeDemo() function is partially shown in Listing 2. The only new code in it is the effect variable calls that obtain access so we can set the light and camera position.
Listing 1. Partial Look at the Lighting Demo’s Global Variables
…
ID3D10Effect *g_shader = NULL; ID3D10EffectTechnique *g_lightingTech = NULL; ID3D10EffectVectorVariable *g_lightPosEffectVar = NULL; ID3D10EffectVectorVariable *g_eyePosEffectVar = NULL; ID3D10EffectMatrixVariable *g_worldEffectVar = NULL; ID3D10EffectMatrixVariable *g_viewEffectVar = NULL; ID3D10EffectMatrixVariable *g_projEffectVar = NULL;
D3DXMATRIX g_worldMat, g_viewMat, g_projMat;
// Scene rotations. float g_xRot = 0.0f; float g_yRot = 0.0f;
|
Listing 2. Partial Look at the InitializeDemo() Function
bool InitializeDemo() { // Load the shader.
DWORD shaderFlags = D3D10_SHADER_ENABLE_STRICTNESS;
#if defined( DEBUG ) || defined( _DEBUG ) shaderFlags |= D3D10_SHADER_DEBUG; #endif
ID3D10Blob *errors = NULL;
HRESULT hr = D3DX10CreateEffectFromFile("LambertBlinnPhong.fx", NULL, NULL, "fx_4_0", shaderFlags, 0, g_d3dDevice, NULL, NULL, &g_shader, &errors, NULL);
if(errors != NULL) { MessageBox(NULL, (LPCSTR)errors->GetBufferPointer(), "Error in Shader!", MB_OK);
errors->Release() }
if(FAILED(hr)) return false;
g_lightingTech = g_shader->GetTechniqueByName( "BlinnPhongSpecular");
g_worldEffectVar = g_shader->GetVariableByName( "World")->AsMatrix();
g_viewEffectVar = g_shader->GetVariableByName( "View")->AsMatrix();
g_projEffectVar = g_shader->GetVariableByName( "Projection")->AsMatrix();
g_lightPosEffectVar = g_shader->GetVariableByName( "lightPos")->AsVector();
g_eyePosEffectVar = g_shader->GetVariableByName( "eyePos")->AsVector();
…
return true; }
|
In the Update()
function the world matrix is rotated along the X and Y axes so we can
see the specular lighting changes as the orientation moves in real
time. In the RenderScene() function the only new code is the
code that passes the light and camera positions to the shaders. The
light position is located 5 units along the negative Z axis, while the
camera position is located at the origin. The object itself is
positioned 6 units along the Z axis, which can be seen in the Update() function.
The Update() and RenderScene() functions are shown in Listing 3. Figure 1 shows a screenshot from the running demo.
Listing 3. The Update() and RenderScene() Functions from the Lighting Demo
void Update() { g_xRot += 0.0001f; g_yRot += 0.0002f;
if(g_xRot < 0) g_xRot = 359; else if(g_xRot >= 360) g_xRot = 0;
if(g_yRot < 0) g_yRot = 359; else if(g_yRot >= 360) g_yRot = 0;
D3DXMATRIX trans, rotX, rotY;
D3DXMatrixRotationX(&rotX, g_xRot); D3DXMatrixRotationY(&rotY, g_yRot); D3DXMatrixTranslation(&trans, 0, 0, 6);
g_worldMat = (rotX * rotY) * trans; }
void RenderScene() { float col[4] = { 0, 0, 0, 1 };
g_d3dDevice->ClearRenderTargetView(g_renderTargetView, col); g_d3dDevice->ClearDepthStencilView(g_depthStencilView, D3D10_CLEAR_DEPTH, 1.0f, 0);
g_d3dDevice->IASetInputLayout(g_layout); g_d3dDevice->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
D3D10_TECHNIQUE_DESC techDesc; g_lightingTech->GetDesc(&techDesc);
unsigned int stride = sizeof(DX10Vertex); unsigned int offset = 0;
float lightPos[4] = { 0, 0, -5, 1 }; float eyePos[4] = { 0, 0, 0, 1 };
g_worldEffectVar->SetMatrix((float*)&g_worldMat); g_lightPosEffectVar->SetFloatVector(lightPos); g_eyePosEffectVar->SetFloatVector(eyePos);
for(int m = 0; m < (int)g_meshes.size(); m++) { g_d3dDevice->IASetVertexBuffers(0, 1, &g_meshes[m].m_vertices, &stride, &offset);
for(unsigned int i = 0; i < techDesc.Passes; i++) { g_lightingTech->GetPassByIndex(i)->Apply(0); g_d3dDevice->Draw(g_meshes[m].m_totalVerts, 0); } }
g_swapChain->Present(0, 0);
Update(); }
|
|