-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoadMesh.java
303 lines (258 loc) · 12.2 KB
/
RoadMesh.java
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package mygame;
import com.jme3.math.FastMath;
import com.jme3.math.Vector3f;
import com.jme3.scene.Mesh;
import com.jme3.scene.VertexBuffer;
import com.jme3.util.BufferUtils;
import java.util.ArrayList;
import java.util.List;
import static javax.swing.Spring.height;
/**
*
* @author Julien Green
*/
public class RoadMesh extends Mesh {
private float width;
private List<Vector3f> cp;
/**
* Serialization only. Do not use.
*/
public RoadMesh() {
}
/**
* Create a quad with the given width and height. The quad is always created
* in the XY plane.
*
* @param width The X extent or width
* @param controlPoint
*/
public RoadMesh(float width, float height, List<Vector3f> controlPoint) {
updateGeometry(width, height, controlPoint);
}
/**
* Create a quad with the given width and height. The quad is always created
* in the XY plane.
*
* @param width The X extent or width
* @param controlPoint
* @param flipCoords If true, the texture coordinates will be flipped along
* the Y axis.
*/
public RoadMesh(float width, float height, List<Vector3f> controlPoint, boolean flipCoords) {
updateGeometry(width, height, controlPoint, flipCoords, false);
}
/**
* Create a quad with the given width and height. The quad is always created
* in the XY plane.
*
* @param width The X extent or width
* @param height
* @param controlPoint
* @param flipCoords If true, the texture coordinates will be flipped along
* the Y axis.
* @param tessellation Set if to use Tesselation indexation
*/
public RoadMesh(float width, float height, List<Vector3f> controlPoint, boolean flipCoords, boolean tessellation) {
updateGeometry(width, height, controlPoint, flipCoords, tessellation);
}
// public float getHeight() {
// return height;
// }
public float getWidth() {
return width;
}
public void updateGeometry(float width, float height, List<Vector3f> controlPoint) {
updateGeometry(width, height, controlPoint, false, false);
}
@Override
public void clearBuffer(VertexBuffer.Type type) {
super.clearBuffer(type); //To change body of generated methods, choose Tools | Templates.
}
public void updateGeometry(float width, float height, List<Vector3f> controlPoint, boolean flipCoords, boolean tessellation) {
this.width = width;
if (controlPoint.get(0).z > controlPoint.get(3).z) {
cp = new ArrayList();
cp.add(controlPoint.get(3));
cp.add(controlPoint.get(2));
cp.add(controlPoint.get(1));
cp.add(controlPoint.get(0));
} else {
cp = controlPoint;
}
float lenght = FastMath.getBezierP1toP2Length(cp.get(0), cp.get(1), cp.get(2), cp.get(3));
int modulo = (int) (lenght % height);
int nbSection = (int) ((lenght - modulo) / height);
//nbSection = (int) ((lenght) / width)+1;
List<Vector3f> computePosition = new ArrayList();
for (float i = 0; i <= nbSection; i++) {
if (i < nbSection) {
Vector3f v1 = FastMath.interpolateBezier(i / nbSection, cp.get(0), cp.get(1), cp.get(2), cp.get(3));
Vector3f v2 = FastMath.interpolateBezier((i + 1) / nbSection, cp.get(0), cp.get(1), cp.get(2), cp.get(3));
Vector3f vv1 = v2.subtract(v1);
float anglex = FastMath.acos(vv1.x / height);
float anglez = FastMath.asin(vv1.z / height);
float angle1 = anglex - FastMath.HALF_PI;
float angle2 = anglex + FastMath.HALF_PI;
float angle3 = anglez - FastMath.HALF_PI;
float angle4 = anglez + FastMath.HALF_PI;
computePosition.add( v1.add(new Vector3f(FastMath.cos(angle1) * width , 1, FastMath.sin(angle1) * width )));
computePosition.add( v1.add(new Vector3f(FastMath.cos(angle2) * width , 1, FastMath.sin(angle2) * width )));
}
else {
Vector3f v1 = FastMath.interpolateBezier((i) / nbSection, cp.get(0), cp.get(1), cp.get(2), cp.get(3));
Vector3f v2 = FastMath.interpolateBezier((i+1) / nbSection, cp.get(0), cp.get(1), cp.get(2), cp.get(3));
Vector3f vv1 = v2.subtract(v1);
float anglex = FastMath.acos(vv1.x / height);
float anglez = FastMath.asin(vv1.z / height);
float angle1 = anglex - FastMath.HALF_PI;
float angle2 = anglex + FastMath.HALF_PI;
float angle3 = anglez - FastMath.HALF_PI;
float angle4 = anglez + FastMath.HALF_PI;
computePosition.add( v1.add(new Vector3f(FastMath.cos(angle1) * width , 1, FastMath.sin(angle1) * width )));
computePosition.add( v1.add(new Vector3f(FastMath.cos(angle2) * width , 1, FastMath.sin(angle2) * width )));
// computePosition.add(v1);
// computePosition.add(v1);
}
}
// Vector3f v1 = FastMath.interpolateBezier(0 / nbSection, cp.get(0), cp.get(1), cp.get(2), cp.get(3));
// Vector3f v2 = FastMath.interpolateBezier(0.1f / nbSection, cp.get(0), cp.get(1), cp.get(2), cp.get(3));
//
// Vector3f vv1 = v2.subtract(v1);
// float anglex = FastMath.acos(vv1.x / width);
// float anglez = FastMath.asin(vv1.z / width);
//
// float angle1 = anglex - FastMath.HALF_PI;
// float angle2 = anglex + FastMath.HALF_PI;
//
// float angle3 = anglez - FastMath.HALF_PI;
// float angle4 = anglez + FastMath.HALF_PI;
//
// List<Vector3f> bezier1 = new ArrayList();
// bezier1.add(cp.get(0).add(new Vector3f(FastMath.cos(angle1) * (width/2), 1, FastMath.sin(angle3) * (width/2))));
// bezier1.add(cp.get(1).add(new Vector3f(FastMath.cos(angle1) * (width/2), 1, FastMath.sin(angle3) * (width/2))));
// bezier1.add(cp.get(2).add(new Vector3f(FastMath.cos(angle1) * (width/2), 1, FastMath.sin(angle3) * (width/2))));
// bezier1.add(cp.get(3).add(new Vector3f(FastMath.cos(angle1) * (width/2), 1, FastMath.sin(angle3) * (width/2))));
//
// List<Vector3f> bezier2 = new ArrayList();
// bezier2.add(cp.get(0).add(new Vector3f(FastMath.cos(angle2) * (width/2), 1, FastMath.sin(angle4) * (width/2))));
// bezier2.add(cp.get(1).add(new Vector3f(FastMath.cos(angle2) * (width/2), 1, FastMath.sin(angle4) * (width/2))));
// bezier2.add(cp.get(2).add(new Vector3f(FastMath.cos(angle2) * (width/2), 1, FastMath.sin(angle4) * (width/2))));
// bezier2.add(cp.get(3).add(new Vector3f(FastMath.cos(angle2) * (width/2), 1, FastMath.sin(angle4) * (width/2))));
//
// for (float i = 0; i <= nbSection; i++) {
// if(1 < nbSection){
// computePosition.add(FastMath.interpolateBezier(i / nbSection, bezier1.get(0), bezier1.get(1), bezier1.get(2), bezier1.get(3)));
// computePosition.add(FastMath.interpolateBezier(i / nbSection, bezier2.get(0), bezier2.get(1), bezier2.get(2), bezier2.get(3)));
// } else {
//
// }
// }
float[] vertexPosition = new float[(nbSection + 1) * 2 * 3];
float[] vertexTexCoord = new float[nbSection * 4 * 2];
float[] vertexNormalCoord = new float[nbSection * 4 * 3];
int[] vertexIndex = new int[(nbSection) * 2 * 3];
int i = 0;
while (i <= nbSection) {
int inPos = i * 2;
int pos = i * 6;
if (i % 2 == 0) {
vertexPosition[pos + 0] = computePosition.get(inPos).x;
vertexPosition[pos + 1] = computePosition.get(inPos).y;//Height <---- Will use the 3D Interpolation of the Start and End Height
vertexPosition[pos + 2] = computePosition.get(inPos).z;
vertexPosition[pos + 3] = computePosition.get(inPos + 1).x;
vertexPosition[pos + 4] = computePosition.get(inPos + 1).y;//Height <---- Will use the 3D Interpolation of the Start and End Height
vertexPosition[pos + 5] = computePosition.get(inPos + 1).z;
} else {
vertexPosition[pos + 3] = computePosition.get(inPos).x;
vertexPosition[pos + 4] = computePosition.get(inPos).y;//Height <---- Will use the 3D Interpolation of the Start and End Height
vertexPosition[pos + 5] = computePosition.get(inPos).z;
vertexPosition[pos + 0] = computePosition.get(inPos + 1).x;
vertexPosition[pos + 1] = computePosition.get(inPos + 1).y;//Height <---- Will use the 3D Interpolation of the Start and End Height
vertexPosition[pos + 2] = computePosition.get(inPos + 1).z;
}
i++;
}
System.out.println(i);
i = 0;
while (i <= nbSection) {
int pos = i * 4;
if (i % 2 == 0) {
vertexTexCoord[pos + 0] = 0;
vertexTexCoord[pos + 1] = 0;
vertexTexCoord[pos + 2] = 1;
vertexTexCoord[pos + 3] = 0;
} else {
vertexTexCoord[pos + 0] = 1;
vertexTexCoord[pos + 1] = 1;
vertexTexCoord[pos + 2] = 0;
vertexTexCoord[pos + 3] = 1;
}
i++;
}
i = 0;
while (i < nbSection) {
int pos = i * 6;
vertexNormalCoord[pos + 0] = 0;
vertexNormalCoord[pos + 1] = 0;
vertexNormalCoord[pos + 2] = 1;
vertexNormalCoord[pos + 3] = 0;
vertexNormalCoord[pos + 4] = 0;
vertexNormalCoord[pos + 5] = 1;
i++;
}
i = 0;
if (tessellation) {
while (i < nbSection) {
int inPos = i * 2;
int pos = i * 4;
if (i % 2 == 0) {
vertexIndex[pos + 0] = inPos + 0;
vertexIndex[pos + 1] = inPos + 1;
vertexIndex[pos + 2] = inPos + 2;
vertexIndex[pos + 3] = inPos + 3;
} else {
vertexIndex[pos + 0] = inPos + 1;
vertexIndex[pos + 1] = inPos + 0;
vertexIndex[pos + 2] = inPos + 3;
vertexIndex[pos + 3] = inPos + 2;
}
i++;
}
setBuffer(VertexBuffer.Type.Index, 4, vertexIndex);
setMode(Mesh.Mode.Patch);
setPatchVertexCount(4);
} else {
while (i < nbSection) {
int inPos = i * 2;
int pos = i * 6;
if (i % 2 == 0) {
vertexIndex[pos + 0] = inPos + 0;
vertexIndex[pos + 1] = inPos + 1;
vertexIndex[pos + 2] = inPos + 2;
vertexIndex[pos + 3] = inPos + 0;
vertexIndex[pos + 4] = inPos + 2;
vertexIndex[pos + 5] = inPos + 3;
} else {
vertexIndex[pos + 0] = inPos + 0;
vertexIndex[pos + 1] = inPos + 2;
vertexIndex[pos + 2] = inPos + 1;
vertexIndex[pos + 3] = inPos + 0;
vertexIndex[pos + 4] = inPos + 3;
vertexIndex[pos + 5] = inPos + 2;
}
i++;
}
setBuffer(VertexBuffer.Type.Index, 3, vertexIndex);
}
setBuffer(VertexBuffer.Type.Position, 3, vertexPosition);
setBuffer(VertexBuffer.Type.TexCoord, 2, vertexTexCoord);
setBuffer(VertexBuffer.Type.Normal, 3, vertexNormalCoord);
updateBound();
setStatic();
}
}