|
|
|
@ -20,6 +20,7 @@ import com.jme3.util.BufferUtils; |
|
|
|
|
* @author Marcin Roguski (Kealthas) |
|
|
|
|
*/ |
|
|
|
|
public class Surface extends Mesh { |
|
|
|
|
|
|
|
|
|
private SplineType type; //the type of the surface
|
|
|
|
|
private List<List<Vector4f>> controlPoints; //space control points and their weights
|
|
|
|
|
private List<Float>[] knots; //knots of the surface
|
|
|
|
@ -47,7 +48,7 @@ public class Surface extends Mesh { |
|
|
|
|
this.knots = nurbKnots; |
|
|
|
|
this.basisUFunctionDegree = basisUFunctionDegree; |
|
|
|
|
CurveAndSurfaceMath.prepareNurbsKnots(nurbKnots[0], basisUFunctionDegree); |
|
|
|
|
if(nurbKnots[1]!=null) { |
|
|
|
|
if (nurbKnots[1] != null) { |
|
|
|
|
this.basisVFunctionDegree = basisVFunctionDegree; |
|
|
|
|
CurveAndSurfaceMath.prepareNurbsKnots(nurbKnots[1], basisVFunctionDegree); |
|
|
|
|
} |
|
|
|
@ -79,19 +80,19 @@ public class Surface extends Mesh { |
|
|
|
|
boolean smooth = true;//TODO: take smoothing into consideration
|
|
|
|
|
float minUKnot = this.getMinUNurbKnot(); |
|
|
|
|
float maxUKnot = this.getMaxUNurbKnot(); |
|
|
|
|
float deltaU = (maxUKnot - minUKnot)/uSegments; |
|
|
|
|
float deltaU = (maxUKnot - minUKnot) / uSegments; |
|
|
|
|
|
|
|
|
|
float minVKnot = this.getMinVNurbKnot(); |
|
|
|
|
float maxVKnot = this.getMaxVNurbKnot(); |
|
|
|
|
float deltaV = (maxVKnot - minVKnot)/vSegments; |
|
|
|
|
float deltaV = (maxVKnot - minVKnot) / vSegments; |
|
|
|
|
|
|
|
|
|
Vector3f[] vertices = new Vector3f[(uSegments + 1) * (vSegments + 1)]; |
|
|
|
|
|
|
|
|
|
float u = minUKnot, v = minVKnot; |
|
|
|
|
int arrayIndex = 0; |
|
|
|
|
|
|
|
|
|
for(int i=0;i<=vSegments; ++i) { |
|
|
|
|
for(int j=0;j<=uSegments; ++j) { |
|
|
|
|
for (int i = 0; i <= vSegments; ++i) { |
|
|
|
|
for (int j = 0; j <= uSegments; ++j) { |
|
|
|
|
Vector3f interpolationResult = new Vector3f(); |
|
|
|
|
CurveAndSurfaceMath.interpolate(u, v, controlPoints, knots, basisUFunctionDegree, basisVFunctionDegree, interpolationResult); |
|
|
|
|
vertices[arrayIndex++] = interpolationResult; |
|
|
|
@ -105,27 +106,27 @@ public class Surface extends Mesh { |
|
|
|
|
int uVerticesAmount = uSegments + 1; |
|
|
|
|
int[] indices = new int[uSegments * vSegments * 6]; |
|
|
|
|
arrayIndex = 0; |
|
|
|
|
for(int i=0;i<vSegments; ++i) { |
|
|
|
|
for(int j=0;j<uSegments; ++j) { |
|
|
|
|
indices[arrayIndex++] = j + i*uVerticesAmount; |
|
|
|
|
indices[arrayIndex++] = j + i*uVerticesAmount + 1; |
|
|
|
|
indices[arrayIndex++] = j + i*uVerticesAmount + uVerticesAmount; |
|
|
|
|
indices[arrayIndex++] = j + i*uVerticesAmount + 1; |
|
|
|
|
indices[arrayIndex++] = j + i*uVerticesAmount + uVerticesAmount + 1; |
|
|
|
|
indices[arrayIndex++] = j + i*uVerticesAmount + uVerticesAmount; |
|
|
|
|
for (int i = 0; i < vSegments; ++i) { |
|
|
|
|
for (int j = 0; j < uSegments; ++j) { |
|
|
|
|
indices[arrayIndex++] = j + i * uVerticesAmount; |
|
|
|
|
indices[arrayIndex++] = j + i * uVerticesAmount + 1; |
|
|
|
|
indices[arrayIndex++] = j + i * uVerticesAmount + uVerticesAmount; |
|
|
|
|
indices[arrayIndex++] = j + i * uVerticesAmount + 1; |
|
|
|
|
indices[arrayIndex++] = j + i * uVerticesAmount + uVerticesAmount + 1; |
|
|
|
|
indices[arrayIndex++] = j + i * uVerticesAmount + uVerticesAmount; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//normalMap merges normals of faces that will be rendered smooth
|
|
|
|
|
Map<Vector3f, Vector3f> normalMap = new HashMap<Vector3f, Vector3f>(vertices.length); |
|
|
|
|
for(int i=0;i<indices.length;i+=3) { |
|
|
|
|
for (int i = 0; i < indices.length; i += 3) { |
|
|
|
|
Vector3f n = FastMath.computeNormal(vertices[indices[i]], vertices[indices[i + 1]], vertices[indices[i + 2]]); |
|
|
|
|
this.addNormal(n, normalMap, smooth, vertices[indices[i]], vertices[indices[i + 1]], vertices[indices[i + 2]]); |
|
|
|
|
} |
|
|
|
|
//preparing normal list (the order of normals must match the order of vertices)
|
|
|
|
|
float[] normals = new float[vertices.length * 3]; |
|
|
|
|
arrayIndex = 0; |
|
|
|
|
for(int i=0;i<vertices.length;++i) { |
|
|
|
|
for (int i = 0; i < vertices.length; ++i) { |
|
|
|
|
Vector3f n = normalMap.get(vertices[i]); |
|
|
|
|
normals[arrayIndex++] = n.x; |
|
|
|
|
normals[arrayIndex++] = n.y; |
|
|
|
@ -156,7 +157,7 @@ public class Surface extends Mesh { |
|
|
|
|
* @return the amount of V control points |
|
|
|
|
*/ |
|
|
|
|
public int getVControlPointsAmount() { |
|
|
|
|
return controlPoints.get(0)==null ? 0 : controlPoints.get(0).size(); |
|
|
|
|
return controlPoints.get(0) == null ? 0 : controlPoints.get(0).size(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -237,9 +238,9 @@ public class Surface extends Mesh { |
|
|
|
|
* a list of vertices read from the blender file |
|
|
|
|
*/ |
|
|
|
|
private void addNormal(Vector3f normalToAdd, Map<Vector3f, Vector3f> normalMap, boolean smooth, Vector3f... vertices) { |
|
|
|
|
for(Vector3f v : vertices) { |
|
|
|
|
for (Vector3f v : vertices) { |
|
|
|
|
Vector3f n = normalMap.get(v); |
|
|
|
|
if(!smooth || n == null) { |
|
|
|
|
if (!smooth || n == null) { |
|
|
|
|
normalMap.put(v, normalToAdd.clone()); |
|
|
|
|
} else { |
|
|
|
|
n.addLocal(normalToAdd).normalizeLocal(); |
|
|
|
@ -258,15 +259,15 @@ public class Surface extends Mesh { |
|
|
|
|
private void validateInputData(List<List<Vector4f>> controlPoints, List<Float>[] nurbKnots, |
|
|
|
|
int uSegments, int vSegments) { |
|
|
|
|
int uPointsAmount = controlPoints.get(0).size(); |
|
|
|
|
for(int i=1;i<controlPoints.size();++i) { |
|
|
|
|
if(controlPoints.get(i).size()!=uPointsAmount) { |
|
|
|
|
for (int i = 1; i < controlPoints.size(); ++i) { |
|
|
|
|
if (controlPoints.get(i).size() != uPointsAmount) { |
|
|
|
|
throw new IllegalArgumentException("The amount of 'U' control points is invalid!"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if(uSegments<=0) { |
|
|
|
|
if (uSegments <= 0) { |
|
|
|
|
throw new IllegalArgumentException("U segments amount should be positive!"); |
|
|
|
|
} |
|
|
|
|
if(vSegments<0) { |
|
|
|
|
if (vSegments < 0) { |
|
|
|
|
throw new IllegalArgumentException("V segments amount cannot be negative!"); |
|
|
|
|
} |
|
|
|
|
if (nurbKnots.length != 2) { |
|
|
|
@ -274,7 +275,7 @@ public class Surface extends Mesh { |
|
|
|
|
} |
|
|
|
|
for (int i = 0; i < nurbKnots.length; ++i) { |
|
|
|
|
for (int j = 0; j < nurbKnots[i].size() - 1; ++j) { |
|
|
|
|
if (nurbKnots[i].get(j) > nurbKnots[i].get(j+1)) { |
|
|
|
|
if (nurbKnots[i].get(j) > nurbKnots[i].get(j + 1)) { |
|
|
|
|
throw new IllegalArgumentException("The knots' values cannot decrease!"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|