Skip to content

Commit

Permalink
Implement proper sun intersection.
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatRedox committed Aug 20, 2024
1 parent 04c382d commit 0365cd9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,48 @@
import dev.thatredox.chunkynative.common.export.Packer;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import se.llbit.chunky.renderer.scene.sky.Sun;
import se.llbit.math.ColorUtil;

public class PackedSun implements Packer {
public final int flags;
public final long texture;
public final float intensity;
public final float altitude;
public final float azimuth;
public final float luminosity;
public final int sunColor;

public PackedSun(Sun sun, AbstractTextureLoader texturePalette) {
flags = sun.drawTexture() ? 1 : 0;
texture = texturePalette.get(Sun.texture).get();
intensity = (float) sun.getIntensity();
altitude = (float) sun.getAltitude();
azimuth = (float) sun.getAzimuth();
luminosity = (float) sun.getLuminosity();
sunColor = ColorUtil.getRGB(sun.getColor());
}

/**
* Pack the sun into 6 ints.
* Pack the sun into 8 ints.
* 0: Flags. 1 if the sun should be drawn. 0 if not.
* 1 & 2: Sun texture reference.
* 3: float sun intensity
* 4: float sun altitude
* 5: float sun azimuth
* 6: float sun luminosity
* 7: Sun color in (A)RGB
*/
@Override
public IntArrayList pack() {
IntArrayList out = new IntArrayList(6);
IntArrayList out = new IntArrayList(8);
out.add(flags);
out.add((int) (texture >>> 32));
out.add((int) texture);
out.add(Float.floatToIntBits(intensity));
out.add(Float.floatToIntBits(altitude));
out.add(Float.floatToIntBits(azimuth));
out.add(Float.floatToIntBits(luminosity));
out.add(sunColor);
return out;
}
}
19 changes: 15 additions & 4 deletions src/main/opencl/kernel/include/sky.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
#include "rt.h"
#include "textureAtlas.h"
#include "random.h"
#include "material.h"

typedef struct {
int flags;
int textureSize;
int texture;
float intensity;
float luminosity;
float3 su;
float3 sv;
float3 sw;
float4 color;
} Sun;

Sun Sun_new(__global const int* data) {
Expand All @@ -22,6 +25,8 @@ Sun Sun_new(__global const int* data) {
sun.textureSize = data[1];
sun.texture = data[2];
sun.intensity = as_float(data[3]);
sun.luminosity = as_float(data[6]);
sun.color = colorFromArgb(data[7]);

float phi = as_float(data[4]);
float theta = as_float(data[5]);
Expand Down Expand Up @@ -54,10 +59,16 @@ bool Sun_intersect(Sun self, image2d_array_t atlas, Ray ray, MaterialSample* sam
if (a >= 0 && a < width2) {
float b = M_PI_2_F - acos(dot(direction, self.sv)) + width;
if (b >= 0 && b < width2) {
float4 color = Atlas_read_uv(a / width2, b / width2,
self.texture, self.textureSize, atlas);
color *= self.intensity;
sample->color += color;
if (ray.flags & RAY_INDIRECT) {
float4 color = self.color;
color *= self.luminosity;
sample->color += color;
} else {
float4 color = Atlas_read_uv(a / width2, b / width2,
self.texture, self.textureSize, atlas);
color *= self.intensity;
sample->color += color;
}
return true;
}
}
Expand Down

0 comments on commit 0365cd9

Please # to comment.