-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
445 lines (370 loc) · 16 KB
/
Form1.cs
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
using Mogre;
using MogreNewt.CollisionPrimitives;
namespace MOgreEditor
{
public partial class Form1 : Form
{
Root root;
RenderWindow window;
SceneManager sceneManager;
Camera camera;
SceneNode sinbadNode;
SceneNode cameraNode;
Viewport viewport;
bool isRunning;
EditorScene editorScene;
Vector3 currentObjectPosition;
Rotation currentObjectRotation;
SceneNode selectedObjectSceneNode;
RaySceneQuery mRaySceneQuery;
public Form1()
{
InitializeComponent();
IntPtr t = MOgreControl1.Handle;
/*TreeNode rootNode = new TreeNode("Root");
// Добавляем новый дочерний узел к rootNode
rootNode.Nodes.Add(new TreeNode("Смартфоны"));
// Добавляем rootNode вместе с дочерними узлами в TreeView
treeView1.Nodes.Add(rootNode);
// Добавляем второй очерний узел к первому узлу в TreeView
treeView1.Nodes[0].Nodes.Add(new TreeNode("Планшеты"));*/
root = new Root();
if (root.ShowConfigDialog())
{
NameValuePairList misc = new NameValuePairList();
misc["externalWindowHandle"] = t.ToString();
root.Initialise(false, "MOgre3D");
window = root.CreateRenderWindow("Main RenderWindow", 800, 600, false, misc);
sceneManager = root.CreateSceneManager(SceneType.ST_GENERIC);
camera = sceneManager.CreateCamera("Camera");
// camera.SetPosition(0, 0, 50);
// camera.LookAt(new Vector3(0, 0, 0));
camera.NearClipDistance = 5.0f;
viewport = window.AddViewport(camera);
viewport.BackgroundColour = ColourValue.Black;
camera.AspectRatio = (float)viewport.ActualWidth / (float)viewport.ActualHeight;
ResourceGroupManager.Singleton.AddResourceLocation("../../Media/packs/Sinbad.zip", "Zip");
ResourceGroupManager.Singleton.InitialiseAllResourceGroups();
editorScene = new EditorScene(treeView1, sceneManager);
sinbadNode = editorScene.AddEditorSceneNode("Sinbad", "Sinbad.mesh");
sinbadNode = editorScene.AddEditorSceneNode("Sinbad1", "Sinbad.mesh");
editorScene.AddEditorSceneNode("Camera", "Camera");
editorScene.GetEditorSceneNodeByName("Sinbad1").Position = new Vector3(10.0f, 2.0f, 3.0f);
camera.Position = new Vector3(0, 0, -50);
camera.LookAt(editorScene.GetEditorSceneNodeByName("Sinbad1").Position);
MOgreControl1.myMouseMoved += mouseMovedEvent;
MOgreControl1.myMouseDown += mouseDownEvent;
mRaySceneQuery = sceneManager.CreateRayQuery(new Ray());
isRunning = true;
Thread thread = new Thread(Go);
thread.Start();
}
}
private void mouseDownEvent(object sender, EventArgs e)
{
int x1 = ((System.Windows.Forms.MouseEventArgs)e).X;
int y1 = ((System.Windows.Forms.MouseEventArgs)e).Y;
label7.Text = "X = " + x1.ToString() + " Y = " + y1.ToString();
// Setup the ray scene query
float screenX = x1 / (float)800;
float screenY = y1 / (float)600;
Ray mouseRay = camera.GetCameraToViewportRay(screenX, screenY);
mRaySceneQuery.Ray = mouseRay;
// Execute query
RaySceneQueryResult result = mRaySceneQuery.Execute();
RaySceneQueryResult.Enumerator itr = (RaySceneQueryResult.Enumerator)(result.GetEnumerator());
// Get results, create a node/entity on the position
if (itr != null && itr.MoveNext())
{
int yy = 1;
label8.Text = itr.Current.movable.ParentNode.Name;
label9.Text = ((Mogre.Entity)itr.Current.movable).MovableType;
label10.Text = itr.Current.movable.Name;
label11.Text = itr.Current.distance.ToString("F3");
// ((Mogre.SceneNode)itr.Current.movable.ParentNode).ShowBoundingBox = !((Mogre.SceneNode)itr.Current.movable.ParentNode).ShowBoundingBox;
if (selectedObjectSceneNode != null)
selectedObjectSceneNode.ShowBoundingBox = false;
selectedObjectSceneNode = (Mogre.SceneNode)itr.Current.movable.ParentNode;
/*if (useCurrent)
{
mCurrentObject.Position = itr.Current.worldFragment.singleIntersection;
}
else
{
Entity ent = sceneMgr.CreateEntity(
"Robot" + mCount.ToString(), "robot.mesh");
mCurrentObject = sceneMgr.RootSceneNode.CreateChildSceneNode(
"RobotNode" + mCount.ToString(),
itr.Current.worldFragment.singleIntersection);
mCount++;
mCurrentObject.AttachObject(ent);
mCurrentObject.SetScale(0.1f, 0.1f, 0.1f);
}*/
}
// throw new NotImplementedException();
}
private void mouseMovedEvent(object sender, EventArgs e)
{
// throw new NotImplementedException();
}
public void Go()
{
Stopwatch stopwatch = Stopwatch.StartNew();
long prev = stopwatch.ElapsedMilliseconds;
long prevGUI = stopwatch.ElapsedMilliseconds;
long now = stopwatch.ElapsedMilliseconds;
long delta;
long deltaGUI;
while (root != null && isRunning)
{
now = stopwatch.ElapsedMilliseconds;
delta = now - prev;
deltaGUI = now - prevGUI;
if (delta > 16)
{
root.RenderOneFrame();
prev = now;
}
else
{
if (deltaGUI > 500)
{
prevGUI = now;
if (selectedObjectSceneNode != null)
{
UpdateTextObject(0);
}
else
{
UpdateTextObject(1);
}
}
/*
Euler euler = new Euler(sinbadNode.Orientation);
euler.AddYaw(new Degree(0.1f));
sinbadNode.Orientation = euler.ToQuaternion();
camera.LookAt(sinbadNode.Position);
Vector3 tt1 = new Vector3(0, 0, -50);
camera.Position = sinbadNode.Position + sinbadNode.Orientation * tt1;
*/
Thread.Sleep(1);
}
Application.DoEvents();
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
isRunning = false;
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
TreeNode treeNode = e.Node;
EditorSceneNode sceneNode1 = null;
try
{
if (treeNode != null)
{
if (treeNode.Name != null)
{
bool found = false;
foreach (EditorSceneNode sceneNode in editorScene.children)
{
if (sceneNode.sceneNode != null)
{
sceneNode.sceneNode.ShowBoundingBox = false;
if (sceneNode.name == treeNode.Text)
{
sceneNode1 = sceneNode;
found = true;
}
}
}
if (found)
{
selectedObjectSceneNode = sceneNode1.sceneNode;
UpdateTextObject(0);
}
else
{
if (treeNode.Text == "Camera")
{
selectedObjectSceneNode = null;
UpdateTextObject(1);
}
}
}
}
}
catch
{
}
}
private void UpdateTextObject(int v)
{
try
{
if (v == 0)
{
if (selectedObjectSceneNode != null)
{
selectedObjectSceneNode.ShowBoundingBox = true;
tbPositionX.Invoke(new Action(() => tbPositionX.Text = selectedObjectSceneNode.Position.x.ToString()));
tbPositionY.Invoke(new Action(() => tbPositionY.Text = selectedObjectSceneNode.Position.y.ToString()));
tbPositionZ.Invoke(new Action(() => tbPositionZ.Text = selectedObjectSceneNode.Position.z.ToString()));
Euler euler = new Euler(selectedObjectSceneNode.Orientation);
tbYaw.Invoke(new Action(() => tbYaw.Text = euler.Yaw.ValueDegrees.ToString()));
tbPitch.Invoke(new Action(() => tbPitch.Text = euler.Pitch.ValueDegrees.ToString()));
tbRoll.Invoke(new Action(() => tbRoll.Text = euler.Roll.ValueDegrees.ToString()));
}
}
else if (v == 1)
{
if ((editorScene != null) && (editorScene.camera != null))
{
Camera camera = editorScene.camera;
tbPositionX.Invoke(new Action(() => tbPositionX.Text = camera.Position.x.ToString()));
tbPositionY.Invoke(new Action(() => tbPositionY.Text = camera.Position.y.ToString()));
tbPositionZ.Invoke(new Action(() => tbPositionZ.Text = camera.Position.z.ToString()));
Euler euler = new Euler(camera.Orientation);
tbYaw.Invoke(new Action(() => tbYaw.Text = euler.Yaw.ValueDegrees.ToString()));
tbPitch.Invoke(new Action(() => tbPitch.Text = euler.Pitch.ValueDegrees.ToString()));
tbRoll.Invoke(new Action(() => tbRoll.Text = euler.Roll.ValueDegrees.ToString()));
}
}
}
catch { }
}
private void SaveMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "XML files(*.xml)|*.xml|All files(*.*)|*.*";
if (saveFileDialog.ShowDialog() != DialogResult.Cancel)
{
SaveScene.WriteScene(saveFileDialog.FileName, editorScene);
}
}
private void LoadMenuItem_Click(object sender, EventArgs e)
{
try
{
editorScene.Dispose();
editorScene.treeView1.Nodes.Clear();
editorScene = new EditorScene(treeView1, sceneManager);
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "XML files(*.xml)|*.xml|All files(*.*)|*.*";
if (openFileDialog.ShowDialog() != DialogResult.Cancel)
{
SaveScene.ReadScene(openFileDialog.FileName, editorScene);
selectedObjectSceneNode = null;
}
}
catch { }
}
private void NewMenuItem_Click(object sender, EventArgs e)
{
try
{
editorScene.Dispose();
editorScene.treeView1.Nodes.Clear();
editorScene = new EditorScene(treeView1, sceneManager);
selectedObjectSceneNode = null;
editorScene.children.Clear();
editorScene.sceneManager.ClearScene();
editorScene.camera = new Camera("Camera", editorScene.sceneManager);
editorScene.camera.Position = new Vector3(0, 0, -50);
editorScene.camera.Orientation = new Quaternion(0, 0, 1, 0);
editorScene.camera.Direction = new Vector3(0, 0, 0);
editorScene.AddEditorSceneNode("Camera", "Camera");
}
catch { }
}
private void tbPositionX_TextChanged(object sender, EventArgs e)
{
try
{
if (selectedObjectSceneNode != null)
{
Vector3 pos = selectedObjectSceneNode.Position;
float tx1;
bool t2 = float.TryParse(tbPositionX.Text, out tx1);
if (t2)
{
Vector3 vector = new Vector3(tx1, pos.y, pos.z);
selectedObjectSceneNode.Position = vector;
}
}
}
catch { }
}
private void tbPositionY_TextChanged(object sender, EventArgs e)
{
try
{
if (selectedObjectSceneNode != null)
{
Vector3 pos = selectedObjectSceneNode.Position;
float tx1;
bool t2 = float.TryParse(tbPositionY.Text, out tx1);
if (t2)
{
Vector3 vector = new Vector3(pos.x, tx1, pos.z);
selectedObjectSceneNode.Position = vector;
}
}
}
catch { }
}
private void tbPositionZ_TextChanged(object sender, EventArgs e)
{
try
{
if (selectedObjectSceneNode != null)
{
Vector3 pos = selectedObjectSceneNode.Position;
float tx1;
bool t2 = float.TryParse(tbPositionZ.Text, out tx1);
if (t2)
{
Vector3 vector = new Vector3(pos.x, pos.y, tx1);
selectedObjectSceneNode.Position = vector;
}
}
}
catch { }
}
}
}
// sinbadNode = sceneManager.RootSceneNode.CreateChildSceneNode("Sinbad");
// sinbadNode.AttachObject(ent);
/* cameraNode = sinbadNode.CreateChildSceneNode();
cameraNode.Position = new Vector3(0, 0, 50);
cameraNode.LookAt(sinbadNode.Position, Node.TransformSpace.TS_WORLD);
mNode->setPosition(targetPos + rotateQuaternion * aroundPos);
// quite sure you want the SceneNode to face the "targetPos"
mNode->lookAt(targetPos, Node::TS_WORLD);
// or
mNode->setOrientation(aroundPos.getRotationTo(targetPos));
*/
/*SceneNode sinbadNode1 = sceneManager.RootSceneNode.CreateChildSceneNode("Sinbad1");
sinbadNode1.AttachObject(ent1);*/
// sinbadNode.Yaw(new Radian(0.01f));
// sinbadNode.Pitch(new Radian(0.01f));
/*Quaternion t1 = sinbadNode.Orientation;
Matrix3 rotMatrix= t1.ToRotationMatrix();
Radian mYaw;
Radian mPitch;
Radian mRoll;
rotMatrix.ToEulerAnglesYXZ(out mYaw, out mPitch, out mRoll);*/