-
Notifications
You must be signed in to change notification settings - Fork 0
/
MeshGenerator.cs
305 lines (269 loc) · 11.4 KB
/
MeshGenerator.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
/*
-----------------------------------------------------------------------------
This source file is part of mogre-procedural
For the latest info, see http://code.google.com/p/mogre-procedural/
my blog:http://hi.baidu.com/rainssoft
this is overwrite ogre-procedural c++ project using c#, look ogre-procedural c++ source http://code.google.com/p/ogre-procedural/
Copyright (c) 2013-2020 rains soft
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/
//#ifndef PROCEDURAL_MESH_GENERATOR_INCLUDED
#define PROCEDURAL_MESH_GENERATOR_INCLUDED
//write with new std .... ok
namespace Mogre_Procedural
{
using System;
using System.Collections.Generic;
using System.Text;
using Mogre;
using Math = Mogre.Math;
using Mogre_Procedural.std;
//C++ TO C# CONVERTER TODO TASK: The original C++ template specifier was replaced with a C# generic specifier, which may not produce the same behavior:
//ORIGINAL LINE: template <typename T>
//*
//\defgroup objgengrp Object generators
//Elements for procedural mesh generation of various objects.
//@{
//@}
//
//*
//\ingroup objgengrp
//Superclass of everything that builds meshes
//
public abstract class MeshGenerator<T>
{
/// A pointer to the default scene manager
//Ogre::SceneManager* mSceneMgr;
/// U tile for texture coords generation
protected float mUTile = 0f;
/// V tile for texture coords generation
protected float mVTile = 0f;
/// Whether to produces normals or not
protected bool mEnableNormals;
/// The number of texture coordinate sets to include
protected byte mNumTexCoordSet;
/// Rectangle in which the texture coordinates will be placed
protected Vector2 mUVOrigin = new Vector2();
/// If set to true, the UV coordinates coming from the mesh generator will be switched.
/// It can be used, for example, if your texture doesn't fit the mesh generator's assumptions about UV.
/// If UV were to fit in a given rectangle, they still fit in it after the switch.
protected bool mSwitchUV;
/// Orientation to apply the mesh
protected Quaternion mOrientation = new Quaternion();
/// Scale to apply the mesh
protected Vector3 mScale = new Vector3();
/// Position to apply to the mesh
protected Vector3 mPosition = new Vector3();
// Whether a transform has been defined or not
protected bool mTransform;
/// Default constructor
/// \exception Ogre::InvalidStateException Scene Manager is not set in OGRE root object
public MeshGenerator() {
mUTile = 1.0f;
mVTile = 1.0f;
mEnableNormals = true;
mNumTexCoordSet = 1;
mUVOrigin = new Vector2(0f, 0f);
mSwitchUV = false;
mOrientation = Quaternion.IDENTITY;
mScale = new Vector3(1f, 1f, 1f);
mPosition = new Vector3(0f, 0f, 0f);
mTransform = false;
}
// *
// * Builds a mesh.
// * @param name of the mesh for the MeshManager
// * @param group ressource group in which the mesh will be created
//
public MeshPtr realizeMesh(string name) {
return realizeMesh(name, "General");
}
public MeshPtr realizeMesh() {
return realizeMesh("", "General");
}
//
//ORIGINAL LINE: Ogre::MeshPtr realizeMesh(const string& name = "", const Ogre::String& group = "General")
public MeshPtr realizeMesh(string name, string group) {
TriangleBuffer tbuffer = new TriangleBuffer();
addToTriangleBuffer(ref tbuffer);
MeshPtr mesh = null;// MeshManager.Singleton.CreateManual(name, group); //new MeshPtr();
if (name == "")
mesh = tbuffer.transformToMesh(Utils.getName("mesh_procedural_"), group);
else
mesh = tbuffer.transformToMesh(name, group);
return mesh;
}
// *
// * Outputs a triangleBuffer
//
//
//ORIGINAL LINE: TriangleBuffer buildTriangleBuffer() const
public TriangleBuffer buildTriangleBuffer() {
TriangleBuffer tbuffer = new TriangleBuffer();
addToTriangleBuffer(ref tbuffer);
return tbuffer;
}
// *
// * Overloaded by each generator to implement the specifics
//
public abstract void addToTriangleBuffer(ref TriangleBuffer buffer);
// *
// * Sets U Tile, ie the number by which u texture coordinates are multiplied (default=1)
//
public MeshGenerator<T> setUTile(float uTile) {
mUTile = uTile;
//return( this);
return this;
}
protected float cosf(float a) {
return Math.Cos(a);
}
protected float sinf(float a) {
return Math.Sin(a);
}
protected float atan2f(float p, float p_2) {
return Math.ATan2(p, p_2).ValueRadians;
}
protected float sqrtf(float p) {
return Math.Sqrt(p);
}
protected Real max(Real p, Real p_2) {
return System.Math.Max(p, p_2);
}
protected static void OGRE_EXCEPT(string p, string p_2, string p_3) {
throw new Exception(p + "_" + p_2 + "_" + p_3);
}
// *
// * Sets V Tile, ie the number by which v texture coordinates are multiplied (default=1)
//
public MeshGenerator<T> setVTile(float vTile) {
mVTile = vTile;
//return (T)( this);
return this;
}
// *
// * Sets the texture rectangle
//
public MeshGenerator<T> setTextureRectangle(RealRect textureRectangle) {
mUVOrigin = new Vector2(textureRectangle.top, textureRectangle.left);
mUTile = textureRectangle.right - textureRectangle.left;
mVTile = textureRectangle.bottom - textureRectangle.top;
//return (T)this;
return this;
}
// *
// * Sets whether normals are enabled or not (default=true)
//
public MeshGenerator<T> setEnableNormals(bool enableNormals) {
mEnableNormals = enableNormals;
//return (T)( this);
return this;
}
// *
// * Sets the number of texture coordintate sets (default=1)
//
public MeshGenerator<T> setNumTexCoordSet(byte numTexCoordSet) {
mNumTexCoordSet = numTexCoordSet;
//return (T)( this);
return this;
}
/// Sets whether to switch U and V texture coordinates
public MeshGenerator<T> setSwitchUV(bool switchUV) {
mSwitchUV = switchUV;
//return (T)( this);
return this;
}
/// Sets an orientation to give when building the mesh
public MeshGenerator<T> setOrientation(Quaternion orientation) {
mOrientation = orientation;
mTransform = true;
//return (T)( this);
return this;
}
/// Sets a translation baked into the resulting mesh
public MeshGenerator<T> setPosition(Vector3 position) {
mPosition = position;
mTransform = true;
//return (T)( this);
return this;
}
/// Sets a translation baked into the resulting mesh
public MeshGenerator<T> setPosition(float x, float y, float z) {
mPosition = new Vector3(x, y, z);
mTransform = true;
//return (T)( this);
return this;
}
/// Sets a scale baked into the resulting mesh
public MeshGenerator<T> setScale(Vector3 scale) {
mScale = scale;
mTransform = true;
//return (T)( this);
return this;
}
/// Sets a uniform scale baked into the resulting mesh
public MeshGenerator<T> setScale(float scale) {
mScale = new Vector3(scale);
mTransform = true;
//return (T)( this);
return this;
}
/// Sets a scale baked into the resulting mesh
public MeshGenerator<T> setScale(float x, float y, float z) {
mScale = new Vector3(x, y, z);
mTransform = true;
//return (T)( this);
return this;
}
/// Resets all transforms (orientation, position and scale) that would have been applied to the mesh to their default values
public MeshGenerator<T> resetTransforms() {
mTransform = false;
mPosition = Vector3.ZERO;
mOrientation = Quaternion.IDENTITY;
mScale = new Vector3(1f);
//return (T)( this);
return this;
}
/// Adds a new point to a triangle buffer, using the format defined for that MeshGenerator
/// @param buffer the triangle buffer to update
/// @param position the position of the new point
/// @param normal the normal of the new point
/// @param uv the uv texcoord of the new point
//
//ORIGINAL LINE: inline void addPoint(TriangleBuffer& buffer, const Ogre::Vector3& position, const Ogre::Vector3& normal, const Ogre::Vector2& uv) const
protected void addPoint(ref TriangleBuffer buffer, Vector3 position, Vector3 normal, Vector2 uv) {
if (mTransform)
buffer.position(mPosition + mOrientation * (mScale * position));
else
buffer.position(position);
if (mEnableNormals) {
if (mTransform)
buffer.normal(mOrientation * normal);
else
buffer.normal(normal);
}
if (mSwitchUV)
for (byte i = 0; i < mNumTexCoordSet; i++)
buffer.textureCoord(mUVOrigin.x + uv.y * mUTile, mUVOrigin.y + uv.x * mVTile);
else
for (byte i = 0; i < mNumTexCoordSet; i++)
buffer.textureCoord(mUVOrigin.x + uv.x * mUTile, mUVOrigin.y + uv.y * mVTile);
}
}
//
}