Skip to content

Commit

Permalink
Sorter: Fix out of bounds exceptions
Browse files Browse the repository at this point in the history
Fixes processing#3476

1) Arrays were not resized
2) There was a wrong index when rotating right
  • Loading branch information
JakubValtar committed Jul 17, 2015
1 parent a3a32b5 commit 45e4a7d
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions core/src/processing/opengl/PGraphicsOpenGL.java
Expand Up @@ -13562,31 +13562,37 @@ static protected class DepthSorter {
static final int Z = 2;
static final int W = 3;

int[] triangleIndices;
int[] texCacheMap;
int[] indexCacheMap;
int[] triangleIndices = new int[0];
int[] texCacheMap = new int[0];
int[] indexCacheMap = new int[0];

float[] screenVertices;
float[] screenVertices = new float[0];

int[] swapped;
int[] marked;
int[] swapped = new int[8];
int[] marked = new int[8];

PGraphicsOpenGL pg;
TessGeometry tessGeo;

DepthSorter (PGraphicsOpenGL pg) {
this.pg = pg;
}

int triangleCount = 256;

triangleIndices = new int[triangleCount];
texCacheMap = new int[triangleCount];
indexCacheMap = new int[triangleCount];

screenVertices = new float[9*triangleCount];
void checkIndexBuffers(int newTriangleCount) {
if (triangleIndices.length < newTriangleCount) {
int newSize = (newTriangleCount / 4 + 1) * 5;
triangleIndices = new int[newSize];
texCacheMap = new int[newSize];
indexCacheMap = new int[newSize];
}
}

swapped = new int[8];
marked = new int[8];
void checkVertexBuffer(int newVertexCount) {
int coordCount = 3*newVertexCount;
if (screenVertices.length < coordCount) {
int newSize = (coordCount / 4 + 1) * 5;
screenVertices = new float[newSize];
}
}

// Sorting --------------------------------------------
Expand All @@ -13595,13 +13601,13 @@ void sort(TessGeometry tessGeo) {
this.tessGeo = tessGeo;

int triangleCount = tessGeo.polyIndexCount / 3;
checkIndexBuffers(triangleCount);

{ // Map vertices to screen
float[] polyVertices = tessGeo.polyVertices;
int polyVertexCount = tessGeo.polyVertexCount;
if (screenVertices.length < 3*polyVertexCount) {
screenVertices = new float[3*polyVertexCount];
}
checkVertexBuffer(polyVertexCount);

for (int i = 0; i < polyVertexCount; i++) {
float x = polyVertices[4*i+X];
float y = polyVertices[4*i+Y];
Expand Down Expand Up @@ -13826,7 +13832,7 @@ static int[] arrayCheck(int[] array, int size, int requested) {
if (size + requested > array.length) {
int newLength = Math.max(size + requested, 2 * array.length);
int[] temp = new int[newLength];
PApplet.arrayCopy(array, 0, temp, 0, size);
System.arraycopy(array, 0, temp, 0, size);
array = temp;
}
return array;
Expand All @@ -13846,7 +13852,7 @@ static void rotateRight(int[] array, int i1, int i2) {
array[i] = array[i-1];
}
} else {
PApplet.arrayCopy(array, i1, array, i2, i2 - i1);
System.arraycopy(array, i1, array, i1 + 1, i2 - i1);
}
array[i1] = temp;
}
Expand Down

0 comments on commit 45e4a7d

Please sign in to comment.