html: tessellate, only use triangle indices

This commit is contained in:
Hannes Janetzek 2013-07-04 07:02:09 +02:00
parent aea7cf2a5f
commit 90ef5c26e9
2 changed files with 113 additions and 93 deletions

View File

@ -4,11 +4,9 @@ import org.oscim.backend.Log;
import org.oscim.renderer.sublayers.VertexItem; import org.oscim.renderer.sublayers.VertexItem;
import com.google.gwt.core.client.JavaScriptException; 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.JsArrayInteger;
import com.google.gwt.core.client.JsArrayNumber; import com.google.gwt.core.client.JsArrayNumber;
import com.google.gwt.core.client.JsArrayUtils; import com.google.gwt.core.client.JsArrayUtils;
import com.google.gwt.typedarrays.shared.Float32Array;
import com.google.gwt.typedarrays.shared.Int32Array; import com.google.gwt.typedarrays.shared.Int32Array;
public class Triangulator { public class Triangulator {
@ -16,32 +14,36 @@ public class Triangulator {
public static synchronized int triangulate(float[] points, int ppos, int plen, short[] index, public static synchronized int triangulate(float[] points, int ppos, int plen, short[] index,
int ipos, int rings, int vertexOffset, VertexItem outTris) { int ipos, int rings, int vertexOffset, VertexItem outTris) {
JavaScriptObject o; //JavaScriptObject o;
Int32Array io;
try{ try{
o = tessellate(JsArrayUtils.readOnlyJsArray(points), ppos, plen, io = tessellate(JsArrayUtils.readOnlyJsArray(points), ppos, plen,
JsArrayUtils.readOnlyJsArray(index), ipos, rings); JsArrayUtils.readOnlyJsArray(index), ipos, rings);
} catch(JavaScriptException e){ } catch(JavaScriptException e){
e.printStackTrace(); e.printStackTrace();
return 0; return 0;
} }
Float32Array vo = getPoints(o); //Float32Array vo = getPoints(o);
Int32Array io = getIndices(o); //Int32Array io = getIndices(o);
if (vo.length() != plen) {
// TODO handle different output points
Log.d("", "other points out" + plen + ":" + vo.length() + ", " + io.length());
//for (int i = 0; i < vo.length(); i += 2)
// Log.d("<", vo.get(i) + " " + vo.get(i + 1));
//for (int i = ppos; i < ppos + plen; i += 2)
// Log.d(">", points[i]+ " " + points[i + 1]);
if (io == null){
Log.d("Triangulator", "building tessellation failed");
return 0; return 0;
} }
// if (vo.length() != plen) {
// // TODO handle different output points
// Log.d("", "other points out" + plen + ":" + vo.length() + ", " + io.length());
//
// //for (int i = 0; i < vo.length(); i += 2)
// // Log.d("<", vo.get(i) + " " + vo.get(i + 1));
// //for (int i = ppos; i < ppos + plen; i += 2)
// // Log.d(">", points[i]+ " " + points[i + 1]);
//
// return 0;
// }
int numIndices = io.length(); int numIndices = io.length();
for (int k = 0, cnt = 0; k < numIndices; k += cnt) { for (int k = 0, cnt = 0; k < numIndices; k += cnt) {
@ -66,19 +68,26 @@ public class Triangulator {
return numIndices; return numIndices;
} }
static native JavaScriptObject tessellate(JsArrayNumber points, int pOffset, int pLength, static native Int32Array tessellate(JsArrayNumber points, int pOffset, int pLength,
JsArrayInteger bounds, int bOffset, int bLength)/*-{ JsArrayInteger bounds, int bOffset, int bLength)/*-{
return $wnd.tessellate(points, pOffset, pOffset + pLength, bounds, return $wnd.tessellate(points, pOffset, pOffset + pLength, bounds,
bOffset, bOffset + bLength); bOffset, bOffset + bLength);
}-*/; }-*/;
static native Float32Array getPoints(JavaScriptObject result)/*-{ // static native JavaScriptObject tessellate(JsArrayNumber points, int pOffset, int pLength,
return result.vertices; // JsArrayInteger bounds, int bOffset, int bLength)/*-{
}-*/; //
// return $wnd.tessellate(points, pOffset, pOffset + pLength, bounds,
// bOffset, bOffset + bLength);
// }-*/;
static native Int32Array getIndices(JavaScriptObject result)/*-{ // static native Float32Array getPoints(JavaScriptObject result)/*-{
return result.triangles; // return result.vertices;
}-*/; // }-*/;
// static native Int32Array getIndices(JavaScriptObject result)/*-{
// return result.triangles;
// }-*/;
} }

View File

@ -2,10 +2,12 @@ tessellate = (function() {
Module.TOTAL_MEMORY = 1024 * 1024; Module.TOTAL_MEMORY = 1024 * 1024;
var c_tessellate = Module.cwrap('tessellate', 'void', ['number', 'number', 'number', var c_tessellate = Module.cwrap('tessellate', 'void', [ 'number', 'number',
'number', 'number', 'number']); 'number', 'number', 'number', 'number' ]);
// special tessellator for extrusion layer
var tessellate = function(vertices, v_start, v_end, boundaries, b_start, b_end) { // special tessellator for extrusion layer - only returns triangle indices
var tessellate = function(vertices, v_start, v_end, boundaries, b_start,
b_end) {
var i; var i;
var v_len = (v_end - v_start); var v_len = (v_end - v_start);
@ -33,8 +35,8 @@ var tessellate = function(vertices, v_start, v_end, boundaries, b_start, b_end)
var pnverts = Module._malloc(4); var pnverts = Module._malloc(4);
var pntris = Module._malloc(4); var pntris = Module._malloc(4);
c_tessellate(ppcoordinates_out, pnverts, pptris_out, pntris, c_tessellate(ppcoordinates_out, pnverts, pptris_out, pntris, contours,
contours, contours+4*(b_len + 1)); contours + 4 * (b_len + 1));
var pcoordinates_out = Module.getValue(ppcoordinates_out, 'i32'); var pcoordinates_out = Module.getValue(ppcoordinates_out, 'i32');
var ptris_out = Module.getValue(pptris_out, 'i32'); var ptris_out = Module.getValue(pptris_out, 'i32');
@ -42,14 +44,20 @@ var tessellate = function(vertices, v_start, v_end, boundaries, b_start, b_end)
var nverts = Module.getValue(pnverts, 'i32'); var nverts = Module.getValue(pnverts, 'i32');
var ntris = Module.getValue(pntris, 'i32'); var ntris = Module.getValue(pntris, 'i32');
var result_vertices = new Float32Array(nverts * 2); // var result_vertices = new Float32Array(nverts * 2);
var result_triangles = new Int32Array(ntris * 3); var result_triangles = null;
if (nverts * 2 == v_len) {
result_triangles = new Int32Array(ntris * 3);
// 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]);
// }
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]);
}
for (i = 0; i < 3 * ntris; ++i) { for (i = 0; i < 3 * ntris; ++i) {
result_triangles[i] = Module.getValue(ptris_out + i * 4, 'i32') * 2; result_triangles[i] = Module.getValue(ptris_out + i * 4, 'i32') * 2;
} }
@ -64,7 +72,7 @@ var tessellate = function(vertices, v_start, v_end, boundaries, b_start, b_end)
if (!((boundaries[b_start + j] >> 1) & 1)) if (!((boundaries[b_start + j] >> 1) & 1))
continue; continue;
console.log("shift " + boundaries[b_start + j]); // console.log("shift " + boundaries[b_start + j]);
for ( var n = ntris * 3, tri = 0; tri < n; tri++) for ( var n = ntris * 3, tri = 0; tri < n; tri++)
if (result_triangles[tri] >= start) if (result_triangles[tri] >= start)
@ -72,6 +80,7 @@ var tessellate = function(vertices, v_start, v_end, boundaries, b_start, b_end)
start += 2; start += 2;
} }
}
Module._free(pnverts); Module._free(pnverts);
Module._free(pntris); Module._free(pntris);
@ -81,10 +90,12 @@ var tessellate = function(vertices, v_start, v_end, boundaries, b_start, b_end)
Module._free(ptris_out); Module._free(ptris_out);
Module._free(p); Module._free(p);
Module._free(contours); Module._free(contours);
return {
vertices: result_vertices, return result_triangles;
triangles: result_triangles // return {
}; // vertices: result_vertices,
// triangles: result_triangles
// };
}; };
return tessellate; return tessellate;