1 //
2 // Copyright (c) 2008-2017 the Urho3D project.
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 //
22
23 #include <Urho3D/Core/CoreEvents.h>
24 #include <Urho3D/Engine/Engine.h>
25 #include <Urho3D/Graphics/Camera.h>
26 #include <Urho3D/Graphics/Graphics.h>
27 #include <Urho3D/Graphics/Material.h>
28 #include <Urho3D/Graphics/Model.h>
29 #include <Urho3D/Graphics/Octree.h>
30 #include <Urho3D/Graphics/Renderer.h>
31 #include <Urho3D/Graphics/StaticModel.h>
32 #include <Urho3D/Input/Input.h>
33 #include <Urho3D/Resource/ResourceCache.h>
34 #include <Urho3D/Scene/Scene.h>
35 #include <Urho3D/UI/Font.h>
36 #include <Urho3D/UI/Text.h>
37 #include <Urho3D/UI/UI.h>
38
39 #include "StaticScene.h"
40
41 #include <Urho3D/DebugNew.h>
42
URHO3D_DEFINE_APPLICATION_MAIN(StaticScene)43 URHO3D_DEFINE_APPLICATION_MAIN(StaticScene)
44
45 StaticScene::StaticScene(Context* context) :
46 Sample(context)
47 {
48 }
49
Start()50 void StaticScene::Start()
51 {
52 // Execute base class startup
53 Sample::Start();
54
55 // Create the scene content
56 CreateScene();
57
58 // Create the UI content
59 CreateInstructions();
60
61 // Setup the viewport for displaying the scene
62 SetupViewport();
63
64 // Hook up to the frame update events
65 SubscribeToEvents();
66
67 // Set the mouse mode to use in the sample
68 Sample::InitMouseMode(MM_RELATIVE);
69 }
70
CreateScene()71 void StaticScene::CreateScene()
72 {
73 ResourceCache* cache = GetSubsystem<ResourceCache>();
74
75 scene_ = new Scene(context_);
76
77 // Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
78 // show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates; it
79 // is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
80 // optimizing manner
81 scene_->CreateComponent<Octree>();
82
83 // Create a child scene node (at world origin) and a StaticModel component into it. Set the StaticModel to show a simple
84 // plane mesh with a "stone" material. Note that naming the scene nodes is optional. Scale the scene node larger
85 // (100 x 100 world units)
86 Node* planeNode = scene_->CreateChild("Plane");
87 planeNode->SetScale(Vector3(100.0f, 1.0f, 100.0f));
88 StaticModel* planeObject = planeNode->CreateComponent<StaticModel>();
89 planeObject->SetModel(cache->GetResource<Model>("Models/Plane.mdl"));
90 planeObject->SetMaterial(cache->GetResource<Material>("Materials/StoneTiled.xml"));
91
92 // Create a directional light to the world so that we can see something. The light scene node's orientation controls the
93 // light direction; we will use the SetDirection() function which calculates the orientation from a forward direction vector.
94 // The light will use default settings (white light, no shadows)
95 Node* lightNode = scene_->CreateChild("DirectionalLight");
96 lightNode->SetDirection(Vector3(0.6f, -1.0f, 0.8f)); // The direction vector does not need to be normalized
97 Light* light = lightNode->CreateComponent<Light>();
98 light->SetLightType(LIGHT_DIRECTIONAL);
99
100 // Create more StaticModel objects to the scene, randomly positioned, rotated and scaled. For rotation, we construct a
101 // quaternion from Euler angles where the Y angle (rotation about the Y axis) is randomized. The mushroom model contains
102 // LOD levels, so the StaticModel component will automatically select the LOD level according to the view distance (you'll
103 // see the model get simpler as it moves further away). Finally, rendering a large number of the same object with the
104 // same material allows instancing to be used, if the GPU supports it. This reduces the amount of CPU work in rendering the
105 // scene.
106 const unsigned NUM_OBJECTS = 200;
107 for (unsigned i = 0; i < NUM_OBJECTS; ++i)
108 {
109 Node* mushroomNode = scene_->CreateChild("Mushroom");
110 mushroomNode->SetPosition(Vector3(Random(90.0f) - 45.0f, 0.0f, Random(90.0f) - 45.0f));
111 mushroomNode->SetRotation(Quaternion(0.0f, Random(360.0f), 0.0f));
112 mushroomNode->SetScale(0.5f + Random(2.0f));
113 StaticModel* mushroomObject = mushroomNode->CreateComponent<StaticModel>();
114 mushroomObject->SetModel(cache->GetResource<Model>("Models/Mushroom.mdl"));
115 mushroomObject->SetMaterial(cache->GetResource<Material>("Materials/Mushroom.xml"));
116 }
117
118 // Create a scene node for the camera, which we will move around
119 // The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
120 cameraNode_ = scene_->CreateChild("Camera");
121 cameraNode_->CreateComponent<Camera>();
122
123 // Set an initial position for the camera scene node above the plane
124 cameraNode_->SetPosition(Vector3(0.0f, 5.0f, 0.0f));
125 }
126
CreateInstructions()127 void StaticScene::CreateInstructions()
128 {
129 ResourceCache* cache = GetSubsystem<ResourceCache>();
130 UI* ui = GetSubsystem<UI>();
131
132 // Construct new Text object, set string to display and font to use
133 Text* instructionText = ui->GetRoot()->CreateChild<Text>();
134 instructionText->SetText("Use WASD keys and mouse/touch to move");
135 instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
136
137 // Position the text relative to the screen center
138 instructionText->SetHorizontalAlignment(HA_CENTER);
139 instructionText->SetVerticalAlignment(VA_CENTER);
140 instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
141 }
142
SetupViewport()143 void StaticScene::SetupViewport()
144 {
145 Renderer* renderer = GetSubsystem<Renderer>();
146
147 // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen. We need to define the scene and the camera
148 // at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
149 // use, but now we just use full screen and default render path configured in the engine command line options
150 SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
151 renderer->SetViewport(0, viewport);
152 }
153
MoveCamera(float timeStep)154 void StaticScene::MoveCamera(float timeStep)
155 {
156 // Do not move if the UI has a focused element (the console)
157 if (GetSubsystem<UI>()->GetFocusElement())
158 return;
159
160 Input* input = GetSubsystem<Input>();
161
162 // Movement speed as world units per second
163 const float MOVE_SPEED = 20.0f;
164 // Mouse sensitivity as degrees per pixel
165 const float MOUSE_SENSITIVITY = 0.1f;
166
167 // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
168 IntVector2 mouseMove = input->GetMouseMove();
169 yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
170 pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
171 pitch_ = Clamp(pitch_, -90.0f, 90.0f);
172
173 // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
174 cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
175
176 // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
177 // Use the Translate() function (default local space) to move relative to the node's orientation.
178 if (input->GetKeyDown(KEY_W))
179 cameraNode_->Translate(Vector3::FORWARD * MOVE_SPEED * timeStep);
180 if (input->GetKeyDown(KEY_S))
181 cameraNode_->Translate(Vector3::BACK * MOVE_SPEED * timeStep);
182 if (input->GetKeyDown(KEY_A))
183 cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
184 if (input->GetKeyDown(KEY_D))
185 cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
186 }
187
SubscribeToEvents()188 void StaticScene::SubscribeToEvents()
189 {
190 // Subscribe HandleUpdate() function for processing update events
191 SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(StaticScene, HandleUpdate));
192 }
193
HandleUpdate(StringHash eventType,VariantMap & eventData)194 void StaticScene::HandleUpdate(StringHash eventType, VariantMap& eventData)
195 {
196 using namespace Update;
197
198 // Take the frame time step, which is stored as a float
199 float timeStep = eventData[P_TIMESTEP].GetFloat();
200
201 // Move the camera, scale movement with time step
202 MoveCamera(timeStep);
203 }
204