use glCullFace GL_FRONT to avoid having to flip triangles

triangle-jni: some loop optimizations
This commit is contained in:
Hannes Janetzek 2013-01-04 21:05:27 +01:00
parent 45b19d6a91
commit 35923938db
3 changed files with 44 additions and 58 deletions

View File

@ -41,7 +41,7 @@ jint Java_org_quake_triangle_TriangleJNI_triangulate(JNIEnv *env, jclass c,
mylog(buf);
}
#endif
int num_segments = num_points;
int num_segments = num_points; // - (closed ? (num_rings - 1) : 0);
in.segmentlist = (int *) malloc(num_segments * 2 * sizeof(int));
in.numberofsegments = num_segments;
in.numberofholes = num_rings - 1;
@ -52,8 +52,8 @@ jint Java_org_quake_triangle_TriangleJNI_triangulate(JNIEnv *env, jclass c,
rings = (int*) malloc(num_rings * sizeof(int));
}
int seg = 0;
int hole = 0;
int *seg = in.segmentlist;
float *hole = in.holelist;
int ring;
int point;
@ -69,7 +69,8 @@ jint Java_org_quake_triangle_TriangleJNI_triangulate(JNIEnv *env, jclass c,
// add holes: we need a point inside the hole...
// this is just a heuristic, assuming that two
// 'parallel' lines have a distance of at least
// 1 unit.
// 1 unit. you'll notice when things went wrong
// when the hole is rendered instead of the poly
if (ring > 0)
{
int k = point * 2;
@ -94,22 +95,24 @@ jint Java_org_quake_triangle_TriangleJNI_triangulate(JNIEnv *env, jclass c,
float centerx = cx + vx / 2 - ux;
float centery = cy + vy / 2 - uy;
snprintf(buf, 128, "a: %f in:(%.2f %.2f) "
"cur:(%.2f %.2f), next:(%.2f %.2f)\n",
a, centerx, centery, cx, cy, nx,ny);
mylog(buf);
/* snprintf(buf, 128, "a: %f in:(%.2f %.2f) " */
/* "cur:(%.2f %.2f), next:(%.2f %.2f)\n", */
/* a, centerx, centery, cx, cy, nx,ny); */
/* mylog(buf); */
in.holelist[hole++] = centerx;
in.holelist[hole++] = centery;
*hole++ = centerx;
*hole++ = centery;
}
in.segmentlist[seg++] = point + (num_points - 1);
in.segmentlist[seg++] = point;
//if (!closed){
*seg++ = point + (num_points - 1);
*seg++ = point;
//}
for (len = point + num_points - 1; point < len; point++)
{
in.segmentlist[seg++] = point;
in.segmentlist[seg++] = point + 1;
*seg++ = point;
*seg++ = point + 1;
}
}
#ifdef TESTING
@ -152,24 +155,27 @@ jint Java_org_quake_triangle_TriangleJNI_triangulate(JNIEnv *env, jclass c,
#endif
// ----------- fix offset to vertex buffer indices -------------
// scale to stride
// scale to stride and add offset
short stride = 2;
int n, m;
for (i = 0, n = out.numberoftriangles * 3; i < n; i++)
out.trianglelist[i] *= stride;
// correct offsetting is tricky (and probably not a general case):
// 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...
if (offset < 0)
offset = 0;
short off = offset;
int add = 0;
int start = 0;
INDICE *tri = out.trianglelist;
n = out.numberoftriangles * 3;
while (n-- > 0)
*tri++ = *tri * stride + offset;
// correct offsetting is tricky (but this is probably not a
// general case):
// 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...
int start = offset;
for (j = 0, m = in.numberofholes; j < m; j++)
{
@ -177,35 +183,14 @@ jint Java_org_quake_triangle_TriangleJNI_triangulate(JNIEnv *env, jclass c,
if (rings[j] % 2 == 0)
continue;
#ifdef TESTING
snprintf(buf, 128, "add offset: %d\n", start);
mylog(buf);
#endif
tri = out.trianglelist;
n = out.numberoftriangles * 3;
for (i = 0, n = out.numberoftriangles * 3; i < n; i++)
if (out.trianglelist[i] >= start)
out.trianglelist[i] += stride;
for (;n-- > 0; tri++)
if (*tri >= start)
*tri += stride;
start += stride;
#ifdef TESTING
for (i = 0; i < out.numberoftriangles; i++)
{
snprintf(buf, 128, "> %d, %d, %d\n",out.trianglelist[i*3],
out.trianglelist[i*3+1],
out.trianglelist[i*3+2]);
mylog(buf);
}
#endif
}
// flip direction and add offset
for (i = 0, n = out.numberoftriangles * 3; i < n; i += 3)
{
out.trianglelist[i+0] = out.trianglelist[i+0] + offset;
unsigned short tmp = out.trianglelist[i+1];
out.trianglelist[i+1] = out.trianglelist[i+2] + offset;
out.trianglelist[i+2] = tmp + offset;
}
free(in.segmentlist);

View File

@ -145,8 +145,8 @@ public class ExtrusionLayer extends Layer {
i = 0;
}
indices[i++] = first;
indices[i++] = (short) (first + k + 4);
indices[i++] = (short) (first + k + 2);
indices[i++] = (short) (first + k + 4);
}
mCurIndices[IND_ROOF].used = i;
}
@ -316,12 +316,12 @@ public class ExtrusionLayer extends Layer {
}
indices[ind + 0] = s0;
indices[ind + 1] = s1;
indices[ind + 2] = s2;
indices[ind + 1] = s2;
indices[ind + 2] = s1;
indices[ind + 3] = s1;
indices[ind + 4] = s3;
indices[ind + 5] = s2;
indices[ind + 4] = s2;
indices[ind + 5] = s3;
mCurIndices[even].used += 6;
even = (even + 1) % 2;

View File

@ -184,6 +184,7 @@ public class BuildingOverlay2 extends RenderOverlay {
GLRenderer.enableVertexArrays(hBuildingVertexPosition, -1);
GLES20.glEnable(GLES20.GL_CULL_FACE);
GLES20.glCullFace(GLES20.GL_FRONT);
GLES20.glEnable(GLES20.GL_POLYGON_OFFSET_FILL);
GLES20.glEnable(GLES20.GL_DEPTH_TEST);
GLES20.glDepthFunc(GLES20.GL_LESS);