-
Notifications
You must be signed in to change notification settings - Fork 0
/
ManualObjectExample.cs
126 lines (110 loc) · 4.98 KB
/
ManualObjectExample.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
using System;
using Mogre;
namespace Mogre.Tutorials
{
/// <summary>
/// This class exemplify how to describe manually a mesh setting up the vertex and index buffer
/// </summary>
class ManualObjectExample
{
private SceneManager mSceneMgr;
private ManualObject manualObj;
private Entity manualObjEntity;
private SceneNode manualObjNode;
/// <summary>
/// Constructor, it stores a reference to the scenegraph into a private field
/// </summary>
/// <param name="mSceneMgr"> A reference to the scene manager </param>
public ManualObjectExample(SceneManager mSceneMgr)
{
this.mSceneMgr = mSceneMgr;
}
/// <summary>
/// This private method defines the vertex and index buffers of a quad as a triangle list.
/// </summary>
private MeshPtr Quad()
{
manualObj = mSceneMgr.CreateManualObject("maualObjQuad");
manualObj.Begin("void", RenderOperation.OperationTypes.OT_TRIANGLE_LIST); // Starts filling the manual object as a triangle list
// ---- Vertex Buffer ----
manualObj.Position(new Vector3(5, 5, 0)); // --- Vertex 0 ---
manualObj.Position(new Vector3(-5, 5, 0)); // --- Vertex 1 ---
manualObj.Position(new Vector3(5, -5, 0)); // --- Vertex 2 ---
manualObj.Position(new Vector3(-5, -5, 0)); // --- Vertex 3 ---
// ---- Index Buffer ----
// --- Triangle 0 ---
manualObj.Index(0);
manualObj.Index(1);
manualObj.Index(2);
// --- Triangle 1 ---
manualObj.Index(1);
manualObj.Index(3);
manualObj.Index(2);
manualObj.End(); // Closes the manual objet
return manualObj.ConvertToMesh("Quad"); // Prepares the data to be sent to the GPU
}
/// <summary>
/// This metod loads a quad in an entity and attaches it to the scenegraph
/// </summary>
public void Load()
{
Quad();
manualObjEntity = mSceneMgr.CreateEntity("Quad_Endtity", "Quad"); // Creates a new entity which contains the geometry
manualObjNode = mSceneMgr.CreateSceneNode("Quad_Node"); // Creates a new node for the scene graph
manualObjNode.AttachObject(manualObjEntity); // Attaches the entity (geometry) to the node
mSceneMgr.RootSceneNode.AddChild(manualObjNode); // Adds the node as child of the root of the scene graph
}
/// <summary>
/// This method detaches the node from the scene graph and destroies the node and the entity
/// </summary>
public void Dispose()
{
if (manualObjNode != null)
{
manualObjNode.Parent.RemoveChild(manualObjNode);
manualObjNode.DetachAllObjects();
manualObjNode.Dispose();
manualObjNode = null;
}
if (manualObjEntity != null)
{
manualObjEntity.Dispose();
manualObjEntity = null;
}
}
#region NonStaticGeo
/// <summary>
/// This method creates 1000 quads and attaches them to the scenegraph
/// </summary>
public void NonStaticGeometry()
{
Quad();
for (int i = 0; i < 100; i++)
for (int j = 0; j < 100; j++)
{
manualObjEntity = mSceneMgr.CreateEntity("Quad");
manualObjNode = mSceneMgr.RootSceneNode.CreateChildSceneNode(new Vector3(i * 5, j * 5, 0));
manualObjNode.AttachObject(manualObjEntity);
}
}
#endregion
#region StaticGeo
private StaticGeometry staticGeo;
/// <summary>
/// This method creates 4000 quads and attaches them to the scenegraph as static geometry
/// </summary>
public void StaticGeometry()
{
Quad();
staticGeo = mSceneMgr.CreateStaticGeometry("staticQuads"); // Initializes the static geometry container
for(int i=0; i<200; i++)
for (int j = 0; j < 200; j++)
{
manualObjEntity = mSceneMgr.CreateEntity("Quad"); // Loads the quad in an entity
staticGeo.AddEntity(manualObjEntity, new Vector3(i * 5, j * 5, 0)); // adds it to the scenegraph as static geo
}
staticGeo.Build(); // Prepares the static geometry to be rendered
}
#endregion
}
}