void gs_main_menu::update(StringHash eventType,VariantMap& eventData) { float timeStep=eventData[Update::P_TIMESTEP].GetFloat(); weather.update(timeStep); // Movement speed as world units per second //float MOVE_SPEED=1.0f; // Mouse sensitivity as degrees per pixel const float MOUSE_SENSITIVITY=0.1f; // camera movement Input* input=GetSubsystem<Input>(); Node* cameraNode_=globals::instance()->camera->GetNode(); // Let the camera speed high as its position above the terrain. Max 500m/s Terrain* terr = globals::instance()->scene->GetChild("Terrain")->GetComponent<Terrain>(); Vector3 vv = cameraNode_->GetPosition(); float hh = terr->GetHeight(vv); float MOVE_SPEED = Clamp(vv.y_ - hh, 5.0f, 500.0f); if(input->GetQualifierDown(1)) // 1 is shift, 2 is ctrl, 4 is alt MOVE_SPEED*=20; if(input->GetKeyDown('W')) cameraNode_->Translate(Vector3(0,0, 1)*MOVE_SPEED*timeStep); if(input->GetKeyDown('S')) cameraNode_->Translate(Vector3(0,0,-1)*MOVE_SPEED*timeStep); if(input->GetKeyDown('A')) cameraNode_->Translate(Vector3(-1,0,0)*MOVE_SPEED*timeStep); if(input->GetKeyDown('D')) cameraNode_->Translate(Vector3( 1,0,0)*MOVE_SPEED*timeStep); if(input->GetKeyDown('Q')) cameraNode_->Translate(Vector3(0,-1,0)*MOVE_SPEED*timeStep); if(input->GetKeyDown('E')) cameraNode_->Translate(Vector3(0,1,0)*MOVE_SPEED*timeStep); if(!GetSubsystem<Input>()->IsMouseVisible()) { IntVector2 mouseMove=input->GetMouseMove(); if(mouseMove.x_>-2000000000&&mouseMove.y_>-2000000000) { static float yaw_=0; static float pitch_=0; yaw_+=MOUSE_SENSITIVITY*mouseMove.x_; pitch_+=MOUSE_SENSITIVITY*mouseMove.y_; pitch_=Clamp(pitch_,-90.0f,90.0f); // Reset rotation and set yaw and pitch again cameraNode_->SetDirection(Vector3::FORWARD); cameraNode_->Yaw(yaw_); cameraNode_->Pitch(pitch_); } Vector3 v = cameraNode_->GetPosition(); float h = terr->GetHeight(v); if (v.y_ < h + 1.0f) { v.y_ = h + 1.0f; cameraNode_->SetPosition(v); } } }
void DuckHuntMain::UpdateScene(float dt) { // // Control the camera. // if (GetAsyncKeyState('W') & 0x8000) mCam.Walk(10.0f*dt); if (GetAsyncKeyState('S') & 0x8000) mCam.Walk(-10.0f*dt); if (GetAsyncKeyState('A') & 0x8000) mCam.Strafe(-10.0f*dt); if (GetAsyncKeyState('D') & 0x8000) mCam.Strafe(10.0f*dt); // // Animate the lights (and hence shadows). // //// //// Walk/fly mode //// //if (GetAsyncKeyState('2') & 0x8000) // mWalkCamMode = true; //if (GetAsyncKeyState('3') & 0x8000) // mWalkCamMode = false; //// //// Clamp camera to terrain surface in walk mode. //// XMFLOAT3 camPos = mCam.GetPosition(); float y = mTerrain.GetHeight(camPos.x, camPos.z); mCam.SetPosition(camPos.x, y + 2.0f, camPos.z); BuildShadowTransform(); mCam.UpdateViewMatrix(); mSystem->update(); }
void myInit() { glActiveTexture(GL_TEXTURE0); //make the background look like the sky float blue[4] = {0.5,0.5,1.0,0.0}; glClearColor(0.5, 0.5, 1.0, 0.0); glShadeModel(GL_SMOOTH); glEnable(GL_FOG); glFogfv(GL_FOG_COLOR,blue); glFogf(GL_FOG_MODE, GL_EXP2); glFogf(GL_FOG_START, 200); glFogf(GL_FOG_END, 1000); glFogf(GL_FOG_DENSITY, gFogDensity); //initial terrain myTerrain.initializeTerrain("../Data/Texture/Terrain/sand.tga", "../Data/Texture/Terrain/cactus.tga"); //initial ground collision square groundCollSquare.setParameters(TVector(0.0, 1.0, 0.0), gCenterPoint); //initial tank tankHeightPos = myTerrain.GetHeight(gCenterPoint.X()+100.0, gCenterPoint.Z()); myTank.setParameters("T-90.3DS", "../T-90/",TVector(gCenterPoint.X()-600.0, tankHeightPos + 9.0, gCenterPoint.Z()-20), 0.0, &myTerrain, &collisionBoxArray, &wall); myTank.initTank(); unsigned int brickTexture; createTexture("../Data/Texture/Brick/brick.bmp", brickTexture); wall.Init(myTank.getPosition().X() + 50, myTank.getPosition().Y()-8, myTank.getPosition().Z() + 20, NUM_BRICKS, brickTexture, &myTerrain); }
void VehicleDemo::CreateScene() { ResourceCache* cache = GetSubsystem<ResourceCache>(); scene_ = new Scene(context_); // Create scene subsystem components scene_->CreateComponent<Octree>(); scene_->CreateComponent<PhysicsWorld>(); // Create camera and define viewport. We will be doing load / save, so it's convenient to create the camera outside the scene, // so that it won't be destroyed and recreated, and we don't have to redefine the viewport on load cameraNode_ = new Node(context_); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->SetFarClip(500.0f); GetSubsystem<Renderer>()->SetViewport(0, new Viewport(context_, scene_, camera)); // Create static scene content. First create a zone for ambient lighting and fog control Node* zoneNode = scene_->CreateChild("Zone"); Zone* zone = zoneNode->CreateComponent<Zone>(); zone->SetAmbientColor(Color(0.15f, 0.15f, 0.15f)); zone->SetFogColor(Color(0.5f, 0.5f, 0.7f)); zone->SetFogStart(300.0f); zone->SetFogEnd(500.0f); zone->SetBoundingBox(BoundingBox(-2000.0f, 2000.0f)); // Create a directional light with cascaded shadow mapping Node* lightNode = scene_->CreateChild("DirectionalLight"); lightNode->SetDirection(Vector3(0.3f, -0.5f, 0.425f)); Light* light = lightNode->CreateComponent<Light>(); light->SetLightType(LIGHT_DIRECTIONAL); light->SetCastShadows(true); light->SetShadowBias(BiasParameters(0.00025f, 0.5f)); light->SetShadowCascade(CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f)); light->SetSpecularIntensity(0.5f); // Create heightmap terrain with collision Node* terrainNode = scene_->CreateChild("Terrain"); terrainNode->SetPosition(Vector3::ZERO); Terrain* terrain = terrainNode->CreateComponent<Terrain>(); terrain->SetPatchSize(64); terrain->SetSpacing(Vector3(2.0f, 0.1f, 2.0f)); // Spacing between vertices and vertical resolution of the height map terrain->SetSmoothing(true); terrain->SetHeightMap(cache->GetResource<Image>("Textures/HeightMap.png")); terrain->SetMaterial(cache->GetResource<Material>("Materials/Terrain.xml")); // The terrain consists of large triangles, which fits well for occlusion rendering, as a hill can occlude all // terrain patches and other objects behind it terrain->SetOccluder(true); RigidBody* body = terrainNode->CreateComponent<RigidBody>(); body->SetCollisionLayer(2); // Use layer bitmask 2 for static geometry CollisionShape* shape = terrainNode->CreateComponent<CollisionShape>(); shape->SetTerrain(); // Create 1000 mushrooms in the terrain. Always face outward along the terrain normal const unsigned NUM_MUSHROOMS = 1000; for (unsigned i = 0; i < NUM_MUSHROOMS; ++i) { Node* objectNode = scene_->CreateChild("Mushroom"); Vector3 position(Random(2000.0f) - 1000.0f, 0.0f, Random(2000.0f) - 1000.0f); position.y_ = terrain->GetHeight(position) - 0.1f; objectNode->SetPosition(position); // Create a rotation quaternion from up vector to terrain normal objectNode->SetRotation(Quaternion(Vector3::UP, terrain->GetNormal(position))); objectNode->SetScale(3.0f); StaticModel* object = objectNode->CreateComponent<StaticModel>(); object->SetModel(cache->GetResource<Model>("Models/Mushroom.mdl")); object->SetMaterial(cache->GetResource<Material>("Materials/Mushroom.xml")); object->SetCastShadows(true); RigidBody* body = objectNode->CreateComponent<RigidBody>(); body->SetCollisionLayer(2); CollisionShape* shape = objectNode->CreateComponent<CollisionShape>(); shape->SetTriangleMesh(object->GetModel(), 0); } }
void Water::CreateScene() { ResourceCache* cache = GetContext()->m_ResourceCache.get(); scene_ = new Scene(GetContext()); // Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000) scene_->CreateComponent<Octree>(); // Create a Zone component for ambient lighting & fog control Node* zoneNode = scene_->CreateChild("Zone"); Zone* zone = zoneNode->CreateComponent<Zone>(); zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f)); zone->SetAmbientColor(Color(0.15f, 0.15f, 0.15f)); zone->SetFogColor(Color(1.0f, 1.0f, 1.0f)); zone->SetFogStart(500.0f); zone->SetFogEnd(750.0f); // Create a directional light to the world. Enable cascaded shadows on it Node* lightNode = scene_->CreateChild("DirectionalLight"); lightNode->SetDirection(Vector3(0.6f, -1.0f, 0.8f)); Light* light = lightNode->CreateComponent<Light>(); light->SetLightType(LIGHT_DIRECTIONAL); light->SetCastShadows(true); light->SetShadowBias(BiasParameters(0.00025f, 0.5f)); light->SetShadowCascade(CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f)); light->SetSpecularIntensity(0.5f); // Apply slightly overbright lighting to match the skybox light->SetColor(Color(1.2f, 1.2f, 1.2f)); // Create skybox. The Skybox component is used like StaticModel, but it will be always located at the camera, giving the // illusion of the box planes being far away. Use just the ordinary Box model and a suitable material, whose shader will // generate the necessary 3D texture coordinates for cube mapping Node* skyNode = scene_->CreateChild("Sky"); skyNode->SetScale(500.0f); // The scale actually does not matter Skybox* skybox = skyNode->CreateComponent<Skybox>(); skybox->SetModel(cache->GetResource<Model>("Models/Box.mdl")); skybox->SetMaterial(cache->GetResource<Material>("Materials/Skybox.xml")); // Create heightmap terrain Node* terrainNode = scene_->CreateChild("Terrain"); terrainNode->SetPosition(Vector3(0.0f, 0.0f, 0.0f)); Terrain* terrain = terrainNode->CreateComponent<Terrain>(); terrain->SetPatchSize(64); terrain->SetSpacing(Vector3(2.0f, 0.5f, 2.0f)); // Spacing between vertices and vertical resolution of the height map terrain->SetSmoothing(true); terrain->SetHeightMap(cache->GetResource<Image>("Textures/HeightMap.png")); terrain->SetMaterial(cache->GetResource<Material>("Materials/Terrain.xml")); // The terrain consists of large triangles, which fits well for occlusion rendering, as a hill can occlude all // terrain patches and other objects behind it terrain->SetOccluder(true); // Create 1000 boxes in the terrain. Always face outward along the terrain normal unsigned NUM_OBJECTS = 1000; for (unsigned i = 0; i < NUM_OBJECTS; ++i) { Node* objectNode = scene_->CreateChild("Box"); Vector3 position(Random(2000.0f) - 1000.0f, 0.0f, Random(2000.0f) - 1000.0f); position.y_ = terrain->GetHeight(position) + 2.25f; objectNode->SetPosition(position); // Create a rotation quaternion from up vector to terrain normal objectNode->SetRotation(Quaternion(Vector3(0.0f, 1.0f, 0.0f), terrain->GetNormal(position))); objectNode->SetScale(5.0f); StaticModel* object = objectNode->CreateComponent<StaticModel>(); object->SetModel(cache->GetResource<Model>("Models/Box.mdl")); object->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml")); object->SetCastShadows(true); } Node* shipNode = scene_->CreateChild("Ship"); shipNode->SetPosition(Vector3(0.0f, 4.6f, 0.0f)); //shipNode->SetRotation(Quaternion(0.0f, Random(360.0f), 0.0f)); shipNode->SetScale(0.5f + Random(2.0f)); StaticModel* shipObject = shipNode->CreateComponent<StaticModel>(); shipObject->SetModel(cache->GetResource<Model>("Models/ship04.mdl")); shipObject->SetMaterial(0,cache->GetResource<Material>("Materials/ship04_Material0.xml")); shipObject->SetMaterial(1,cache->GetResource<Material>("Materials/ship04_Material1.xml")); shipObject->SetMaterial(2,cache->GetResource<Material>("Materials/ship04_Material2.xml")); shipObject->SetCastShadows(true); // Create a water plane object that is as large as the terrain waterNode_ = scene_->CreateChild("Water"); waterNode_->SetScale(Vector3(2048.0f, 1.0f, 2048.0f)); waterNode_->SetPosition(Vector3(0.0f, 5.0f, 0.0f)); StaticModel* water = waterNode_->CreateComponent<StaticModel>(); water->SetModel(cache->GetResource<Model>("Models/Plane.mdl")); water->SetMaterial(cache->GetResource<Material>("Materials/Water.xml")); // Set a different viewmask on the water plane to be able to hide it from the reflection camera water->SetViewMask(0x80000000); // Create the camera. Set far clip to match the fog. Note: now we actually create the camera node outside // the scene, because we want it to be unaffected by scene load / save cameraNode_ = new Node(GetContext()); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->setFarClipDistance(750.0f); // Set an initial position for the camera scene node above the ground cameraNode_->SetPosition(Vector3(0.0f, 7.0f, -20.0f)); }
void Projekt::UpdateScene(float dt) { mPlayer.Update(dt, mDirectInput); for (UINT i = 0; i < mGenSkinnedInstances.size(); ++i) mGenSkinnedInstances[i].Update(dt); /* // Up if (mDirectInput->GetKeyboardState()[DIK_W] && 0x80) { mPlayer.GetCamera()->walk(30.0f*dt); } // Left if (mDirectInput->GetKeyboardState()[DIK_A] & 0x80) { mPlayer.GetCamera()->strafe(-30.0f*dt); } // Down if (mDirectInput->GetKeyboardState()[DIK_S] & 0x80) { mPlayer.GetCamera()->walk(-30.0f*dt); } // Right if (mDirectInput->GetKeyboardState()[DIK_D] & 0x80) { mPlayer.GetCamera()->strafe(30.0f*dt); } // Mouse has moved in x-axis if (mDirectInput->MouseHasMoved()) { // Make each pixel correspond to a quarter of a degree. float dx = XMConvertToRadians(0.25f*static_cast<float>(mDirectInput->GetMouseState().lX)); float dy = XMConvertToRadians(0.25f*static_cast<float>(mDirectInput->GetMouseState().lY)); mPlayer.GetCamera()->yaw(dx); mPlayer.GetCamera()->pitch(dy); } */ //DetectInput(dt); // Movement // if(GetAsyncKeyState('W') & 0x8000) // mPlayer.GetCamera()->walk(30.0f*dt); // // if(GetAsyncKeyState('S') & 0x8000) // mPlayer.GetCamera()->walk(-30.0f*dt); // // if(GetAsyncKeyState('A') & 0x8000) // mPlayer.GetCamera()->strafe(-30.0f*dt); // // if(GetAsyncKeyState('D') & 0x8000) // mPlayer.GetCamera()->strafe(30.0f*dt); // Change shadow map resolution // if(GetAsyncKeyState('1') & 0x8000) // { // mShadowMapSize = 256; // mShadowMap->setResolution(mDirect3D->GetDevice(), mShadowMapSize, mShadowMapSize); // } // // if(GetAsyncKeyState('2') & 0x8000) // { // mShadowMapSize = 512; // mShadowMap->setResolution(mDirect3D->GetDevice(), mShadowMapSize, mShadowMapSize); // } // // if(GetAsyncKeyState('3') & 0x8000) // { // mShadowMapSize = 1024; // mShadowMap->setResolution(mDirect3D->GetDevice(), mShadowMapSize, mShadowMapSize); // } // // if(GetAsyncKeyState('4') & 0x8000) // { // mShadowMapSize = 2048; // mShadowMap->setResolution(mDirect3D->GetDevice(), mShadowMapSize, mShadowMapSize); // } // // if(GetAsyncKeyState('5') & 0x8000) // { // mShadowMapSize = 4096; // mShadowMap->setResolution(mDirect3D->GetDevice(), mShadowMapSize, mShadowMapSize); // } // // if(GetAsyncKeyState('6') & 0x8000) // { // mShadowMapSize = 8192; // mShadowMap->setResolution(mDirect3D->GetDevice(), mShadowMapSize, mShadowMapSize); // } // Walk/fly mode // if(GetAsyncKeyState('Z') & 0x8000) // mWalkCamMode = true; // if(GetAsyncKeyState('X') & 0x8000) // mWalkCamMode = false; // Walk mode if (mWalkCamMode) { XMFLOAT3 camPos = mPlayer.GetCamera()->getPosition(); float y = mTerrain.GetHeight(camPos.x, camPos.z); mPlayer.GetCamera()->setPosition(camPos.x, y + 2.0f, camPos.z); } // Update particle systems mFire.update(dt, mTimer.getTimeElapsedS()); // Build shadow map transform buildShadowTransform(); // Update camera mPlayer.GetCamera()->updateViewMatrix(); //------------------------------------------------------------------ // Frustum culling //------------------------------------------------------------------ mVisibleObjectCount = 0; if (mFrustumCullingEnabled) { XMVECTOR detView = XMMatrixDeterminant(mPlayer.GetCamera()->getViewMatrix()); XMMATRIX invView = XMMatrixInverse(&detView, mPlayer.GetCamera()->getViewMatrix()); for (UINT i = 0; i < mGenericInstances.size(); ++i) { mGenericInstances[i].isVisible = false; XMMATRIX W = XMLoadFloat4x4(&mGenericInstances[i].world); XMMATRIX invWorld = XMMatrixInverse(&XMMatrixDeterminant(W), W); // View space to the object's local space. XMMATRIX toLocal = XMMatrixMultiply(invView, invWorld); // Decompose the matrix into its individual parts. XMVECTOR scale; XMVECTOR rotQuat; XMVECTOR translation; XMMatrixDecompose(&scale, &rotQuat, &translation, toLocal); // Transform the camera frustum from view space to the object's local space. XNA::Frustum localspaceFrustum; XNA::TransformFrustum(&localspaceFrustum, &mCamFrustum, XMVectorGetX(scale), rotQuat, translation); // Perform the box/frustum intersection test in local space. if(XNA::IntersectAxisAlignedBoxFrustum(&mGenericInstances[i].model->boundingBox, &localspaceFrustum) != 0) { // Write the instance data to dynamic VB of the visible objects. //dataView[mVisibleObjectCount++] = mInstancedData[i]; mVisibleObjectCount++; mGenericInstances[i].isVisible = true; } } } else { for (UINT i = 0; i < mGenericInstances.size(); ++i) { mGenericInstances[i].isVisible = true; mVisibleObjectCount++; } } std::wostringstream outs; outs.precision(6); outs << L" " << mVisibleObjectCount << L" objects visible out of " << mGenericInstances.size(); mMainWndCaption = outs.str(); }