// Load the scene objects. auto loadMeshTask = Mesh::LoadFromFileAsync( m_graphics, L"gamelevel.cmo", L"", L"", m_meshModels) .then([this]() { // Load the teapot from a separate file and add it to the vector of meshes. return Mesh::LoadFromFileAsync(
void Game::CreateDeviceDependentResources() { m_graphics.Initialize(m_deviceResources->GetD3DDevice(), m_deviceResources->GetD3DDeviceContext(), m_deviceResources->GetDeviceFeatureLevel()); // Set DirectX to not cull any triangles so the entire mesh will always be shown. CD3D11_RASTERIZER_DESC d3dRas(D3D11_DEFAULT); d3dRas.CullMode = D3D11_CULL_NONE; d3dRas.MultisampleEnable = true; d3dRas.AntialiasedLineEnable = true; ComPtr<ID3D11RasterizerState> p3d3RasState; m_deviceResources->GetD3DDevice()->CreateRasterizerState(&d3dRas, &p3d3RasState); m_deviceResources->GetD3DDeviceContext()->RSSetState(p3d3RasState.Get()); // Load the scene objects. auto loadMeshTask = Mesh::LoadFromFileAsync( m_graphics, L"gamelevel.cmo", L"", L"", m_meshModels); (loadMeshTask).then([this]() { // Scene is ready to be rendered. m_loadingComplete = true; }); }
void Game::ReleaseDeviceDependentResources() { for (Mesh* m : m_meshModels) { delete m; } m_meshModels.clear(); m_loadingComplete = false; }
void Game::Update(DX::StepTimer const& timer) { // Rotate scene. m_rotation = static_cast<float>(timer.GetTotalSeconds()) * 0.5f; }
void Game::Render() { // Loading is asynchronous. Only draw geometry after it's loaded. if (!m_loadingComplete) { return; } auto context = m_deviceResources->GetD3DDeviceContext(); // Set render targets to the screen. auto rtv = m_deviceResources->GetBackBufferRenderTargetView(); auto dsv = m_deviceResources->GetDepthStencilView(); ID3DllRenderTargetView *const targets[1] = { rtv }; context->OMSetRenderTargets(1, targets, dsv); // Draw our scene models. XMMATRIX rotation = XMMatrixRotationY(m_rotation); for (UINT i = 0; i < m_meshModels.size() ; i++) { XMMATRIX modelTransform = rotation; String^ meshName = ref new String (m_meshModels [i]->Name ()) ; m_graphics.UpdateMiscConstants(m_miscConstants); m_meshModels[i]->Render(m_graphics, modelTransform); } } ToggleHitEffect : . void Game :: ToggleHitEf feet (String^ object) { }
String^ Game: :OnHitobject (int x , int y) { String^ result = nullptr; XMFLOAT3 point; XMFLOAT3 dir; m_graphics.GetCamera().GetWorldLine(x, y, &point, &dir); XMFLOAT4X4 world; XMMATRIX worldMat = XMMatrixRotationY(m_rotation); XMStoreFloat4x4(&world, worldMat); float closestT = FLT_MAX; for (Mesh* m : m_meshModels) { XMFLOAT4X4 meshTransform = world; auto name = ref new String(m->Name()); float t = 0; bool hit = HitTestingHelpers::LineHitTest(*m, &point, &dir, SmeshTransform, &t); if (hit && t < closestT) { result = name; } } return result; }
class Game { public: // snip private: // snip float m_translation;
void Game::Update(DX::StepTimer consts timer) { // Rotate scene. m_rotation = static_cast<float>(timer.GetTotalSeconds()) * 0.5f; const float maxHeight = 7. Of; auto totalTime = (float) fmod(timer.GetTotalSeconds(), 2.0f); m_translation = totalTime > 1.0f ? maxHeight - (maxHeight * (totalTime - 1.0f)) : maxHeight *totalTime; }
void Game::Render() { // snip // Draw our scene models. XMMATRIX rotation = XMMatrixRotationY(m_rotation); rotation *= XMMatrixTranslation(0, m_translation, 0);
void Game::Update(DX::StepTimer consts timer) { // Rotate scene. m_rotation = static_cast<float>(timer.GetTotalSeconds()) * 0.5f; auto totalTime = (float) fmod(timer.GetTotalSeconds(), 2.0f); m_translation = 10*totalTime - 5 *totalTime*totalTime; }
void Game::CreateDeviceDependentResources() { // snip // Load the scene objects. auto loadMeshTask = Mesh::LoadFromFileAsync( m_graphics, L"gamelevel.cmo", L"", L"", m_meshModels) .then([this]() { return Mesh::LoadFromFileAsync( m_graphics, L"field.cmo", L"", L"", m_meshModels, false // Do not clear the vector of meshes ); }); (loadMeshTask) .then([this] () { // Scene is ready to be rendered. m_loadingComplete = true; }); }
// Renders one frame using the Starter Kit helpers, void Game::Render() { // snip for (UINT i = 0; i < m_meshModels.size(); i++) { XMMATRIX modelTransform = rotation; String^ meshName = ref new String(m_meshModels[i]->Name()); m_graphics.UpdateMiscConstants(m_miscConstants); if (String::CompareOrdinal(meshName, L"Sphere_Node") == 0) m_meshModels[i]->Render(m_graphics, modelTransform); else m_meshModels[i]->Render(m_graphics, XMMatrixIdentity()); } }
rotation *= XMMatrixTranslation(63.0, m_translation, 0);
m_graphics.GetCamera().SetViewport((UINT) outputSize.Width, (UINT) outputSize.Height); m_graphics.GetCamera().SetPosition(XMFLOAT3(25.Of, 10.0f, 0.0f)); m_graphics.GetCamera().SetLookAt(XMFLOAT3(100.0f, 0.0f, 0.0f)); float aspectRatio = outputSize.Width / outputSize.Height; float fovAngleY = 30.0f * XM_PI / 180.0f; if (aspectRatio < 1.0f) { // Portrait or snap view m_graphics.GetCamera().SetUpVector(XMFLOAT3(1.0f, 0.0f, 0.0f)); fovAngleY = 120.0f * XM_PI / 180.0f; } else { // Landscape view. m_graphics.GetCamera().SetUpVector(XMFLOAT3(0.0f, 1.0f, 0.0f)); } m_graphics.GetCamera().SetProjection(fovAngleY, aspectRatio, 1.0f, 100.0f);
auto loadMeshTask = Mesh::LoadFromFileAsync( m_graphics, L"gamelevel.cmo", L"", L"", m_meshModels) .then([this]() { return Mesh::LoadFromFileAsync( m_graphics, L"field.cmo", L"", L"", m_meshModels, false // Do not clear the vector of meshes ); }).then([this]() { return Mesh::LoadFromFileAsync( m_graphics, L"soccer_goal.cmo", L"", L"", m_meshModels, false // Do not clear the vector of meshes ); });
auto goalTransform = XMMatrixScaling(2.0f, 2.0f, 2.0f) * XMMatrixRotationY(-XM_PIDIV2)* XMMatrixTranslation(85.5f, -0.5, 0); for (UINT i = 0; i < m_meshModels.size() ; i++) { XMMATRIX modelTransform = rotation; String'^ meshName = ref new String (m_meshModels [i]->Name ()) ; m_graphics.UpdateMiscConstants(m_miscConstants); if (String::CompareOrdinal(meshName, L"Sphere_Node") == 0) m_meshModels[i]->Render(m_graphics, modelTransform); else if (String::CompareOrdinal(meshName, L"Plane_Node") == 0) m_meshModels[i]->Render(m_graphics, XMMatrixIdentity()); else m_meshModels[i]->Render(m_graphics, goalTransform); }
void Game::Update(DX::StepTimer consts timer) { // Rotate scene. m_rotation = static_cast<float>(timer.GetTotalSeconds()) * 0.5f; auto totalTime = (float) fmod(timer.GetTotalSeconds(), 2.3f); m_translationX = 63.0 + 11.5 * totalTime; m_translationY = 11.5 * totalTime - 5 * totalTime*totalTime; }
void Game::Update(DX::StepTimer consts timer) { // Rotate scene. m_rotation = static_cast<float>(timer.GetTotalSeconds()) * 0.5f; auto totalTime = (float) fmod(timer.GetTotalSeconds(), 2.3f); m_translationX = 63.0 + 11.5 * totalTime; m_translationY = 11.5 * totalTime - 5 * totalTime*totalTime; m_translationZ = 3 * totalTime; } Z Render: rotation *= XMMatrixTranslation(m_translationX, m_translationY, m_translationZ); … .
auto loadMeshTask = Mesh::LoadFromFileAsync( m_graphics, L"gamelevel.cmo", L"", L"", m_meshModels) .then([this]() { return Mesh::LoadFromFileAsync( m_graphics, L"field.cmo", L"", L"", m_meshModels, false // Do not clear the vector of meshes ); }).then([this]() { return Mesh::LoadFromFileAsync( m_graphics, L"soccer_goal.cmo", L"", L"", m_meshModels, false // Do not clear the vector of meshes ); }).then([this]() { return Mesh::LoadFromFileAsync( m_graphics, L"goalkeeper.cmo", L"", L"", m_meshModels, false // Do not clear the vector of meshes ); });
void Game::Render() { // snip auto goalTransform = XMMatrixScaling(2.0f, 2.0f, 2.0f) * XMMatrixRotationY(-XM_PIDIV2)* XMMatrixTranslation(85.5f, -0.5f, 0); auto goalkeeperTransform = XMMatrixTranslation(85.65f, 1.4f, 0) ; for (UINT i = 0; i < m_meshModels.size(); i++) { XMMATRIX modelTransform = rotation; String^ meshName = ref new String (m_meshModels [i]->Name ()) ; m_graphics.UpdateMiscConstants(m_miscConstants); if (String::CompareOrdinal(meshName, L"Sphere_Node") == 0) m_meshModels[i]->Render(m_graphics, modelTransform); else if (String::CompareOrdinal(meshName, L"Plane_Node") == 0) m_meshModels[i]->Render(m_graphics, XMMatrixIdentity()); else if (String::CompareOrdinal(meshName, L"Cube_Node") == 0) m_meshModels[i]->Render(m_graphics, goalkeeperTransform); else m_meshModels[i]->Render(m_graphics, goalTransform); } }
<Page x:Class="StarterKit.DirectXPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:StarterKit" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" KeyDown="OnKeyDown">
void DirectXPage::OnKeyDown(Platform::ObjectΛ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgsΛ e) { m_main->OnKeyDown( ->Key); }
class StarterKitMain : public DX::IDeviceNotify { public: StarterKitMain(const std::shared_ptr<DX::DeviceResources>& deviceResources); ~StarterKitMain(); // Public methods passed straight to the Game renderer. Platform: : String'^ OnHitObject (int x, int y) { return m_sceneRenderer->OnHitObject(x, y); } void OnKeyDown(Windows::System::VirtualKey key) { m_sceneRenderer->OnKeyDown(key); } … .
class Game { public: Gamefconst std::shared_ptr<DX::DeviceResources>& deviceResources); void CreateDeviceDependentResources(); void CreateWindowSizeDependentResources(); void ReleaseDeviceDependentResources(); void Update(DX::StepTimer consts timer); void Render(); void OnKeyDown(Windows::System::VirtualKey key); …
class Game { // snip private: // snip float m_goalkeeperPosition;
void Game::OnKeyDown(Windows::System::VirtualKey key) { const float MaxGoalkeeperPosition = 6.0; const float MinGoalkeeperPosition = -6.0; if (key == Windows::System::VirtualKey::Right) m_goalkeeperPosition = m_goalkeeperPosition >= MaxGoalkeeperPosition ? m_goalkeeperPosition : m_goalkeeperPosition + 0.1f; else if (key == Windows::System::VirtualKey::Left) m_goalkeeperPosition = m_goalkeeperPosition <= MinGoalkeeperPosition ? m_goalkeeperPosition : m_goalkeeperPosition - 0.1f; }
class Game { public: // snip private: // snip bool m_isAnimating;
void Game::Update(DX::StepTimer consts timer) { if (m_isAnimating) { m_rotation = static_cast<float>(timer.GetTotalSeconds()) * 0.5f; auto totalTime = (float) fmod(timer.GetTotalSeconds(), 2.3f); m_translationX = 63.0f + 11.5f * totalTime; m_translationY = 11.5f * totalTime - 5.Of * totalTime*totalTime; m_translationZ = 3.0f * totalTime; } } void Game::Render() { // snip XMMATRIX modelTransform; if (m_isAnimating) { modelTransform = XMMatrixRotationY(m_rotation); modelTransform *= XMMatrixTranslation(m_translationX, m_translationY, m_translationZ); } else modelTransform = XMMatrixTranslation(63.0f, 0.0f, 0.0f); … .
void Game::OnKeyDown(Windows::System::VirtualKey key) { const float MaxGoalkeeperPosition = 6.0f; if (m_isAnimating) { auto goalKeeperVelocity = key == Windows::System::VirtualKey::Right ? 0.1f : -0.1f; m_goalkeeperPosition = fabs(m_goalkeeperPosition) >= MaxGoalkeeperPosition ? m_goalkeeperPosition : m_goalkeeperPosition + goalKeeperVelocity; } }
class Game { public: // snip private: // snip bool m_isKick;
void Game::OnKeyDown(Windows::System::VirtualKey key) { const float MaxGoalkeeperPosition = 6. Of; if (m_isAnimating) { auto goalKeeperVelocity = key == Windows::System::VirtualKey::Right ? 0.1f : -0.1f; m_goalkeeperPosition = fabs(m_goalkeeperPosition) >= MaxGoalkeeperPosition ? m_goalkeeperPosition : m_goalkeeperPosition + goalKeeperVelocity; } else if ( y == Windows::System::VirtualKey::Space) m_isKick = true; }
void Game::Update(DX::StepTimer consts timer) { if (m_isKick) { m_startTime = static_cast<float>(timer.GetTotalSeconds()); m_isAnimating = true; m_isKick = false; } if (m_isAnimating) { auto totalTime = static_cast<float>(timer.GetTotalSeconds()) – m_startTime; m_rotation = totalTime * 0.5f; m_translationX = 63.0f + 11.5f * totalTime; m_translationY = 11.5f * totalTime - 5.Of * totalTime*totalTime; m_translationZ = 3.0f * totalTime; if (totalTime > 2.3f) ResetGame(); } }
void Game::ResetGame() { m_isAnimating = false; m_goalkeeperPosition = 0; }
void Game::Update(DX::StepTimer const& timer) { if (m_isKick) { m_startTime = static_cast<float>(timer.GetTotalSeconds()); m_isAnimating = true; m_isKick = false; m_ballAngle = (static_cast <float> (rand()) / static_cast <float> (RAND_MAX) -0.5f) * 6.0f; } … .
void Game::CreateDeviceDependentResources() { srand(static_cast <unsigned int> (time(0))); … .
void Game::Update(DX::StepTimer consts timer) { if (m_isKick) { m_startTime = static_cast<float>(timer.GetTotalSeconds()); m_isAnimating = true; m_isKick = false; m_isGoal = m_isCaught = false; m_ballAngle = (static_cast <float> (rand()) / static_cast <float> (RAND_MAX) -0.5f) * 6.0f; } if (m_isAnimating) { auto totalTime = static_cast<float>(timer.GetTotalSeconds()) – m_startTime; m_rotation = totalTime * 0.5f; if ( !m_isCaught) { // ball traveling m_translationX = 63.0f + 11.5f * totalTime; m_translationY = 11.5f * totalTime - 5.0f * totalTime*totalTime; m_translationZ = m_ballAngle * totalTime; } else { // if ball is caught, position it in the center of the goalkeeper m_translationX = 83.35f; m_translationY = 1.8f; m_translationZ = m_goalkeeperPosition; } if (!m_isGoal && !m_isCaught && m_translationX >= 85.5f) { // ball passed the goal line - goal or caught auto ballMin = m_translationZ - 0.5f + 7.0f; auto ballMax = m_translationZ + 0.5f + 7.0f; auto goalkeeperMin = m_goalkeeperPosition - 1.0f + 7.0f; auto goalkeeperMax = m_goalkeeperPosition + 1.0f + 7.0f; m_isGoal = (goalkeeperMax < ballMin || goalkeeperMin > ballMax); m_isCaught = !m_isGoal; } if (totalTime > 2.3f) ResetGame(); } }
Source: https://habr.com/ru/post/241085/
All Articles