forked from Alfwich/CubicEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynamicMesh.cpp
261 lines (199 loc) · 6.79 KB
/
DynamicMesh.cpp
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
#include "DynamicMesh.h"
Model* DynamicMesh::generatePlain( double w, double d, int sections, Material* material )
{
Model* newMesh = new Model;
std::vector<Vertex*> verts;
std::vector<Vertex*>::iterator verts_iter;
std::vector<Point2D*> textures;
std::vector<Face*> faces;
std::vector<Bone*> bones;
double segmentWidth = w / sections;
double segmentDepth = d / sections;
double halfWidth = w / 2.0;
double halfDepth = d / 2.0;
int xMoveDistance = sections + 1;
int yMoveDistance = 1;
int amountOfVerts = static_cast<int>( pow( sections+1, 2.0 ) );
newMesh->amountOfVerts = amountOfVerts;
newMesh->amountOfFaces = static_cast<int>(pow( sections, 2.0 ) * 2.0);
newMesh->amountOfTextures = 4;
newMesh->allocateMemory();
newMesh->hasBeenAlteredSinceLoad = true;
int vertPos = 0;
// Create verts
for( double i = 0; i < sections+1; i++ )
{
for( double j = 0; j < sections+1; j++ )
{
newMesh->verts[ vertPos ].position.x = i;
newMesh->verts[ vertPos ].position.z = j;
vertPos++;
}
}
// Move verts equal distances apart, and center mesh
for( int i = 0; i < newMesh->amountOfVerts; i++ )
{
newMesh->verts[ i ].position.x *= segmentWidth;
newMesh->verts[ i ].position.x -= halfWidth;
newMesh->verts[ i ].position.z *= segmentDepth;
newMesh->verts[ i ].position.z -= halfDepth;
}
// Create the textures
newMesh->textures[0].x = 0.0;
newMesh->textures[0].y = 0.0;
newMesh->textures[1].x = 0.0;
newMesh->textures[1].y = 1.0;
newMesh->textures[2].x = 1.0;
newMesh->textures[2].y = 1.0;
newMesh->textures[3].x = 1.0;
newMesh->textures[3].y = 0.0;
int facePos = 0;
// Create the faces
for( int i = 0; i < sections; i++ )
{
for( int j = 0; j < sections; j++ )
{
newMesh->faces[facePos].verts[0] = &newMesh->verts[( i * xMoveDistance ) + ( j * yMoveDistance )];
newMesh->faces[facePos].verts[1] = &newMesh->verts[( i * xMoveDistance ) + ( (j+1) * yMoveDistance )];
newMesh->faces[facePos].verts[2] = &newMesh->verts[( (i+1) * xMoveDistance ) + ( (j+1) * yMoveDistance )];
newMesh->faces[facePos].textures[0] = &newMesh->textures[0];
newMesh->faces[facePos].textures[1] = &newMesh->textures[1];
newMesh->faces[facePos].textures[2] = &newMesh->textures[2];
if( material != NULL )
newMesh->faces[facePos].material = material->name;
facePos++;
newMesh->faces[facePos].verts[0] = &newMesh->verts[( i * xMoveDistance ) + ( j * yMoveDistance )];
newMesh->faces[facePos].verts[1] = &newMesh->verts[( (i+1) * xMoveDistance ) + ( (j+1) * yMoveDistance )];
newMesh->faces[facePos].verts[2] = &newMesh->verts[( (i+1) * xMoveDistance ) + ( j * yMoveDistance )];
newMesh->faces[facePos].textures[0] = &newMesh->textures[0];
newMesh->faces[facePos].textures[1] = &newMesh->textures[2];
newMesh->faces[facePos].textures[2] = &newMesh->textures[3];
if( material != NULL )
newMesh->faces[facePos].material = material->name;
facePos++;
}
}
newMesh->calcSizeAndCenter();
newMesh->calcNormals();
newMesh->setupRenderPackages();
newMesh->updateRenderPackages();
newMesh->loaded = true;
return newMesh;
}
Model* DynamicMesh::generateBox( double w, double h, double d, int sections, std::string material )
{
return NULL;
}
void DynamicMesh::addNoise( double x, double y, double z, Model* model )
{
if( model == NULL )
return;
std::vector<Vertex*>::iterator iter;
for( int i = 0; i < model->amountOfVerts+1; i++ )
{
model->verts[i].position.x += Math::randomDouble( 0.0, x );
model->verts[i].position.y += Math::randomDouble( 0.0, y );
model->verts[i].position.z += Math::randomDouble( 0.0, z );
}
model->calcNormals();
model->calcSizeAndCenter();
model->updateRenderPackages();
}
// Will multiply the modes pixels by a sin wave
void DynamicMesh::addSin( Point3D position, double size, double a, double per, double phs, StaticObject* obj, bool (*DynamicMeshFunction)(Model*) )
{
Model* model = obj->getModel();
Point3D objPos = obj->GetPosition();
bool changedVerts = false;
if( model == NULL )
return;
std::vector<Vertex*>::iterator iter;
for( int i = 0; i < model->amountOfVerts+1; i++ )
{
double distanceFromPos = sqrt( pow( position.x - ( model->verts[i].position.x + objPos.x ), 2.0 ) + pow( position.z - ( model->verts[i].position.z + objPos.z ), 2.0 ) );
if( distanceFromPos > size )
continue;
model->verts[i].position.y = ( a * sin( per * distanceFromPos + phs ) );
changedVerts = true;
}
if( changedVerts )
{
if( DynamicMeshFunction != NULL )
{
if( DynamicMeshFunction( model ) )
model->setupRenderPackages();
}
model->hasBeenAlteredSinceLoad = true;
model->calcNormals();
model->updateRenderPackages();
}
}
void DynamicMesh::addPress( Point3D position, double pressure, double size, double falloff, double damper, StaticObject* obj, bool (*DynamicMeshFunction)(Model*) )
{
if( obj != NULL && obj->getModel() == NULL )
return;
Model* model = obj->getModel();
Point3D objPos = obj->GetPosition();
bool changedVerts = false;
for( int i = 0; i < model->amountOfVerts+1; i++ )
{
double distanceFromPos = sqrt( pow( position.x - ( model->verts[i].position.x + objPos.x ), 2.0 ) + pow( position.z - ( model->verts[i].position.z + objPos.z ), 2.0 ) );
if( distanceFromPos > size || ( pressure - ( distanceFromPos / falloff ) ) < 0 )
continue;
model->verts[i].position.y -= ( ( pressure - ( distanceFromPos / falloff ) ) * damper );
changedVerts = true;
}
if( changedVerts )
{
if( DynamicMeshFunction != NULL )
{
if( DynamicMeshFunction( model ) )
model->setupRenderPackages();
}
model->hasBeenAlteredSinceLoad = true;
model->calcNormals();
model->updateRenderPackages();
}
}
void DynamicMesh::Flatten( Point3D position, double size, double zero, double rate, StaticObject* obj, bool (*DynamicMeshFunction)(Model*) )
{
if( obj != NULL && obj->getModel() == NULL )
return;
Model* model = obj->getModel();
Point3D objPos = obj->GetPosition();
bool changedVerts = false;
for( int i = 0; i < model->amountOfVerts+1; i++ )
{
double distanceFromPos = sqrt( pow( position.x - ( model->verts[i].position.x + objPos.x ), 2.0 ) + pow( position.z - ( model->verts[i].position.z + objPos.z ), 2.0 ) );
if( distanceFromPos > size )
continue;
if( model->verts[i].position.y < zero )
{
model->verts[i].position.y += rate;
if( model->verts[i].position.y > zero )
{
model->verts[i].position.y = zero;
}
}
else if ( model->verts[i].position.y > zero )
{
model->verts[i].position.y -= rate;
if( model->verts[i].position.y < zero )
{
model->verts[i].position.y = zero;
}
}
changedVerts = true;
}
if( changedVerts )
{
if( DynamicMeshFunction != NULL )
{
if( DynamicMeshFunction( model ) )
model->setupRenderPackages();
}
model->hasBeenAlteredSinceLoad = true;
model->calcNormals();
model->updateRenderPackages();
}
}