use of org.sunflow.core.primitive.QuadMesh in project joons-renderer by joonhyublee.
the class BezierMesh method tesselate.
public PrimitiveList tesselate() {
float[] vertices = new float[patches.length * (subdivs + 1) * (subdivs + 1) * 3];
float[] normals = smooth ? new float[patches.length * (subdivs + 1) * (subdivs + 1) * 3] : null;
float[] uvs = new float[patches.length * (subdivs + 1) * (subdivs + 1) * 2];
int[] indices = new int[patches.length * subdivs * subdivs * (quads ? 4 : (2 * 3))];
int vidx = 0, pidx = 0;
float step = 1.0f / subdivs;
int vstride = subdivs + 1;
Point3 p = new Point3();
Vector3 n = smooth ? new Vector3() : null;
for (float[] patch : patches) {
// create patch vertices
for (int i = 0, voff = 0; i <= subdivs; i++) {
float u = i * step;
float[] bu = bernstein(u);
float[] bdu = bernsteinDeriv(u);
for (int j = 0; j <= subdivs; j++, voff += 3) {
float v = j * step;
float[] bv = bernstein(v);
float[] bdv = bernsteinDeriv(v);
getPatchPoint(u, v, patch, bu, bv, bdu, bdv, p, n);
vertices[vidx + voff + 0] = p.x;
vertices[vidx + voff + 1] = p.y;
vertices[vidx + voff + 2] = p.z;
if (smooth) {
normals[vidx + voff + 0] = n.x;
normals[vidx + voff + 1] = n.y;
normals[vidx + voff + 2] = n.z;
}
uvs[(vidx + voff) / 3 * 2 + 0] = u;
uvs[(vidx + voff) / 3 * 2 + 1] = v;
}
}
// generate patch triangles
for (int i = 0, vbase = vidx / 3; i < subdivs; i++) {
for (int j = 0; j < subdivs; j++) {
int v00 = (i + 0) * vstride + (j + 0);
int v10 = (i + 1) * vstride + (j + 0);
int v01 = (i + 0) * vstride + (j + 1);
int v11 = (i + 1) * vstride + (j + 1);
if (quads) {
indices[pidx + 0] = vbase + v01;
indices[pidx + 1] = vbase + v00;
indices[pidx + 2] = vbase + v10;
indices[pidx + 3] = vbase + v11;
pidx += 4;
} else {
// add 2 triangles
indices[pidx + 0] = vbase + v00;
indices[pidx + 1] = vbase + v10;
indices[pidx + 2] = vbase + v01;
indices[pidx + 3] = vbase + v10;
indices[pidx + 4] = vbase + v11;
indices[pidx + 5] = vbase + v01;
pidx += 6;
}
}
}
vidx += vstride * vstride * 3;
}
ParameterList pl = new ParameterList();
pl.addPoints("points", InterpolationType.VERTEX, vertices);
if (quads) {
pl.addIntegerArray("quads", indices);
} else {
pl.addIntegerArray("triangles", indices);
}
pl.addTexCoords("uvs", InterpolationType.VERTEX, uvs);
if (smooth) {
pl.addVectors("normals", InterpolationType.VERTEX, normals);
}
PrimitiveList m = quads ? new QuadMesh() : new TriangleMesh();
m.update(pl, null);
pl.clear(true);
return m;
}
Aggregations