-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
333 lines (268 loc) · 9.83 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include <string>
#include <sstream>
#include <math.h>
#include <ctime>
#include <iostream>
#include <Urho3D/Core/CoreEvents.h>
#include <Urho3D/Engine/Application.h>
#include <Urho3D/Engine/Engine.h>
#include <Urho3D/Input/Input.h>
#include <Urho3D/Input/InputEvents.h>
#include <Urho3D/Resource/ResourceCache.h>
#include <Urho3D/Resource/XMLFile.h>
#include <Urho3D/IO/Log.h>
#include <Urho3D/UI/UI.h>
#include <Urho3D/UI/Text.h>
#include <Urho3D/UI/Font.h>
#include <Urho3D/UI/Button.h>
#include <Urho3D/UI/Window.h>
#include <Urho3D/UI/UIEvents.h>
#include <Urho3D/Scene/Scene.h>
#include <Urho3D/Scene/SceneEvents.h>
#include <Urho3D/Graphics/Graphics.h>
#include <Urho3D/Graphics/Camera.h>
#include <Urho3D/Graphics/Geometry.h>
#include <Urho3D/Graphics/Renderer.h>
#include <Urho3D/Graphics/DebugRenderer.h>
#include <Urho3D/Graphics/Octree.h>
#include <Urho3D/Graphics/Light.h>
#include <Urho3D/Graphics/Model.h>
#include <Urho3D/Graphics/StaticModel.h>
#include <Urho3D/Graphics/Material.h>
#include <Urho3D/Graphics/Skybox.h>
#include <Urho3D\Graphics\BillboardSet.h>
#include <Urho3D/Core/Context.h>
//////////////////////
#include "Tree.h"
using namespace Urho3D;
/**
* Using the convenient Application API we don't have
* to worry about initializing the engine or writing a main.
* You can probably mess around with initializing the engine
* and running a main manually, but this is convenient and portable.
*/
class MyApp : public Application
{
public:
int framecount_;
float time_;
SharedPtr<Text> text_;
SharedPtr<Scene> scene_;
SharedPtr<Node> cameraNode_;
SharedPtr<Window> window_;
MyApp(Context * context) : Application(context), framecount_(0), time_(0)
{
}
virtual void Setup()
{
engineParameters_["FullScreen"] = false;
engineParameters_["WindowWidth"] = 1280;
engineParameters_["WindowHeight"] = 720;
engineParameters_["WindowResizable"] = true;
}
virtual void Start()
{
OpenConsoleWindow();
ResourceCache* cache = GetSubsystem<ResourceCache>();
//GUI
initUI(context_);
//RENDERING
scene_ = new Scene(context_);
scene_->CreateComponent<Octree>();
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"));
cameraNode_ = scene_->CreateChild("Camera");
cameraNode_->SetPosition(Vector3(-15, 40, -15));
Camera* camera = cameraNode_->CreateComponent<Camera>();
camera->SetFarClip(2000);
{ // SunLight
Node* lightNode = scene_->CreateChild();
lightNode->SetDirection(Vector3::FORWARD);
lightNode->Yaw(30);
lightNode->Pitch(75);
Light* light = lightNode->CreateComponent<Light>();
light->SetLightType(LIGHT_DIRECTIONAL);
light->SetBrightness(1.6);
light->SetColor(Color(1.0, .99, 0.99, 1));
light->SetCastShadows(true);
}
Tree::tParameters treeparams = {
Tree::CrownType::BOX, 1500, {0, 20, 0}, {16,16,16},
8.0f, 3.0f, 1.0f,
{Tree::vec3f(0,0,0)},
1, true, 0.2, 2.5, 4, 7,
{50,50,50}
};
SharedPtr<Model> treemodel = Tree::getUrhoModelFromParams(context_, treeparams);
Node* treenode = scene_->CreateChild("tree-node");
StaticModel* treeobj = treenode->CreateComponent<StaticModel>();
treeobj->SetModel(treemodel);
treeobj->SetMaterial(cache->GetResource<Material>("Materials/TreeTrunk.xml"));
treeobj->SetCastShadows(true);
{ //save geometry to external file!
PODVector<Drawable*> pod;
pod.Push(treeobj);
String filename = "output.obj";
SharedPtr<File>file = SharedPtr<File>(new File(context_, filename, FILE_WRITE));
WriteDrawablesToOBJ(pod, file, false, false);
file->Close();
}
Node* boxnode = scene_->CreateChild("ground");
boxnode->SetPosition({0,0,0});
boxnode->SetScale({15,1,15});
StaticModel* boxobj = boxnode->CreateComponent<StaticModel>();
boxobj->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
boxobj->SetMaterial(cache->GetResource<Material>("Materials/Dirt.xml"));
boxobj->SetCastShadows(true);
// Now we setup the viewport. Of course, you can have more than one!
Renderer* renderer = GetSubsystem<Renderer>();
SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
renderer->SetViewport(0, viewport);
SubscribeToEvent(E_POSTRENDERUPDATE, URHO3D_HANDLER(MyApp, HandlePostRenderUpdate));
SubscribeToEvent(E_KEYDOWN, URHO3D_HANDLER(MyApp, HandleKeyDown));
SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(MyApp, HandleUpdate));
SubscribeToEvent(E_UIMOUSECLICK, URHO3D_HANDLER(MyApp, HandleControlClicked));
}
virtual void Stop()
{
}
void initUI(Context* context_)
{
auto* cache = GetSubsystem<ResourceCache>();
auto* graphics = GetSubsystem<Graphics>();
auto* ui_root = GetSubsystem<UI>()->GetRoot();
auto* ui_ = GetSubsystem<UI>();
auto* style = cache->GetResource<XMLFile>("UI/DefaultStyle.xml");
ui_root->SetDefaultStyle(style);
context_->GetSubsystem<Urho3D::Input>()->SetMouseVisible(true);
static Urho3D::Cursor* cursor = new Urho3D::Cursor(context_);
cursor->SetShape(Urho3D::CursorShape::CS_IBEAM);
//if(!GetSubsystem<Urho3D::UI>()->GetCursor()) // If I uncomment this it doesn't crash but does also nothing
///////separate
//GetSubsystem<Urho3D::UI>()->SetCursor(cursor);
text_ = new Text(context_);
text_->SetText("Keys: Mouse2 = rotate camera, Mouse2 + AWSD = move camera, Shift = fast mode, Esc = quit.\nWait a bit to see FPS.");
text_->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 10);
text_->SetColor(Color(0, 0, 0));
text_->SetHorizontalAlignment(HA_LEFT);
text_->SetVerticalAlignment(VA_TOP);
ui_root->AddChild(text_);
window_ = new Window(context_);
ui_root->AddChild(window_);
window_->SetMinWidth(384);
window_->SetLayout(LM_VERTICAL, 6, IntRect(6, 6, 6, 6));
//window_->SetAlignment(HA_LEFT, VA_CENTER);
window_->SetPosition({ 10, 35 });
window_->SetName("Window");
window_->SetColor({0, 0, 0, 0.5});
// Create Window 'titlebar' container
auto* titleBar = new UIElement(context_);
titleBar->SetMinSize(0, 24);
titleBar->SetVerticalAlignment(VA_TOP);
titleBar->SetLayoutMode(LM_HORIZONTAL);
// Create the Window title Text
auto* windowTitle = new Text(context_);
windowTitle->SetName("WindowTitle");
windowTitle->SetText("Hello GUI!");
// Create the Window's close button
auto* buttonClose = new Button(context_);
buttonClose->SetName("CloseButton");
// Add the controls to the title bar
titleBar->AddChild(windowTitle);
titleBar->AddChild(buttonClose);
// Add the title bar to the Window
window_->AddChild(titleBar);
window_->SetStyleAuto();
windowTitle->SetStyleAuto();
buttonClose->SetStyle("CloseButton");
}
void HandleControlClicked(StringHash eventType, VariantMap& eventData) //handles clicks anywhere on the screen
{
// Get the Text control acting as the Window's title
auto* windowTitle = window_->GetChildStaticCast<Text>("WindowTitle", true);
// Get control that was clicked
auto* clicked = static_cast<UIElement*>(eventData[UIMouseClick::P_ELEMENT].GetPtr());
String name = "...?";
if (clicked)
{
// Get the name of the control that was clicked
name = clicked->GetName();
}
// Update the Window's title text
windowTitle->SetText("Hello " + name + "!");
}
void HandlePostRenderUpdate(StringHash eventType, VariantMap& eventData)
{
}
void HandleKeyDown(StringHash eventType, VariantMap& eventData)
{
using namespace KeyDown;
int key = eventData[P_KEY].GetInt();
if (key == KEY_ESCAPE)
engine_->Exit();
/*if (key == KEY_TAB) // toggle mouse cursor when pressing tab
{
GetSubsystem<Input>()->SetMouseVisible(!GetSubsystem<Input>()->IsMouseVisible());
//GetSubsystem<Input>()->SetMouseGrabbed(!GetSubsystem<Input>()->IsMouseGrabbed());
}*/
}
void HandleUpdate(StringHash eventType, VariantMap& eventData)
{
float timeStep = eventData[Update::P_TIMESTEP].GetFloat();
framecount_++;
time_ += timeStep;
// Movement speed as world units per second
float MOVE_SPEED = 10.0f;
// Mouse sensitivity as degrees per pixel
const float MOUSE_SENSITIVITY = 0.1f;
if (time_ >= 3.14159 * 2)
{
std::string str;
{
std::ostringstream ss;
ss << (float)framecount_ / time_;
std::string s(ss.str());
str.append(s.substr(0, 6));
}
str.append(" fps \nPlease hold Mouse2 to move and rotate the camera");
String s(str.c_str(), str.size());
text_->SetText(s);
//URHO3D_LOGINFO(s); // this shows how to put stuff into the log
framecount_ = 0;
time_ = 0;
}
auto* input = GetSubsystem<Input>();
input->SetMouseVisible(!input->GetMouseButtonDown(4));
if (input->GetMouseButtonDown(4)) { //m2 pressed
if (input->GetQualifierDown(1)) // 1 is shift, 2 is ctrl, 4 is alt
MOVE_SPEED *= 10;
if (input->GetScancodeDown(26))
cameraNode_->Translate(Vector3(0, 0, 1)*MOVE_SPEED*timeStep);
if (input->GetScancodeDown(22))
cameraNode_->Translate(Vector3(0, 0, -1)*MOVE_SPEED*timeStep);
if (input->GetScancodeDown(4))
cameraNode_->Translate(Vector3(-1, 0, 0)*MOVE_SPEED*timeStep);
if (input->GetScancodeDown(7))
cameraNode_->Translate(Vector3(1, 0, 0)*MOVE_SPEED*timeStep);
}
//if (!GetSubsystem<Input>()->IsMouseVisible())
if (input->GetMouseButtonDown(4))
{
// Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
IntVector2 mouseMove = input->GetMouseMove();
static float yaw_ = 45;
static float pitch_ = 45;
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_);
}
}
};
URHO3D_DEFINE_APPLICATION_MAIN(MyApp)