void SpecularLightingDemo::UpdateAmbientLight(const GameTime& gameTime) { float elapsedTime = gameTime.ElapsedGameTimeSeconds().count(); static float ambientr = mPSCBufferPerFrame.AmbientColor.x; static float ambientg = mPSCBufferPerFrame.AmbientColor.y; static float ambientb = mPSCBufferPerFrame.AmbientColor.z; //Color if (mKeyboard->IsKeyDown(Keys::NumPad1) || mGamePad->IsButtonDown(GamePadButtons::A)) { ambientr = max(0.0f, min(ambientr + elapsedTime, 1.0f)); mPSCBufferPerFrame.AmbientColor = XMFLOAT4(ambientr, ambientg, ambientb, 1.0f); } else if (mKeyboard->IsKeyDown(Keys::D1) || mGamePad->IsButtonDown(GamePadButtons::X)) { ambientr = max(0.0f, min(ambientr - elapsedTime, 1.0f)); mPSCBufferPerFrame.AmbientColor = XMFLOAT4(ambientr, ambientg, ambientb, 1.0f); } if (mKeyboard->IsKeyDown(Keys::NumPad2) || mGamePad->IsButtonDown(GamePadButtons::A)) { ambientg = max(0.0f, min(ambientg + elapsedTime, 1.0f)); mPSCBufferPerFrame.AmbientColor = XMFLOAT4(ambientr, ambientg, ambientb, 1.0f); } else if (mKeyboard->IsKeyDown(Keys::D2) || mGamePad->IsButtonDown(GamePadButtons::X)) { ambientg = max(0.0f, min(ambientg - elapsedTime, 1.0f)); mPSCBufferPerFrame.AmbientColor = XMFLOAT4(ambientr, ambientg, ambientb, 1.0f); } if (mKeyboard->IsKeyDown(Keys::NumPad3) || mGamePad->IsButtonDown(GamePadButtons::A)) { ambientb = max(0.0f, min(ambientb + elapsedTime, 1.0f)); mPSCBufferPerFrame.AmbientColor = XMFLOAT4(ambientr, ambientg, ambientb, 1.0f); } else if (mKeyboard->IsKeyDown(Keys::D3) || mGamePad->IsButtonDown(GamePadButtons::X)) { ambientb = max(0.0f, min(ambientb - elapsedTime, 1.0f)); mPSCBufferPerFrame.AmbientColor = XMFLOAT4(ambientr, ambientg, ambientb, 1.0f); } }
void SpecularLightingDemo::Update(const GameTime & gameTime) { static float angle = 0.0f; if (mAnimationEnabled) { angle += gameTime.ElapsedGameTimeSeconds().count() * ModelRotationRate; XMStoreFloat4x4(&mWorldMatrix, XMMatrixRotationY(angle)); } if (mKeyboard != nullptr && mKeyboard->WasKeyPressedThisFrame(Keys::Space)) { ToggleAnimation(); } if (mGamePad != nullptr) { auto& gamePadState = mGamePad->CurrentState(); if (gamePadState.IsConnected()) { if (mGamePad->WasButtonPressedThisFrame(GamePadButtons::Start)) { ToggleGamePadControls(); } if (mGamePad->WasButtonPressedThisFrame(GamePadButtons::LeftStick)) { ToggleAnimation(); } UpdateAmbientLight(gameTime); UpdateSpecularLight(gameTime); UpdateDirectionalLight(gameTime, gamePadState); } } mProxyModel->Update(gameTime); }
void SpecularLightingDemo::UpdateDirectionalLight(const GameTime& gameTime, const GamePad::State& gamePadState) { float elapsedTime = gameTime.ElapsedGameTimeSeconds().count(); static float lightr = mPSCBufferPerFrame.LightColor.x; static float lightg = mPSCBufferPerFrame.LightColor.y; static float lightb = mPSCBufferPerFrame.LightColor.z; //Color if (mKeyboard->IsKeyDown(Keys::NumPad4) || mGamePad->IsButtonDown(GamePadButtons::Y)) { lightr = max(0.0f, min(lightr + elapsedTime, 1.0f)); mPSCBufferPerFrame.LightColor = XMFLOAT4(lightr, lightg, lightb, 1.0f); } else if (mKeyboard->IsKeyDown(Keys::D4) || mGamePad->IsButtonDown(GamePadButtons::B)) { lightr = max(0.0f, min(lightr - elapsedTime, 1.0f)); mPSCBufferPerFrame.LightColor = XMFLOAT4(lightr, lightg, lightb, 1.0f); } if (mKeyboard->IsKeyDown(Keys::NumPad5) || mGamePad->IsButtonDown(GamePadButtons::Y)) { lightg = max(0.0f, min(lightg + elapsedTime, 1.0f)); mPSCBufferPerFrame.LightColor = XMFLOAT4(lightr, lightg, lightb, 1.0f); } else if (mKeyboard->IsKeyDown(Keys::D5) || mGamePad->IsButtonDown(GamePadButtons::B)) { lightg = max(0.0f, min(lightg - elapsedTime, 1.0f)); mPSCBufferPerFrame.LightColor = XMFLOAT4(lightr, lightg, lightb, 1.0f); } if (mKeyboard->IsKeyDown(Keys::NumPad6) || mGamePad->IsButtonDown(GamePadButtons::Y)) { lightb = max(0.0f, min(lightb + elapsedTime, 1.0f)); mPSCBufferPerFrame.LightColor = XMFLOAT4(lightr, lightg, lightb, 1.0f); } else if (mKeyboard->IsKeyDown(Keys::D6) || mGamePad->IsButtonDown(GamePadButtons::B)) { lightb = max(0.0f, min(lightb - elapsedTime, 1.0f)); mPSCBufferPerFrame.LightColor = XMFLOAT4(lightr, lightg, lightb, 1.0f); } if (mUseGamePadForDirectionalLight) { // Rotate directional light XMFLOAT2 rotationAmount; rotationAmount.x = -gamePadState.thumbSticks.rightX * LightRotationRate.x * elapsedTime; rotationAmount.y = gamePadState.thumbSticks.rightY * LightRotationRate.y * elapsedTime; XMMATRIX lightRotationMatrix = XMMatrixIdentity(); if (rotationAmount.x != 0) { lightRotationMatrix = XMMatrixRotationY(rotationAmount.x); } if (rotationAmount.y != 0) { XMMATRIX lightRotationAxisMatrix = XMMatrixRotationAxis(mDirectionalLight->RightVector(), rotationAmount.y); lightRotationMatrix *= lightRotationAxisMatrix; } if (rotationAmount.x != 0.0f || rotationAmount.y != 0.0f) { mDirectionalLight->ApplyRotation(lightRotationMatrix); mProxyModel->ApplyRotation(lightRotationMatrix); mPSCBufferPerFrame.LightDirection = mDirectionalLight->DirectionToLight(); mGame->Direct3DDeviceContext()->UpdateSubresource(mPSConstantBufferPF.Get(), 0, nullptr, &mPSCBufferPerFrame, 0, 0); } } }