-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProceduralTriangleBuffer.cs
487 lines (439 loc) · 18.3 KB
/
ProceduralTriangleBuffer.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*
-----------------------------------------------------------------------------
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_TRIANGLEBUFFER_INCLUDED
#define PROCEDURAL_TRIANGLEBUFFER_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;
//* This is ogre-procedural's temporary mesh buffer.
// * It stores all the info needed to build an Ogre Mesh, yet is intented to be more flexible, since
// * there is no link towards hardware.
//
//C++ TO C# CONVERTER WARNING: The original type declaration contained unconverted modifiers:
//ORIGINAL LINE: class _ProceduralExport TriangleBuffer
public class TriangleBuffer
{
public class Vertex
{
public Vector3 mPosition = new Vector3();
public Vector3 mNormal = new Vector3();
public Vector2 mUV = new Vector2();
}
protected std_vector<int> mIndices = new std_vector<int>();
protected std_vector<Vertex> mVertices = new std_vector<Vertex>();
//std::vector<Vertex>::iterator mCurrentVertex;
protected int globalOffset;
protected int mEstimatedVertexCount;
protected int mEstimatedIndexCount;
protected Vertex mCurrentVertex;
public TriangleBuffer() {
globalOffset = 0;
mEstimatedVertexCount = 0;
mEstimatedIndexCount = 0;
mCurrentVertex = null;
}
public void append(TriangleBuffer other) {
rebaseOffset();
foreach (var it in other.mIndices) {
mIndices.push_back(globalOffset + it);
}
foreach (var it in other.mVertices) {
mVertices.push_back(it);
}
}
/// Gets a modifiable reference to vertices
public std_vector<Vertex> getVertices() {
return mVertices;
}
/// Gets a non-modifiable reference to vertices
//
//ORIGINAL LINE: const List<Vertex>& getVertices() const
public Vertex[] _getVertices() {
return mVertices.ToArray();
}
/// Gets a modifiable reference to vertices
public std_vector<int> getIndices() {
return mIndices;
}
/// Gets a non-modifiable reference to indices
//
//ORIGINAL LINE: const List<int>& getIndices() const
public int[] _getIndices() {
return mIndices.ToArray();
}
// *
// * Rebase index offset : call that function before you add a new mesh to the triangle buffer
//
public void rebaseOffset() {
globalOffset = mVertices.size();
}
// *
// * Builds an Ogre Mesh from this buffer.
//
public MeshPtr transformToMesh(string name) {
return transformToMesh(name, "General");
}
//
//ORIGINAL LINE: Ogre::MeshPtr transformToMesh(const string& name, const Ogre::String& group = "General") const
//
public MeshPtr transformToMesh(string name, string group) {
//Mogre.SceneManager sceneMgr = Root.Singleton.GetSceneManagerIterator().Current;
Mogre.SceneManagerEnumerator.SceneManagerIterator item = Root.Singleton.GetSceneManagerIterator();
item.MoveNext();
Mogre.SceneManager sceneMgr= item.Current;
item.Dispose();
Mogre.ManualObject manual = sceneMgr.CreateManualObject(name);
manual.Begin("BaseWhiteNoLighting", RenderOperation.OperationTypes.OT_TRIANGLE_LIST);
foreach (var it in mVertices) {
manual.Position(it.mPosition);
manual.TextureCoord(it.mUV);
manual.Normal(it.mNormal);
}
foreach (var it in mIndices) {
manual.Index((ushort)it);
}
manual.End();
Mogre.MeshPtr mesh = manual.ConvertToMesh(name, group);
sceneMgr.DestroyManualObject(manual);
return mesh;
}
MeshPtr CreateMesh(string Name, string Group, IndexData IndexDataArg, VertexData VertexDataArg, AxisAlignedBox BoundingBox) {
Mogre.MeshPtr mMesh = Mogre.MeshManager.Singleton.CreateManual(Name, Group);
SubMesh SubMesh = mMesh.CreateSubMesh();
//Shallow copy the IndexBuffer argument into the SubMesh's indexData property
SubMesh.indexData.indexBuffer = IndexDataArg.indexBuffer;
SubMesh.indexData.indexCount = IndexDataArg.indexCount;
//Deep copy the VertexData argument into the Mesh's sharedVertexData
SubMesh.useSharedVertices = true;
mMesh.sharedVertexData = new VertexData();
mMesh.sharedVertexData.vertexBufferBinding.SetBinding(0, VertexDataArg.vertexBufferBinding.GetBuffer(0));
VertexDeclaration vdc = new VertexDeclaration();
VertexDataArg.vertexDeclaration.CopyTo(vdc);
mMesh.sharedVertexData.vertexDeclaration = vdc;
mMesh.sharedVertexData.vertexCount = VertexDataArg.vertexCount;
mMesh._setBounds(BoundingBox);
mMesh.Load();
return mMesh;
}
//* Adds a new vertex to the buffer
public TriangleBuffer vertex(Vertex v) {
mVertices.push_back(v);
mCurrentVertex = mVertices.back(); //mVertices[mVertices.Count - 1];
return this;
}
//* Adds a new vertex to the buffer
public TriangleBuffer vertex(Vector3 position, Vector3 normal, Vector2 uv) {
Vertex v = new Vertex();
//
//ORIGINAL LINE: v.mPosition = position;
v.mPosition = (position);
//
//ORIGINAL LINE: v.mNormal = normal;
v.mNormal = (normal);
//
//ORIGINAL LINE: v.mUV = uv;
v.mUV = (uv);
mVertices.push_back(v);
mCurrentVertex = mVertices.back();//mVertices[mVertices.Count - 1];
return this;
}
//* Adds a new vertex to the buffer
public TriangleBuffer position(Vector3 pos) {
Vertex v = new Vertex();
//
//ORIGINAL LINE: v.mPosition = pos;
v.mPosition = (pos);
mVertices.push_back(v);
mCurrentVertex = mVertices.back(); //mVertices[mVertices.Count - 1];
return this;
}
//* Adds a new vertex to the buffer
public TriangleBuffer position(float x, float y, float z) {
Vertex v = new Vertex();
v.mPosition = new Vector3(x, y, z);
mVertices.push_back(v);
mCurrentVertex = mVertices.back(); //mVertices[mVertices.Count - 1];
return this;
}
//* Sets the normal of the current vertex
public TriangleBuffer normal(Vector3 normal) {
//
//ORIGINAL LINE: mCurrentVertex->mNormal = normal;
mCurrentVertex.mNormal = (normal);
return this;
}
//* Sets the texture coordinates of the current vertex
public TriangleBuffer textureCoord(float u, float v) {
mCurrentVertex.mUV = new Vector2(u, v);
return this;
}
//* Sets the texture coordinates of the current vertex
public TriangleBuffer textureCoord(Vector2 vec) {
//
//ORIGINAL LINE: mCurrentVertex->mUV = vec;
mCurrentVertex.mUV = (vec);
return this;
}
// *
// * Adds an index to the index buffer.
// * Index is relative to the latest rebaseOffset().
//
public TriangleBuffer index(int i) {
mIndices.push_back(globalOffset + i);
return this;
}
// *
// * Adds a triangle to the index buffer.
// * Index is relative to the latest rebaseOffset().
//
public TriangleBuffer triangle(int i1, int i2, int i3) {
mIndices.push_back(globalOffset + i1);
mIndices.push_back(globalOffset + i2);
mIndices.push_back(globalOffset + i3);
return this;
}
/// Applies a matrix to transform all vertices inside the triangle buffer
public TriangleBuffer applyTransform(Matrix4 matrix) {
foreach (var it in mVertices) {
it.mPosition = matrix * it.mPosition;
it.mNormal = matrix * it.mNormal;
it.mNormal = it.mNormal.NormalisedCopy;
}
return this;
}
/// Applies the translation immediately to all the points contained in that triangle buffer
/// @param amount translation vector
public TriangleBuffer translate(Vector3 amount) {
foreach (var it in mVertices) {
it.mPosition += amount;
}
return this;
}
/// Applies the translation immediately to all the points contained in that triangle buffer
public TriangleBuffer translate(float x, float y, float z) {
return translate(new Vector3(x, y, z));
}
/// Applies the rotation immediately to all the points contained in that triangle buffer
/// @param quat the rotation quaternion to apply
public TriangleBuffer rotate(Quaternion quat) {
foreach (var it in mVertices) {
it.mPosition = quat * it.mPosition;
it.mNormal = quat * it.mNormal;
it.mNormal = it.mNormal.NormalisedCopy;
}
return this;
}
/// Applies an immediate scale operation to that triangle buffer
/// @param scale Scale vector
public TriangleBuffer scale(Vector3 scale) {
foreach (var it in mVertices) {
it.mPosition = scale * it.mPosition;
}
return this;
}
/// Applies an immediate scale operation to that triangle buffer
/// @param x X scale component
/// @param y Y scale component
/// @param z Z scale component
public TriangleBuffer scale(float x, float y, float z) {
return scale(new Vector3(x, y, z));
}
/// Applies normal inversion on the triangle buffer
public TriangleBuffer invertNormals() {
foreach (var it in mVertices) {
it.mNormal = -it.mNormal;
}
for (int i = 0; i < mIndices.size(); ++i) {
if (i % 3 == 1) {
//std::swap(mIndices[i], mIndices[i-1]);
list_swap<int>(mIndices, i, i - 1);
}
}
return this;
}
private void list_swap<T>(List<T> list, int index1, int index2) {
T t = list[index1];
list[index1] = list[index2];
list[index2] = t;
}
// *
// * Gives an estimation of the number of vertices need for this triangle buffer.
// * If this function is called several times, it means an extra vertices count, not an absolute measure.
//
public void estimateVertexCount(uint vertexCount) {
mEstimatedVertexCount += (int)vertexCount;
//mVertices.Capacity = mEstimatedVertexCount;
mVertices.reserve(mEstimatedVertexCount);
}
// *
// * Gives an estimation of the number of indices needed for this triangle buffer.
// * If this function is called several times, it means an extra indices count, not an absolute measure.
//
public void estimateIndexCount(uint indexCount) {
mEstimatedIndexCount += (int)indexCount;
//mIndices.Capacity = mEstimatedIndexCount;
mIndices.reserve(mEstimatedIndexCount);
}
}
//void TriangleBuffer::importEntity(Entity* entity)
// {
// bool added_shared = false;
// int current_offset = 0;
// int shared_offset = 0;
// int next_offset = 0;
// int index_offset = 0;
// int vertex_count = 0;
// int index_count = 0;
//
// Ogre::MeshPtr mesh = entity->getMesh();
//
//
// bool useSoftwareBlendingVertices = entity->hasSkeleton();
//
// if (useSoftwareBlendingVertices)
// entity->_updateAnimation();
//
// // Calculate how many vertices and indices we're going to need
// for (unsigned short i = 0; i < mesh->getNumSubMeshes(); ++i)
// {
// Ogre::SubMesh* submesh = mesh->getSubMesh( i );
//
// // We only need to add the shared vertices once
// if(submesh->useSharedVertices)
// {
// if( !added_shared )
// {
// vertex_count += mesh->sharedVertexData->vertexCount;
// added_shared = true;
// }
// }
// else
// {
// vertex_count += submesh->vertexData->vertexCount;
// }
//
// // Add the indices
// index_count += submesh->indexData->indexCount;
// }
//
//
// // Allocate space for the vertices and indices
// estimateVertexCount(vertex_count);
// estimateIndexCount(index_count);
//
// added_shared = false;
//
// // Run through the submeshes again, adding the data into the arrays
// for ( unsigned short i = 0; i < mesh->getNumSubMeshes(); ++i)
// {
// Ogre::SubMesh* submesh = mesh->getSubMesh(i);
//
// //----------------------------------------------------------------
// // GET VERTEXDATA
// //----------------------------------------------------------------
// Ogre::VertexData* vertex_data;
//
// //When there is animation:
// if(useSoftwareBlendingVertices)
// vertex_data = submesh->useSharedVertices ? entity->_getSkelAnimVertexData() : entity->getSubEntity(i)->_getSkelAnimVertexData();
// else
// vertex_data = submesh->useSharedVertices ? mesh->sharedVertexData : submesh->vertexData;
//
//
// if((!submesh->useSharedVertices)||(submesh->useSharedVertices && !added_shared))
// {
// if(submesh->useSharedVertices)
// {
// added_shared = true;
// shared_offset = current_offset;
// }
//
// const Ogre::VertexElement* posElem =
// vertex_data->vertexDeclaration->findElementBySemantic(Ogre::VES_POSITION);
//
// Ogre::HardwareVertexBufferSharedPtr vbuf =
// vertex_data->vertexBufferBinding->getBuffer(posElem->getSource());
//
// unsigned char* vertex =
// static_cast<unsigned char*>(vbuf->lock(Ogre::HardwareBuffer::HBL_READ_ONLY));
//
// // There is _no_ baseVertexPointerToElement() which takes an Ogre::float or a double
// // as second argument. So make it float, to avoid trouble when Ogre::float will
// // be comiled/typedefed as double:
// // Ogre::Real* pReal;
// float* pReal;
//
// for( int j = 0; j < vertex_data->vertexCount; ++j, vertex += vbuf->getVertexSize())
// {
// posElem->baseVertexPointerToElement(vertex, &pReal);
//
// Ogre::Vector3 pt(pReal[0], pReal[1], pReal[2]);
//
// vertices[current_offset + j] = (orient * (pt * scale)) + position;
// }
//
// vbuf->unlock();
// next_offset += vertex_data->vertexCount;
// }
//
//
// Ogre::IndexData* index_data = submesh->indexData;
// int numTris = index_data->indexCount / 3;
// Ogre::HardwareIndexBufferSharedPtr ibuf = index_data->indexBuffer;
//
// bool use32bitindexes = (ibuf->getType() == Ogre::HardwareIndexBuffer::IT_32BIT);
//
// void* hwBuf = ibuf->lock(Ogre::HardwareBuffer::HBL_READ_ONLY);
//
// int offset = (submesh->useSharedVertices)? shared_offset : current_offset;
// int index_start = index_data->indexStart;
// int last_index = numTris*3 + index_start;
//
// if (use32bitindexes) {
// Ogre::uint32* hwBuf32 = static_cast<Ogre::uint32*>(hwBuf);
// for (int k = index_start; k < last_index; ++k)
// {
// indices[index_offset++] = hwBuf32[k] + static_cast<Ogre::uint32>( offset );
// }
// } else {
// Ogre::uint16* hwBuf16 = static_cast<Ogre::uint16*>(hwBuf);
// for (int k = index_start; k < last_index; ++k)
// {
// indices[ index_offset++ ] = static_cast<Ogre::uint32>( hwBuf16[k] ) +
// static_cast<Ogre::uint32>( offset );
// }
// }
//
// ibuf->unlock();
// current_offset = next_offset;
// }
// }
}