Switch RayTracerSimple to Spectrum

This commit is contained in:
kappa 2023-09-26 15:46:33 +02:00
parent 9edea04d9c
commit f2e20940bc
14 changed files with 1713 additions and 144 deletions

View file

@ -1,93 +1,14 @@
package xyz.marsavic.gfxlab;
import javafx.util.Pair;
import java.util.Arrays;
import java.util.Comparator;
@FunctionalInterface
public interface Spectrum {
static final double minWavelength = 380;
static final double maxWavelength = 750;
public double at(double wavelength);
public final Spectrum WHITE = w -> 1.0;
public final Spectrum BLACK = w -> 0.0;
}
class DoublePairKeyComparator implements Comparator<Pair<Double, Double>> {
@Override
public int compare(Pair<Double, Double> o1, Pair<Double, Double> o2) {
double k1 = o1.getKey(), k2 = o2.getKey();
if (k1 > k2)
return 1;
else if (k1 < k2) {
return -1;
} else {
return 0;
}
}
}
class SplineSpectrum implements Spectrum {
Pair<Double, Double>[] samples;
double[] m;
public SplineSpectrum(Pair<Double, Double>[] samples) {
if (samples == null) {
throw new NullPointerException();
}
this.samples = samples;
int n = samples.length;
double[] d = new double[n - 1];
m = new double[n];
for (int i = 1; i < samples.length; i++) {
double h = samples[i].getKey() - samples[i - 1].getKey();
if (h <= 0.0)
throw new IllegalArgumentException("Samples must have strictly increasing x coordinates.");
d[i] = (samples[i].getValue() - samples[i - 1].getValue());
}
m[0] = d[0];
for (int i = 1; i < n - 1; i++) {
if (d[i] == 0.0) {
m[i] = 0.0;
m[i + 1] = 0.0;
} else {
double a = m[i] / d[i];
double b = m[i + 1] / d[i];
double h = a*a+b*b;
if (h > 9.0) {
double t = 3.0 / h;
m[i] = t * a * d[i];
m[i + 1] = t * b * d[i];
}
}
}
}
@Override
public double at(double wavelength) {
final int n = samples.length;
if (wavelength <= samples[0].getKey()) {
return samples[0].getValue();
}
if (wavelength >= samples[n-1].getKey()) {
return samples[n-1].getValue();
}
Double.valueOf(wavelength);
int i = Arrays.binarySearch(samples,
new Pair<Double, Double>(Double.valueOf(wavelength), null),
new DoublePairKeyComparator());
if (i >= 0) {
return samples[i].getValue();
}
i = -(i+1);
double h = samples[i+1].getKey() - samples[i].getKey();
double t = (wavelength - samples[i].getKey()) / h;
return (samples[i].getValue() * (1 + 2*t) + h*m[i]*t) * (1 - t) * (1 - t)
+ (samples[i+1].getValue() * (3 - 2*t) + h * m[i+1] * (t - 1)) * t * t;
}
}

View file

@ -0,0 +1,86 @@
package xyz.marsavic.gfxlab;
import javafx.util.Pair;
import java.util.Arrays;
import java.util.Comparator;
public class SplineSpectrum implements Spectrum {
Pair<Double, Double>[] samples;
double[] m;
public SplineSpectrum(Pair<Double, Double>[] samples) {
if (samples == null) {
throw new NullPointerException();
}
this.samples = samples;
int n = samples.length;
double[] d = new double[n - 1];
m = new double[n];
for (int i = 1; i < samples.length; i++) {
double h = samples[i].getKey() - samples[i - 1].getKey();
if (h <= 0.0)
throw new IllegalArgumentException("Samples must have strictly increasing x coordinates.");
d[i] = (samples[i].getValue() - samples[i - 1].getValue());
}
m[0] = d[0];
for (int i = 1; i < n - 1; i++) {
if (d[i] == 0.0) {
m[i] = 0.0;
m[i + 1] = 0.0;
} else {
double a = m[i] / d[i];
double b = m[i + 1] / d[i];
double h = a*a+b*b;
if (h > 9.0) {
double t = 3.0 / h;
m[i] = t * a * d[i];
m[i + 1] = t * b * d[i];
}
}
}
}
@Override
public double at(double wavelength) {
final int n = samples.length;
if (wavelength <= samples[0].getKey()) {
return samples[0].getValue();
}
if (wavelength >= samples[n-1].getKey()) {
return samples[n-1].getValue();
}
Double.valueOf(wavelength);
int i = Arrays.binarySearch(samples,
new Pair<Double, Double>(Double.valueOf(wavelength), null),
new DoublePairKeyComparator());
if (i >= 0) {
return samples[i].getValue();
}
i = -(i+1);
double h = samples[i+1].getKey() - samples[i].getKey();
double t = (wavelength - samples[i].getKey()) / h;
return (samples[i].getValue() * (1 + 2*t) + h*m[i]*t) * (1 - t) * (1 - t)
+ (samples[i+1].getValue() * (3 - 2*t) + h * m[i+1] * (t - 1)) * t * t;
}
}
class DoublePairKeyComparator implements Comparator<Pair<Double, Double>> {
@Override
public int compare(Pair<Double, Double> o1, Pair<Double, Double> o2) {
double k1 = o1.getKey(), k2 = o2.getKey();
if (k1 > k2)
return 1;
else if (k1 < k2) {
return -1;
} else {
return 0;
}
}
}

