-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOgreMeshData.cs
356 lines (315 loc) · 15.3 KB
/
OgreMeshData.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
/*
-----------------------------------------------------------------------------
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.
-----------------------------------------------------------------------------
*/
namespace Mogre_Procedural{
using System;
using System.Collections.Generic;
using System.Text;
using Mogre;
/// <summary>
/// mesh数据 内部根据参数不做处理或者自动销毁
/// 当autodispose_meshPtr=true,MeshPtr参数在初始化完毕之后就销毁。这里需要注意
/// 当autodispose_meshPtr=false, 传进的MeshPtr参数需外面管理销毁以防止.net自动回收该指针造成崩溃
/// </summary>
public class OgreMeshData : IDisposable {
// SGD :2013/6/20 9:31:35
// 说明:建立meshdata缓存 ?
public string meshName;
public Mogre.RenderOperation.OperationTypes opType;
private Vector3[] _vertices;
private uint[] _indices;
private Vector2[] _textureCroodnitas;
private Vector3 _scale = Vector3.UNIT_SCALE;
private Quaternion _quaoffset = Quaternion.IDENTITY;
private Vector3 _posoffset = Vector3.ZERO;
/// <summary>
/// key submesh, keyvaluepair(start,end)
/// </summary>
private Dictionary<uint, KeyValuePair<uint, uint>> _index_Submesh;
private bool _readTextureCoordinate = false;
//private MeshPtr _meshPtr;
public Vector3[] Vertices {
get {
return this._vertices;
}
}
public uint[] Indices {
get {
return this._indices;
}
}
public int TriangleCount {
get {
return this._indices.Length / 3;
}
}
public Vector2[] _TextureCoordnitas {
get { return _textureCroodnitas; }
}
/// <summary>
/// 通过朝向与坐标变换
/// </summary>
/// <param name="originalVextex"></param>
/// <returns></returns>
public static Vector3 GetVextexTrans(Vector3 originalVextex, Quaternion euler, Vector3 posOffset) {
//(orientation * (pt * scale)) + position
return GetVextexTrans(originalVextex, euler, Vector3.UNIT_SCALE, posOffset);
}
public static Vector3 GetVextexTrans(Vector3 originalVextex, Quaternion euler, Vector3 scale, Vector3 posOffset) {
//(orientation * (pt * scale)) + position
Vector3 renderpos = (euler * (originalVextex * scale)) + posOffset;
return renderpos;
}
//
public float[] _getPoints() {
//get {
// extract the points out of the vertices
float[] points = new float[this.Vertices.Length * 3];
int i = 0;
foreach (Vector3 vertex in this.Vertices) {
points[i + 0] = vertex.x;
points[i + 1] = vertex.y;
points[i + 2] = vertex.z;
i += 3;
}
return points;
//}
}
/// <summary>
/// 根据索引数组中的位置信息取得属于第几个submesh
/// </summary>
/// <param name="Indices_Index">索引数组中的位置</param>
/// <returns></returns>
public uint getSubMesh(uint Indices_Index) {
uint sub = 0;
foreach (var v in this._index_Submesh) {
if (Indices_Index >= v.Value.Key && Indices_Index <= v.Value.Value) {
return v.Key;
break;
}
sub++;
}
return sub;
}
#region OgreMeshData
public OgreMeshData(MeshPtr meshPtr, bool autodispose_meshPtr) {
Initiliase(meshPtr, autodispose_meshPtr,Vector3.UNIT_SCALE);
}
public OgreMeshData(MeshPtr meshPtr,bool autodispose_meshPtr, float unitScale) {
Initiliase(meshPtr,autodispose_meshPtr, Vector3.UNIT_SCALE * unitScale);
}
public OgreMeshData(MeshPtr meshPtr, bool autodispose_meshPtr, Vector3 scale) {
Initiliase(meshPtr,autodispose_meshPtr, scale);
}
public OgreMeshData(MeshPtr meshPtr, bool autodispose_meshPtr, Vector3 scale, bool readtexturecoord) {
Initiliase(meshPtr,autodispose_meshPtr, scale, this._quaoffset,this._posoffset, readtexturecoord);
}
public OgreMeshData(MeshPtr meshPtr, bool autodispose_meshPtr, Vector3 scale, Quaternion qua, Vector3 pos, bool readtexturecoord) {
Initiliase(meshPtr,autodispose_meshPtr, scale, qua,pos, readtexturecoord);
}
#endregion
#region read
private void Initiliase(MeshPtr meshPtr, bool autodispose_meshPtr, Vector3 scale) {
Initiliase(meshPtr,autodispose_meshPtr, scale, Quaternion.IDENTITY,Vector3.ZERO,false);
}
private void Initiliase(MeshPtr meshPtr, bool autodispose_meshPtr, Vector3 scale, Quaternion qua, Vector3 pos, bool readtextcroodnitas) {
this._scale = scale;
//this._meshPtr = meshPtr;
this.meshName = meshPtr.Name;
this.opType = meshPtr.GetSubMesh(0).operationType;
this._readTextureCoordinate = readtextcroodnitas;
this._quaoffset = qua;
this._posoffset = pos;
PrepareBuffers(meshPtr);
ReadData(meshPtr);
if (autodispose_meshPtr) {
meshPtr.Dispose();
meshPtr = null;
}
}
private void PrepareBuffers(MeshPtr _meshPtr) {
uint indexCount = 0;
uint vertexCount = 0;
// Add any shared vertices
if (_meshPtr.sharedVertexData != null)
vertexCount = _meshPtr.sharedVertexData.vertexCount;
// Calculate the number of vertices and indices in the sub meshes
for (ushort i = 0; i < _meshPtr.NumSubMeshes; i++) {
SubMesh subMesh = _meshPtr.GetSubMesh(i);
// we have already counted the vertices that are shared
if (subMesh.useSharedVertices == false)
vertexCount += subMesh.vertexData.vertexCount;
indexCount += subMesh.indexData.indexCount;
}
// Allocate space for the vertices and indices
_vertices = new Vector3[vertexCount];
_indices = new uint[indexCount];
_index_Submesh = new Dictionary<uint, KeyValuePair<uint, uint>>();
if (_readTextureCoordinate) {
_textureCroodnitas = new Vector2[vertexCount];
}
}
private void ReadData(MeshPtr _meshPtr) {
int indexOffset = 0;
uint vertexOffset = 0;
// read the index and vertex data from the mesh
for (ushort i = 0; i < _meshPtr.NumSubMeshes; i++) {
SubMesh subMesh = _meshPtr.GetSubMesh(i);
//index
int indexOffset2 = ReadIndexData(indexOffset, vertexOffset, subMesh.indexData);
_index_Submesh.Add(i, new KeyValuePair<uint, uint>((uint)indexOffset, (uint)indexOffset2));
indexOffset = indexOffset2;
//vertex
if (subMesh.useSharedVertices == false) {
vertexOffset = ReadVertexData(vertexOffset, subMesh.vertexData);
}
}
// add the shared vertex data
if (_meshPtr.sharedVertexData != null) {
vertexOffset = ReadVertexData(vertexOffset, _meshPtr.sharedVertexData);
}
}
//增加读取纹理坐标
private unsafe uint ReadVertexData(uint vertexOffset, VertexData vertexData) {
VertexElement posElem = vertexData.vertexDeclaration.FindElementBySemantic(VertexElementSemantic.VES_POSITION);
HardwareVertexBufferSharedPtr vertexBuffer = vertexData.vertexBufferBinding.GetBuffer(posElem.Source);
byte* vertexMemory = (byte*)vertexBuffer.Lock(HardwareBuffer.LockOptions.HBL_READ_ONLY);
float* pElem;
bool needrecomputeVertex = false;
if (this._quaoffset != Quaternion.IDENTITY || this._posoffset != Vector3.ZERO) {
needrecomputeVertex = true;
}
//texture
if (_readTextureCoordinate) {
ReadTextureCoordData(vertexOffset, vertexMemory, vertexBuffer.VertexSize, vertexData);
}
if (needrecomputeVertex) {
for (uint i = 0; i < vertexData.vertexCount; i++) {
posElem.BaseVertexPointerToElement(vertexMemory, &pElem);
Vector3 point = new Vector3(pElem[0], pElem[1], pElem[2]);
// vertices[current_offset + j] = (orientation * (pt * scale)) + position;
_vertices[vertexOffset] = GetVextexTrans(point, this._quaoffset, this._scale, this._posoffset); //point * this.scale;
vertexMemory += vertexBuffer.VertexSize;
vertexOffset++;
}
}
else {
for (uint i = 0; i < vertexData.vertexCount; i++) {
posElem.BaseVertexPointerToElement(vertexMemory, &pElem);
Vector3 point = new Vector3(pElem[0], pElem[1], pElem[2]);
_vertices[vertexOffset] = point * this._scale;
vertexMemory += vertexBuffer.VertexSize;
vertexOffset++;
}
}
vertexBuffer.Unlock();
// SGD :2013/6/8 13:43:51
// 说明:销毁指针引用 防止.NET回收
vertexBuffer.Dispose();
return vertexOffset;
}
// SGD :2013/6/18 15:02:51
// 说明:顶点纹理坐标
private unsafe void ReadTextureCoordData(uint vertexOffset, byte* vertexBuffer_Lock, uint vertexBuffer_VertexSize, VertexData vertexData) {
VertexElement texElem = vertexData.vertexDeclaration.FindElementBySemantic(VertexElementSemantic.VES_TEXTURE_COORDINATES);
float* tReal;
if (texElem == null) {
for (uint i = 0; i < vertexData.vertexCount; i++) {
this._textureCroodnitas[vertexOffset + i] = new Vector2(0f, 0f);
}
}
else {
//byte* vextex = vertexBuffer_Lock;
//for (uint i = 0; i < vertexData.vertexCount; i++) {
// texElem.BaseVertexPointerToElement(vextex, &tReal);
// this._textureCroodnitas[vertexOffset + i] = new Vector2(tReal[0], tReal[1]);
// vextex += vertexBuffer_VertexSize;
//}
//读取纹理坐标修正 2013/12/18
VertexData vertex_data=vertexData;
HardwareVertexBufferSharedPtr vbufPtr_tex = vertex_data.vertexBufferBinding.GetBuffer(texElem.Source);
byte* vertex_tex = (byte*)vbufPtr_tex.Lock(HardwareBuffer.LockOptions.HBL_READ_ONLY);
for (uint i = 0; i < vertex_data.vertexCount; ++i, /*vertex += vbufPtr.VertexSize,*/ vertex_tex += vbufPtr_tex.VertexSize) {
//posElem.BaseVertexPointerToElement(vertex, &pReal);
texElem.BaseVertexPointerToElement(vertex_tex, &tReal);
this._textureCroodnitas[vertexOffset + i] = new Vector2(tReal[0], tReal[1]);
vertex_tex += vertexBuffer_VertexSize;
//Vector3 pt = new Vector3(pReal[0], pReal[1], pReal[2]);
//vertices[current_offset + j] = (orientation * (pt * scale)) + position;
//tex_Cors[current_offset + j] = new Vector2(tReal[0], tReal[1]);
}
vbufPtr_tex.Unlock();
vbufPtr_tex.Dispose();
}
}
private unsafe int ReadIndexData(int indexOffset, uint vertexOffset, IndexData indexData) {
// get index data
HardwareIndexBufferSharedPtr indexBuf = indexData.indexBuffer;
HardwareIndexBuffer.IndexType indexType = indexBuf.Type;
uint* pLong = (uint*)(indexBuf.Lock(HardwareBuffer.LockOptions.HBL_READ_ONLY));
ushort* pShort = (ushort*)pLong;
for (uint i = 0; i < indexData.indexCount; i++) {
if (indexType == HardwareIndexBuffer.IndexType.IT_32BIT)
_indices[indexOffset] = pLong[i] + vertexOffset;
else
_indices[indexOffset] = pShort[i] + vertexOffset;
indexOffset++;
}
indexBuf.Unlock();
// SGD :2013/6/8 13:42:42
// 说明:销毁指针引用 防止.NET回收
indexBuf.Dispose();
return indexOffset;
}
#endregion
#region IDisposable 成员
public bool IsDisposed { get { return disposedValue; } }
private bool disposedValue;
protected virtual void Dispose(bool disposing) {
if (!this.disposedValue) {
if (!disposing) {
Console.WriteLine("~StaticMeshData()");
}
this._indices = null;
this._vertices = null;
this._index_Submesh.Clear();
this._index_Submesh = null;
this._textureCroodnitas = null;
// SGD :2013/6/8 13:46:08
// 说明:销毁指针引用 防止.NET回收
//this._meshPtr.Dispose();
//this._meshPtr = null;
}
this.disposedValue = true;
}
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
~OgreMeshData() {
Dispose(false);
}
#endregion
}
}