Tessellator: process all polygons from GeometryBuffer
(not just the first)
This commit is contained in:
@@ -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;
|
||||
}-*/;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user