#include "Graphics2.h" Graphics2 app; void Graphics2::CreateSceneGraph() { _boostMultiplier = _boostMin; _flySpeed = 1; _turnSpeed = 1; _invertPitch = -1; _currentController = GamePadController(); _currentPlayerObject = nullptr; GetCamera()->SetCameraPosition(0.0f, 550.0f, -80.0f); SceneGraphPointer sceneGraph = GetSceneGraph(); // This is where you add nodes to the scene graph //shared_ptr cube = make_shared(L"Body", L"Textures\\woodbox.bmp"); //cube->SetWorldTransform(XMMatrixScaling(5.0f, 8.0f, 2.5f) * XMMatrixTranslation(0, 23.0f, 0)); //sceneGraph->Add(cube); DirectXFramework::GetDXFramework()->GetGlobalLighting()->SetAmbientLight(XMFLOAT4(0.7f, 0.7f, 0.7f, 1.0f)); DirectXFramework::GetDXFramework()->GetGlobalLighting()->SetDirectionalLight(XMVectorSet(0.5f, -1.0f, -1.0f, 0.0f), XMFLOAT4(0.5f, 0.5f, 0.5f, 1.0f)); shared_ptr skyDome = make_shared(L"SkyDome", L"Textures\\SkyWater.dds", 30.0f); sceneGraph->Add(skyDome); //shared_ptr terrainNode = make_shared(L"MainTerrain", L"Textures\\Example_HeightMap.raw", L"RandomWords"); shared_ptr terrainNode = make_shared(L"MainTerrain", L"LovelyCat", 10.0f, 20.0f, 1.0f, 1); terrainNode->SetAmbientLight(DirectXFramework::GetDXFramework()->GetGlobalLighting()->GetAmbientLight()); terrainNode->SetDirectionalLight(DirectXFramework::GetDXFramework()->GetGlobalLighting()->GetDirectionalLightDirection(), DirectXFramework::GetDXFramework()->GetGlobalLighting()->GetDirectionalLightColor()); terrainNode->SetWaterColor(XMFLOAT4(SharedMethods::RGBValueToIntensity(0x84), SharedMethods::RGBValueToIntensity(0xC1), SharedMethods::RGBValueToIntensity(0xF9), 1.0f)); //terrainNode->SetWaterColor(XMFLOAT4(1.0f, 0.0f, 0.8f, 1.0f)); //terrainNode->SetWaterColor(XMFLOAT4(0.0f, 0.0f, 1.0f, 1.0f)); sceneGraph->Add(terrainNode); shared_ptr treeGroupA = make_shared(L"TreeGroupA"); shared_ptr treeNode1 = make_shared(L"Tree1", L"Models\\Tree\\Tree1.3ds"); treeNode1->SetWorldTransform(XMMatrixScaling(0.005f, 0.01f, 0.005f) * XMMatrixRotationAxis(XMVectorSet(1.0f, 0.0f, 1.0f, 0.0f), 0.1f * XM_PI) * XMMatrixTranslation(0.0f, 320.0f, 250.0f)); treeGroupA->Add(treeNode1); shared_ptr treeNode2 = make_shared(L"Tree2", L"Models\\Tree\\Tree1.3ds"); treeNode2->SetWorldTransform(XMMatrixScaling(0.002f, 0.002f, 0.002f) * XMMatrixTranslation(-150.0f, 380.0f, 150.0f)); treeGroupA->Add(treeNode2); shared_ptr treeNode3 = make_shared(L"Tree3", L"Models\\Tree\\Tree1.3ds"); treeNode3->SetWorldTransform(XMMatrixScaling(0.005f, 0.005f, 0.005f) * XMMatrixRotationAxis(XMVectorSet(1.0f, 0.0f, 1.0f, 0.0f), -0.1f * XM_PI) * XMMatrixTranslation(100.0f, 290.0f, 350.0f)); treeGroupA->Add(treeNode3); shared_ptr treeNode4 = make_shared(L"Tree4", L"Models\\Tree\\Tree1.3ds"); treeNode4->SetWorldTransform(XMMatrixScaling(0.005f, 0.01f, 0.005f) * XMMatrixRotationAxis(XMVectorSet(1.0f, 0.0f, 1.0f, 0.0f), 0.1f * XM_PI) * XMMatrixTranslation(0.0f, 320.0f, 250.0f)); treeGroupA->Add(treeNode4); sceneGraph->Add(treeGroupA); shared_ptr plane1Node = make_shared(L"Plane1", L"Models\\Plane\\Bonanza.3DS"); plane1Node->SetStartOrientation(XMMatrixRotationAxis(XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f), XM_PI) * XMMatrixRotationAxis(XMVectorSet(1.0f, 0.0f, 0.0f, 0.0f), 0.5f * XM_PI)); plane1Node->SetNodePosition(0.0f, 550.0f, -50.0f); sceneGraph->Add(plane1Node); //_currentPlayerObject = plane1Node; //GetCamera()->SetFollowNode(plane1Node, XMFLOAT3(0.0f, 30.0f, -80.0f), false); SetBackgroundColour(XMFLOAT4(0.29f, 0.38f, 0.72f, 1.0f)); //SetBackgroundColour(XMFLOAT4(SharedMethods::RGBValueToIntensity(0x89), 0, 1, 1)); _currentRotation = 0; _currentSideRotation = 0; _currentPropRotation = 0; } void Graphics2::UpdateSceneGraph() { GetCurrentControlInputs(); SceneGraphPointer sceneGraph = GetSceneGraph(); XMVECTOR startCameraPos = GetCamera()->GetCameraPosition(); if (_currentPlayerObject != nullptr) { _currentPlayerObject->SetForwardBack(_flySpeed * _boostMultiplier); } for (ControlInputs currentControl : _currentInputs) { switch (currentControl) { case ControlInputs::Forward: if (_currentPlayerObject == nullptr) { GetCamera()->SetForwardBack(_flySpeed * _boostMultiplier); } break; case ControlInputs::Back: if (_currentPlayerObject == nullptr) { GetCamera()->SetForwardBack(-_flySpeed * _boostMultiplier); } break; case ControlInputs::StrafeLeft: if (_currentPlayerObject != nullptr) { _currentPlayerObject->SetLeftRight(-_flySpeed * _boostMultiplier); } else { GetCamera()->SetLeftRight(-_flySpeed * _boostMultiplier); } break; case ControlInputs::StrafeRight: if (_currentPlayerObject != nullptr) { _currentPlayerObject->SetLeftRight(_flySpeed * _boostMultiplier); } else { GetCamera()->SetLeftRight(_flySpeed * _boostMultiplier); } break; case ControlInputs::TurnLeft: if (_currentPlayerObject != nullptr) { _currentPlayerObject->SetYaw(-_turnSpeed); } else { GetCamera()->SetYaw(-_turnSpeed); } break; case ControlInputs::TurnRight: if (_currentPlayerObject != nullptr) { _currentPlayerObject->SetYaw(_turnSpeed); } else { GetCamera()->SetYaw(_turnSpeed); } break; case ControlInputs::Up: if (_currentPlayerObject != nullptr) { _currentPlayerObject->SetPitch(_turnSpeed * _invertPitch); } else { GetCamera()->SetPitch(_turnSpeed * _invertPitch); } break; case ControlInputs::Down: if (_currentPlayerObject != nullptr) { _currentPlayerObject->SetPitch(-_turnSpeed * _invertPitch); } else { GetCamera()->SetPitch(-_turnSpeed * _invertPitch); } break; } } shared_ptr plane1 = dynamic_pointer_cast(sceneGraph->Find(L"Plane1")); if (plane1 != nullptr) { //plane1->SetWorldTransform(SharedMethods::RotateFromPoint(-60.0f, 0, 0, XMMatrixRotationAxis(XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f), _currentRotation * XM_PI / 180.0f))); //plane1->AddToWorldTransform(XMMatrixRotationAxis(XMVectorSet(1.0f, 0.0f, 0.0f, 0.0f), 90.0f * XM_PI / 180.0f)); //sceneGraph->Find(L"Plane1")->GetFirstChild()->GetFirstChild()->SetWorldTransform(XMMatrixRotationAxis(XMVectorSet(0.0f, -1.0f, -1.0f, 0.0f), _currentSideRotation * XM_PI / 180.0f)); //sceneGraph->Find(L"Plane1")->Update((SharedMethods::RotateFromPoint(30.0f, -20.0f, 0, XMMatrixRotationAxis(XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f), _currentSideRotation * XM_PI / 180.0f))) * XMMatrixRotationAxis(XMVectorSet(0.0f, -1.0f, 0.0f, 0.0f), _currentRotation * XM_PI / 180.0f)); plane1->Find(L"airscrew")->SetWorldTransform(SharedMethods::RotateFromPoint(0.0f, 15.471f, 14.5f, XMMatrixRotationAxis(XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f), _currentPropRotation * XM_PI / 180.0f))); } if (_currentRotation == 360) { _currentRotation = 0; _currentSideRotation = 0; _currentPropRotation = 0; } else { _currentRotation += 1; _currentSideRotation += 1; _currentPropRotation += 100; } // Check the camera and our terrain boundaries XMVECTOR currentCameraPos = GetCamera()->GetCameraPosition(); shared_ptr mainTerrain = dynamic_pointer_cast(sceneGraph->Find(L"MainTerrain")); GetCamera()->Update(); if (mainTerrain != nullptr) { bool boundaryHit = false; XMFLOAT4 cameraOffset = XMFLOAT4(XMVectorGetX(startCameraPos), XMVectorGetY(startCameraPos), XMVectorGetZ(startCameraPos), 0.0f); if (XMVectorGetY(currentCameraPos) < mainTerrain->GetHeightAtPoint(XMVectorGetX(currentCameraPos), XMVectorGetZ(currentCameraPos), true) + 4.0f) { cameraOffset.y = cameraOffset.y + 4.0f; boundaryHit = true; } float currentCameraXPos = XMVectorGetX(currentCameraPos); float xOffset = 150.0f; if (currentCameraXPos > 0) { xOffset = -xOffset; } float currentCameraZPos = XMVectorGetZ(currentCameraPos); float zOffset = 150.0f; if (currentCameraZPos > 0) { zOffset = -zOffset; } if (!mainTerrain->CheckXBoundary(currentCameraXPos + -xOffset)) { cameraOffset.x = cameraOffset.x + xOffset; boundaryHit = true; } if (!mainTerrain->CheckZBoundary(currentCameraZPos + -zOffset)) { cameraOffset.z = cameraOffset.z + zOffset; boundaryHit = true; } if (boundaryHit) { if (_currentPlayerObject != nullptr) { _currentPlayerObject->SetNodePosition(cameraOffset); } else { GetCamera()->SetCameraPosition(cameraOffset); } } } // Reset the control array ready for the next frames input ResetCurrentControlInputs(); } void Graphics2::GetCurrentControlInputs() { // Check if the window has focus before accepting any keypresses if (GetForegroundWindow() == Framework::GetHWnd()) { // Check if any connected controllers have inputs bool boostHit = false; _currentController.ProcessGameController(_currentInputs, boostHit); if (GetAsyncKeyState(VK_SHIFT) || boostHit) { _currentInputs.insert(ControlInputs::Fire1); _boostMultiplier = SharedMethods::Clamp(_boostMultiplier + _boostStep, 1, _boostMax); } else { _boostMultiplier = SharedMethods::Clamp(_boostMultiplier - _boostStep, 1, _boostMax); } // Forward if (GetAsyncKeyState(0x57)) { _currentInputs.insert(ControlInputs::Forward); } // Back if (GetAsyncKeyState(0x53)) { _currentInputs.insert(ControlInputs::Back); } // Turn Left if (GetAsyncKeyState(0x41)) { _currentInputs.insert(ControlInputs::TurnLeft); } // Turn Right if (GetAsyncKeyState(0x44)) { _currentInputs.insert(ControlInputs::TurnRight); } // Strafe Left if (GetAsyncKeyState(0x51)) { _currentInputs.insert(ControlInputs::StrafeLeft); } // Strafe Right if (GetAsyncKeyState(0x45)) { _currentInputs.insert(ControlInputs::StrafeRight); } // "Jump" if (GetAsyncKeyState(VK_SPACE)) { _currentInputs.insert(ControlInputs::Up); } // "Crouch" if (GetAsyncKeyState(VK_CONTROL)) { _currentInputs.insert(ControlInputs::Down); } } } void Graphics2::ResetCurrentControlInputs() { _currentInputs.clear(); }