Skip to content

Commit 066722c

Browse files
committed
Ported GLSL renderering from PGraphicsAndroid3D to PGraphicsOpenGL, several fixes
1 parent 87e0aca commit 066722c

23 files changed

Lines changed: 3917 additions & 3995 deletions

android/core/src/processing/core/PGL.java

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import java.nio.Buffer;
2727
import java.nio.ByteBuffer;
28+
import java.nio.IntBuffer;
2829

2930
import javax.microedition.khronos.egl.EGL10;
3031
import javax.microedition.khronos.egl.EGLConfig;
@@ -380,8 +381,8 @@ public boolean contextIsCurrent(Context other) {
380381
}
381382

382383
static public short makeIndex(int intIdx) {
383-
// When the index value is greater than 32767 subtracting 65536
384-
// will make it as a short to wrap around to the negative range, which
384+
// When the index value is greater than 32767, subtracting 65536
385+
// will make it (as a short) to wrap around to the negative range, which
385386
// is all we need to later pass these numbers to opengl (which will
386387
// interpret them as unsigned shorts). See discussion here:
387388
// http://stackoverflow.com/questions/4331021/java-opengl-gldrawelements-with-32767-vertices
@@ -396,6 +397,30 @@ public void disableTexturing(int target) {
396397
//gl.glDisable(target);
397398
}
398399

400+
public void initTexture(int target, int width, int height, int format, int type) {
401+
// Doing in patches of 16x16 pixels to avoid creating a (potentially)
402+
// very large transient array which in certain situations (memory-
403+
// constrained android devices) might lead to an out-of-memory error.
404+
int[] texels = new int[16 * 16];
405+
for (int y = 0; y < height + 16; y += 16) {
406+
int h = PApplet.min(16, height - y);
407+
for (int x = 0; x < width + 16; x += 16) {
408+
int w = PApplet.min(16, width - x);
409+
gl.glTexSubImage2D(target, 0, x, y, w, h, format, type, IntBuffer.wrap(texels));
410+
}
411+
}
412+
}
413+
414+
public String getShaderLog(int id) {
415+
int[] compiled = new int[1];
416+
GLES20.glGetShaderiv(id, GLES20.GL_COMPILE_STATUS, compiled, 0);
417+
if (compiled[0] == 0) {
418+
return GLES20.glGetShaderInfoLog(id);
419+
} else {
420+
return "";
421+
}
422+
}
423+
399424
///////////////////////////////////////////////////////////////////////////////////
400425

401426
// Frame rendering
@@ -660,12 +685,12 @@ public void glVertexAttribPointer(int loc, int size, int type, boolean normalize
660685
}
661686

662687

663-
public ByteBuffer glMapBuffer(int target, int acccess) {
688+
public ByteBuffer glMapBuffer(int target, int access) {
664689
//return gl2f.glMapBuffer(GL.GL_ARRAY_BUFFER, GL2.GL_READ_WRITE);
665690
return null;
666691
}
667692

668-
public ByteBuffer glMapBufferRange(int target, int offset, int length, int acccess) {
693+
public ByteBuffer glMapBufferRange(int target, int offset, int length, int access) {
669694
//return gl2x.glMapBufferRange(GL.GL_ARRAY_BUFFER, offset, length, GL2.GL_READ_WRITE);
670695
return null;
671696
}
@@ -707,7 +732,7 @@ public void glFramebufferTexture2D(int target, int attachment, int texTarget, in
707732
}
708733

709734
public void glBindRenderbuffer(int target, int id) {
710-
GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, id);
735+
GLES20.glBindRenderbuffer(target, id);
711736
}
712737

713738
public void glRenderbufferStorageMultisample(int target, int samples, int format, int width, int height) {
@@ -842,16 +867,6 @@ public void glAttachShader(int prog, int shader) {
842867
GLES20.glAttachShader(prog, shader);
843868
}
844869

845-
public String getShaderLog(int id) {
846-
int[] compiled = new int[1];
847-
GLES20.glGetShaderiv(id, GLES20.GL_COMPILE_STATUS, compiled, 0);
848-
if (compiled[0] == 0) {
849-
return GLES20.glGetShaderInfoLog(id);
850-
} else {
851-
return "";
852-
}
853-
}
854-
855870
/////////////////////////////////////////////////////////////////////////////////
856871

857872
// Viewport
@@ -895,7 +910,7 @@ public void setReadBuffer(int buf) {
895910
}
896911

897912
public void glReadPixels(int x, int y, int width, int height, int format, int type, Buffer buffer) {
898-
GLES20.glReadPixels(x, y, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buffer);
913+
GLES20.glReadPixels(x, y, width, height, format, type, buffer);
899914
}
900915

901916
public void setDrawBuffer(int buf) {

android/core/src/processing/core/PGraphics.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,6 @@ public void normal(float nx, float ny, float nz) {
907907
normalY = ny;
908908
normalZ = nz;
909909

910-
/*
911910
// if drawing a shape and the normal hasn't been set yet,
912911
// then we need to set the normals for each vertex so far
913912
if (shape != 0) {
@@ -918,11 +917,9 @@ public void normal(float nx, float ny, float nz) {
918917
// a separate normal for each vertex
919918
normalMode = NORMAL_MODE_VERTEX;
920919
}
921-
}
922-
*/
920+
}
923921
}
924922

925-
926923
/**
927924
* Set texture mode to either to use coordinates based on the IMAGE
928925
* (more intuitive for new users) or NORMALIZED (better for advanced chaps)

android/core/src/processing/core/PGraphicsAndroid3D.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -780,8 +780,6 @@ protected void deleteFinalizedRenderBufferObjects() {
780780
// GLSL Program Objects -----------------------------------------------
781781

782782
protected int createGLSLProgramObject() {
783-
784-
pg.report("before delete");
785783
deleteFinalizedGLSLProgramObjects();
786784

787785
int id = pgl.glCreateProgram();
@@ -2203,7 +2201,7 @@ public void flush() {
22032201
}
22042202

22052203
if (flushMode == FLUSH_WHEN_FULL && !hints[DISABLE_TRANSFORM_CACHE]) {
2206-
//popMatrix();
2204+
popMatrix();
22072205
}
22082206
}
22092207

@@ -6327,7 +6325,7 @@ public void allocate() {
63276325
}
63286326

63296327
public void trim() {
6330-
if (vertexCount < vertices.length / 3) {
6328+
if (0 < vertexCount && vertexCount < vertices.length / 3) {
63316329
trimVertices();
63326330
trimColors();
63336331
trimNormals();
@@ -6337,9 +6335,12 @@ public void trim() {
63376335
trimAmbient();
63386336
trimSpecular();
63396337
trimEmissive();
6340-
trimShininess();
6338+
trimShininess();
6339+
}
6340+
6341+
if (0 < edgeCount && edgeCount < edges.length) {
63416342
trimEdges();
6342-
}
6343+
}
63436344
}
63446345

63456346
public void dispose() {
@@ -6437,7 +6438,7 @@ public int addVertex(float x, float y, float z,
64376438
index = 3 * vertexCount;
64386439
normals[index++] = nx;
64396440
normals[index++] = ny;
6440-
normals[index ] = ny;
6441+
normals[index ] = nz;
64416442

64426443
index = 2 * vertexCount;
64436444
texcoords[index++] = u;
@@ -7065,7 +7066,7 @@ public void allocate() {
70657066
}
70667067

70677068
public void trim() {
7068-
if (fillVertexCount < fillVertices.length / 3) {
7069+
if (0 < fillVertexCount && fillVertexCount < fillVertices.length / 3) {
70697070
trimFillVertices();
70707071
trimFillColors();
70717072
trimFillNormals();
@@ -7076,31 +7077,31 @@ public void trim() {
70767077
trimFillShininess();
70777078
}
70787079

7079-
if (fillIndexCount < fillIndices.length) {
7080+
if (0 < fillIndexCount && fillIndexCount < fillIndices.length) {
70807081
trimFillIndices();
70817082
}
70827083

7083-
if (lineVertexCount < lineVertices.length / 3) {
7084+
if (0 < lineVertexCount && lineVertexCount < lineVertices.length / 3) {
70847085
trimLineVertices();
70857086
trimLineColors();
70867087
trimLineAttributes();
70877088
}
70887089

7089-
if (lineIndexCount < lineIndices.length) {
7090+
if (0 < lineIndexCount && lineIndexCount < lineIndices.length) {
70907091
trimLineIndices();
70917092
}
70927093

7093-
if (pointVertexCount < pointVertices.length / 3) {
7094+
if (0 < pointVertexCount && pointVertexCount < pointVertices.length / 3) {
70947095
trimPointVertices();
70957096
trimPointColors();
70967097
trimPointAttributes();
70977098
}
70987099

7099-
if (pointIndexCount < pointIndices.length) {
7100+
if (0 < pointIndexCount && pointIndexCount < pointIndices.length) {
71007101
trimPointIndices();
71017102
}
71027103
}
7103-
7104+
71047105
protected void trimFillVertices() {
71057106
float temp[] = new float[3 * fillVertexCount];
71067107
PApplet.arrayCopy(fillVertices, 0, temp, 0, 3 * fillVertexCount);
@@ -7174,7 +7175,7 @@ protected void trimLineAttributes() {
71747175
}
71757176

71767177
protected void trimLineIndices() {
7177-
short temp[] = new short[lineVertexCount];
7178+
short temp[] = new short[lineIndexCount];
71787179
PApplet.arrayCopy(lineIndices, 0, temp, 0, lineIndexCount);
71797180
lineIndices = temp;
71807181
}
@@ -8571,10 +8572,11 @@ public void tessellatePolygon(boolean solid, boolean closed, boolean calcNormals
85718572

85728573
// Vertex data includes coordinates, colors, normals, texture coordinates, and material properties.
85738574
double[] vertex = new double[] { in.vertices [3 * i + 0], in.vertices [3 * i + 1], in.vertices[3 * i + 2],
8574-
in.colors [4 * i + 0], in.colors [4 * i + 1], in.colors [4 * i + 2], in.colors[4 * i + 3],
8575+
in.colors [i],
85758576
in.normals [3 * i + 0], in.normals [3 * i + 1], in.normals [3 * i + 2],
85768577
in.texcoords[2 * i + 0], in.texcoords[2 * i + 1],
85778578
in.ambient[i], in.specular[i], in.emissive[i], in.shininess[i] };
8579+
85788580
gluTess.addVertex(vertex);
85798581
}
85808582
gluTess.endContour();

android/core/src/processing/core/PTexture.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.LinkedList;
2929
import java.util.NoSuchElementException;
3030

31+
3132
/**
3233
* This class wraps an OpenGL texture.
3334
* By Andres Colubri
@@ -782,20 +783,8 @@ protected void allocate() {
782783
// from w/h in the case that the GPU doesn't support NPOT textures)
783784
pgl.glTexImage2D(glTarget, 0, glFormat, glWidth, glHeight, 0, PGL.GL_RGBA, PGL.GL_UNSIGNED_BYTE, null);
784785

785-
// Once OpenGL knows the size of the new texture, we make sure it doesn't
786-
// contain any garbage in the region of interest (0, 0, width, height):
787-
// Doing in patches of 16x16 pixels to avoid creating a (potentially)
788-
// very large transient array which in certain situations (memory-
789-
// constrained android devices) might lead to an out-of-memory error.
790-
int[] texels = new int[16 * 16];
791-
for (int y = 0; y < height + 16; y += 16) {
792-
int h = PApplet.min(16, height - y);
793-
for (int x = 0; x < width + 16; x += 16) {
794-
int w = PApplet.min(16, width - x);
795-
setTexels(texels, x, y, w, h);
796-
}
797-
}
798-
texels = null;
786+
// Makes sure that the texture buffer in video memory doesn't contain any garbage.
787+
pgl.initTexture(glTarget, width, height, PGL.GL_RGBA, PGL.GL_UNSIGNED_BYTE);
799788

800789
pgl.glBindTexture(glTarget, 0);
801790
pgl.disableTexturing(glTarget);

core/src/processing/core/PConstants.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ public interface PConstants {
4646
static public final int FLUSH_CONTINUOUSLY = 0;
4747
static public final int FLUSH_WHEN_FULL = 1;
4848

49+
// shaders
50+
51+
static public final int FILL_SHADER_SIMPLE = 0;
52+
static public final int FILL_SHADER_LIT = 1;
53+
static public final int FILL_SHADER_TEX = 2;
54+
static public final int FILL_SHADER_FULL = 3;
55+
static public final int LINE_SHADER = 4;
56+
static public final int POINT_SHADER = 5;
57+
4958
// vertex fields
5059

5160
static public final int X = 0; // model coords xyz (formerly MX/MY/MZ)

0 commit comments

Comments
 (0)