Multiple point lights
This commit is contained in:
@@ -12,6 +12,7 @@ ApplicationClass::ApplicationClass()
|
||||
m_Bitmap = 0;
|
||||
m_Sprite = 0;
|
||||
m_Timer = 0;
|
||||
m_Lights = 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -57,7 +58,7 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
|
||||
}
|
||||
|
||||
// Set the initial position of the camera.
|
||||
m_Camera->SetPosition(0.0f, 0.0f, -10.0f);
|
||||
m_Camera->SetPosition(0.0f, 2.0f, -12.0f);
|
||||
m_Camera->SetRotation(0.0f, 0.0f, 0.0f);
|
||||
|
||||
// Create and initialize the texture shader object.
|
||||
@@ -114,7 +115,7 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
|
||||
}
|
||||
|
||||
// Set the file name of the model.
|
||||
strcpy_s(modelFilename, "sphere.txt");
|
||||
strcpy_s(modelFilename, "plane.txt");
|
||||
|
||||
// Set the file name of the textures.
|
||||
strcpy_s(textureFilename1, "stone01.tga");
|
||||
@@ -141,14 +142,24 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create and initialize the light object.
|
||||
m_Light = new LightClass;
|
||||
// Set the number of lights we will use.
|
||||
m_numLights = 4;
|
||||
|
||||
m_Light->SetAmbientColor(0.15f, 0.15f, 0.15f, 1.0f);
|
||||
m_Light->SetDiffuseColor(1.0f, 1.0f, 1.0f, 1.0f);
|
||||
m_Light->SetDirection(1.0f, 0.0f, 1.0f);
|
||||
m_Light->SetSpecularColor(1.0f, 1.0f, 1.0f, 1.0f);
|
||||
m_Light->SetSpecularPower(32.0f);
|
||||
// Create and initialize the light objects array.
|
||||
m_Lights = new LightClass[m_numLights];
|
||||
|
||||
// Manually set the color and position of each light.
|
||||
m_Lights[0].SetDiffuseColor(1.0f, 0.0f, 0.0f, 1.0f); // Red
|
||||
m_Lights[0].SetPosition(-3.0f, 1.0f, 3.0f);
|
||||
|
||||
m_Lights[1].SetDiffuseColor(0.0f, 1.0f, 0.0f, 1.0f); // Green
|
||||
m_Lights[1].SetPosition(3.0f, 1.0f, 3.0f);
|
||||
|
||||
m_Lights[2].SetDiffuseColor(0.0f, 0.0f, 1.0f, 1.0f); // Blue
|
||||
m_Lights[2].SetPosition(-3.0f, 1.0f, -3.0f);
|
||||
|
||||
m_Lights[3].SetDiffuseColor(1.0f, 1.0f, 1.0f, 1.0f); // White
|
||||
m_Lights[3].SetPosition(3.0f, 1.0f, -3.0f);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -171,12 +182,12 @@ void ApplicationClass::Shutdown()
|
||||
m_Sprite = 0;
|
||||
}
|
||||
|
||||
// Release the light object.
|
||||
if (m_Light)
|
||||
{
|
||||
delete m_Light;
|
||||
m_Light = 0;
|
||||
}
|
||||
// Release the light objects.
|
||||
if(m_Lights)
|
||||
{
|
||||
delete [] m_Lights;
|
||||
m_Lights = 0;
|
||||
}
|
||||
|
||||
// Release the light shader object.
|
||||
if (m_LightShader)
|
||||
@@ -292,6 +303,8 @@ bool ApplicationClass::Frame()
|
||||
bool ApplicationClass::Render(float rotation, float x, float y, float z)
|
||||
{
|
||||
XMMATRIX worldMatrix, viewMatrix, orthoMatrix, projectionMatrix, rotateMatrix, translateMatrix, scaleMatrix, srMatrix;
|
||||
XMFLOAT4 diffuseColor[4], lightPosition[4];
|
||||
int i;
|
||||
bool result;
|
||||
|
||||
// Clear the buffers to begin the scene.
|
||||
@@ -339,7 +352,15 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z)
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get the light properties.
|
||||
for (i = 0; i < m_numLights; i++)
|
||||
{
|
||||
// Create the diffuse color array from the four light colors.
|
||||
diffuseColor[i] = m_Lights[i].GetDiffuseColor();
|
||||
|
||||
// Create the light position array from the four light positions.
|
||||
lightPosition[i] = m_Lights[i].GetPosition();
|
||||
}
|
||||
|
||||
// Render the model using the multitexture shader.
|
||||
result = m_MultiTextureShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix,
|
||||
@@ -356,15 +377,6 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z)
|
||||
// Render the model using the multitexture shader.
|
||||
m_Model->Render(m_Direct3D->GetDeviceContext());
|
||||
|
||||
// Render the model using the light shader.
|
||||
result = m_LightShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, m_Model->GetTexture(0),
|
||||
m_Light->GetDirection(), m_Light->GetAmbientColor(), m_Light->GetDiffuseColor(),
|
||||
m_Camera->GetPosition(), m_Light->GetSpecularColor(), m_Light->GetSpecularPower());
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
scaleMatrix = XMMatrixScaling(2.0f, 2.0f, 2.0f); // Build the scaling matrix.
|
||||
rotateMatrix = XMMatrixRotationY(-rotation); // Build the rotation matrix.
|
||||
translateMatrix = XMMatrixTranslation(-x, -y, -z); // Build the translation matrix.
|
||||
@@ -376,6 +388,14 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z)
|
||||
// Put the model vertex and index buffers on the graphics pipeline to prepare them for drawing.
|
||||
m_Model->Render(m_Direct3D->GetDeviceContext());
|
||||
|
||||
// Render the model using the light shader.
|
||||
result = m_LightShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, m_Model->GetTexture(0),
|
||||
diffuseColor, lightPosition);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Turn the Z buffer back on now that all 2D rendering has completed.
|
||||
m_Direct3D->TurnZBufferOn();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user