|
| 1 | +PShape buildLightMap(int mapSize,float LEDSize) { |
| 2 | + PGraphics pg; |
| 3 | + int x,y; |
| 4 | + float dx,dy,center,dist,maxDist; |
| 5 | + float alpha; |
| 6 | + float LEDCore = LEDSize * 0.4; |
| 7 | + println("Building light map..."); |
| 8 | + |
| 9 | + center = mapSize / 2; |
| 10 | + maxDist = sqrt(center * center); |
| 11 | + |
| 12 | + pg = createGraphics(mapSize,mapSize); |
| 13 | + |
| 14 | + pg.beginDraw(); |
| 15 | + pg.noStroke(); |
| 16 | + pg.noFill(); |
| 17 | + |
| 18 | + for (y = 0; y < mapSize; y++) { |
| 19 | + for (x = 0; x < mapSize; x++) { |
| 20 | + |
| 21 | + dx = (float) x - center; |
| 22 | + dy = (float) y - center; |
| 23 | + dist = sqrt(dx * dx + dy * dy); |
| 24 | + if (dist <= LEDCore) { |
| 25 | + dist = alpha = 255; |
| 26 | + } |
| 27 | + else { |
| 28 | + dist = 1-(dist / maxDist); // normalized distance |
| 29 | + alpha = 150; // 200 * pow(dist,2); |
| 30 | + dist = 255 * pow(dist, 3); |
| 31 | + } |
| 32 | + pg.set(x,y,color(dist,dist,dist,alpha)); |
| 33 | + } |
| 34 | + |
| 35 | + } |
| 36 | + pg.endDraw(); |
| 37 | + |
| 38 | + PImage tex = pg.get(); |
| 39 | + PShape sh = texturedPlane(tex,mapSize,mapSize, 0); |
| 40 | + sh.disableStyle(); |
| 41 | + |
| 42 | + println("Light map ready."); |
| 43 | + return sh; |
| 44 | +} |
| 45 | + |
| 46 | +PShape buildHighlightMap(int mapSize) { |
| 47 | + PGraphics pg; |
| 48 | + int x,y; |
| 49 | + float dx,dy,center,dist,maxDist; |
| 50 | + float alpha; |
| 51 | + println("Building highlight map..."); |
| 52 | + |
| 53 | + center = mapSize / 2; |
| 54 | + maxDist = sqrt(center * center); |
| 55 | + |
| 56 | + pg = createGraphics(mapSize,mapSize); |
| 57 | + |
| 58 | + pg.beginDraw(); |
| 59 | + pg.noStroke(); |
| 60 | + pg.noFill(); |
| 61 | + |
| 62 | + for (y = 0; y < mapSize; y++) { |
| 63 | + for (x = 0; x < mapSize; x++) { |
| 64 | + |
| 65 | + dx = (float) x - center; |
| 66 | + dy = (float) y - center; |
| 67 | + dist = sqrt(dx * dx + dy * dy); |
| 68 | + dist = 1-(dist / maxDist); // normalized distance |
| 69 | + alpha = 255 * pow(dist,2); |
| 70 | + if (alpha < 10) { |
| 71 | + alpha = 0; |
| 72 | + dist = 0; |
| 73 | + } else { |
| 74 | + dist = 255 * pow(dist, 1.75); |
| 75 | + } |
| 76 | + pg.set(x,y,color(dist,dist,dist,alpha)); |
| 77 | + } |
| 78 | + } |
| 79 | + pg.endDraw(); |
| 80 | + |
| 81 | + PImage tex = pg.get(); |
| 82 | + PShape sh = texturedPlane(tex,mapSize,mapSize, 0); |
| 83 | + sh.disableStyle(); |
| 84 | + sh.translate(-4,-4,0); |
| 85 | + |
| 86 | + println("Highlight map ready."); |
| 87 | + return sh; |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | +// set the coordinates of the individual LEDs in our data |
| 92 | +// transfer "texture" |
| 93 | +void initLEDCoords(float xSize,float ySize) { |
| 94 | + int tx = -width / 2; |
| 95 | + int ty = -height / 2; |
| 96 | + int n = 0; |
| 97 | + for (float y = 0; y < ySize; y++) { |
| 98 | + for (float x = 0; x < xSize; x++) { |
| 99 | + float x1 = (x + 1) / (xSize + 1); |
| 100 | + float y1 = (y + 1) / (xSize + 1); |
| 101 | + leds[n][0] = tx + (int) (x1 * width); |
| 102 | + leds[n][1] = ty + (int) (y1 * width); |
| 103 | + n++; |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// textured x/y background rect of specified size, centered at origin facing viewer |
| 109 | +PShape backgroundPlane(float x, float y, float z) { |
| 110 | + PImage tex = loadImage("plasticbg.png"); |
| 111 | + return texturedPlane(tex,x,y,z); |
| 112 | +} |
| 113 | + |
| 114 | +// textured x/y background rect of specified size, centered at origin facing viewer |
| 115 | +PShape texturedPlane(PImage tex,float x, float y, float z) { |
| 116 | + textureMode(NORMAL); |
| 117 | + x /= 2; y /=2; z /=2; |
| 118 | + PShape sh = createShape(); |
| 119 | + sh.beginShape(QUADS); |
| 120 | + sh.noStroke(); |
| 121 | + sh.shininess(100); |
| 122 | + sh.texture(tex); |
| 123 | + |
| 124 | + sh.vertex(-x, -y, z, 0, 0); |
| 125 | + sh.vertex( x, -y, z, 1, 0); |
| 126 | + sh.vertex( x, y, z, 1, 1); |
| 127 | + sh.vertex(-x, y, z, 0, 1); |
| 128 | + |
| 129 | + sh.endShape(CLOSE); |
| 130 | + return sh; |
| 131 | +} |
| 132 | + |
| 133 | + |
| 134 | +// Our model LED -- a thin rectangular box, but with no bottom face |
| 135 | +PShape LEDPad(float xs, float ys, float zs) { |
| 136 | + textureMode(NORMAL); |
| 137 | + PImage tex = loadImage("texLED.jpg"); |
| 138 | + xs /= 2; ys /=2; zs /=2; |
| 139 | + PShape sh = createShape(); |
| 140 | + sh.beginShape(QUADS); |
| 141 | +// sh.setStroke(true); |
| 142 | + sh.shininess(1000); |
| 143 | + sh.specular(255); |
| 144 | + sh.texture(tex); |
| 145 | + |
| 146 | + // +Z "front" face |
| 147 | + sh.vertex(-xs, -ys, zs, 0, 0); |
| 148 | + sh.vertex( xs, -ys, zs, 1, 0); |
| 149 | + sh.vertex( xs, ys, zs, 1, 1); |
| 150 | + sh.vertex(-xs, ys, zs, 0, 1); |
| 151 | + |
| 152 | + // +Y "bottom" face |
| 153 | + sh.vertex(-xs, ys, zs, 0, 0); |
| 154 | + sh.vertex( xs, ys, zs, 1, 0); |
| 155 | + sh.vertex( xs, ys, -zs, 1, 1); |
| 156 | + sh.vertex(-xs, ys, -zs, 0, 1); |
| 157 | + |
| 158 | + // -Y "top" face |
| 159 | + sh.vertex(-xs, -ys, -zs, 0, 0); |
| 160 | + sh.vertex( xs, -ys, -zs, 1, 0); |
| 161 | + sh.vertex( xs, -ys, zs, 1, 1); |
| 162 | + sh.vertex(-xs, -ys, zs, 0, 1); |
| 163 | + |
| 164 | + // +X "right" face |
| 165 | + sh.vertex( xs, -ys, zs, 0, 0); |
| 166 | + sh.vertex( xs, -ys, -zs, 1, 0); |
| 167 | + sh.vertex( xs, ys, -zs, 1, 1); |
| 168 | + sh.vertex( xs, ys, zs, 0, 1); |
| 169 | + |
| 170 | + // -X "left" face |
| 171 | + sh.vertex(-xs, -ys, -zs, 0, 0); |
| 172 | + sh.vertex(-xs, -ys, zs, 1, 0); |
| 173 | + sh.vertex(-xs, ys, zs, 1, 1); |
| 174 | + sh.vertex(-xs, ys, -zs, 0, 1); |
| 175 | + |
| 176 | + sh.endShape(CLOSE); |
| 177 | + sh.disableStyle(); |
| 178 | + return sh; |
| 179 | +} |
| 180 | + |
| 181 | +// Generic textured box of specified size, centered at the origin, |
| 182 | +// in case we need it later... |
| 183 | +PShape TexturedBox(PImage tex,float xs, float ys, float zs) { |
| 184 | + xs /= 2; ys /=2; zs /=2; |
| 185 | + textureMode(NORMAL); |
| 186 | + PShape sh = createShape(); |
| 187 | + sh.beginShape(QUADS); |
| 188 | + sh.texture(tex); |
| 189 | + |
| 190 | + // +Z "front" face |
| 191 | + sh.vertex(-xs, -ys, zs, 0, 0); |
| 192 | + sh.vertex( xs, -ys, zs, 1, 0); |
| 193 | + sh.vertex( xs, ys, zs, 1, 1); |
| 194 | + sh.vertex(-xs, ys, zs, 0, 1); |
| 195 | + |
| 196 | + // -Z "back" face |
| 197 | + sh.vertex( xs, -ys, -zs, 0, 0); |
| 198 | + sh.vertex(-xs, -ys, -zs, 1, 0); |
| 199 | + sh.vertex(-xs, ys, -zs, 1, 1); |
| 200 | + sh.vertex( xs, ys, -zs, 0, 1); |
| 201 | + |
| 202 | + // +Y "bottom" face |
| 203 | + sh.vertex(-xs, ys, zs, 0, 0); |
| 204 | + sh.vertex( xs, ys, zs, 1, 0); |
| 205 | + sh.vertex( xs, ys, -zs, 1, 1); |
| 206 | + sh.vertex(-xs, ys, -zs, 0, 1); |
| 207 | + |
| 208 | + // -Y "top" face |
| 209 | + sh.vertex(-xs, -ys, -zs, 0, 0); |
| 210 | + sh.vertex( xs, -ys, -zs, 1, 0); |
| 211 | + sh.vertex( xs, -ys, zs, 1, 1); |
| 212 | + sh.vertex(-xs, -ys, zs, 0, 1); |
| 213 | + |
| 214 | + // +X "right" face |
| 215 | + sh.vertex( xs, -ys, zs, 0, 0); |
| 216 | + sh.vertex( xs, -ys, -zs, 1, 0); |
| 217 | + sh.vertex( xs, ys, -zs, 1, 1); |
| 218 | + sh.vertex( xs, ys, zs, 0, 1); |
| 219 | + |
| 220 | + // -X "left" face |
| 221 | + sh.vertex(-xs, -ys, -zs, 0, 0); |
| 222 | + sh.vertex(-xs, -ys, zs, 1, 0); |
| 223 | + sh.vertex(-xs, ys, zs, 1, 1); |
| 224 | + sh.vertex(-xs, ys, -zs, 0, 1); |
| 225 | + |
| 226 | + sh.endShape(CLOSE); |
| 227 | + return sh; |
| 228 | +} |
0 commit comments