Skip to content

Commit

Permalink
Added caustics viewer and reorganized structure
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianBohne committed Mar 10, 2024
1 parent e1a1e11 commit bfee09b
Show file tree
Hide file tree
Showing 17 changed files with 136 additions and 21 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
*.mp4
frames
1 change: 1 addition & 0 deletions ImageCausticsGenerator/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
frames
44 changes: 24 additions & 20 deletions ImageCaustics.pde → ...sticsGenerator/ImageCausticsGenerator.pde
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class MovedPixel {
this.ogX = ogX;
this.ogY = ogY;
}

}

PImage image;
Expand All @@ -26,23 +25,30 @@ float refractiveIndex = 1.45;

PGraphics normalMap;

void setup() {
void settings() {

//// Albert Einstein
//image = loadImage("resources/albert.jpg");

// Lion
size(612, 408); // 408 is just so the image to mp4 works
image = loadImage("resources/lion-612x407.jpg");

//// Circle / Torus
//size(639, 360);
//image = loadImage("resources/circle-639-360.jpg");

//// Lines
//size(612, 322); // 322 is just so the image to mp4 works
//image = loadImage("resources/lines-612x321.jpg");

//// Moon
//size(716, 694); // 694 is just so the image to mp4 works
//image = loadImage("resources/moon-716x693.jpg");

//// Skull xray
//image = loadImage("resources/skull.jpg");

size(image.width + image.width % 2, image.height + image.height % 2); // Just making sure the size is even
}

void setup() {
image.loadPixels();

horizontalCurrent = new ArrayList<ArrayList<MovedPixel>>(image.height);
Expand Down Expand Up @@ -98,9 +104,6 @@ void draw() {
}

drawCurrentImage();

//saveFrame("frames/frame####.png");

}

void mousePressed() {
Expand All @@ -114,7 +117,7 @@ void mousePressed() {
void createNormalMap() {
colorMode(RGB);

normalMap = createGraphics(image.width, image.height + 100);
normalMap = createGraphics(image.width, image.height);
normalMap.beginDraw();
normalMap.background(color(
map(0, -1, 1, 0, 255),
Expand Down Expand Up @@ -250,20 +253,21 @@ void drawCurrentImage() {
}
loadPixels();
if (updateImage) {
for (int i = 0; i < currentImage.size(); i++) {
pixels[i] = color(map(currentImage.get(i).size(), 0, maxLight, 0, 255));
for (int y = 0; y < image.height; y++) {
for (int x = 0; x < image.width; x++) {
pixels[x + y * width] = color(map(currentImage.get(x + y * image.width).size(), 0, maxLight, 0, 255));
}
}
} else {
float t = cos((frameCount - frameOffset) * 0.01)*0.5 + 0.5;

for (int i = 0; i < currentImage.size(); i ++) {
int x = i % image.width;
int y = i / image.width;

for (MovedPixel pixel : currentImage.get(i)) {
float px = lerp(pixel.ogX, x, t);
float py = lerp(pixel.ogY, y, t);
pixels[floor(px) + floor(py) * image.width] = color(red(pixels[floor(px) + floor(py) * image.width]) + 255.0/maxLight);
for (int y = 0; y < image.height; y++) {
for (int x = 0; x < image.width; x++) {
for (MovedPixel pixel : currentImage.get(x + y * image.width)) {
float px = lerp(pixel.ogX, x, t);
float py = lerp(pixel.ogY, y, t);
pixels[floor(px) + floor(py) * width] = color(red(pixels[floor(px) + floor(py) * width]) + 255.0/maxLight);
}
}
}
}
Expand Down
Binary file added ImageCausticsGenerator/resources/albert.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
Binary file added ImageCausticsGenerator/resources/skull.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions ImageCausticsViewer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
frames
110 changes: 110 additions & 0 deletions ImageCausticsViewer/ImageCausticsViewer.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@

float refractiveIndex = 1.45;
float brightness = 25.0;
float projectionDistance = 1.0;

PImage rawNormalMap;
PGraphics normalMap;
PVector[] normals;

float pitch = 0.0;
float yaw = 0.0;

PMatrix rotation;

void settings() {
rawNormalMap = loadImage("resources/normals_lion.png");
size(rawNormalMap.width + rawNormalMap.width % 2, rawNormalMap.height + rawNormalMap.height % 2); // Just making sure the size is even
}

void setup() {
normalMap = createGraphics(rawNormalMap.width, rawNormalMap.height);
// Smooth normal map
normalMap.beginDraw();
normalMap.image(rawNormalMap, 0, 0);
normalMap.filter(BLUR, 0); // It's very sensitive to blurring

// Calculate normals from normal map
normalMap.loadPixels();
normals = new PVector[normalMap.pixels.length];
for (int i = 0; i < normals.length; i ++) {
normals[i] = new PVector(
map(red(normalMap.pixels[i]), 0, 255, -1, 1),
map(green(normalMap.pixels[i]), 0, 255, -1, 1),
map(blue(normalMap.pixels[i]), 0, 255, -1, 1)
);
}
normalMap.endDraw();
}

void draw() {

if (keyPressed && key == CODED) {
if (keyCode == UP) {
projectionDistance = max(projectionDistance - 0.05, 0);
println(projectionDistance);
} else if (keyCode == DOWN) {
projectionDistance = max(projectionDistance + 0.05, 0);
println(projectionDistance);
}
}


pitch = map(mouseY, 0, height, PI/4, -PI/4);
yaw = map(mouseX, 0, width, -PI/4, PI/4);

PMatrix3D rotation = new PMatrix3D();

rotation.rotateX(pitch);
rotation.rotateY(yaw);

background(0);
loadPixels();

PVector imageBasisX = mult(rotation, new PVector(1, 0));
PVector imageBasisY = mult(rotation, new PVector(0, 1));
PVector mapOffset = new PVector(normalMap.width/2, normalMap.height/2);

PVector l = refract(new PVector(0, 0, 1), mult(rotation, new PVector(0, 0, 1)), 1/refractiveIndex);

for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
PVector coords = new PVector(x - width/2, y - height/2);
float transformedY = (coords.y - coords.x * imageBasisX.y / imageBasisX.x) / (imageBasisY.y - imageBasisY.x * imageBasisX.y / imageBasisX.x);
float transformedX = (coords.x - transformedY * imageBasisY.x) / imageBasisX.x;
PVector transformed = new PVector(transformedX, transformedY);
transformed.z = transformed.x * imageBasisX.z + transformed.y * imageBasisY.z;
transformed.add(mapOffset);

if (transformed.x >= 0 && transformed.x < normalMap.width && transformed.y >= 0 && transformed.y < normalMap.height) {
PVector normal = mult(rotation, normals[floor(transformed.x) + floor(transformed.y) * normalMap.width]);

PVector v = refract(l, normal, refractiveIndex);
PVector projected = new PVector(x, y).add(v.mult(max(normalMap.width, normalMap.height) * projectionDistance + transformed.z));

int px = floor(projected.x);
int py = floor(projected.y);
if (px >= 0 && px < width && py >= 0 && py < height) {
pixels[px + py * width] = color(red(pixels[px + py * width]) + brightness);
}

//pixels[x + y * width] = color(map(transformed.x, 0, normalMap.width, 0, 255), map(transformed.y, 0, normalMap.height, 0, 255), transformed.z);
} else {
pixels[x + y * width] = color(red(pixels[x + y * width]) + brightness);
}
}
}
updatePixels();
}

PVector refract(PVector l, PVector n, float r) {
return PVector.mult(l, r).sub(PVector.mult(n, r * n.dot(l) - sqrt(1 - r * r * (1 - pow(n.dot(l), 2)))));
}

PVector mult(PMatrix3D mat, PVector vec) {
return new PVector(
mat.m00 * vec.x + mat.m01 * vec.y + mat.m02 * vec.z,
mat.m10 * vec.x + mat.m11 * vec.y + mat.m12 * vec.z,
mat.m20 * vec.x + mat.m21 * vec.y + mat.m22 * vec.z
);
}
Binary file added ImageCausticsViewer/resources/normals.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ImageCausticsViewer/resources/normals_albert.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ImageCausticsViewer/resources/normals_lion.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ImageCausticsViewer/resources/normals_lion_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ImageCausticsViewer/resources/normals_moon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ImageCausticsViewer/resources/normals_skull.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit bfee09b

Please sign in to comment.