View file

@ -1,20 +1,21 @@
package xyz.marsavic.gfxlab.graphics3d;
import xyz.marsavic.gfxlab.Color;
import xyz.marsavic.gfxlab.Spectrum;
import xyz.marsavic.gfxlab.Vec3;
/** Point light. */
public record Light(
Vec3 p,
Color c
Spectrum s
) {
public static Light pc(Vec3 p, Color c) {
return new Light(p, c);
public static Light pc(Vec3 p, Spectrum s) {
return new Light(p, s);
}
public static Light p(Vec3 p) {
return pc(p, Color.WHITE);
return pc(p, wavelength -> 0);
}
}

View file

@ -1,30 +1,42 @@
package xyz.marsavic.gfxlab.graphics3d;
import javafx.util.Pair;
import xyz.marsavic.gfxlab.Color;
import xyz.marsavic.gfxlab.Spectrum;
import xyz.marsavic.gfxlab.SplineSpectrum;
public record Material(
Color diffuse,
Color specular,
Spectrum diffuse,
Spectrum specular,
double shininess,
Color reflective,
Color refractive,
double refractiveIndex
Spectrum reflective,
Spectrum refractive,
Spectrum refractiveIndex
) {
public Material diffuse (Color diffuse ) { return new Material(diffuse, specular, shininess, reflective, refractive, refractiveIndex); }
public Material specular (Color specular ) { return new Material(diffuse, specular, shininess, reflective, refractive, refractiveIndex); }
public Material diffuse (Spectrum diffuse) { return new Material(diffuse, specular, shininess, reflective, refractive, refractiveIndex); }
public Material specular (Spectrum specular ) { return new Material(diffuse, specular, shininess, reflective, refractive, refractiveIndex); }
public Material shininess (double shininess ) { return new Material(diffuse, specular, shininess, reflective, refractive, refractiveIndex); }
public Material reflective (Color reflective ) { return new Material(diffuse, specular, shininess, reflective, refractive, refractiveIndex); }
public Material refractive (Color refractive ) { return new Material(diffuse, specular, shininess, reflective, refractive, refractiveIndex); }
public Material refractiveIndex(double refractiveIndex) { return new Material(diffuse, specular, shininess, reflective, refractive, refractiveIndex); }
public Material reflective (Spectrum reflective ) { return new Material(diffuse, specular, shininess, reflective, refractive, refractiveIndex); }
public Material refractive (Spectrum refractive ) { return new Material(diffuse, specular, shininess, reflective, refractive, refractiveIndex); }
public Material refractiveIndex(Spectrum refractiveIndex) { return new Material(diffuse, specular, shininess, reflective, refractive, refractiveIndex); }
// Since refractive index is a function from wavelength to a real number, it can be viewed as a spectrum
public static final Material BLACK = new Material(Color.BLACK, Color.BLACK, 32, Color.BLACK, Color.BLACK, 1.5);
public static final Material BLACK = new Material(w -> 0, w -> 0, 32, w -> 0, w -> 0, w -> 1.5);
public static Material matte (Color c) { return BLACK.diffuse(c); }
public static Material matte (double k) { return matte(Color.gray(k)); }
public static Material matte ( ) { return matte(Color.WHITE); }
public static Material matte (Spectrum s) { return BLACK.diffuse(s); }
public static Material matte (double k) { return matte(w -> k); }
public static Material matte ( ) { return matte(w -> 1.0); } // TODO: potentially have to replace with D65
public static final Material MATTE = matte();
public static final Material MIRROR = BLACK.reflective(Color.WHITE);
public static final Material GLASS = BLACK.refractive(Color.WHITE).refractiveIndex(1.5);
public static final Material MIRROR = BLACK.reflective(new SplineSpectrum(new Pair[]{
new Pair<Double, Double>(248.0, 92.6),
new Pair<Double, Double>(400.0, 92.0),
new Pair<Double, Double>(532.0, 91.6),
new Pair<Double, Double>(633.0, 90.7),
new Pair<Double, Double>(800.0, 86.8)
}));
public static final Material GLASS = BLACK.refractive(w -> 1.0)
.refractiveIndex(w -> 1.6 + (w-400)/(800-400) * (1.55 - 1.6)); /* Made to roughly resemble refractive index
of BaK4 crown glass*/
}

View file

@ -1,6 +1,7 @@
package xyz.marsavic.gfxlab.graphics3d;
import xyz.marsavic.gfxlab.Color;
import xyz.marsavic.gfxlab.Spectrum;
import java.util.ArrayList;
import java.util.Collection;
@ -12,16 +13,13 @@ public interface Scene {
Collection<Light> lights();
default Color colorBackground() {
return Color.BLACK;
}
Spectrum backgroundSpectrum = wavelength -> 0;
class Base implements Scene {
protected Solid solid;
protected final List<Light> lights = new ArrayList<>();
protected final Color colorBackground = Color.BLACK;
@Override
public Solid solid() {
@ -33,10 +31,7 @@ public interface Scene {
return lights;
}
@Override
public Color colorBackground() {
return colorBackground;
}
public final Spectrum backgroundSpectrum = wavelength -> 0;
}

View file

@ -4,11 +4,12 @@ import xyz.marsavic.gfxlab.Color;
import xyz.marsavic.gfxlab.Vec3;
import xyz.marsavic.gfxlab.graphics3d.*;
public class RayTracerSimple extends RayTracer {
private static final int spectrumSamples = 20;
static final double minWavelength = 380;
static final double maxWavelength = 780;
private static final double EPSILON = 1e-9;
public RayTracerSimple(Scene scene, Camera camera) {
super(scene, camera);
}
@ -19,13 +20,26 @@ public class RayTracerSimple extends RayTracer {
}
protected Color sample(Ray ray, int depthRemaining) {
double x = 0.0, y = 0.0, z = 0.0;
for (int i = 0; i < spectrumSamples; i++) {
double wavelength = minWavelength + (((double) i)/spectrumSamples)*(maxWavelength - minWavelength);
double intensity = sample(ray, depthRemaining, wavelength);
x += intensity * Xyz.x[(int) wavelength];
y += intensity * Xyz.y[(int) wavelength];
z += intensity * Xyz.z[(int) wavelength];
}
return Color.xyz(x,y,z);
}
protected double sample(Ray ray, int depthRemaining, double wavelength) {
if (depthRemaining == 0) {
return Color.BLACK;
return 0.0;
}
Hit hit = scene.solid().firstHit(ray, EPSILON);
if (hit == null) {
return scene.colorBackground();
return scene.backgroundSpectrum.at(wavelength);
}
Vec3 p = ray.at(hit.t()); // The hit point
@ -35,8 +49,8 @@ public class RayTracerSimple extends RayTracer {
Vec3 r = GeometryUtils.reflectedN(n_, i); // Reflected ray (i reflected over n)
Vec3 r_ = r.div(lI); // Reflected ray (i reflected over n)
Color lightDiffuse = Color.BLACK; // The sum of diffuse contributions from all the lights
Color lightSpecular = Color.BLACK; // The sum of specular contributions from all the lights
double lightDiffuse = 0.0; // The sum of diffuse contributions from all the lights
double lightSpecular = 0.0; // The sum of specular contributions from all the lights
Material material = hit.material();
@ -51,34 +65,35 @@ public class RayTracerSimple extends RayTracer {
double cosLN = n_.dot(l) / lL; // Cosine of the angle between l and n_
if (cosLN > 0) { // If the light is above the surface
Color irradiance = light.c().mul(cosLN / lLSqr);
double irradiance = light.s().at(wavelength) * cosLN / lLSqr;
// The irradiance represents how much light is received by a unit area of the surface. It is
// proportional to the cosine of the incoming angle and inversely proportional to the distance squared
// (inverse-square law).
lightDiffuse = lightDiffuse.add(irradiance);
lightDiffuse = lightDiffuse+irradiance;
double cosLR = l.dot(r_);
if (cosLR > 0) { // If the angle between l and r is acute
cosLR /= lL;
lightSpecular = lightSpecular.add(irradiance.mul(Math.pow(cosLR, material.shininess())));
lightSpecular = lightSpecular + irradiance * Math.pow(cosLR, material.shininess());
}
}
}
Color result = Color.BLACK;
result = result.add(material.diffuse ().mul(lightDiffuse ));
result = result.add(material.specular().mul(lightSpecular));
double result = 0.0;
result += material.diffuse().at(wavelength) * lightDiffuse;
result += material.specular().at(wavelength) * lightSpecular;
if (material.reflective().notZero()) {
double reflective = material.reflective().at(wavelength);
if (reflective != 0.0) {
// When material has reflective properties we recursively find the color visible along the ray (p, r).
Color lightReflected = sample(Ray.pd(p, r), depthRemaining - 1);
result = result.add(material.reflective().mul(lightReflected));
double lightReflected = sample(Ray.pd(p, r), depthRemaining - 1, wavelength);
result += material.reflective().at(wavelength) * lightReflected;
}
if (material.refractive().notZero()) {
if (material.refractive().at(wavelength) != 0.0) {
Vec3 b; // refracted light vector
double rInd = 1/material.refractiveIndex();
double rInd = 1/material.refractiveIndex().at(wavelength);
double iCosN = i.dot(n_);
if (iCosN < 0) {
@ -98,11 +113,10 @@ public class RayTracerSimple extends RayTracer {
}
b = bRejection.add(bProjection);
}
Color lightRefracted = sample(Ray.pd(p, b), depthRemaining - 1);
result = result.add(material.refractive().mul(lightRefracted));
double lightRefracted = sample(Ray.pd(p, b), depthRemaining - 1, wavelength);
result = result + material.refractive().at(wavelength) * lightRefracted;
}
return result;
}
}

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,7 @@
/*
package xyz.marsavic.gfxlab.graphics3d.scene;
import xyz.marsavic.gfxlab.Color;
import xyz.marsavic.gfxlab.Vec3;
import xyz.marsavic.gfxlab.graphics3d.Light;
@ -56,3 +58,4 @@ public class DiscoRoom extends Scene.Base {
}
}
*/

View file

@ -1,3 +1,4 @@
/*
package xyz.marsavic.gfxlab.graphics3d.scene;
import xyz.marsavic.geometry.Vector;
@ -50,3 +51,4 @@ public class Mirrors extends Scene.Base {
}
}
*/

View file

@ -1,5 +1,7 @@
/*
package xyz.marsavic.gfxlab.graphics3d.scene;
import xyz.marsavic.gfxlab.Color;
import xyz.marsavic.gfxlab.Vec3;
import xyz.marsavic.gfxlab.graphics3d.Light;
@ -48,3 +50,4 @@ public class RefractionTest extends Scene.Base {
}
}
*/

View file

@ -1,4 +1,4 @@
package xyz.marsavic.gfxlab.graphics3d.scene;
/* package xyz.marsavic.gfxlab.graphics3d.scene;
import xyz.marsavic.gfxlab.Color;
import xyz.marsavic.gfxlab.Vec3;
@ -32,3 +32,4 @@ public class SceneTest1 extends Scene.Base{
}
}
*/

View file

@ -0,0 +1,65 @@
package xyz.marsavic.gfxlab.graphics3d.scene;
import javafx.util.Pair;
import xyz.marsavic.gfxlab.Spectrum;
import xyz.marsavic.gfxlab.SplineSpectrum;
import xyz.marsavic.gfxlab.Vec3;
import xyz.marsavic.gfxlab.graphics3d.Light;
import xyz.marsavic.gfxlab.graphics3d.Material;
import xyz.marsavic.gfxlab.graphics3d.Scene;
import xyz.marsavic.gfxlab.graphics3d.Solid;
import xyz.marsavic.gfxlab.graphics3d.solids.Ball;
import xyz.marsavic.gfxlab.graphics3d.solids.Group;
import xyz.marsavic.gfxlab.graphics3d.solids.HalfSpace;
import xyz.marsavic.gfxlab.graphics3d.textures.Grid;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
public class SpectrumTest extends Scene.Base {
public SpectrumTest() {
var materialUVWalls = Grid.standard(Spectrum.WHITE);
var materialUVWallsL = Grid.standard(new SplineSpectrum(new Pair[]{
new Pair<Double, Double>(450.0, 0.2),
new Pair<Double, Double>(550.0, 0.35),
new Pair<Double, Double>(650.0, 0.8),
}));
var materialUVWallsR = Grid.standard(new SplineSpectrum(new Pair[]{
new Pair<Double, Double>(450.0, 0.3),
new Pair<Double, Double>(550.0, 0.75),
new Pair<Double, Double>(650.0, 0.3),
}));
Collection<Solid> solids = new ArrayList<>();
Collections.addAll(solids,
HalfSpace.pn(Vec3.xyz(-1, 0, 0), Vec3.xyz( 1, 0, 0), materialUVWallsL),
HalfSpace.pn(Vec3.xyz( 1, 0, 0), Vec3.xyz(-1, 0, 0), materialUVWallsR),
HalfSpace.pn(Vec3.xyz( 0, -1, 0), Vec3.xyz( 0, 1, 0), materialUVWalls),
HalfSpace.pn(Vec3.xyz( 0, 1, 0), Vec3.xyz( 0, -1, 0), materialUVWalls),
HalfSpace.pn(Vec3.xyz( 0, 0, 1), Vec3.xyz( 0, 0, -1), materialUVWalls),
Ball.cr(Vec3.xyz(-0.3, 0.3, 0.0), 0.4, uv -> Material.GLASS.refractive(new SplineSpectrum(
new Pair[]{
new Pair<Double, Double>(450.0, 0.8),
new Pair<Double, Double>(550.0, 0.35),
new Pair<Double, Double>(650.0, 0.2),
}
))),
Ball.cr(Vec3.xyz( 0.4, -0.4, 0.0), 0.4, uv -> Material.GLASS),
Ball.cr(Vec3.xyz(-0.3, -0.4, -0.6), 0.4, uv -> Material.GLASS.refractiveIndex(w -> 2.5)),
Ball.cr(Vec3.xyz( 0.4, 0.3, 0.6), 0.4, uv -> Material.GLASS.refractiveIndex(w -> 0.6))
);
Collections.addAll(lights,
Light.pc(Vec3.xyz(-0.7, 0.7, -0.7), Spectrum.WHITE),
Light.pc(Vec3.xyz(-0.7, 0.7, 0.7), Spectrum.WHITE),
Light.pc(Vec3.xyz( 0.7, 0.7, -0.7), Spectrum.WHITE),
Light.pc(Vec3.xyz( 0.7, 0.7, 0.7), Spectrum.WHITE)
);
solid = Group.of(solids);
}
}

View file

@ -4,6 +4,7 @@ package xyz.marsavic.gfxlab.graphics3d.textures;
import xyz.marsavic.functions.interfaces.F1;
import xyz.marsavic.geometry.Vector;
import xyz.marsavic.gfxlab.Color;
import xyz.marsavic.gfxlab.Spectrum;
import xyz.marsavic.gfxlab.graphics3d.Material;
@ -33,22 +34,22 @@ public class Grid implements F1<Material, Vector> {
}
public static Grid standard(Color color) {
public static Grid standard(Spectrum spectrum) {
return new Grid(
Vector.xy(0.25, 0.25),
Vector.xy(0.01, 0.01),
Material.matte(color),
Material.matte(color.mul(0.75))
Material.matte(spectrum),
Material.matte(w -> spectrum.at(w) * 0.75)
);
}
public static Grid standardUnit(Color color) {
public static Grid standardUnit(Spectrum spectrum) {
return new Grid(
Vector.UNIT_DIAGONAL,
Vector.xy(1.0/64),
Material.matte(color),
Material.matte(color.mul(0.75))
Material.matte(spectrum),
Material.matte(w -> spectrum.at(w) * 0.75)
);
}
}

View file

@ -8,7 +8,7 @@ import xyz.marsavic.gfxlab.graphics3d.Affine;
import xyz.marsavic.gfxlab.graphics3d.cameras.Perspective;
import xyz.marsavic.gfxlab.graphics3d.cameras.TransformedCamera;
import xyz.marsavic.gfxlab.graphics3d.raytracers.RayTracerSimple;
import xyz.marsavic.gfxlab.graphics3d.scene.RefractionTest;
import xyz.marsavic.gfxlab.graphics3d.scene.SpectrumTest;
import xyz.marsavic.gfxlab.gui.UtilsGL;
import xyz.marsavic.gfxlab.tonemapping.ColorTransform;
import xyz.marsavic.gfxlab.tonemapping.ToneMapping;
@ -35,7 +35,7 @@ public class GfxLab {
e(RayTracerSimple::new,
// e(RefractionTest::new),
// e(DiscoRoom::new, val(16), val(16), val(0x3361EB272FEA4C62L)),
e(RefractionTest::new),
e(SpectrumTest::new),
// e(Mirrors::new, val(3)),
e(TransformedCamera::new,
e(Perspective::new, val(1.0/3)),