mirror of
https://github.com/processing/processing4.git
synced 2026-06-16 04:26:26 +02:00
Auto normal selection method
This commit is contained in:
@@ -7015,7 +7015,12 @@ public class PApplet extends Activity implements PConstants, Runnable {
|
||||
g.edge(edge);
|
||||
}
|
||||
|
||||
|
||||
public void autoNormal(boolean auto) {
|
||||
g.autoNormal(auto);
|
||||
}
|
||||
|
||||
|
||||
public void normal(float nx, float ny, float nz) {
|
||||
g.normal(nx, ny, nz);
|
||||
}
|
||||
|
||||
@@ -365,19 +365,6 @@ public interface PConstants {
|
||||
*/
|
||||
static final int SHAPE = 5;
|
||||
|
||||
|
||||
// normal modes.
|
||||
|
||||
/**
|
||||
* In the vertex mode, the user should specify a normal for
|
||||
* each vertex.
|
||||
*/
|
||||
static final int MANUAL = 6;
|
||||
|
||||
/**
|
||||
* Normal vectors are automatically calculated if not set.
|
||||
*/
|
||||
static final int AUTO = 7;
|
||||
|
||||
// text alignment modes
|
||||
// are inherited from LEFT, CENTER, RIGHT
|
||||
|
||||
@@ -482,8 +482,7 @@ public class PGraphics extends PImage implements PConstants {
|
||||
|
||||
// ........................................................
|
||||
|
||||
/// Current mode for normals, one of AUTO, SHAPE, or VERTEX
|
||||
protected int normalMode;
|
||||
protected boolean autoNormal;
|
||||
|
||||
/** Current normal vector. */
|
||||
public float normalX, normalY, normalZ;
|
||||
@@ -686,7 +685,7 @@ public class PGraphics extends PImage implements PConstants {
|
||||
rectMode(CORNER);
|
||||
ellipseMode(DIAMETER);
|
||||
|
||||
normalMode = AUTO;
|
||||
autoNormal = true;
|
||||
|
||||
// no current font
|
||||
textFont = null;
|
||||
@@ -860,10 +859,10 @@ public class PGraphics extends PImage implements PConstants {
|
||||
|
||||
|
||||
/**
|
||||
* Sets the normal mode, either AUTO, SHAPE or VERTEX.
|
||||
* Sets the automatic normal calculation mode.
|
||||
*/
|
||||
public void normalMode(int mode) {
|
||||
this.normalMode = mode;
|
||||
public void autoNormal(boolean auto) {
|
||||
this.autoNormal = auto;
|
||||
}
|
||||
|
||||
|
||||
@@ -936,11 +935,26 @@ public class PGraphics extends PImage implements PConstants {
|
||||
|
||||
vertex[EDGE] = edge ? 1 : 0;
|
||||
|
||||
if (fill) {
|
||||
vertex[R] = fillR;
|
||||
vertex[G] = fillG;
|
||||
vertex[B] = fillB;
|
||||
vertex[A] = fillA;
|
||||
boolean textured = textureImage != null;
|
||||
if (fill || textured) {
|
||||
if (textured) {
|
||||
vertex[R] = fillR;
|
||||
vertex[G] = fillG;
|
||||
vertex[B] = fillB;
|
||||
vertex[A] = fillA;
|
||||
} else {
|
||||
if (tint) {
|
||||
vertex[R] = tintR;
|
||||
vertex[G] = tintG;
|
||||
vertex[B] = tintB;
|
||||
vertex[A] = tintA;
|
||||
} else {
|
||||
vertex[R] = 1;
|
||||
vertex[G] = 1;
|
||||
vertex[B] = 1;
|
||||
vertex[A] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (stroke) {
|
||||
@@ -954,6 +968,28 @@ public class PGraphics extends PImage implements PConstants {
|
||||
vertex[U] = textureU;
|
||||
vertex[V] = textureV;
|
||||
|
||||
if (autoNormal) {
|
||||
float norm2 = normalX * normalX + normalY * normalY + normalZ * normalZ;
|
||||
if (norm2 < EPSILON) {
|
||||
vertex[HAS_NORMAL] = 0;
|
||||
} else {
|
||||
if (Math.abs(norm2 - 1) > EPSILON) {
|
||||
// The normal vector is not normalized.
|
||||
float norm = PApplet.sqrt(norm2);
|
||||
normalX /= norm;
|
||||
normalY /= norm;
|
||||
normalZ /= norm;
|
||||
}
|
||||
vertex[HAS_NORMAL] = 1;
|
||||
}
|
||||
} else {
|
||||
vertex[HAS_NORMAL] = 1;
|
||||
}
|
||||
|
||||
vertex[NX] = normalX;
|
||||
vertex[NY] = normalY;
|
||||
vertex[NZ] = normalZ;
|
||||
|
||||
vertexCount++;
|
||||
}
|
||||
|
||||
@@ -1040,23 +1076,23 @@ public class PGraphics extends PImage implements PConstants {
|
||||
vertex[U] = textureU;
|
||||
vertex[V] = textureV;
|
||||
|
||||
// TODO: properly define auto-normalization methods to enable/disable this:
|
||||
/*
|
||||
float norm2 = normalX * normalX + normalY * normalY + normalZ * normalZ;
|
||||
if (norm2 < EPSILON) {
|
||||
vertex[HAS_NORMAL] = 0;
|
||||
} else {
|
||||
if (Math.abs(norm2 - 1) > EPSILON) {
|
||||
// The normal vector is not normalized.
|
||||
float norm = PApplet.sqrt(norm2);
|
||||
normalX /= norm;
|
||||
normalY /= norm;
|
||||
normalZ /= norm;
|
||||
if (autoNormal) {
|
||||
float norm2 = normalX * normalX + normalY * normalY + normalZ * normalZ;
|
||||
if (norm2 < EPSILON) {
|
||||
vertex[HAS_NORMAL] = 0;
|
||||
} else {
|
||||
if (Math.abs(norm2 - 1) > EPSILON) {
|
||||
// The normal vector is not normalized.
|
||||
float norm = PApplet.sqrt(norm2);
|
||||
normalX /= norm;
|
||||
normalY /= norm;
|
||||
normalZ /= norm;
|
||||
}
|
||||
vertex[HAS_NORMAL] = 1;
|
||||
}
|
||||
} else {
|
||||
vertex[HAS_NORMAL] = 1;
|
||||
}
|
||||
*/
|
||||
vertex[HAS_NORMAL] = 1;
|
||||
|
||||
vertex[NX] = normalX;
|
||||
vertex[NY] = normalY;
|
||||
|
||||
@@ -849,7 +849,7 @@ public class PGraphicsAndroid3D extends PGraphics {
|
||||
texCoordBuffer[t].rewind();
|
||||
}
|
||||
|
||||
// Each frame starts with multitexturing disabled.
|
||||
// Each frame starts with multitexturing disabled.
|
||||
usingMultitexturing = false;
|
||||
textureUnit = 0;
|
||||
clearMultitextures();
|
||||
@@ -899,8 +899,6 @@ public class PGraphicsAndroid3D extends PGraphics {
|
||||
// The ambient and diffuse components for each vertex are taken
|
||||
// from the glColor/color buffer setting:
|
||||
gl.glEnable(GL10.GL_COLOR_MATERIAL);
|
||||
// glColorMaterial is not available in GLES, I suspect that is using
|
||||
// GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT_AND_DIFFUSE implicitly
|
||||
// For a quick overview of how the lighting model works in OpenGL
|
||||
// see this page:
|
||||
// http://www.sjbaker.org/steve/omniv/opengl_lighting.html
|
||||
@@ -910,16 +908,11 @@ public class PGraphicsAndroid3D extends PGraphics {
|
||||
gl.glEnable(GL10.GL_RESCALE_NORMAL);
|
||||
|
||||
// Light model defaults:
|
||||
// The default ambient light for the entire scene is (0.2, 0.2, 0.2),
|
||||
// The default opengl ambient light is (0.2, 0.2, 0.2), so
|
||||
// here we set our own default value.
|
||||
gl.glLightModelfv(GL10.GL_LIGHT_MODEL_AMBIENT, baseLight, 0);
|
||||
gl.glLightModelx(GL10.GL_LIGHT_MODEL_TWO_SIDE, 0);
|
||||
|
||||
// I think this is OpenGL 1.2 only, issue 345:
|
||||
// http://code.google.com/p/processing/issues/detail?id=345
|
||||
// should take care of it:
|
||||
// gl.glEnable(GL10.GL_AUTO_NORMAL);
|
||||
|
||||
|
||||
shapeFirst = 0;
|
||||
|
||||
// The current normal vector is set to zero.
|
||||
@@ -1253,8 +1246,6 @@ public class PGraphicsAndroid3D extends PGraphics {
|
||||
|
||||
clearMultitextures();
|
||||
clearMultitextures0();
|
||||
|
||||
normalMode = AUTO;
|
||||
}
|
||||
|
||||
// public void edge(boolean e)
|
||||
@@ -1391,36 +1382,8 @@ public class PGraphicsAndroid3D extends PGraphics {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void vertex(float x, float y) {
|
||||
super.vertex(x, y);
|
||||
|
||||
/*
|
||||
// TODO: Use multitexture information.
|
||||
float[] vertex = vertices[vertexCount - 1];
|
||||
boolean textured = textureImage != null;
|
||||
if (fill || textured) {
|
||||
if (textured) {
|
||||
vertex[R] = fillR;
|
||||
vertex[G] = fillG;
|
||||
vertex[B] = fillB;
|
||||
vertex[A] = fillA;
|
||||
} else {
|
||||
if (tint) {
|
||||
vertex[R] = tintR;
|
||||
vertex[G] = tintG;
|
||||
vertex[B] = tintB;
|
||||
vertex[A] = tintA;
|
||||
} else {
|
||||
vertex[R] = 1;
|
||||
vertex[G] = 1;
|
||||
vertex[B] = 1;
|
||||
vertex[A] = 1;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
setVertexTex(vertexCount - 1);
|
||||
}
|
||||
|
||||
@@ -1453,7 +1416,7 @@ public class PGraphicsAndroid3D extends PGraphics {
|
||||
System.arraycopy(vertexTex, 0, tempi, 0, vertexCount);
|
||||
vertexTex = tempi;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void setVertexTex(int n) {
|
||||
PImage[] p = vertexTex[n];
|
||||
@@ -2284,12 +2247,12 @@ public class PGraphicsAndroid3D extends PGraphics {
|
||||
float b[] = vertices[triangles[i][VERTEX2]];
|
||||
float c[] = vertices[triangles[i][VERTEX3]];
|
||||
|
||||
if (normalMode == AUTO && (a[HAS_NORMAL] == 0 ||
|
||||
b[HAS_NORMAL] == 0 ||
|
||||
c[HAS_NORMAL] == 0)) {
|
||||
if (autoNormal && (a[HAS_NORMAL] == 0 ||
|
||||
b[HAS_NORMAL] == 0 ||
|
||||
c[HAS_NORMAL] == 0)) {
|
||||
// Ok, some of the vertices defining the current triangle have not been
|
||||
// assigned a normal, and the normal mode is AUTO, so we generate the normal
|
||||
// for all the vertices of this triangle.
|
||||
// assigned a normal, and the automatic normal calculation is enabled, so
|
||||
// we generate the normal for all the vertices of this triangle.
|
||||
|
||||
// Assuming CW vertex ordering, so the outside direction for this triangle
|
||||
// should be given by the cross product (b - a) x (b - c):
|
||||
|
||||
@@ -2585,8 +2585,8 @@ public class PShape3D extends PShape implements PConstants {
|
||||
int tMode0 = a3d.textureMode;
|
||||
a3d.textureMode = NORMAL;
|
||||
|
||||
int nMode0 = a3d.normalMode;
|
||||
a3d.normalMode = AUTO;
|
||||
boolean auto0 = a3d.autoNormal;
|
||||
a3d.autoNormal = true;
|
||||
|
||||
// Using RGB mode for coloring.
|
||||
int cMode0 = a3d.colorMode;
|
||||
@@ -2735,7 +2735,7 @@ public class PShape3D extends PShape implements PConstants {
|
||||
// Restore texture, color, and normal modes.
|
||||
a3d.textureMode = tMode0;
|
||||
a3d.colorMode = cMode0;
|
||||
a3d.normalMode = nMode0;
|
||||
a3d.autoNormal = auto0;
|
||||
|
||||
// Restore colors
|
||||
a3d.calcR = specularR0;
|
||||
|
||||
Reference in New Issue
Block a user