/** Bump Mapping Dave Bollinger, Dec 2006 http://www.davebollinger.com for Processing 0123 Beta Old School Quick & Dirty Fake Bump Mapping Move mouse to change light position Press left mouse to display source image Press right mouse to display bump image */ PImage src, bmp, dst; int [][] normals; void setup() { size(200,200,P3D); src = loadImage("eames.jpg"); bmp = loadImage("bump.jpg"); normals = new int[bmp.width * bmp.height][2]; calcNormals(bmp, normals); dst = src.get(); } void draw() { bumpAdjustImage(src,dst,normals,mouseX,mouseY); background((mousePressed) ? ((mouseButton==LEFT) ? src : bmp) : dst); } int peg(int n) { return (n < 0) ? 0 : ((n > 255) ? 255 : n); } int isqrt(int n) { int guess=0; int bit = 1 << 15; do { guess ^= bit; if (guess * guess > n ) guess ^= bit; } while ((bit >>= 1) != 0); return guess; } void bumpAdjustImage(PImage src, PImage dst, int [][] normals, int lx, int ly) { int srcw = src.width, srch = src.height; int [] sp = src.pixels, dp = dst.pixels; int idx = 0; for (int y=0; y> 16; int g = (c & 0xFF00) >> 8; int b = c & 0xFF; dp[idx] = (c & 0xFF000000) | ((peg((r * q)>>8)) << 16) | ((peg((g * q)>>8)) << 8) | (peg((b * q)>>8)) ; idx++; } } } void calcNormals(PImage bmp, int [][] normals) { int bmpw = bmp.width; int bmph = bmp.height; int [] pix = bmp.pixels; int idx = bmpw + 1; for (int y=bmph-2; y>0; y--) { for (int x=bmpw-2; x>0; x--) { normals[idx][0] = (pix[idx+1]&0xff) - (pix[idx-1]&0xff); normals[idx][1] = (pix[idx+bmpw]&0xff) - (pix[idx-bmpw]&0xff); idx++; } idx += 2; } }