Fixed issues with textures. Now completely working.
Also fixed bug with segfaulting on exitmain
parent
cf75e66684
commit
d4a9d729a0
Binary file not shown.
|
@ -24,3 +24,69 @@
|
|||
1 2445 1668662938007083220 CMakeFiles/Step_3.dir/src/engine/math/bvh.cpp.o 2818d3869cd677e8
|
||||
1 2042 1668663039946062342 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 9281ecbfd964c525
|
||||
2 1786 1668662937351064044 CMakeFiles/Step_3.dir/src/engine/util/parser.cpp.o e9e3fe6fe9a4de66
|
||||
9 2217 1669050816595021546 CMakeFiles/Step_3.dir/src/engine/util/parser.cpp.o 80ee4e1261cd645e
|
||||
6 2328 1669050816707024623 CMakeFiles/Step_3.dir/src/engine/util/loaders.cpp.o c81f0f24f2ebce49
|
||||
2 3533 1669050817911057707 CMakeFiles/Step_3.dir/src/engine/image/image.cpp.o 74ebce9e3746476
|
||||
3 873 1669050852736015051 CMakeFiles/Step_3.dir/src/graphics/gl/gl.c.o c243981994ce0811
|
||||
2 1948 1669050853816044755 CMakeFiles/Step_3.dir/src/engine/math/colliders.cpp.o 49528379fc85b78
|
||||
3 1980 1669050853844045523 CMakeFiles/Step_3.dir/src/graphics/gl/shader.cpp.o cdbe2fcc55d335a9
|
||||
1 2102 1669050853968048936 CMakeFiles/Step_3.dir/src/engine/globals.cpp.o 8fd37dd250c0c57b
|
||||
3 2209 1669050854076051904 CMakeFiles/Step_3.dir/src/graphics/gl/gl.cpp.o 27ebeccd2f38d7f7
|
||||
2209 2402 1669050854268057187 CMakeFiles/Step_3.dir/src/graphics/imgui/imgui_impl_glfw.cpp.o 310c2607009eba21
|
||||
2 2423 1669050854288057735 CMakeFiles/Step_3.dir/src/engine/util/debug.cpp.o 41c6f1bdaa5e38f5
|
||||
2 2460 1669050854324058727 CMakeFiles/Step_3.dir/src/engine/util/models.cpp.o cb39ce8c856d1d8c
|
||||
2 2474 1669050854340059168 CMakeFiles/Step_3.dir/src/graphics/debug_gui.cpp.o 738602a72b552e04
|
||||
2423 2603 1669050854468062689 CMakeFiles/Step_3.dir/src/graphics/imgui/imgui_impl_opengl3.cpp.o f69d69f5fadcde41
|
||||
2 2748 1669050854612066649 CMakeFiles/Step_3.dir/src/engine/math/bvh.cpp.o c8d4462e974892bb
|
||||
2402 2909 1669050854776071157 CMakeFiles/Step_3.dir/src/graphics/imgui/imgui_impl_x11.cpp.o 8002180f0368d0ae
|
||||
2 2986 1669050854848073140 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 7d8b0912b2dcd105
|
||||
2 3325 1669050855188082492 CMakeFiles/Step_3.dir/src/engine/raytracing.cpp.o afb5968833aed95e
|
||||
1980 3391 1669050855256084360 CMakeFiles/Step_3.dir/src/graphics/imgui/imgui_demo.cpp.o dc0c8b425019900b
|
||||
2 3524 1669050855388087992 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 9026a4a98b896ce1
|
||||
2603 3662 1669050855528091841 CMakeFiles/Step_3.dir/src/graphics/input.cpp.o 572a0a724d80e2d3
|
||||
873 3669 1669050855536092064 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o 8f22bfe01b4af85f
|
||||
2460 4278 1669050856144108784 CMakeFiles/Step_3.dir/src/graphics/imgui/imgui_tables.cpp.o 920916506d18de2c
|
||||
2102 4555 1669050856420116379 CMakeFiles/Step_3.dir/src/graphics/imgui/imgui_draw.cpp.o 8b0c755c9ee468e9
|
||||
2748 4681 1669050856548119900 CMakeFiles/Step_3.dir/src/opencl/cl.cpp.o 45b82f6048f98464
|
||||
2474 5294 1669050857160136734 CMakeFiles/Step_3.dir/src/graphics/imgui/imgui_widgets.cpp.o fe3a1955d79f977a
|
||||
1948 5556 1669050857420143883 CMakeFiles/Step_3.dir/src/graphics/imgui/imgui.cpp.o f512f3e5a828153f
|
||||
5556 5839 1669050857704151695 Step_3 f7680d786a7d5c95
|
||||
1 1967 1669051269279518448 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 7d8b0912b2dcd105
|
||||
1968 2275 1669051269587526981 Step_3 f7680d786a7d5c95
|
||||
2 1984 1669053527179723700 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 7d8b0912b2dcd105
|
||||
1 2481 1669053527675737789 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 9026a4a98b896ce1
|
||||
2481 2725 1669053527919744719 Step_3 f7680d786a7d5c95
|
||||
1 1872 1669053555796536345 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 7d8b0912b2dcd105
|
||||
1872 2161 1669053556084544523 Step_3 f7680d786a7d5c95
|
||||
1 1967 1669053613450172408 CMakeFiles/Step_3.dir/src/engine/globals.cpp.o 8fd37dd250c0c57b
|
||||
1 2149 1669053613634177628 CMakeFiles/Step_3.dir/src/engine/util/debug.cpp.o 41c6f1bdaa5e38f5
|
||||
2 2398 1669053613882184663 CMakeFiles/Step_3.dir/src/graphics/debug_gui.cpp.o 738602a72b552e04
|
||||
2 2488 1669053613970187158 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 7d8b0912b2dcd105
|
||||
2 2571 1669053614054189543 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o 8f22bfe01b4af85f
|
||||
1 2867 1669053614350197938 CMakeFiles/Step_3.dir/src/engine/raytracing.cpp.o afb5968833aed95e
|
||||
1 3097 1669053614582204520 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 9026a4a98b896ce1
|
||||
3097 3355 1669053614838211781 Step_3 f7680d786a7d5c95
|
||||
1 2137 1669053859941153113 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o 8f22bfe01b4af85f
|
||||
1 2146 1669053859949153340 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 7d8b0912b2dcd105
|
||||
1 2639 1669053860441167256 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 9026a4a98b896ce1
|
||||
2 2134 1669053873165527108 CMakeFiles/Step_3.dir/src/engine/raytracing.cpp.o afb5968833aed95e
|
||||
1 2043 1669053933727239285 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 7d8b0912b2dcd105
|
||||
1 2074 1669053933759240190 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o 8f22bfe01b4af85f
|
||||
1 2232 1669053933915244600 CMakeFiles/Step_3.dir/src/engine/raytracing.cpp.o afb5968833aed95e
|
||||
1 2532 1669053934215253079 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 9026a4a98b896ce1
|
||||
2532 2779 1669053934463260090 Step_3 f7680d786a7d5c95
|
||||
1 1948 1669053973612366442 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 7d8b0912b2dcd105
|
||||
2 1968 1669053973632367007 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o 8f22bfe01b4af85f
|
||||
1 2382 1669053974044378649 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 9026a4a98b896ce1
|
||||
1 2334 1669053984328669229 CMakeFiles/Step_3.dir/src/engine/raytracing.cpp.o afb5968833aed95e
|
||||
2334 2667 1669053984660678609 Step_3 f7680d786a7d5c95
|
||||
2 1957 1669055694936199936 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o 8f22bfe01b4af85f
|
||||
1 1983 1669055694960200605 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 7d8b0912b2dcd105
|
||||
1 2158 1669055695136205515 CMakeFiles/Step_3.dir/src/engine/raytracing.cpp.o afb5968833aed95e
|
||||
1 2453 1669055695428213659 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 9026a4a98b896ce1
|
||||
2453 2734 1669055695712221581 Step_3 f7680d786a7d5c95
|
||||
1 2071 1669055771390332839 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 7d8b0912b2dcd105
|
||||
1 2129 1669055771450334513 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o 8f22bfe01b4af85f
|
||||
1 2200 1669055771518336410 CMakeFiles/Step_3.dir/src/engine/raytracing.cpp.o afb5968833aed95e
|
||||
1 2623 1669055771942348242 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 9026a4a98b896ce1
|
||||
2624 2877 1669055772194355273 Step_3 f7680d786a7d5c95
|
||||
|
|
Binary file not shown.
|
@ -1,3 +1,3 @@
|
|||
Start testing: Nov 17 00:30 EST
|
||||
Start testing: Nov 21 13:36 EST
|
||||
----------------------------------------------------------
|
||||
End testing: Nov 17 00:30 EST
|
||||
End testing: Nov 21 13:36 EST
|
||||
|
|
|
@ -142,6 +142,7 @@ namespace Raytracing {
|
|||
return aMax == bMax && aMin == bMin;
|
||||
}
|
||||
|
||||
#ifndef USE_SIMD_CPU
|
||||
inline bool operator&=(const AABB& a, const AABB& b) {
|
||||
const auto& aMax = a.getMax();
|
||||
const auto& aMin = a.getMin();
|
||||
|
@ -151,6 +152,7 @@ namespace Raytracing {
|
|||
return (aMax < bMax + E && aMax > bMax - E && aMin < bMin + E && aMin > bMin - E);
|
||||
return aMax == bMax && aMin == bMin;
|
||||
}
|
||||
#endif
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& out, const AABB& v) {
|
||||
auto max = v.getMax();
|
||||
|
|
|
@ -401,7 +401,7 @@ namespace Raytracing {
|
|||
|
||||
// same as above but for right sided constants
|
||||
inline Vec4 operator*(const Vec4& v, PRECISION_TYPE c) {
|
||||
return c * v;
|
||||
return {v.x() * c, v.y() * c, v.z() * c, v.w() * c};
|
||||
}
|
||||
|
||||
// divides the vector by the constant c
|
||||
|
|
|
@ -33,7 +33,7 @@ namespace Raytracing {
|
|||
// the length of the vector from its origin in its direction.
|
||||
PRECISION_TYPE length{0};
|
||||
// Texture UV Coords.
|
||||
PRECISION_TYPE u,v;
|
||||
PRECISION_TYPE u, v;
|
||||
};
|
||||
|
||||
struct ScatterResults {
|
||||
|
@ -55,7 +55,6 @@ namespace Raytracing {
|
|||
// returns true if the ray was scattered along with the scattered ray, otherwise will return false with empty ray.
|
||||
// the returned vec4 is the attenuation color
|
||||
[[nodiscard]] virtual ScatterResults scatter(const Ray& ray, const HitData& hitData) const = 0;
|
||||
[[nodiscard]] virtual Vec4 getColor(PRECISION_TYPE u, PRECISION_TYPE v, const Vec4& point) const = 0;
|
||||
|
||||
[[nodiscard]] Vec4 getBaseColor() const { return baseColor; }
|
||||
virtual ~Material() = default;
|
||||
|
|
|
@ -21,12 +21,6 @@ namespace Raytracing {
|
|||
bool hasNormals = false;
|
||||
AABB aabb;
|
||||
|
||||
Triangle(const Vec4& v1, const Vec4& v2, const Vec4& v3): vertex1(v1), vertex2(v2), vertex3(v3) {}
|
||||
|
||||
Triangle(const Vec4& v1, const Vec4& v2, const Vec4& v3,
|
||||
const Vec4& n1, const Vec4& n2, const Vec4& n3): vertex1(v1), vertex2(v2), vertex3(v3),
|
||||
hasNormals(true), normal1(n1), normal2(n2), normal3(n3) {}
|
||||
|
||||
Triangle(const Vec4& v1, const Vec4& v2, const Vec4& v3,
|
||||
const Vec4& uv1, const Vec4& uv2, const Vec4& uv3,
|
||||
const Vec4& n1, const Vec4& n2, const Vec4& n3): vertex1(v1), vertex2(v2), vertex3(v3),
|
||||
|
|
|
@ -65,9 +65,6 @@ namespace Raytracing {
|
|||
explicit DiffuseMaterial(const Vec4& scatterColor): Material(scatterColor) {}
|
||||
|
||||
[[nodiscard]] virtual ScatterResults scatter(const Ray& ray, const HitData& hitData) const override;
|
||||
[[nodiscard]] virtual Vec4 getColor(PRECISION_TYPE u, PRECISION_TYPE v, const Vec4& point) const override {
|
||||
return this->baseColor;
|
||||
}
|
||||
};
|
||||
|
||||
class MetalMaterial : public Material {
|
||||
|
@ -80,9 +77,6 @@ namespace Raytracing {
|
|||
explicit MetalMaterial(const Vec4& metalColor): Material(metalColor) {}
|
||||
|
||||
[[nodiscard]] virtual ScatterResults scatter(const Ray& ray, const HitData& hitData) const override;
|
||||
[[nodiscard]] virtual Vec4 getColor(PRECISION_TYPE u, PRECISION_TYPE v, const Vec4& point) const override {
|
||||
return this->baseColor;
|
||||
}
|
||||
};
|
||||
|
||||
class BrushedMetalMaterial : public MetalMaterial {
|
||||
|
@ -92,20 +86,18 @@ namespace Raytracing {
|
|||
explicit BrushedMetalMaterial(const Vec4& metalColor, PRECISION_TYPE fuzzyness): MetalMaterial(metalColor), fuzzyness(fuzzyness) {}
|
||||
|
||||
[[nodiscard]] virtual ScatterResults scatter(const Ray& ray, const HitData& hitData) const override;
|
||||
[[nodiscard]] virtual Vec4 getColor(PRECISION_TYPE u, PRECISION_TYPE v, const Vec4& point) const override {
|
||||
return this->baseColor;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
class TexturedMaterial : public Material {
|
||||
protected:
|
||||
int width{}, height{}, channels{}, rowWidth{};
|
||||
int width{}, height{}, channels{};
|
||||
unsigned char* data;
|
||||
public:
|
||||
explicit TexturedMaterial(const std::string& file);
|
||||
|
||||
[[nodiscard]] virtual ScatterResults scatter(const Ray& ray, const HitData& hitData) const override;
|
||||
[[nodiscard]] virtual Vec4 getColor(PRECISION_TYPE u, PRECISION_TYPE v, const Vec4& point) const override;
|
||||
[[nodiscard]] Vec4 getColor(PRECISION_TYPE u, PRECISION_TYPE v, const Vec4& point) const;
|
||||
|
||||
~TexturedMaterial();
|
||||
};
|
||||
|
|
|
@ -25,7 +25,7 @@ namespace Raytracing {
|
|||
class DebugMenus {
|
||||
public:
|
||||
static void add(const std::shared_ptr<DebugObject>& object);
|
||||
static void remove(const std::shared_ptr<DebugObject>& object);
|
||||
static void remove(DebugObject* object);
|
||||
static void render();
|
||||
};
|
||||
class DebugBVH : public DebugObject {
|
||||
|
|
Binary file not shown.
After Width: | Height: | Size: 2.7 KiB |
|
@ -14,7 +14,7 @@ void main() {
|
|||
vec4 textureColor = texture(tex, outUv);
|
||||
//FragColor = vec4(textureColor.rgb, 1.0f);
|
||||
if (useWhite == 0)
|
||||
FragColor = vec4(vec3(1.0, 0.0f, 0.0f) * dot(lightDir, outNormal) * vec3(outUv, 1.0), 1.0f);
|
||||
FragColor = vec4(vec3(1.0, 1.0f, 0.0f) * dot(lightDir, outNormal) * vec3(outUv, 0.0), 1.0f);
|
||||
else
|
||||
FragColor = vec4(color, 1.0f);
|
||||
}
|
|
@ -135,7 +135,10 @@ int main(int argc, char** args) {
|
|||
world.add("greenMetal", new Raytracing::MetalMaterial{Raytracing::Vec4{0.4, 1.0, 0.4, 1}});
|
||||
world.add("redMetal", new Raytracing::BrushedMetalMaterial{Raytracing::Vec4{1.0, 0.4, 0.4, 1}, 0.6f});
|
||||
world.add("blueMetal", new Raytracing::MetalMaterial{Raytracing::Vec4{0.4, 0.4, 1.0, 1}});
|
||||
world.add("test", new Raytracing::TexturedMaterial{parser.getOptionValue("--resources") + "029a_-_Survival_of_the_Idiots_349.jpg"});
|
||||
world.add("magic", new Raytracing::TexturedMaterial{parser.getOptionValue("--resources") + "760213.png"});
|
||||
world.add("thinkers", new Raytracing::TexturedMaterial{parser.getOptionValue("--resources") + "1616466348379.png"});
|
||||
world.add("sponge", new Raytracing::TexturedMaterial{parser.getOptionValue("--resources") + "029a_-_Survival_of_the_Idiots_349.jpg"});
|
||||
world.add("cat", new Raytracing::TexturedMaterial{parser.getOptionValue("--resources") + "livingmylifeinstereodoesntseemthatbad.PNG"});
|
||||
|
||||
world.add(new Raytracing::SphereObject({0, -100.5, -1, 0}, 100, world.getMaterial("greenDiffuse")));
|
||||
|
||||
|
@ -144,7 +147,7 @@ int main(int argc, char** args) {
|
|||
world.add(new Raytracing::ModelObject({5, 1, 0}, house, world.getMaterial("redDiffuse")));
|
||||
world.add(new Raytracing::ModelObject({0, 0, -5}, house, world.getMaterial("blueDiffuse")));
|
||||
world.add(new Raytracing::ModelObject({0, 0, 5}, house, world.getMaterial("blueDiffuse")));
|
||||
world.add(new Raytracing::ModelObject({0, 5, 0}, debugCube, world.getMaterial("test")));
|
||||
world.add(new Raytracing::ModelObject({0, 5, 0}, debugCube, world.getMaterial("magic")));
|
||||
|
||||
if (parser.hasOption("--gui") || parser.hasOption("-g")) {
|
||||
#ifdef COMPILE_GUI
|
||||
|
|
|
@ -133,7 +133,6 @@ namespace Raytracing {
|
|||
int x = imageBounds.x + loopX;
|
||||
int y = imageBounds.y + loopY;
|
||||
Raytracing::Vec4 color;
|
||||
// TODO: profile for speed;
|
||||
for (int s = 0; s < raysPerPixel; s++) {
|
||||
// simulate anti aliasing by generating rays with very slight random directions
|
||||
color = color + raycast(camera.projectRay(x + rnd.getDouble(), y + rnd.getDouble()));
|
||||
|
|
|
@ -47,7 +47,7 @@ namespace Raytracing {
|
|||
// the hit point is where the ray is when extended to the root
|
||||
auto RayAtRoot = ray.along(root);
|
||||
// The normal of a sphere is just the point of the hit minus the center position
|
||||
auto normal = (RayAtRoot - position).normalize();
|
||||
auto normal = (RayAtRoot - position) / radius;
|
||||
|
||||
/*if (Raytracing::vec4::dot(ray.getDirection(), normal) > 0.0) {
|
||||
tlog << "ray inside sphere\n";
|
||||
|
@ -55,9 +55,10 @@ namespace Raytracing {
|
|||
tlog << "ray outside sphere\n";
|
||||
*/
|
||||
// calculate the uv coords and normalize to [0, 1]
|
||||
PRECISION_TYPE u = (atan2(-RayAtRoot.z(), RayAtRoot.x()) + std::numbers::pi) / (2 * std::numbers::pi);
|
||||
PRECISION_TYPE v = acos(RayAtRoot.y()) / std::numbers::pi;
|
||||
return {true, RayAtRoot, normal, root, clamp(u, 0, 1.0), clamp(v, 0, 1.0)};
|
||||
PRECISION_TYPE u = (atan2(-normal.z(), normal.x()) + std::numbers::pi) / (2 * std::numbers::pi);
|
||||
PRECISION_TYPE v = acos(normal.y()) / std::numbers::pi;
|
||||
// have to invert the v since we have to invert the v again later due to triangles
|
||||
return {true, RayAtRoot, normal, root, u, 1.0 - v};
|
||||
}
|
||||
|
||||
std::pair<HitData, Object*> World::checkIfHit(const Ray& ray, PRECISION_TYPE min, PRECISION_TYPE max) const {
|
||||
|
@ -147,16 +148,12 @@ namespace Raytracing {
|
|||
}
|
||||
Vec4 TexturedMaterial::getColor(PRECISION_TYPE u, PRECISION_TYPE v, const Vec4& point) const {
|
||||
// if we are unable to load the image return the debug color.
|
||||
// This causes major issues (force this to happen, you'll see), indicates issue + looks really cool.
|
||||
if (!data)
|
||||
return Vec4{0, 1, 0.2} * Vec4{u, v, 1.0};
|
||||
return Vec4{0.2, 1, 0} * Vec4{u, v, 1.0};
|
||||
|
||||
// if you render out the debug color above you'll notice that the UV coords are rotated.
|
||||
// you can also see this from the debug view, which *as of now* is rendering based on UV coords * normals * red
|
||||
// so let's transform it back and ensure that our UV coords are within image bounds.
|
||||
u = clamp(u, 0, 1);
|
||||
// fix that pesky issue
|
||||
v = 1.0 - clamp(v, 0, 1);
|
||||
u = clamp(u, 0.0, 1.0);
|
||||
// fix that pesky issue of the v being rotated 90* compared to the image
|
||||
v = 1.0 - clamp(v, 0.0, 1.0);
|
||||
|
||||
auto imageX = (int)(width * u);
|
||||
auto imageY = (int)(height * v);
|
||||
|
@ -168,7 +165,7 @@ namespace Raytracing {
|
|||
// this is best done with a single division followed by multiple multiplication.
|
||||
// since this function needs to be cheap to run.
|
||||
const PRECISION_TYPE colorFactor = 1.0 / 255.0;
|
||||
const auto pixelData = data + (imageY * rowWidth + imageX * channels);
|
||||
const auto pixelData = data + (imageY * channels * width + imageX * channels);
|
||||
|
||||
return {pixelData[0] * colorFactor, pixelData[1] * colorFactor, pixelData[2] * colorFactor};
|
||||
}
|
||||
|
@ -178,11 +175,14 @@ namespace Raytracing {
|
|||
if (!data)
|
||||
flog << "Unable to load image file " << file << "!\n";
|
||||
else
|
||||
ilog << "Loaded image " << file << "!\n";
|
||||
rowWidth = width * channels;
|
||||
ilog << "Loaded image " << file << " with " << width << " " << height << " " << channels << "!\n";
|
||||
}
|
||||
TexturedMaterial::~TexturedMaterial() {
|
||||
delete(data);
|
||||
stbi_image_free(data);
|
||||
}
|
||||
|
||||
PRECISION_TYPE sign(PRECISION_TYPE i){
|
||||
return i >= 0 ? 1 : -1;
|
||||
}
|
||||
|
||||
static HitData checkIfTriangleGotHit(const Triangle& theTriangle, const Vec4& position, const Ray& ray, PRECISION_TYPE min, PRECISION_TYPE max) {
|
||||
|
@ -219,32 +219,47 @@ namespace Raytracing {
|
|||
// ray intersects
|
||||
Vec4 rayIntersectionPoint = ray.along(t);
|
||||
Vec4 normal;
|
||||
|
||||
// calculate triangle berry centric coords
|
||||
// first we need the vector that runs between the vertex and the intersection point for all three vertices
|
||||
// we must subtract the position of the triangle from the intersection point because this calc must happen in triangle space not world space.
|
||||
// you won't believe the time it took me to figure this out, since the U coord was correct but the V coord was always 1.
|
||||
auto vertex1ToIntersect = theTriangle.vertex1 - (rayIntersectionPoint - position);
|
||||
auto vertex2ToIntersect = theTriangle.vertex2 - (rayIntersectionPoint - position);
|
||||
auto vertex3ToIntersect = theTriangle.vertex3 - (rayIntersectionPoint - position);
|
||||
|
||||
// the magnitude of the cross product of two vectors is double the area formed by the triangle of their intersection.
|
||||
auto fullAreaVec = Vec4::cross(theTriangle.vertex1 - theTriangle.vertex2, theTriangle.vertex1 - theTriangle.vertex3);
|
||||
auto areaVert1Vec = Vec4::cross(vertex2ToIntersect, vertex3ToIntersect);
|
||||
auto areaVert2Vec = Vec4::cross(vertex3ToIntersect, vertex1ToIntersect);
|
||||
auto areaVert3Vec = Vec4::cross(vertex1ToIntersect, vertex2ToIntersect);
|
||||
auto fullArea = 1.0 / fullAreaVec.magnitude();
|
||||
// scale the area of sub triangles to be proportion to the area of the triangle
|
||||
auto areaVert1 = areaVert1Vec.magnitude() * fullArea;
|
||||
auto areaVert2 = areaVert2Vec.magnitude() * fullArea;
|
||||
auto areaVert3 = areaVert3Vec.magnitude() * fullArea;
|
||||
|
||||
// normal = theTriangle.findClosestNormal(rayIntersectionPoint - position);
|
||||
if (theTriangle.hasNormals) // returning the closest normal is extra computation when n1 would likely be fine.
|
||||
if (theTriangle.hasNormals) {
|
||||
// returning the closest normal is extra computation when n1 would likely be fine.
|
||||
normal = theTriangle.normal1;
|
||||
else {
|
||||
// the above point still stands, but since we have to compute the berry centric factors anyway
|
||||
// we can use them in the same way to use from for UVs to get the correct normal.
|
||||
// but since the three normals should always be facing the same way anyway, so we really don't need to do this.
|
||||
// I'm keeping this here in case that fact changes.
|
||||
//normal = theTriangle.normal1 * areaVert1 + theTriangle.normal2 * areaVert2 + theTriangle.normal3 * areaVert3;
|
||||
} else {
|
||||
// standard points to normal algorithm but using already computed edges
|
||||
normal = Vec4{edge1.y() * edge2.z(), edge1.z() * edge2.x(), edge1.x() * edge2.y()} -
|
||||
Vec4{edge1.z() * edge2.y(), edge1.x() * edge2.z(), edge1.y() * edge2.x()};
|
||||
}
|
||||
|
||||
// calculate triangle UV
|
||||
// calculate the vector that runs between the vertex and the intersection point for all three vertices
|
||||
auto vertex1ToIntersect = theTriangle.vertex1 - rayIntersectionPoint;
|
||||
auto vertex2ToIntersect = theTriangle.vertex2 - rayIntersectionPoint;
|
||||
auto vertex3ToIntersect = theTriangle.vertex3 - rayIntersectionPoint;
|
||||
|
||||
// the magnitude of the cross product of two vectors is double the area formed by the triangle of their intersection.
|
||||
auto fullArea = 1 / Vec4::cross(theTriangle.vertex1 - theTriangle.vertex2, theTriangle.vertex1 - theTriangle.vertex3).magnitude();
|
||||
// scale the area of sub triangles to be proportion to the area of the triangle
|
||||
auto areaVert1 = Vec4::cross(vertex2ToIntersect, vertex3ToIntersect).magnitude() * fullArea;
|
||||
auto areaVert2 = Vec4::cross(vertex3ToIntersect, vertex1ToIntersect).magnitude() * fullArea;
|
||||
auto areaVert3 = Vec4::cross(vertex1ToIntersect, vertex2ToIntersect).magnitude() * fullArea;
|
||||
|
||||
// that area is how much each UV factors into the final UV coord
|
||||
auto uv = theTriangle.uv1 * areaVert1 + theTriangle.uv2 * areaVert2 + theTriangle.uv3 * areaVert3;
|
||||
// since the z and w component isn't used it's best to do this individually. (Where's that TODO on lower order vectors!!!)
|
||||
auto t_u = theTriangle.uv1.x() * areaVert1 + theTriangle.uv2.x() * areaVert2 + theTriangle.uv3.x() * areaVert3;
|
||||
auto t_v = theTriangle.uv1.y() * areaVert1 + theTriangle.uv2.y() * areaVert2 + theTriangle.uv3.y() * areaVert3;
|
||||
|
||||
return {true, rayIntersectionPoint, normal, t, clamp(uv.x(), 0, 1.0), clamp(uv.y(), 0, 1.0)};
|
||||
return {true, rayIntersectionPoint, normal, t, t_u, t_v};
|
||||
}
|
||||
|
||||
return {false, Vec4(), Vec4(), 0};
|
||||
|
|
|
@ -50,9 +50,9 @@ namespace Raytracing {
|
|||
void DebugMenus::add(const std::shared_ptr<DebugObject>& object) {
|
||||
objects.push_back(object);
|
||||
}
|
||||
void DebugMenus::remove(const std::shared_ptr<DebugObject>& object) {
|
||||
void DebugMenus::remove(DebugObject* object) {
|
||||
objects.erase(std::remove_if(objects.begin(), objects.end(), [&](const auto& item) -> bool {
|
||||
return item.get() == object.get();
|
||||
return item.get() == object;
|
||||
}), objects.end());
|
||||
}
|
||||
DebugBVH::DebugBVH(BVHTree* bvhTree, Shader& shader): m_bvhTree(bvhTree), m_shader(shader) {
|
||||
|
@ -284,6 +284,6 @@ namespace Raytracing {
|
|||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
}
|
||||
DebugBVH::~DebugBVH() {
|
||||
DebugMenus::remove(std::shared_ptr<DebugObject>(this));
|
||||
DebugMenus::remove(this);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue