-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProceduralTubeGenerator.cs
242 lines (212 loc) · 11.3 KB
/
ProceduralTubeGenerator.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
/*
-----------------------------------------------------------------------------
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.
-----------------------------------------------------------------------------
*/
//write with new std ... ok
namespace Mogre_Procedural
{
using System;
using System.Collections.Generic;
using System.Text;
using Mogre;
using Math = Mogre.Math;
//*
// * \ingroup objgengrp
// * Builds an Y-axis tube mesh, i.e. an emptied cylinder
// * \image html primitive_tube.png
//
//C++ TO C# CONVERTER WARNING: The original type declaration contained unconverted modifiers:
//ORIGINAL LINE: class _ProceduralExport TubeGenerator : public MeshGenerator<TubeGenerator>
public class TubeGenerator : MeshGenerator<TubeGenerator>
{
private uint mNumSegBase;
private uint mNumSegHeight;
private float mOuterRadius = 0f;
private float mInnerRadius = 0f;
private float mHeight = 0f;
/// Constructor with arguments
public TubeGenerator(float outerRadius, float innerRadius, float height, uint numSegBase)
: this(outerRadius, innerRadius, height, numSegBase, 1) {
}
public TubeGenerator(float outerRadius, float innerRadius, float height)
: this(outerRadius, innerRadius, height, 16, 1) {
}
public TubeGenerator(float outerRadius, float innerRadius)
: this(outerRadius, innerRadius, 1.0f, 16, 1) {
}
public TubeGenerator(float outerRadius)
: this(outerRadius, 1.0f, 1.0f, 16, 1) {
}
public TubeGenerator()
: this(2.0f, 1.0f, 1.0f, 16, 1) {
}
//
//ORIGINAL LINE: TubeGenerator(Ogre::float outerRadius=2.0f, Ogre::float innerRadius=1.0f, Ogre::float height=1.0f, uint numSegBase=16, uint numSegHeight=1) : mNumSegBase(numSegBase), mNumSegHeight(numSegHeight), mOuterRadius(outerRadius), mInnerRadius(innerRadius), mHeight(height)
public TubeGenerator(float outerRadius, float innerRadius, float height, uint numSegBase, uint numSegHeight) {
mNumSegBase = numSegBase;
mNumSegHeight = numSegHeight;
mOuterRadius = outerRadius;
mInnerRadius = innerRadius;
mHeight = height;
}
// *
// * Builds the mesh into the given TriangleBuffer
// * @param buffer The TriangleBuffer on where to append the mesh.
//
//
//ORIGINAL LINE: void addToTriangleBuffer(TriangleBuffer& buffer) const
public override void addToTriangleBuffer(ref TriangleBuffer buffer) {
buffer.rebaseOffset();
buffer.estimateVertexCount((mNumSegHeight + 1) * (mNumSegBase + 1) * 2 + (mNumSegBase + 1) * 4);
buffer.estimateIndexCount(6 * (mNumSegBase + 1) * mNumSegHeight * 2 + 6 * mNumSegBase * 2);
float deltaAngle = (Math.TWO_PI / mNumSegBase);
float deltaHeight = mHeight / (float)mNumSegHeight;
int offset = 0;
for (uint i = 0; i <= mNumSegHeight; i++)
for (uint j = 0; j <= mNumSegBase; j++) {
float x0 = mOuterRadius * cosf(j * deltaAngle);
float z0 = mOuterRadius * sinf(j * deltaAngle);
addPoint(ref buffer, new Vector3(x0, i * deltaHeight, z0), new Vector3(x0, 0, z0).NormalisedCopy, new Vector2(j / (float)mNumSegBase, i / (float)mNumSegHeight));
if (i != mNumSegHeight) {
buffer.index(offset + (int)mNumSegBase + 1);
buffer.index(offset);
buffer.index(offset + (int)mNumSegBase);
buffer.index(offset + (int)mNumSegBase + 1);
buffer.index(offset + 1);
buffer.index(offset);
}
offset++;
}
for (uint i = 0; i <= mNumSegHeight; i++)
for (uint j = 0; j <= mNumSegBase; j++) {
float x0 = mInnerRadius * cosf(j * deltaAngle);
float z0 = mInnerRadius * sinf(j * deltaAngle);
addPoint(ref buffer, new Vector3(x0, i * deltaHeight, z0), -new Vector3(x0, 0, z0).NormalisedCopy, new Vector2(j / (float)mNumSegBase, i / (float)mNumSegHeight));
if (i != mNumSegHeight) {
buffer.index(offset + (int)mNumSegBase + 1);
buffer.index(offset + (int)mNumSegBase);
buffer.index(offset);
buffer.index(offset + (int)mNumSegBase + 1);
buffer.index(offset);
buffer.index(offset + 1);
}
offset++;
}
//low cap
for (uint j = 0; j <= mNumSegBase; j++) {
float x0 = mInnerRadius * cosf(j * deltaAngle);
float z0 = mInnerRadius * sinf(j * deltaAngle);
addPoint(ref buffer, new Vector3(x0, 0.0f, z0), Vector3.NEGATIVE_UNIT_Y, new Vector2(j / (float)mNumSegBase, 1.0f));
x0 = mOuterRadius * cosf(j * deltaAngle);
z0 = mOuterRadius * sinf(j * deltaAngle);
addPoint(ref buffer, new Vector3(x0, 0.0f, z0), Vector3.NEGATIVE_UNIT_Y, new Vector2(j / (float)mNumSegBase, 0.0f));
if (j != mNumSegBase) {
buffer.index(offset);
buffer.index(offset + 1);
buffer.index(offset + 3);
buffer.index(offset + 2);
buffer.index(offset);
buffer.index(offset + 3);
}
offset += 2;
}
//high cap
for (uint j = 0; j <= mNumSegBase; j++) {
float x0 = mInnerRadius * cosf(j * deltaAngle);
float z0 = mInnerRadius * sinf(j * deltaAngle);
addPoint(ref buffer, new Vector3(x0, mHeight, z0), Vector3.UNIT_Y, new Vector2(j / (float)mNumSegBase, 0.0f));
x0 = mOuterRadius * cosf(j * deltaAngle);
z0 = mOuterRadius * sinf(j * deltaAngle);
addPoint(ref buffer, new Vector3(x0, mHeight, z0), Vector3.UNIT_Y, new Vector2(j / (float)mNumSegBase, 1.0f));
if (j != mNumSegBase) {
buffer.index(offset + 1);
buffer.index(offset);
buffer.index(offset + 3);
buffer.index(offset);
buffer.index(offset + 2);
buffer.index(offset + 3);
}
offset += 2;
}
}
// *
// Sets the number of segments when rotating around the tube's axis (default=16)
// \exception Ogre::InvalidParametersException Minimum of numSegBase is 1
//
public TubeGenerator setNumSegBase(uint numSegBase) {
if (numSegBase == 0)
OGRE_EXCEPT("Ogre::Exception::ERR_INVALIDPARAMS", "There must be more than 0 segments", "Procedural::TubeGenerator::setNumSegBase(unsigned int)"); ;
mNumSegBase = numSegBase;
return this;
}
// *
// Sets the number of segments along the height of the cylinder (default=1)
// \exception Ogre::InvalidParametersException Minimum of numSegHeight is 1
//
public TubeGenerator setNumSegHeight(uint numSegHeight) {
if (numSegHeight == 0)
OGRE_EXCEPT("Ogre::Exception::ERR_INVALIDPARAMS", "There must be more than 0 segments", "Procedural::TubeGenerator::setNumSegHeight(unsigned int)");
;
mNumSegHeight = numSegHeight;
return this;
}
// *
// Sets the outer radius of the tube (default=2)
// \exception Ogre::InvalidParametersException Radius must be larger than 0!
// \exception Ogre::InvalidParametersException Outer radius must be bigger than inner radius
//
public TubeGenerator setOuterRadius(float outerRadius) {
if (outerRadius <= 0.0f)
OGRE_EXCEPT("Ogre::Exception::ERR_INVALIDPARAMS", "Radius must be larger than 0!", "Procedural::TubeGenerator::setOuterRadius(Ogre::Real)");
if (outerRadius < mInnerRadius)
OGRE_EXCEPT("Ogre::Exception::ERR_INVALIDPARAMS", "Outer radius must be bigger than inner radius!", "Procedural::TubeGenerator::setOuterRadius(Ogre::Real)"); ;
mOuterRadius = outerRadius;
return this;
}
// *
// Sets the inner radius of the tube (default=1)
// \exception Ogre::InvalidParametersException Radius must be larger than 0!
// \exception Ogre::InvalidParametersException Outer radius must be bigger than inner radius
//
public TubeGenerator setInnerRadius(float innerRadius) {
if (innerRadius <= 0.0f)
OGRE_EXCEPT("Ogre::Exception::ERR_INVALIDPARAMS", "Radius must be larger than 0!", "Procedural::TubeGenerator::setInnerRadius(Ogre::Real)");
if (mOuterRadius < innerRadius)
OGRE_EXCEPT("Ogre::Exception::ERR_INVALIDPARAMS", "Outer radius must be bigger than inner radius!", "Procedural::TubeGenerator::setInnerRadius(Ogre::Real)");
;
mInnerRadius = innerRadius;
return this;
}
// *
// Sets the height of the tube (default=1)
// \exception Ogre::InvalidParametersException Height must be larger than 0!
//
public TubeGenerator setHeight(float height) {
if (height <= 0.0f)
OGRE_EXCEPT("Ogre::Exception::ERR_INVALIDPARAMS", "Height must be larger than 0!", "Procedural::TubeGenerator::setHeight(Ogre::Real)");
;
mHeight = height;
return this;
}
}
}