Skip to content
Snippets Groups Projects
Commit fbb8f73c authored by Alex Laptop's avatar Alex Laptop
Browse files

Plane now working

parent c5ad8b0d
No related branches found
No related tags found
1 merge request!101Path tracing
...@@ -27,7 +27,7 @@ struct Sphere{ ...@@ -27,7 +27,7 @@ struct Sphere{
struct Plane{ struct Plane{
vec3 center; vec3 center;
float padding0; int materialIndex;
vec3 N; vec3 N;
float padding1; float padding1;
vec2 extent; vec2 extent;
...@@ -59,7 +59,7 @@ layout( push_constant ) uniform constants{ ...@@ -59,7 +59,7 @@ layout( push_constant ) uniform constants{
int planeCount; int planeCount;
}; };
bool ray_intersect(const vec3 origin, const vec3 dir, out float t0, const int id){ bool raySphereIntersect(const vec3 origin, const vec3 dir, out float t0, const int id){
vec3 L = inSpheres[id].center - origin; vec3 L = inSpheres[id].center - origin;
float tca = dot(L, dir); float tca = dot(L, dir);
float d2 = dot(L, L) - tca * tca; float d2 = dot(L, L) - tca * tca;
...@@ -78,6 +78,30 @@ bool ray_intersect(const vec3 origin, const vec3 dir, out float t0, const int id ...@@ -78,6 +78,30 @@ bool ray_intersect(const vec3 origin, const vec3 dir, out float t0, const int id
return true; return true;
} }
// see: https://www.scratchapixel.com/lessons/3d-basic-rendering/minimal-ray-tracer-rendering-simple-shapes/ray-plane-and-ray-disk-intersection
bool rayPlaneIntersect(const vec3 origin, vec3 direction, out float intersectionDistance, const int id){
Plane plane = inPlanes[id];
vec3 toPlane = plane.center - origin;
float denom = dot(direction, plane.N);
if(abs(denom) < 0.001)
return false;
intersectionDistance = dot(toPlane, plane.N) / denom;
if(intersectionDistance < 0)
return false;
vec3 intersection = origin + direction * intersectionDistance;
vec3 right = plane.N.x < 0.99 ? vec3(1, 0, 0) : vec3(0, 0, 1);
vec3 up = normalize(cross(plane.N, right));
right = cross(plane.N, up);
vec3 centerToIntersection = intersection - plane.center;
float projectedRight = dot(centerToIntersection, right);
float projectedUp = dot(centerToIntersection, up);
return abs(projectedRight) < plane.extent.x && abs(projectedUp) < plane.extent.y;
}
struct Intersection{ struct Intersection{
bool hit; bool hit;
vec3 pos; vec3 pos;
...@@ -93,7 +117,7 @@ Intersection sceneIntersect(const vec3 rayOrigin, const vec3 rayDirection) { ...@@ -93,7 +117,7 @@ Intersection sceneIntersect(const vec3 rayOrigin, const vec3 rayDirection) {
for (int i = 0; i < sphereCount; i++) { for (int i = 0; i < sphereCount; i++) {
float d; float d;
if (ray_intersect(rayOrigin, rayDirection, d, i)) { if (raySphereIntersect(rayOrigin, rayDirection, d, i)) {
intersection.hit = true; intersection.hit = true;
...@@ -105,6 +129,20 @@ Intersection sceneIntersect(const vec3 rayOrigin, const vec3 rayDirection) { ...@@ -105,6 +129,20 @@ Intersection sceneIntersect(const vec3 rayOrigin, const vec3 rayDirection) {
} }
} }
} }
for (int i = 0; i < planeCount; i++){
float d;
if (rayPlaneIntersect(rayOrigin, rayDirection, d, i)) {
intersection.hit = true;
if(d < min_d){
min_d = d;
intersection.pos = rayOrigin + rayDirection * d;
intersection.N = inPlanes[i].N;
intersection.material = inMaterials[inPlanes[i].materialIndex];
}
}
}
return intersection; return intersection;
} }
...@@ -114,7 +152,7 @@ vec3 biasHitPosition(vec3 hitPos, vec3 rayDirection, vec3 N){ ...@@ -114,7 +152,7 @@ vec3 biasHitPosition(vec3 hitPos, vec3 rayDirection, vec3 N){
} }
vec3 computeHitLighting(Intersection intersection, vec3 V, out float outReflectionThroughput){ vec3 computeHitLighting(Intersection intersection, vec3 V, out float outReflectionThroughput){
float lightIntensityDiffuse = 0; float lightIntensityDiffuse = 0;
float lightIntensitySpecular = 0; float lightIntensitySpecular = 0;
......
...@@ -33,10 +33,11 @@ namespace temp { ...@@ -33,10 +33,11 @@ namespace temp {
}; };
struct Plane { struct Plane {
Plane(const glm::vec3& c, const glm::vec3& n, const glm::vec2 e) : center(c), normal(n), extent(e) {} Plane(const glm::vec3& c, const glm::vec3& n, const glm::vec2 e, int m)
: center(c), normal(n), extent(e), materialIndex(m) {}
glm::vec3 center; glm::vec3 center;
float padding0; uint32_t materialIndex;
glm::vec3 normal; glm::vec3 normal;
float padding1; float padding1;
glm::vec2 extent; glm::vec2 extent;
...@@ -79,9 +80,9 @@ int main(int argc, const char** argv) { ...@@ -79,9 +80,9 @@ int main(int argc, const char** argv) {
materials.emplace_back(temp::Material(glm::vec3(0.9, 0.1, 0.0), glm::vec3(0.3, 0.1, 0.1), 10.f)); materials.emplace_back(temp::Material(glm::vec3(0.9, 0.1, 0.0), glm::vec3(0.3, 0.1, 0.1), 10.f));
materials.emplace_back(temp::Material(glm::vec3(0.0, 10.0, 0.8), glm::vec3(1.0, 1.0, 1.0), 1425.f)); materials.emplace_back(temp::Material(glm::vec3(0.0, 10.0, 0.8), glm::vec3(1.0, 1.0, 1.0), 1425.f));
const int ivoryIndex = 0; const uint32_t ivoryIndex = 0;
const int rubberIndex = 1; const uint32_t rubberIndex = 1;
const int mirrorIndex = 2; const uint32_t mirrorIndex = 2;
std::vector<temp::Sphere> spheres; std::vector<temp::Sphere> spheres;
spheres.emplace_back(temp::Sphere(glm::vec3(-3.0, 0.0, 16), 2, ivoryIndex)); spheres.emplace_back(temp::Sphere(glm::vec3(-3.0, 0.0, 16), 2, ivoryIndex));
...@@ -90,7 +91,7 @@ int main(int argc, const char** argv) { ...@@ -90,7 +91,7 @@ int main(int argc, const char** argv) {
spheres.emplace_back(temp::Sphere(glm::vec3( 7.0, 5.0, 18), 4, mirrorIndex)); spheres.emplace_back(temp::Sphere(glm::vec3( 7.0, 5.0, 18), 4, mirrorIndex));
std::vector<temp::Plane> planes; std::vector<temp::Plane> planes;
planes.emplace_back(temp::Plane(glm::vec3(-3.0, 0.0, 16), glm::vec3(0, 1, 0), glm::vec2(1))); planes.emplace_back(temp::Plane(glm::vec3(0, -1, 0), glm::vec3(0, 1, 0), glm::vec2(3), ivoryIndex));
std::vector<temp::Light> lights; std::vector<temp::Light> lights;
lights.emplace_back(temp::Light(glm::vec3(-20, 20, 20), 1.5)); lights.emplace_back(temp::Light(glm::vec3(-20, 20, 20), 1.5));
...@@ -172,12 +173,14 @@ int main(int argc, const char** argv) { ...@@ -172,12 +173,14 @@ int main(int argc, const char** argv) {
glm::mat4 viewToWorld; glm::mat4 viewToWorld;
int32_t lightCount; int32_t lightCount;
int32_t sphereCount; int32_t sphereCount;
int32_t planeCount;
}; };
RaytracingPushConstantData raytracingPushData; RaytracingPushConstantData raytracingPushData;
raytracingPushData.viewToWorld = glm::inverse(cameraManager.getActiveCamera().getView());
raytracingPushData.lightCount = lights.size(); raytracingPushData.lightCount = lights.size();
raytracingPushData.sphereCount = spheres.size(); raytracingPushData.sphereCount = spheres.size();
raytracingPushData.viewToWorld = glm::inverse(cameraManager.getActiveCamera().getView()); raytracingPushData.planeCount = planes.size();
vkcv::PushConstants pushConstantsCompute(sizeof(RaytracingPushConstantData)); vkcv::PushConstants pushConstantsCompute(sizeof(RaytracingPushConstantData));
pushConstantsCompute.appendDrawcall(raytracingPushData); pushConstantsCompute.appendDrawcall(raytracingPushData);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment