Include clamp for XYZ to RGB conversion

This commit is contained in:
kappa 2023-09-27 19:27:19 +02:00
parent 49d109b046
commit 27cdbbd159
5 changed files with 35 additions and 48 deletions

View file

@ -104,12 +104,21 @@ public class Color {
return oklabPolar(v.x(), v.y(), v.z()); return oklabPolar(v.x(), v.y(), v.z());
} }
static double clipDoubleXyz(double x) {
if (x < 0.0)
return 0.0;
return x;
}
public static Color xyz(double x, double y, double z) { public static Color xyz(double x, double y, double z) {
double r = 3.2404542 * x - 1.5371385 * y - 0.4985314 * z; double r = 3.2404542 * x - 1.5371385 * y - 0.4985314 * z;
double g = -0.9692660 * x + 1.8760108 * y + 0.0415560 * z; double g = -0.9692660 * x + 1.8760108 * y + 0.0415560 * z;
double b = 0.0556434 * x - 0.2040259 * y + 1.0572252 * z; double b = 0.0556434 * x - 0.2040259 * y + 1.0572252 * z;
r = clipDoubleXyz(r);
g = clipDoubleXyz(g);
b = clipDoubleXyz(b);
return Color.rgb(r,g,b); return Color.rgb(r,g,b);
} }

View file

