From 880d69dc4066acb308a8c6bd91fec096fb166b1c Mon Sep 17 00:00:00 2001 From: benfry Date: Wed, 19 Apr 2006 00:32:04 +0000 Subject: [PATCH] preliminary support for depth sorting of triangles --- core/PConstants.java | 2 +- core/PGraphics3.java | 54 ++++++++++++++++++++++++++++++++++++++--- core/todo.txt | 18 ++++++++++---- opengl/PGraphicsGL.java | 4 +-- todo.txt | 2 ++ 5 files changed, 69 insertions(+), 11 deletions(-) diff --git a/core/PConstants.java b/core/PConstants.java index 6404afd47..7e8a54749 100644 --- a/core/PConstants.java +++ b/core/PConstants.java @@ -293,7 +293,7 @@ public interface PConstants { //static final int DISABLE_SMOOTH_HACK = 4; static final int DISABLE_DEPTH_TEST = 5; static final int NO_FLYING_POO = 6; - static final int DEPTH_SORT = 7; + static final int ENABLE_DEPTH_SORT = 7; static final int HINT_COUNT = 8; diff --git a/core/PGraphics3.java b/core/PGraphics3.java index 3680cf4ff..2dfb263b1 100644 --- a/core/PGraphics3.java +++ b/core/PGraphics3.java @@ -343,7 +343,7 @@ public class PGraphics3 extends PGraphics { // no need to z order and render // shapes were already rendered in endShape(); // (but can't return, since needs to update memimgsrc - if (hints[DEPTH_SORT]) { + if (hints[ENABLE_DEPTH_SORT]) { if (triangleCount > 0) { depth_sort_triangles(); render_triangles(); @@ -387,7 +387,7 @@ public class PGraphics3 extends PGraphics { shape_index = 0; } - if (hints[DEPTH_SORT]) { + if (hints[ENABLE_DEPTH_SORT]) { // continue with previous vertex, line and triangle count // all shapes are rendered at endFrame(); vertex_start = vertexCount; @@ -1041,7 +1041,7 @@ public class PGraphics3 extends PGraphics { // RENDER SHAPES FILLS HERE WHEN NOT DEPTH SORTING // if true, the shapes will be rendered on endFrame - if (!hints[DEPTH_SORT]) { + if (!hints[ENABLE_DEPTH_SORT]) { if (fill) render_triangles(); if (stroke) render_lines(); } @@ -1319,6 +1319,52 @@ public class PGraphics3 extends PGraphics { protected void depth_sort_triangles() { + depth_sort_triangles_internal(0, triangleCount); + } + + + protected void depth_sort_triangles_internal(int i, int j) { + int pivotIndex = (i+j)/2; + depth_sort_triangles_swap(pivotIndex, j); + int k = depth_sort_triangles_partition(i-1, j); + depth_sort_triangles_swap(k, j); + if ((k-i) > 1) depth_sort_triangles_internal(i, k-1); + if ((j-k) > 1) depth_sort_triangles_internal(k+1, j); + } + + + protected int depth_sort_triangles_partition(int left, int right) { + int pivot = right; + do { + while (depth_sort_triangles_compare(++left, pivot) < 0) { } + while ((right != 0) && + (depth_sort_triangles_compare(--right, pivot) > 0)) { } + depth_sort_triangles_swap(left, right); + } while (left < right); + depth_sort_triangles_swap(left, right); + return left; + } + + + protected void depth_sort_triangles_swap(int a, int b) { + int temp[] = triangles[a]; + triangles[a] = triangles[b]; + triangles[b] = temp; + + float tempf[][] = triangleColors[a]; + triangleColors[a] = triangleColors[b]; + triangleColors[b] = tempf; + } + + + protected float depth_sort_triangles_compare(int a, int b) { + return + (vertices[triangles[b][VERTEX1]][Z] + + vertices[triangles[b][VERTEX2]][Z] + + vertices[triangles[b][VERTEX3]][Z]) - + (vertices[triangles[a][VERTEX1]][Z] + + vertices[triangles[a][VERTEX2]][Z] + + vertices[triangles[a][VERTEX3]][Z]); } @@ -1377,6 +1423,8 @@ public class PGraphics3 extends PGraphics { triangle.setIndex(index); triangle.render(); + //System.out.println(i + " " + a[Z] + " " + b[Z] + " " + c[Z]); + if (raw != null) { if (raw instanceof PGraphics3) { if ((a[VW] != 0) && (b[VW] != 0) && (c[VW] != 0)) { diff --git a/core/todo.txt b/core/todo.txt index 1b0dd8ef5..ef443dc51 100644 --- a/core/todo.txt +++ b/core/todo.txt @@ -1,6 +1,14 @@ 0114 core X added createGraphics(width, height, renderer) X no need to use (..., null) anymore +X fix set() for JAVA2D, also fixes background(PImage) for JAVA2D +X http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Syntax;action=display;num=1145108567 +X remove "max texture size" debug message + +_ flicker with depth sort enabled +_ endRaw() is a problem with hint(ENABLE_DEPTH_SORT) +_ because the triangles won't be rendered by the time endRaw() is called +_ _ gray background in pdf (using both gl and p3d) _ http://dev.processing.org/bugs/show_bug.cgi?id=324 @@ -28,12 +36,7 @@ _ pixel operations are broken in opengl _ get(), set(), copy(), blend(), loadPixels, updatePixels() _ set(x, y, image) y reversed in openGL _ http://dev.processing.org/bugs/show_bug.cgi?id=91 -_ opengl needs to shut itself down properly when closing applet -_ otherwise can crash the whole browser _ when closing a sketch via the close box, make sure stop() getting called -_ get rid of some of the sillier IntBuffer stuff where it's not needed -_ use the version of the prototypes that use an offset into the array -_ need to test performance either way examples @@ -667,6 +670,11 @@ _ http://dev.processing.org/bugs/show_bug.cgi?id=127 PGraphicsGL +_ opengl needs to shut itself down properly when closing applet +_ otherwise can crash the whole browser +_ get rid of some of the sillier IntBuffer stuff where it's not needed +_ use the version of the prototypes that use an offset into the array +_ need to test performance either way _ when creating a new PImage on every frame, slurps a ton of memory _ workaround is to write the code properly, but suggests something bad _ http://dev.processing.org/bugs/show_bug.cgi?id=150 diff --git a/opengl/PGraphicsGL.java b/opengl/PGraphicsGL.java index 174334893..5de8b0de4 100644 --- a/opengl/PGraphicsGL.java +++ b/opengl/PGraphicsGL.java @@ -402,7 +402,7 @@ public class PGraphicsGL extends PGraphics3 { report("top endFrame()"); - if (hints[DEPTH_SORT]) { + if (hints[ENABLE_DEPTH_SORT]) { if (triangleCount > 0) { depth_sort_triangles(); render_triangles(); @@ -799,7 +799,7 @@ public class PGraphicsGL extends PGraphics3 { int maxSize[] = new int[1]; gl.glGetIntegerv(GL.GL_MAX_TEXTURE_SIZE, maxSize, 0); maxTextureSize = maxSize[0]; - System.out.println("max texture size is " + maxTextureSize); + //System.out.println("max texture size is " + maxTextureSize); } if ((width2 > maxTextureSize) || (height2 > maxTextureSize)) { /* diff --git a/todo.txt b/todo.txt index 5bf5e0a5d..0be8e2901 100644 --- a/todo.txt +++ b/todo.txt @@ -1,4 +1,6 @@ 0114 pde +X fix "ignoring illegal line...macosx is missing libjogl_cg.jnilib" error msg + _ jar files like the bad aiexport plugin will cause serious problems _ need to ignore processing.core classes showing up in other jar files