Intersection shader
This commit is contained in:
parent
ebc4127472
commit
133c703160
2 changed files with 39 additions and 0 deletions
39
shaders/glsl/raytracingintersection/intersection.rint
Normal file
39
shaders/glsl/raytracingintersection/intersection.rint
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
/* Copyright (c) 2023, Sascha Willems
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: MIT
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#version 460
|
||||||
|
#extension GL_EXT_ray_tracing : require
|
||||||
|
|
||||||
|
struct Sphere {
|
||||||
|
vec3 center;
|
||||||
|
float radius;
|
||||||
|
vec4 color;
|
||||||
|
};
|
||||||
|
layout(binding = 3, set = 0) buffer Spheres { Sphere s[]; } spheres;
|
||||||
|
|
||||||
|
// Ray-sphere intersection
|
||||||
|
// By Inigo Quilez, from https://iquilezles.org/articles/spherefunctions/
|
||||||
|
float sphIntersect(const Sphere s, vec3 ro, vec3 rd)
|
||||||
|
{
|
||||||
|
vec3 oc = ro - s.center;
|
||||||
|
float b = dot(oc, rd);
|
||||||
|
float c = dot(oc, oc) - s.radius * s.radius;
|
||||||
|
float h = b * b - c;
|
||||||
|
if (h < 0.0) {
|
||||||
|
return -1.0;
|
||||||
|
}
|
||||||
|
h = sqrt(h);
|
||||||
|
return -b - h;
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
Sphere sphere = spheres.s[gl_PrimitiveID];
|
||||||
|
float hit = sphIntersect(sphere, gl_WorldRayOriginEXT, gl_WorldRayDirectionEXT);
|
||||||
|
|
||||||
|
if (hit > 0) {
|
||||||
|
reportIntersectionEXT(hit, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
shaders/glsl/raytracingintersection/intersection.rint.spv
Normal file
BIN
shaders/glsl/raytracingintersection/intersection.rint.spv
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue