From bfb9f8ecf4d1e1e61aab9b8b5f7a11564b4cfade Mon Sep 17 00:00:00 2001 From: codeanticode Date: Tue, 10 Apr 2012 21:20:34 +0000 Subject: [PATCH] Sync with Android --- .../core/src/processing/core/PGraphics.java | 2 + .../processing/opengl/FillShaderVertFull.glsl | 6 +- .../processing/opengl/FillShaderVertLit.glsl | 11 ++-- .../processing/opengl/PGraphicsOpenGL.java | 59 ++++++++++++------- .../core/src/processing/opengl/PShape3D.java | 9 ++- 5 files changed, 56 insertions(+), 31 deletions(-) diff --git a/android/core/src/processing/core/PGraphics.java b/android/core/src/processing/core/PGraphics.java index f0db1d766..3e9db05b3 100644 --- a/android/core/src/processing/core/PGraphics.java +++ b/android/core/src/processing/core/PGraphics.java @@ -323,6 +323,7 @@ public class PGraphics extends PImage implements PConstants { public int ambientColor; public float ambientR, ambientG, ambientB; + protected boolean setAmbient; public int specularColor; public float specularR, specularG, specularB; @@ -4205,6 +4206,7 @@ public class PGraphics extends PImage implements PConstants { ambientR = calcR; ambientG = calcG; ambientB = calcB; + setAmbient = true; } diff --git a/android/core/src/processing/opengl/FillShaderVertFull.glsl b/android/core/src/processing/opengl/FillShaderVertFull.glsl index 6e324d8cf..5734b25bd 100644 --- a/android/core/src/processing/opengl/FillShaderVertFull.glsl +++ b/android/core/src/processing/opengl/FillShaderVertFull.glsl @@ -65,12 +65,14 @@ float spotFactor(vec3 lightPos, vec3 vertPos, vec3 lightNorm, float minCos, floa } float lambertFactor(vec3 lightDir, vec3 vecNormal) { - return max(zero_float, dot(lightDir, vecNormal)); + //return max(zero_float, dot(lightDir, vecNormal)); + return abs(dot(lightDir, vecNormal)); } float blinnPhongFactor(vec3 lightDir, vec3 lightPos, vec3 vecNormal, float shine) { vec3 ldp = normalize(lightDir - lightPos); - return pow(max(zero_float, dot(ldp, vecNormal)), shine); + //return pow(max(zero_float, dot(ldp, vecNormal)), shine); + return pow(abs(dot(ldp, vecNormal)), shine); } void main() { diff --git a/android/core/src/processing/opengl/FillShaderVertLit.glsl b/android/core/src/processing/opengl/FillShaderVertLit.glsl index 198c99ada..02ee447a4 100644 --- a/android/core/src/processing/opengl/FillShaderVertLit.glsl +++ b/android/core/src/processing/opengl/FillShaderVertLit.glsl @@ -62,12 +62,14 @@ float spotFactor(vec3 lightPos, vec3 vertPos, vec3 lightNorm, float minCos, floa } float lambertFactor(vec3 lightDir, vec3 vecNormal) { - return max(zero_float, dot(lightDir, vecNormal)); + //return max(zero_float, dot(lightDir, vecNormal)); + return abs(dot(lightDir, vecNormal)); } float blinnPhongFactor(vec3 lightDir, vec3 lightPos, vec3 vecNormal, float shine) { vec3 ldp = normalize(lightDir - lightPos); - return pow(max(zero_float, dot(ldp, vecNormal)), shine); + //return pow(max(zero_float, dot(ldp, vecNormal)), shine); + return pow(abs(dot(ldp, vecNormal)), shine); } void main() { @@ -118,12 +120,13 @@ void main() { if (any(greaterThan(lightSpecular[i], zero_vec3))) { totalSpecular += lightSpecular[i] * falloff * spotf * blinnPhongFactor(lightDir, lightPos, ecNormal, inShine); - } + } + } // Calculating final color as result of all lights (plus emissive term) vertColor = vec4(totalAmbient, 1) * inAmbient + vec4(totalDiffuse, 1) * inColor + vec4(totalSpecular, 1) * inSpecular + - inEmissive; + inEmissive; } \ No newline at end of file diff --git a/android/core/src/processing/opengl/PGraphicsOpenGL.java b/android/core/src/processing/opengl/PGraphicsOpenGL.java index 1b55a37b4..29d94b190 100644 --- a/android/core/src/processing/opengl/PGraphicsOpenGL.java +++ b/android/core/src/processing/opengl/PGraphicsOpenGL.java @@ -1485,8 +1485,11 @@ public class PGraphicsOpenGL extends PGraphics { lightFalloff(1, 0, 0); lightSpecular(0, 0, 0); - // because y is flipped + // Because y is flipped, the vertices that should be specified by + // the user in CCW order to define a front-facing facet, end up being + // CW. pgl.glFrontFace(PGL.GL_CW); + pgl.glDisable(PGL.GL_CULL_FACE); // Processing uses only one texture unit. pgl.glActiveTexture(PGL.GL_TEXTURE0); @@ -1617,6 +1620,7 @@ public class PGraphicsOpenGL extends PGraphics { pgl.glViewport(viewport[0], viewport[1], viewport[2], viewport[3]); pgl.glFrontFace(PGL.GL_CW); + pgl.glDisable(PGL.GL_CULL_FACE); pgl.glActiveTexture(PGL.GL_TEXTURE0); @@ -1837,6 +1841,9 @@ public class PGraphicsOpenGL extends PGraphics { specular(125); emissive(0); shininess(1); + + // To indicate that the user hasn't set ambient + setAmbient = false; } @@ -4094,11 +4101,14 @@ public class PGraphicsOpenGL extends PGraphics { protected void fillFromCalc() { super.fillFromCalc(); - // Setting the ambient color from the current fill - // is what the old P3D did and allows to have an - // default ambient color when the user doesn't specify - // it explicitly. - ambientFromCalc(); + if (!setAmbient) { + // Setting the ambient color from the current fill + // is what the old P3D did and allows to have an + // default ambient color when the user doesn't specify + // it explicitly. + ambientFromCalc(); + setAmbient = false; + } } @@ -6369,15 +6379,17 @@ public class PGraphicsOpenGL extends PGraphics { float v10y = y0 - y1; float v10z = z0 - z1; - // n = v10 x v12 (so the normal points out following the - // clockwise direction along the vertices of the triangle). - float nx = v10y * v12z - v12y * v10z; - float ny = v10z * v12x - v12z * v10x; - float nz = v10x * v12y - v12x * v10y; + // The automatic normal calculation in Processing assumes + // that vertices as given in CCW order so: + // n = v12 x v10 + // so that the normal outwards. + float nx = v12y * v10z - v10y * v12z; + float ny = v12z * v10x - v10z * v12x; + float nz = v12x * v10y - v10x * v12y; float d = PApplet.sqrt(nx * nx + ny * ny + nz * nz); - nx /= -d; - ny /= -d; - nz /= -d; + nx /= d; + ny /= d; + nz /= d; index = 3 * i0; normals[index++] = nx; @@ -6614,9 +6626,11 @@ public class PGraphicsOpenGL extends PGraphics { int i1 = i; int i0, i2; if (i % 2 == 0) { + // The even triangles (0, 2, 4...) should be CW i0 = i + 1; i2 = i - 1; } else { + // The even triangles (1, 3, 5...) should be CCW i0 = i - 1; i2 = i + 1; } @@ -6672,11 +6686,11 @@ public class PGraphicsOpenGL extends PGraphics { for (int qd = 1; qd < (lastVertex - firstVertex + 1) / 2; qd++) { int i0 = firstVertex + 2 * (qd - 1); int i1 = firstVertex + 2 * (qd - 1) + 1; - int i2 = firstVertex + 2 * qd + 1; - int i3 = firstVertex + 2 * qd; + int i2 = firstVertex + 2 * qd; + int i3 = firstVertex + 2 * qd + 1; - calcTriangleNormal(i0, i1, i3); - calcTriangleNormal(i3, i2, i0); + calcTriangleNormal(i0, i3, i1); + calcTriangleNormal(i0, i2, i3); } } @@ -8572,15 +8586,16 @@ public class PGraphicsOpenGL extends PGraphics { } break; case TRIANGLE_STRIP: - for (int i = 1; i < tessCount - 1; i++) { - addIndex(i); + for (int i = 1; i < tessCount - 1; i++) { if (i % 2 == 0) { - addIndex(i - 1); addIndex(i + 1); + addIndex(i); + addIndex(i - 1); if (calcNormals) calcTriNormal(i + 1, i, i - 1); } else { - addIndex(i + 1); addIndex(i - 1); + addIndex(i); + addIndex(i + 1); if (calcNormals) calcTriNormal(i - 1, i, i + 1); } } diff --git a/android/core/src/processing/opengl/PShape3D.java b/android/core/src/processing/opengl/PShape3D.java index 0040a9a8a..1d4f6f862 100644 --- a/android/core/src/processing/opengl/PShape3D.java +++ b/android/core/src/processing/opengl/PShape3D.java @@ -2594,6 +2594,9 @@ public class PShape3D extends PShape { d = params[2]; } + //in.generateBox(w, h, d); + + float x1 = -w/2f; float x2 = w/2f; float y1 = -h/2f; float y2 = h/2f; float z1 = -d/2f; float z2 = d/2f; @@ -3800,7 +3803,7 @@ public class PShape3D extends PShape { shader.start(); for (int i = 0; i < pointIndexData.size(); i++) { - IndexData index = (IndexData)pointIndexData.get(i); + IndexData index = pointIndexData.get(i); int first = index.first; int offset = index.offset; int size = index.size; @@ -3823,7 +3826,7 @@ public class PShape3D extends PShape { shader.start(); for (int i = 0; i < lineIndexData.size(); i++) { - IndexData index = (IndexData)lineIndexData.get(i); + IndexData index = lineIndexData.get(i); int first = index.first; int offset = index.offset; int size = index.size; @@ -3855,7 +3858,7 @@ public class PShape3D extends PShape { shader.start(); for (int i = 0; i < fillIndexData.size(); i++) { - IndexData index = (IndexData)fillIndexData.get(i); + IndexData index = fillIndexData.get(i); int first = index.first; int offset = index.offset; int size = index.size;