Tessellator: process all polygons from GeometryBuffer
(not just the first)
This commit is contained in:
parent
a0586a1897
commit
36d540da18
@ -2,19 +2,120 @@ package org.oscim.utils;
|
||||
|
||||
import org.oscim.core.GeometryBuffer;
|
||||
import org.oscim.renderer.elements.VertexItem;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.gwt.core.client.JavaScriptException;
|
||||
import com.google.gwt.core.client.JavaScriptObject;
|
||||
import com.google.gwt.core.client.JsArrayInteger;
|
||||
import com.google.gwt.core.client.JsArrayNumber;
|
||||
import com.google.gwt.core.client.JsArrayUtils;
|
||||
import com.google.gwt.typedarrays.shared.Float32Array;
|
||||
import com.google.gwt.typedarrays.shared.Int32Array;
|
||||
|
||||
public class Tessellator {
|
||||
static final Logger log = LoggerFactory.getLogger(Tessellator.class);
|
||||
|
||||
public static int tessellate(GeometryBuffer geom, float scale,
|
||||
VertexItem outPoints, VertexItem outTris, int vertexOffset) {
|
||||
|
||||
int numIndices = 0;
|
||||
int indexPos = 0;
|
||||
int pointPos = 0;
|
||||
int indexEnd = geom.index.length;
|
||||
|
||||
JsArrayNumber jspoints = JsArrayUtils.readOnlyJsArray(geom.points);
|
||||
JsArrayInteger jsindex = JsArrayUtils.readOnlyJsArray(geom.index);
|
||||
|
||||
for (int idx = 0; idx < indexEnd && geom.index[idx] > 0; idx++) {
|
||||
indexPos = idx;
|
||||
|
||||
int numRings = 1;
|
||||
int numPoints = geom.index[idx++];
|
||||
|
||||
for (; idx < indexEnd && geom.index[idx] > 0; idx++) {
|
||||
numRings++;
|
||||
numPoints += geom.index[idx];
|
||||
}
|
||||
|
||||
if (numPoints <= 0 && numRings == 1){
|
||||
log.debug("tessellation skip empty");
|
||||
pointPos += numPoints;
|
||||
continue;
|
||||
}
|
||||
|
||||
TessResult res;
|
||||
try {
|
||||
res = tessellate2(jspoints, pointPos, numPoints,
|
||||
jsindex, indexPos, numRings);
|
||||
} catch (JavaScriptException e) {
|
||||
e.printStackTrace();
|
||||
return 0;
|
||||
}
|
||||
pointPos += numPoints;
|
||||
|
||||
if (res == null) {
|
||||
log.debug("tessellation failed");
|
||||
continue;
|
||||
}
|
||||
|
||||
Int32Array io = res.getIndices(res);
|
||||
int resIndices = io.length();
|
||||
numIndices += resIndices;
|
||||
|
||||
for (int k = 0, cnt = 0; k < resIndices; k += cnt) {
|
||||
|
||||
if (outTris.used == VertexItem.SIZE) {
|
||||
outTris.next = VertexItem.pool.get();
|
||||
outTris = outTris.next;
|
||||
}
|
||||
|
||||
cnt = VertexItem.SIZE - outTris.used;
|
||||
|
||||
if (k + cnt > resIndices)
|
||||
cnt = resIndices - k;
|
||||
|
||||
for (int i = 0; i < cnt; i++)
|
||||
outTris.vertices[outTris.used + i] =
|
||||
(short) (vertexOffset + io.get(k + i));
|
||||
|
||||
outTris.used += cnt;
|
||||
}
|
||||
|
||||
Float32Array po = res.getPoints(res);
|
||||
int resPoints = po.length();
|
||||
|
||||
vertexOffset += (resPoints >> 1);
|
||||
|
||||
for (int k = 0, cnt = 0; k < resPoints; k += cnt) {
|
||||
|
||||
if (outPoints.used == VertexItem.SIZE) {
|
||||
outPoints.next = VertexItem.pool.get();
|
||||
outPoints = outPoints.next;
|
||||
}
|
||||
|
||||
cnt = VertexItem.SIZE - outPoints.used;
|
||||
|
||||
if (k + cnt > resPoints)
|
||||
cnt = resPoints - k;
|
||||
|
||||
for (int i = 0; i < cnt; i++)
|
||||
outPoints.vertices[outPoints.used + i] =
|
||||
(short) (po.get(k + i) * scale);
|
||||
|
||||
outPoints.used += cnt;
|
||||
}
|
||||
|
||||
if (idx >= indexEnd || geom.index[idx] < 0)
|
||||
break;
|
||||
}
|
||||
|
||||
return numIndices;
|
||||
}
|
||||
|
||||
public static int tessellate(float[] points, int ppos, int plen, short[] index,
|
||||
int ipos, int rings, int vertexOffset, VertexItem outTris) {
|
||||
|
||||
//JavaScriptObject o;
|
||||
Int32Array io;
|
||||
try {
|
||||
io = tessellate(JsArrayUtils.readOnlyJsArray(points), ppos, plen,
|
||||
@ -24,9 +125,6 @@ public class Tessellator {
|
||||
return 0;
|
||||
}
|
||||
|
||||
//Float32Array vo = getPoints(o);
|
||||
//Int32Array io = getIndices(o);
|
||||
|
||||
if (io == null) {
|
||||
//log.debug("building tessellation failed");
|
||||
return 0;
|
||||
@ -72,31 +170,31 @@ public class Tessellator {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static int tessellate(GeometryBuffer geom, float scale,
|
||||
VertexItem outPoints, VertexItem outTris, int vertexOffset) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static native Int32Array tessellate(JsArrayNumber points, int pOffset, int pLength,
|
||||
JsArrayInteger bounds, int bOffset, int bLength)/*-{
|
||||
|
||||
return $wnd.tessellate(points, pOffset, pOffset + pLength, bounds,
|
||||
bOffset, bOffset + bLength);
|
||||
bOffset, bOffset + bLength, false);
|
||||
}-*/;
|
||||
|
||||
// static native JavaScriptObject tessellate(JsArrayNumber points, int pOffset, int pLength,
|
||||
// JsArrayInteger bounds, int bOffset, int bLength)/*-{
|
||||
//
|
||||
// return $wnd.tessellate(points, pOffset, pOffset + pLength, bounds,
|
||||
// bOffset, bOffset + bLength);
|
||||
// }-*/;
|
||||
static native TessResult tessellate2(JsArrayNumber points, int pOffset, int pLength,
|
||||
JsArrayInteger bounds, int bOffset, int bLength)
|
||||
/*-{
|
||||
|
||||
// static native Float32Array getPoints(JavaScriptObject result)/*-{
|
||||
// return result.vertices;
|
||||
// }-*/;
|
||||
return $wnd.tessellate(points, pOffset, pOffset + pLength, bounds,
|
||||
bOffset, bOffset + bLength, true);
|
||||
}-*/;
|
||||
|
||||
// static native Int32Array getIndices(JavaScriptObject result)/*-{
|
||||
// return result.triangles;
|
||||
// }-*/;
|
||||
static final class TessResult extends JavaScriptObject {
|
||||
protected TessResult() {
|
||||
}
|
||||
|
||||
native Float32Array getPoints(JavaScriptObject result)/*-{
|
||||
return result.vertices;
|
||||
}-*/;
|
||||
|
||||
native Int32Array getIndices(JavaScriptObject result)/*-{
|
||||
return result.triangles;
|
||||
}-*/;
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ tessellate = (function() {
|
||||
|
||||
// special tessellator for extrusion layer - only returns triangle indices
|
||||
var tessellate = function(vertices, v_start, v_end, boundaries, b_start,
|
||||
b_end) {
|
||||
b_end, mode) {
|
||||
var i;
|
||||
|
||||
var v_len = (v_end - v_start);
|
||||
@ -44,58 +44,62 @@ tessellate = (function() {
|
||||
var nverts = Module.getValue(pnverts, 'i32');
|
||||
var ntris = Module.getValue(pntris, 'i32');
|
||||
|
||||
// var result_vertices = new Float32Array(nverts * 2);
|
||||
var result_triangles = null;
|
||||
var result_vertices = null;
|
||||
|
||||
if (nverts * 2 == v_len) {
|
||||
if (mode){
|
||||
result_triangles = new Int32Array(ntris * 3);
|
||||
for (i = 0; i < 3 * ntris; ++i)
|
||||
result_triangles[i] = Module.getValue(ptris_out + i * 4, 'i32');
|
||||
|
||||
result_vertices = new Float32Array(nverts * 2);
|
||||
for (i = 0; i < 2 * nverts; ++i)
|
||||
result_vertices[i] = Module.getValue(pcoordinates_out + i * 8, 'double');
|
||||
|
||||
} else {
|
||||
if (nverts * 2 == v_len) {
|
||||
result_triangles = new Int32Array(ntris * 3);
|
||||
|
||||
for (i = 0; i < 3 * ntris; ++i) {
|
||||
result_triangles[i] = Module.getValue(ptris_out + i * 4, 'i32') * 2;
|
||||
}
|
||||
// when a ring has an odd number of points one (or rather two)
|
||||
// additional vertices will be added. so the following rings
|
||||
// needs extra offset...
|
||||
var start = 0;
|
||||
for ( var j = 0, m = b_len - 1; j < m; j++) {
|
||||
start += boundaries[b_start + j];
|
||||
|
||||
// for (i=0; i<2*nverts; ++i) {
|
||||
// result_vertices[i] = Module.getValue(pcoordinates_out + i*8,
|
||||
// 'double');
|
||||
// if (result_vertices[i] != vertices[v_start + i])
|
||||
// console.log("i:" + i + " " + result_vertices[i] + " " +
|
||||
// vertices[v_start + i]);
|
||||
// }
|
||||
// even number of points?
|
||||
if (!((boundaries[b_start + j] >> 1) & 1))
|
||||
continue;
|
||||
|
||||
for (i = 0; i < 3 * ntris; ++i) {
|
||||
result_triangles[i] = Module.getValue(ptris_out + i * 4, 'i32') * 2;
|
||||
}
|
||||
// when a ring has an odd number of points one (or rather two)
|
||||
// additional vertices will be added. so the following rings
|
||||
// needs extra offset...
|
||||
var start = 0;
|
||||
for ( var j = 0, m = b_len - 1; j < m; j++) {
|
||||
start += boundaries[b_start + j];
|
||||
for ( var n = ntris * 3, tri = 0; tri < n; tri++)
|
||||
if (result_triangles[tri] >= start)
|
||||
result_triangles[tri] += 2;
|
||||
|
||||
// even number of points?
|
||||
if (!((boundaries[b_start + j] >> 1) & 1))
|
||||
continue;
|
||||
|
||||
// console.log("shift " + boundaries[b_start + j]);
|
||||
|
||||
for ( var n = ntris * 3, tri = 0; tri < n; tri++)
|
||||
if (result_triangles[tri] >= start)
|
||||
result_triangles[tri] += 2;
|
||||
|
||||
start += 2;
|
||||
}
|
||||
start += 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Module._free(pnverts);
|
||||
Module._free(pntris);
|
||||
|
||||
Module._free(ppcoordinates_out);
|
||||
Module._free(pptris_out);
|
||||
Module._free(pcoordinates_out);
|
||||
|
||||
Module._free(pptris_out);
|
||||
Module._free(ptris_out);
|
||||
|
||||
Module._free(p);
|
||||
Module._free(contours);
|
||||
|
||||
return result_triangles;
|
||||
// return {
|
||||
// vertices: result_vertices,
|
||||
// triangles: result_triangles
|
||||
// };
|
||||
|
||||
if (mode)
|
||||
return { vertices: result_vertices, triangles: result_triangles };
|
||||
else
|
||||
return result_triangles;
|
||||
|
||||
};
|
||||
|
||||
return tessellate;
|
||||
|
@ -125,9 +125,6 @@ public class Tessellator {
|
||||
|
||||
int[] result = new int[2];
|
||||
|
||||
//int numPoints = 0;
|
||||
//for (int i = 0; i < rings; i++)
|
||||
// numPoints += index[ipos + i];
|
||||
int numRings = 0;
|
||||
int numPoints = 0;
|
||||
|
||||
@ -143,10 +140,6 @@ public class Tessellator {
|
||||
geom.index, 0,
|
||||
numRings, result);
|
||||
|
||||
//log.debug("got "
|
||||
// + result[RESULT_VERTICES] + " "
|
||||
// + result[RESULT_TRIANGLES]);
|
||||
|
||||
boolean verticesAdded = false;
|
||||
if (numPoints < result[RESULT_VERTICES] * 2) {
|
||||
//log.debug("grow vertices" + geom.pointPos);
|
||||
@ -190,82 +183,96 @@ public class Tessellator {
|
||||
public static int tessellate(GeometryBuffer geom, float scale,
|
||||
VertexItem outPoints, VertexItem outTris, int vertexOffset) {
|
||||
|
||||
int numIndices = 0;
|
||||
int indexPos = 0;
|
||||
int pointPos = 0;
|
||||
int indexEnd = geom.index.length;
|
||||
|
||||
int[] result = new int[2];
|
||||
|
||||
//int numPoints = 0;
|
||||
//for (int i = 0; i < rings; i++)
|
||||
// numPoints += index[ipos + i];
|
||||
float s = scale;
|
||||
scale = 1;
|
||||
|
||||
int numRings = 0;
|
||||
int numPoints = 0;
|
||||
for (int idx = 0; idx < indexEnd && geom.index[idx] > 0; idx++) {
|
||||
indexPos = idx;
|
||||
|
||||
for (int i = 0; i <= geom.indexPos; i++) {
|
||||
if (geom.index[i] <= 0)
|
||||
break;
|
||||
int numRings = 1;
|
||||
int numPoints = geom.index[idx++];
|
||||
|
||||
numRings++;
|
||||
numPoints += (geom.index[i]) / 2;
|
||||
}
|
||||
for (; idx < indexEnd && geom.index[idx] > 0; idx++) {
|
||||
numRings++;
|
||||
numPoints += geom.index[idx];
|
||||
}
|
||||
|
||||
if (numRings == 0 || numPoints == 0) {
|
||||
log.debug("missing " + numPoints + ":" + numRings);
|
||||
return 0;
|
||||
}
|
||||
for (int i = pointPos; i < pointPos + numPoints; i += 2) {
|
||||
geom.points[i + 0] = (int) (geom.points[i + 0] * s);
|
||||
geom.points[i + 1] = (int) (geom.points[i + 1] * s);
|
||||
}
|
||||
|
||||
long ctx = Tessellator.tessellate(geom.points, 0,
|
||||
geom.index, 0,
|
||||
numRings, result);
|
||||
long ctx = Tessellator.tessellate(geom.points, pointPos,
|
||||
geom.index, indexPos,
|
||||
numRings, result);
|
||||
|
||||
if (numPoints >= result[RESULT_VERTICES]) {
|
||||
// TODO use vertices from geom.points
|
||||
}
|
||||
|
||||
if (outPoints.used == VertexItem.SIZE) {
|
||||
outPoints = VertexItem.pool.getNext(outPoints);
|
||||
}
|
||||
|
||||
int cnt;
|
||||
|
||||
while ((cnt = Tessellator.tessGetVerticesWO(ctx,
|
||||
outPoints.vertices,
|
||||
outPoints.used,
|
||||
scale)) > 0) {
|
||||
outPoints.used += cnt;
|
||||
|
||||
if (outPoints.used == VertexItem.SIZE) {
|
||||
outPoints = VertexItem.pool.getNext(outPoints);
|
||||
if (result[RESULT_VERTICES] == 0 || result[RESULT_TRIANGLES] == 0) {
|
||||
log.debug("ppos " + pointPos + " ipos:" + indexPos +
|
||||
" rings:" + numRings + " " + Arrays.toString(geom.index));
|
||||
continue;
|
||||
}
|
||||
// no more points to get.
|
||||
break;
|
||||
}
|
||||
|
||||
int numIndices = 0;
|
||||
pointPos += numPoints;
|
||||
|
||||
if (outTris.used == VertexItem.SIZE)
|
||||
outTris = VertexItem.pool.getNext(outTris);
|
||||
|
||||
while (true) {
|
||||
int cnt = Tessellator.tessGetIndicesWO(ctx,
|
||||
outTris.vertices,
|
||||
outTris.used);
|
||||
if (cnt <= 0)
|
||||
break;
|
||||
|
||||
// shift by vertexOffset
|
||||
for (int pos = outTris.used, end = pos + cnt; pos < end; pos++)
|
||||
outTris.vertices[pos] += vertexOffset;
|
||||
|
||||
outTris.used += cnt;
|
||||
numIndices += cnt;
|
||||
|
||||
if (outTris.used < VertexItem.SIZE)
|
||||
break;
|
||||
|
||||
if (outTris.used == VertexItem.SIZE) {
|
||||
outTris = VertexItem.pool.getNext(outTris);
|
||||
}
|
||||
|
||||
while ((cnt = Tessellator.tessGetIndicesWO(ctx,
|
||||
outTris.vertices,
|
||||
outTris.used)) > 0) {
|
||||
// shift by vertexOffset
|
||||
for (int pos = outTris.used, end = pos + cnt; pos < end; pos++)
|
||||
outTris.vertices[pos] += vertexOffset;
|
||||
|
||||
outTris.used += cnt;
|
||||
numIndices += cnt;
|
||||
|
||||
if (outTris.used == VertexItem.SIZE) {
|
||||
outTris = VertexItem.pool.getNext(outTris);
|
||||
continue;
|
||||
}
|
||||
|
||||
// no more indices to get.
|
||||
break;
|
||||
if (outPoints.used == VertexItem.SIZE)
|
||||
outPoints = VertexItem.pool.getNext(outPoints);
|
||||
|
||||
while (true) {
|
||||
int cnt = Tessellator.tessGetVerticesWO(ctx,
|
||||
outPoints.vertices,
|
||||
outPoints.used,
|
||||
scale);
|
||||
if (cnt <= 0)
|
||||
break;
|
||||
|
||||
outPoints.used += cnt;
|
||||
vertexOffset += cnt >> 1;
|
||||
|
||||
if (outPoints.used < VertexItem.SIZE)
|
||||
break;
|
||||
|
||||
outPoints = VertexItem.pool.getNext(outPoints);
|
||||
}
|
||||
|
||||
Tessellator.tessFinish(ctx);
|
||||
|
||||
if (idx >= indexEnd || geom.index[idx] < 0)
|
||||
break;
|
||||
}
|
||||
|
||||
Tessellator.tessFinish(ctx);
|
||||
if (vertexOffset > Short.MAX_VALUE) {
|
||||
log.debug("too much !!!" + Arrays.toString(geom.index));
|
||||
}
|
||||
|
||||
return numIndices;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user