boolean invertBumps = bumpHeight < 0;
Vector3f position = new Vector3f(0.0f, 0.0f, 0.0f);
Vector3f viewpoint = new Vector3f(width / 2.0f, height / 2.0f, viewDistance);
Vector3f normal = new Vector3f();
Color4f c = new Color4f();
Function2D bump = bumpFunction;
if (bumpSource == BUMPS_FROM_IMAGE || bumpSource == BUMPS_FROM_IMAGE_ALPHA || bumpSource == BUMPS_FROM_MAP || bump == null) {
if ( bumpSoftness != 0 ) {
int bumpWidth = width;
int bumpHeight = height;
int[] bumpPixels = inPixels;
if ( bumpSource == BUMPS_FROM_MAP && bumpFunction instanceof ImageFunction2D ) {
ImageFunction2D if2d = (ImageFunction2D)bumpFunction;
bumpWidth = if2d.getWidth();
bumpHeight = if2d.getHeight();
bumpPixels = if2d.getPixels();
}
Kernel kernel = GaussianFilter.makeKernel( bumpSoftness );
int [] tmpPixels = new int[bumpWidth * bumpHeight];
int [] softPixels = new int[bumpWidth * bumpHeight];
GaussianFilter.convolveAndTranspose( kernel, bumpPixels, tmpPixels, bumpWidth, bumpHeight, true, false, false, ConvolveFilter.CLAMP_EDGES);
GaussianFilter.convolveAndTranspose( kernel, tmpPixels, softPixels, bumpHeight, bumpWidth, true, false, false, ConvolveFilter.CLAMP_EDGES);
bump = new ImageFunction2D(softPixels, bumpWidth, bumpHeight, ImageFunction2D.CLAMP, bumpSource == BUMPS_FROM_IMAGE_ALPHA);
} else
bump = new ImageFunction2D(inPixels, width, height, ImageFunction2D.CLAMP, bumpSource == BUMPS_FROM_IMAGE_ALPHA);
}
Vector3f v1 = new Vector3f();
Vector3f v2 = new Vector3f();
Vector3f n = new Vector3f();
// Loop through each source pixel
for (int y = 0; y < height; y++) {
float ny = y;
position.y = y;
for (int x = 0; x < width; x++) {
float nx = x;
// Calculate the normal at this point
if (bumpSource != BUMPS_FROM_BEVEL) {
// Complicated and slower method
// Calculate four normals using the gradients in +/- X/Y directions
int count = 0;
normal.x = normal.y = normal.z = 0;
float m0 = width45*bump.evaluate(nx, ny);
float m1 = x > 0 ? width45*bump.evaluate(nx - 1.0f, ny)-m0 : -2;
float m2 = y > 0 ? width45*bump.evaluate(nx, ny - 1.0f)-m0 : -2;
float m3 = x < width-1 ? width45*bump.evaluate(nx + 1.0f, ny)-m0 : -2;
float m4 = y < height-1 ? width45*bump.evaluate(nx, ny + 1.0f)-m0 : -2;
if (m1 != -2 && m4 != -2) {
v1.x = -1.0f; v1.y = 0.0f; v1.z = m1;
v2.x = 0.0f; v2.y = 1.0f; v2.z = m4;
n.cross(v1, v2);