@ -10,16 +10,6 @@ public class RayTracerSimple extends RayTracer {
private static final double minWavelength = 380; private static final double minWavelength = 380;
private static final double maxWavelength = 780; private static final double maxWavelength = 780;
private static final double EPSILON = 1e-9; private static final double EPSILON = 1e-9;
private static final double maxXYZ;
static {
double max = 0.0;
for (int i = 0; i < spectrumSamples; i++) {
double wavelength = minWavelength + (((double) i)/spectrumSamples)*(maxWavelength - minWavelength);
max += Xyz.x[(int) wavelength] + Xyz.y[(int) wavelength] + Xyz.z[(int) wavelength];
}
maxXYZ = max;
}
public RayTracerSimple(Scene scene, Camera camera) { public RayTracerSimple(Scene scene, Camera camera) {
super(scene, camera); super(scene, camera);
@ -101,11 +91,12 @@ public class RayTracerSimple extends RayTracer {
if (reflective != 0.0) { if (reflective != 0.0) {
// When material has reflective properties we recursively find the color visible along the ray (p, r). // When material has reflective properties we recursively find the color visible along the ray (p, r).
double lightReflected = sample(Ray.pd(p, r), depthRemaining - 1, wavelength); double lightReflected = sample(Ray.pd(p, r), depthRemaining - 1, wavelength);
result += material.reflective().at(wavelength) * lightReflected; result += reflective * lightReflected;
} }
double refractive = material.refractive().at(wavelength);
if (material.refractive().at(wavelength) != 0.0) { if (refractive != 0.0) {
System.out.println(refractive + " " + ray);
Vec3 b; // refracted light vector Vec3 b; // refracted light vector
double rInd = 1/material.refractiveIndex().at(wavelength); double rInd = 1/material.refractiveIndex().at(wavelength);
@ -128,7 +119,8 @@ public class RayTracerSimple extends RayTracer {
b = bRejection.add(bProjection); b = bRejection.add(bProjection);
} }
double lightRefracted = sample(Ray.pd(p, b), depthRemaining - 1, wavelength); double lightRefracted = sample(Ray.pd(p, b), depthRemaining - 1, wavelength);
result = result + material.refractive().at(wavelength) * lightRefracted; System.out.println("Light: " + lightRefracted + " direction: " + b);
result += refractive * lightRefracted;
} }
return result; return result;

View file

@ -2,6 +2,8 @@ package xyz.marsavic.gfxlab.graphics3d.scene;
import javafx.util.Pair; import javafx.util.Pair;
import xyz.marsavic.functions.interfaces.F1;
import xyz.marsavic.geometry.Vector;
import xyz.marsavic.gfxlab.Spectrum; import xyz.marsavic.gfxlab.Spectrum;
import xyz.marsavic.gfxlab.SplineSpectrum; import xyz.marsavic.gfxlab.SplineSpectrum;
import xyz.marsavic.gfxlab.Vec3; import xyz.marsavic.gfxlab.Vec3;
@ -21,42 +23,27 @@ import java.util.Collections;
public class SpectrumTest extends Scene.Base { public class SpectrumTest extends Scene.Base {
public SpectrumTest() { public SpectrumTest() {
var materialUVWalls = Grid.standard(Spectrum.WHITE); var materialUVWalls = (F1<Material, Vector>) (uv -> Material.matte(1.0));
SplineSpectrum s; SplineSpectrum s;
var materialUVWallsL = Grid.standard(s = new SplineSpectrum(new Pair[]{ var materialUVWallsL = Grid.standard(w -> 0.2);
new Pair<Double, Double>(450.0, 0.2),
new Pair<Double, Double>(500.0, 0.27), var materialUVWallsR = Grid.standard(w -> 0.3);
new Pair<Double, Double>(550.0, 0.35),
new Pair<Double, Double>(700.0, 0.8),
}));
var materialUVWallsR = Grid.standard(new SplineSpectrum(new Pair[]{
new Pair<Double, Double>(450.0, 0.1),
new Pair<Double, Double>(500.0, 0.25),
new Pair<Double, Double>(550.0, 0.2),
new Pair<Double, Double>(650.0, 0.1),
}));
Collection<Solid> solids = new ArrayList<>(); Collection<Solid> solids = new ArrayList<>();
Collections.addAll(solids, Collections.addAll(solids,
HalfSpace.pn(Vec3.xyz(-1, 0, 0), Vec3.xyz( 1, 0, 0), materialUVWallsL), HalfSpace.pn(Vec3.xyz(0, 0, 10), Vec3.xyz( 1, 0, -1), materialUVWallsL),
HalfSpace.pn(Vec3.xyz( 1, 0, 0), Vec3.xyz(-1, 0, 0), materialUVWallsR), HalfSpace.pn(Vec3.xyz( 0, 0, 10), Vec3.xyz(-1, 0, -1), materialUVWallsR),
HalfSpace.pn(Vec3.xyz( 0, -1, 0), Vec3.xyz( 0, 1, 0), materialUVWalls), HalfSpace.pn(Vec3.xyz(0, 2, 0), Vec3.xyz( 0, -1, 0), materialUVWallsL),
HalfSpace.pn(Vec3.xyz( 0, 1, 0), Vec3.xyz( 0, -1, 0), materialUVWalls), HalfSpace.pn(Vec3.xyz(0, -2, 10), Vec3.xyz( 0, 1, 0), materialUVWallsL),
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(w->0.5)), HalfSpace.pn(Vec3.xyz( 0, 0, 8), Vec3.xyz(0, 0, -1), uv -> Material.GLASS),
Ball.cr(Vec3.xyz( 0.4, -0.4, 0.0), 0.4, uv -> Material.GLASS.refractiveIndex( HalfSpace.pn(Vec3.xyz( 0, 0, -6), Vec3.xyz( 0, 0, 1), materialUVWallsR)
w -> 2.6 + (w-400)/(800-400) * (1.55 - 2.6)
)) // Ball.cr(Vec3.xyz(0.0, 0.0, 5), 0.4, uv -> Material.MIRROR)
// 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, Collections.addAll(lights,
Light.ps(Vec3.xyz(-0.7, 0.7, -0.7), Spectrum.WHITE), Light.ps(Vec3.xyz(0, 0.0, -1.0), w -> 1.0)
Light.ps(Vec3.xyz(-0.7, 0.7, 0.7), Spectrum.WHITE),
Light.ps(Vec3.xyz( 0.7, 0.7, -0.7), Spectrum.WHITE),
Light.ps(Vec3.xyz( 0.7, 0.7, 0.7), Spectrum.WHITE)
); );
solid = Group.of(solids); solid = Group.of(solids);

View file

@ -42,7 +42,6 @@ public class GfxLab {
// e(Orthographic::new), // e(Orthographic::new),
e(Affine.IDENTITY e(Affine.IDENTITY
.then(Affine.translation(Vec3.xyz(0, 0, -4))) .then(Affine.translation(Vec3.xyz(0, 0, -4)))
// .then(Affine.rotationAboutY(0.03))
) )
) )
), ),