-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimSurfaceMesh.pde
More file actions
468 lines (364 loc) · 12.8 KB
/
Copy pathSimSurfaceMesh.pde
File metadata and controls
468 lines (364 loc) · 12.8 KB
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
////////////////////////////////////////////////////////////////////////////
// SimSurfaceMesh
// A xdim by zdim mesh of vertices, whihc forms a surface
// Each of these vertices can be given a Y height, so as to form a landscape
//
//
// The mesh can be transformed using setTransformAbs(...) or setTransformRel(....)
// afterwhich you need to call applyTransform() to fix it permanently in the mesh vertices.
//
class SimSurfaceMesh extends SimTransform {
public Mover physics;
int numFacetsX, numFacetsZ;
PVector[] meshVertices;
public int numTriangles = 0;
// mesh coordinates are stored in this array. It is made at the start
//SimRay pick information
public int rayIntersectionTriangleNum;
public PVector rayIntersectionPoint;
PImage textureMap;
public SimSurfaceMesh(int numInX, int numInZ, float scale)
{
// numInX and Z represent the number of Facets generated.
// The number of triangles is (number of Facets)*2
// the number of vertices to make this is (numFacetsX+1) * (numFacetsY+1)
numFacetsX = numInX;
numFacetsZ = numInZ;
//meshVertices = new ArrayList<PVector>();
meshVertices = new PVector[(numFacetsX+1)*(numFacetsZ+1)];
for(int z = 0; z < numFacetsZ+1; z++)
{
for (int x = 0; x < numFacetsX+1; x++)
{
float xf = x * scale;
float yf = 0.0f;
float zf = z * scale;
setMeshVertex(x,z, new PVector(xf, yf, zf) );
}
}
}
void setHeightsFromImage(PImage im, float maxAltitude){
int numInX = getNumVerticesX();
int numInZ = getNumVerticesZ();
int imWidth = im.width;
int imHeight = im.height;
for(int z = 0; z < numInZ; z++)
{
for (int x = 0; x < numInX; x++)
{
int imx = (int) map(x,0,numInX,0,imWidth);
int imy = (int) map(z,0,numInZ,0,imHeight);
color col = im.get(imx,imy);
float hght = map(red(col),0,255,0,maxAltitude);
setVertexY(x,z,hght );
}
}
}
void setTextureMap(PImage t){
textureMap = t.copy();
}
// this permanently applies the transform
void bakeInTransform(){
meshVertices = getTransformedVertices(meshVertices);
setTransformAbs(1.0,0.0,0.0,0.0, vec(0,0,0));
}
boolean intersectsSphere(SimSphere sphere){
for(PVector thisVertex: meshVertices){
PVector transformedVertex = transform(thisVertex);
if( sphere.isPointInside(transformedVertex) ){
swapColliderIDs(sphere);
return true;
}
}
return false;
}
boolean intersectsBox(SimBox box){
for(PVector thisVertex: meshVertices){
PVector transformedVertex = transform(thisVertex);
if( box.isPointInside(transformedVertex) ) {
swapColliderIDs(box);
return true;
}
}
return false;
}
void drawMe() {
if ( textureMap != null) {
drawMe_Texture();
return;
}
int numFacets = getNumFacets();
beginShape(TRIANGLES);
// Center point
for (int i = 0; i < numFacets; i++) {
SimFacet f = getFacet(i);
SimTriangle t1 = f.tri1;
SimTriangle t2 = f.tri2;
// draws ok
drawTransformedVertex(t1.p1);
drawTransformedVertex(t1.p2);
drawTransformedVertex(t1.p3);
// doenst draw
drawTransformedVertex(t2.p1);
drawTransformedVertex(t2.p2);
drawTransformedVertex(t2.p3);
}
endShape();
}
void drawMe_Texture() {
int numFacets = getNumFacets();
beginShape(TRIANGLES);
texture(textureMap);
//g3d.blendMode(REPLACE);
for (int i = 0; i < numFacets; i++) {
SimFacet f = getFacet(i);
SimTriangle t1 = f.tri1;
SimTriangle t2 = f.tri2;
drawTransformedVertex_Texture(t1.p1);
drawTransformedVertex_Texture(t1.p2);
drawTransformedVertex_Texture(t1.p3);
drawTransformedVertex_Texture(t2.p1);
drawTransformedVertex_Texture(t2.p2);
drawTransformedVertex_Texture(t2.p3);
}
endShape();
}
public void drawTransformedVertex_Texture(PVector vertex) {
PVector transformedVector = transform(vertex);
PVector uv = getTextureUV(vertex);
vertex(transformedVector.x, transformedVector.y, transformedVector.z, uv.x, uv.y);
}
PVector getTextureUV(PVector vertex){
int w = textureMap.width;
int h = textureMap.height;
PVector minVertex = getMeshVertex(0, 0);
PVector maxVertex = getMeshVertex(numFacetsX, numFacetsZ);
float u = map(vertex.x, minVertex.x, maxVertex.x, 0, w-1);
float v = map(vertex.z, minVertex.z, maxVertex.z, 0, h-1);
return new PVector(u,v);
}
public boolean collidesWith(SimTransform other){
if(other == this) return false;
String otherClass = getClassName(other);
//println("collidesWith between this ", getClassName(this), " and " , otherClass);
switch(otherClass) {
case "SimSphere":
return intersectsSphere((SimSphere)other);
case "SimBox":
return intersectsBox((SimBox)other);
case "SimSurfaceMesh":
println("SimSurfaceMesh collides with sim surface mesh implemented - use rays");
break;
case "SimModel":
SimTransform boundingGeom = ((SimModel)other).getPreferredBoundingVolume();
return boundingGeom.collidesWith(this);
}
return false;
}
public boolean calcRayIntersection(SimRay sr){
boolean intersectionFound = false;
int numFacets = getNumFacets();
sr.clearIntersectingTriangles();
for (int i = 0; i < numFacets; i++) {
SimFacet f = getTransformedFacet(i);
SimTriangle t1 = f.tri1;
SimTriangle t2 = f.tri2;
if( sr.addIntersectingTriangle(t1) ) intersectionFound = true;
if( sr.addIntersectingTriangle(t2) ) intersectionFound = true;
}
if(intersectionFound){
sr.getNearestTriangleIntersectionPoint();
sr.swapColliderIDs(this);
//println("camera", getCameraPosition(),"grid hit",sr.intersectionPoint);
}
return intersectionFound;
}
////////////////////////////////////////////////
public void setVertexY(int vertexX, int vertexZ, float y){
// there are meshSizeX+1, meshSizeY+1 vertices in this messh
int vertexGridWidth = numFacetsX + 1;
int index = vertexZ * vertexGridWidth + vertexX;
PVector p = meshVertices[index];
p.y = y;
meshVertices[index] = p;
}
public PVector getMeshVertex(int vertexX, int vertexZ){
int vertexGridWidth = numFacetsX + 1;
int index = vertexZ * vertexGridWidth + vertexX;
return meshVertices[index];
}
public void setMeshVertex(int vertexX, int vertexZ, PVector v){
int vertexGridWidth = numFacetsX + 1;
int index = vertexZ * vertexGridWidth + vertexX;
meshVertices[index] = v;
}
/////////////////////////////////////////////
// private below here
int getNumFacets(){
return (numFacetsX)* (numFacetsZ);
}
int getNumTriangles(){
return (numFacetsX)* (numFacetsZ)*2;
}
private int getNumVerticesX(){ return numFacetsX+1;}
private int getNumVerticesZ(){ return numFacetsZ+1;}
SimFacet getTransformedFacet(int index){
SimFacet facet = getFacet( index);
PVector[] verts = facet.getVertices();
PVector[] transformedVerts = new PVector[4];
for(int n = 0; n < 4; n++) transformedVerts[n] = transform(verts[n]);
return new SimFacet(transformedVerts[0],transformedVerts[1],transformedVerts[2],transformedVerts[3]);
}
SimFacet getFacet(int index){
//the vertices under consideration
// A B
// C D
// as indices into the meshVertices array
int vertextGridWidth = numFacetsX+1;
int rowNum = (int)(index/numFacetsX);
int A = index + rowNum;
int B = A + 1;
int C = A + vertextGridWidth;
int D = C + 1;
//println("index ", index, "row ", rowNum," vertices nums ", A,B,C,D);
SimFacet facet = new SimFacet();
// triangle 1
facet.tri1.p1 = meshVertices[D];
facet.tri1.p2 = meshVertices[B];
facet.tri1.p3 = meshVertices[A];
// triangle 2
facet.tri2.p1 = meshVertices[D];
facet.tri2.p2 = meshVertices[A];
facet.tri2.p3 = meshVertices[C];
return facet;
}
SimFacet getFacet(int x, int z)
{
int vertexGridWidth = numFacetsX + 1;
int index = z * vertexGridWidth + x;
return getFacet(index);
}
}// end SimSurfaceMesh class
////////////////////////////////////////////////////////////////////////////
// SimTriangle
// simple containter for a 2d or 3d triange
//
class SimTriangle{
public PVector p1,p2,p3;
public SimTriangle(PVector p1, PVector p2, PVector p3){
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
}
public SimTriangle(){
this.p1 = new PVector(0,0,0);
this.p2 = new PVector(0,0,0);
this.p3 = new PVector(0,0,0);
}
void flip(){
// flips the direction from CW to CCW or visa-versa
PVector oldP2 = this.p2.copy();
PVector oldP3 = this.p3.copy();
this.p2 = oldP3;
this.p3 = oldP2;
// p1 stays the same
}
void printMe(){
println("triange:",p1,p2,p3);
}
void drawMe(){
beginShape(TRIANGLE);
vertex(this.p1.x,this.p1.y,this.p1.z);
vertex(this.p2.x,this.p2.y,this.p2.z);
vertex(this.p3.x,this.p3.y,this.p3.z);
endShape(CLOSE);
}
PVector surfaceNormal(){
PVector edgep1p2 = PVector.sub(p2,p1);
PVector edgep1p3 = PVector.sub(p3,p1);
PVector cross = edgep1p2.cross(edgep1p3);
cross.y *= -1;
return cross;
}
/*
boolean isBackFacing(){
PVector sn = surfaceNormal();
PVector cameraPos = getCameraPosition();
if( sn.dot( cameraPos.sub(p1) ) > 0.0 ) return true;
return false;
}
*/
public boolean calcRayIntersection(SimRay ray)
{
//MOLLER_TRUMBORE algorithm
ray.intersectionPoint = null;
// make local copies so we don't change anything ouside the function
PVector dir = ray.direction.copy();
PVector orig = ray.origin.copy();
PVector v0 = this.p1.copy();
PVector v1 = this.p2.copy();
PVector v2 = this.p3.copy();
PVector edge_v0v1 = v1.sub(v0);
PVector edge_v0v2 = v2.sub(v0);
PVector pvec = dir.cross(edge_v0v2);
float det = edge_v0v1.dot(pvec);
if( nearZero(det) ){
// ray is parallel with triangle plane
// this ignores the direction of triangle winding
return false;
}
float invDet = 1.0/det;
PVector tvec = PVector.sub(orig,v0);
float u = tvec.dot(pvec) * invDet;
if( u < 0 || u > 1) {
return false;
}
PVector qvec = tvec.cross(edge_v0v1);
float v = dir.dot(qvec) * invDet;
if(v < 0 || u + v > 1){
return false;
}
float t = edge_v0v2.dot(qvec) * invDet;
if(t < EPSILON){
// line intersection, not ray intersection...
// to avoid hitting a point behind the camera
return false;
}
PVector addToOrigin = PVector.mult(dir,t);
ray.intersectionPoint = PVector.add(orig,addToOrigin);
// if(isBackFacing()){
// //println("is back facing");
// //return false;
// }
return true;
}
}
////////////////////////////////////////////////////////////////////////////
// SimFacet
// Two triangles ake a facet
//
class SimFacet{
public SimTriangle tri1;
public SimTriangle tri2;
public SimFacet(){
tri1 = new SimTriangle();
tri2 = new SimTriangle();
}
public SimFacet(PVector p1, PVector p2, PVector p3, PVector p4){
setVertices(p1,p2,p3,p4);
}
void setVertices( PVector p1, PVector p2, PVector p3, PVector p4){
// give 4 vertices of a facet, in either winding, create a correct 2-triangle facet
// currently cannot handle "butterfly" quads
tri1 = new SimTriangle(p1,p2,p3);
tri2 = new SimTriangle(p1,p3,p4);
}
PVector[] getVertices(){
PVector[] verts = new PVector[4];
verts[0] = tri1.p1.copy();//p1
verts[1] = tri1.p2.copy();//p2
verts[2] = tri1.p3.copy();//p3
verts[3] = tri2.p3.copy();//p4
return verts;
}
}