-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
1,246 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
This file is part of Magnum. | ||
|
||
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 | ||
Vladimír Vondruš <[email protected]> | ||
2020 — Nghia Truong <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a | ||
copy of this software and associated documentation files (the "Software"), | ||
to deal in the Software without restriction, including without limitation | ||
the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
and/or sell copies of the Software, and to permit persons to whom the | ||
Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included | ||
in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
namespace Magnum { | ||
/** @page examples-raytracing Ray Tracing | ||
@brief Ray Tracing example. | ||
@m_since_latest_{examples} | ||
|
||
@m_footernavigation | ||
|
||
This is an implementation of a simple ray tracer adapted from Peter Shirley's book [Ray Tracing in One Weekend](https://raytracing.github.io/books/RayTracingInOneWeekend.html). | ||
The current implementation runs on single thread and performs iterative rendering to refine the result. Typically, a high quality image can be achieved after around 100 iterations. | ||
|
||
@image html raytracing.png width=400px | ||
|
||
@section examples-raytracing-controls Controls | ||
|
||
- @m_class{m-label m-default} **mouse drag** rotates the camera | ||
- @m_class{m-label m-warning} **Shift** @m_class{m-label m-default} **mouse | ||
drag** pans the camera | ||
- @m_class{m-label m-default} **mouse wheel** zooms in/out | ||
- @m_class{m-label m-default} **R** reset the camera to its original transformation | ||
- @m_class{m-label m-default} **D** toggle Depth-of-Field | ||
- @m_class{m-label m-default} **M** toggle marking the next render block by a different color | ||
- @m_class{m-label m-default} **N** generate a new random scene | ||
- @m_class{m-label m-default} **Spacing** pause/resume rendering | ||
|
||
@section examples-raytracing-credits Credits | ||
|
||
This example was originally contributed by [Nghia Truong](https://github.com/ttnghia). | ||
|
||
@section examples-raytracing-source Source | ||
|
||
Full source code is linked below and also available in the | ||
[magnum-examples GitHub repository](https://github.com/mosra/magnum-examples/tree/master/src/raytracing). | ||
|
||
- @ref raytracing/Camera.h "Camera.h" | ||
- @ref raytracing/CMakeLists.txt "CMakeLists.txt" | ||
- @ref raytracing/Materials.h "Materials.h" | ||
- @ref raytracing/Materials.cpp "Materials.cpp" | ||
- @ref raytracing/Objects.h "Objects.h" | ||
- @ref raytracing/Objects.cpp "Objects.cpp" | ||
- @ref raytracing/RayTracer.h "RayTracer.h" | ||
- @ref raytracing/RayTracer.cpp "RayTracer.cpp" | ||
- @ref raytracing/RayTracingExample.cpp "RayTracingExample.cpp" | ||
- @ref raytracing/RndGenerators.h "RndGenerators.h" | ||
|
||
|
||
@example raytracing/Camera.h @m_examplenavigation{examples-raytracing,raytracing/} @m_footernavigation | ||
@example raytracing/CMakeLists.txt @m_examplenavigation{examples-raytracing,raytracing/} @m_footernavigation | ||
@example raytracing/Materials.h @m_examplenavigation{examples-raytracing,raytracing/} @m_footernavigation | ||
@example raytracing/Materials.cpp @m_examplenavigation{examples-raytracing,raytracing/} @m_footernavigation | ||
@example raytracing/Objects.h @m_examplenavigation{examples-raytracing,raytracing/} @m_footernavigation | ||
@example raytracing/Objects.cpp @m_examplenavigation{examples-raytracing,raytracing/} @m_footernavigation | ||
@example raytracing/RayTracer.h @m_examplenavigation{examples-raytracing,raytracing/} @m_footernavigation | ||
@example raytracing/RayTracer.cpp @m_examplenavigation{examples-raytracing,raytracing/} @m_footernavigation | ||
@example raytracing/RayTracingExample.cpp @m_examplenavigation{examples-raytracing,raytracing/} @m_footernavigation | ||
@example raytracing/RndGenerators.h @m_examplenavigation{examples-raytracing,raytracing/} @m_footernavigation | ||
|
||
|
||
*/ | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# | ||
# This file is part of Magnum. | ||
# | ||
# Original authors — credit is appreciated but not required: | ||
# | ||
# 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 — | ||
# Vladimír Vondruš <[email protected]> | ||
# 2020 — Nghia Truong <[email protected]> | ||
# | ||
# This is free and unencumbered software released into the public domain. | ||
# | ||
# Anyone is free to copy, modify, publish, use, compile, sell, or distribute | ||
# this software, either in source code form or as a compiled binary, for any | ||
# purpose, commercial or non-commercial, and by any means. | ||
# | ||
# In jurisdictions that recognize copyright laws, the author or authors of | ||
# this software dedicate any and all copyright interest in the software to | ||
# the public domain. We make this dedication for the benefit of the public | ||
# at large and to the detriment of our heirs and successors. We intend this | ||
# dedication to be an overt act of relinquishment in perpetuity of all | ||
# present and future rights to this software under copyright law. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
# THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
# | ||
|
||
cmake_minimum_required(VERSION 3.4) | ||
|
||
project(RayTracing CXX) | ||
|
||
# Add module path in case this is project root | ||
if(PROJECT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) | ||
set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/../../modules/" ${CMAKE_MODULE_PATH}) | ||
endif() | ||
|
||
find_package(Corrade REQUIRED Main) | ||
find_package(Magnum REQUIRED | ||
GL) | ||
|
||
if(CORRADE_TARGET_ANDROID) | ||
find_package(Magnum REQUIRED AndroidApplication) | ||
elseif(CORRADE_TARGET_EMSCRIPTEN) | ||
find_package(Magnum REQUIRED EmscriptenApplication) | ||
else() | ||
find_package(Magnum REQUIRED Sdl2Application) | ||
endif() | ||
|
||
set_directory_properties(PROPERTIES CORRADE_USE_PEDANTIC_FLAGS ON) | ||
|
||
add_executable(magnum-raytracing WIN32 | ||
../arcball/ArcBall.cpp | ||
Materials.cpp | ||
Objects.cpp | ||
RayTracer.cpp | ||
RayTracingExample.cpp) | ||
target_link_libraries(magnum-raytracing PRIVATE | ||
Corrade::Main | ||
Magnum::Application | ||
Magnum::GL | ||
Magnum::Magnum) | ||
target_include_directories(magnum-raytracing PRIVATE | ||
${CMAKE_CURRENT_SOURCE_DIR} | ||
${CMAKE_CURRENT_BINARY_DIR}) | ||
|
||
install(TARGETS magnum-raytracing DESTINATION ${MAGNUM_BINARY_INSTALL_DIR}) | ||
|
||
# Make the executable a default target to build & run in Visual Studio | ||
set_property(DIRECTORY ${PROJECT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT magnum-raytracing) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#ifndef Magnum_Examples_RayTracing_Camera_h | ||
#define Magnum_Examples_RayTracing_Camera_h | ||
|
||
/* | ||
This file is part of Magnum. | ||
Original authors — credit is appreciated but not required: | ||
2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 — | ||
Vladimír Vondruš <[email protected]> | ||
2020 — Nghia Truong <[email protected]> | ||
This is free and unencumbered software released into the public domain. | ||
Anyone is free to copy, modify, publish, use, compile, sell, or distribute | ||
this software, either in source code form or as a compiled binary, for any | ||
purpose, commercial or non-commercial, and by any means. | ||
In jurisdictions that recognize copyright laws, the author or authors of | ||
this software dedicate any and all copyright interest in the software to | ||
the public domain. We make this dedication for the benefit of the public | ||
at large and to the detriment of our heirs and successors. We intend this | ||
dedication to be an overt act of relinquishment in perpetuity of all | ||
present and future rights to this software under copyright law. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
#include <Magnum/Math/Functions.h> | ||
#include "RndGenerators.h" | ||
#include "Ray.h" | ||
|
||
namespace Magnum { namespace Examples { | ||
class Camera { | ||
public: | ||
Camera(const Vector3& eye, const Vector3& viewCenter, const Vector3& upDir, | ||
Deg fov, Float aspectRatio, Float lensRadius) : | ||
_origin(eye), | ||
_lensRadius(lensRadius) { | ||
_w = (eye - viewCenter).normalized(); | ||
_u = Math::cross(upDir, _w).normalized(); | ||
_v = Math::cross(_w, _u).normalized(); | ||
|
||
Float halfHeight = Math::tan(fov * 0.5f); | ||
Float halfWidth = aspectRatio * halfHeight; | ||
const Float focusDistance = (eye - viewCenter).length(); | ||
_lowerLeftCorner = _origin - | ||
halfWidth * focusDistance * _u - | ||
halfHeight * focusDistance * _v - | ||
focusDistance * _w; | ||
_horitonalEdge = 2 * halfWidth * focusDistance * _u; | ||
_verticalEdge = 2 * halfHeight * focusDistance * _v; | ||
} | ||
|
||
Ray getRay(Float s, Float t) { | ||
const Vector2 rd = _lensRadius * Rnd::rndInDisk(); | ||
const Vector3 offset = _u * rd.x() + _v * rd.y(); | ||
return Ray(_origin + offset, | ||
_lowerLeftCorner + s * _horitonalEdge + t * _verticalEdge - offset - _origin); | ||
} | ||
|
||
private: | ||
Vector3 _origin; | ||
Float _lensRadius; | ||
Vector3 _u, _v, _w; | ||
Vector3 _lowerLeftCorner; | ||
Vector3 _horitonalEdge, _verticalEdge; | ||
}; | ||
} } | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
This file is part of Magnum. | ||
Original authors — credit is appreciated but not required: | ||
2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 — | ||
Vladimír Vondruš <[email protected]> | ||
2020 — Nghia Truong <[email protected]> | ||
This is free and unencumbered software released into the public domain. | ||
Anyone is free to copy, modify, publish, use, compile, sell, or distribute | ||
this software, either in source code form or as a compiled binary, for any | ||
purpose, commercial or non-commercial, and by any means. | ||
In jurisdictions that recognize copyright laws, the author or authors of | ||
this software dedicate any and all copyright interest in the software to | ||
the public domain. We make this dedication for the benefit of the public | ||
at large and to the detriment of our heirs and successors. We intend this | ||
dedication to be an overt act of relinquishment in perpetuity of all | ||
present and future rights to this software under copyright law. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
#include <Magnum/Math/Functions.h> | ||
|
||
#include "RndGenerators.h" | ||
#include "Materials.h" | ||
#include "Ray.h" | ||
|
||
namespace Magnum { namespace Examples { | ||
struct HitInfo; | ||
|
||
namespace { | ||
inline Float schlick(Float cosine, Float ref_idx) { | ||
Float r0 = (1 - ref_idx) / (1 + ref_idx); | ||
r0 = r0 * r0; | ||
return r0 + (1 - r0) * std::pow((1 - cosine), 5); | ||
} | ||
} | ||
|
||
bool Lambertian::scatter(const Ray&, const HitInfo& hitInfo, Vector3& attenuation, Ray& scatteredRay) const { | ||
const Vector3 target = hitInfo.p + hitInfo.unitNormal + Rnd::randomInSphere(); | ||
scatteredRay = Ray(hitInfo.p, target - hitInfo.p); | ||
attenuation = _albedo; | ||
return true; | ||
} | ||
|
||
bool Metal::scatter(const Ray& r, const HitInfo& hitInfo, Vector3& attenuation, Ray& scatteredRay) const { | ||
const Vector3 reflectedRay = Math::reflect(r.unitDirection, hitInfo.unitNormal); | ||
scatteredRay = Ray(hitInfo.p, reflectedRay + _fuzziness * Rnd::randomInSphere()); | ||
attenuation = _albedo; | ||
return (dot(scatteredRay.unitDirection, hitInfo.unitNormal) > 0); | ||
} | ||
|
||
bool Dielectric::scatter(const Ray& r, const HitInfo& hitInfo, Vector3& attenuation, Ray& scatteredRay) const { | ||
attenuation = Vector3{ 1, 1, 1 }; | ||
|
||
Float ni_over_nt; | ||
Float cosine; | ||
Vector3 outwardNormal; | ||
if(dot(r.unitDirection, hitInfo.unitNormal) > 0) { | ||
outwardNormal = -hitInfo.unitNormal; | ||
ni_over_nt = _refractiveIndex; | ||
cosine = dot(r.unitDirection, hitInfo.unitNormal); | ||
cosine = std::sqrt(1 - _refractiveIndex * _refractiveIndex * (1 - cosine * cosine)); | ||
} else { | ||
outwardNormal = hitInfo.unitNormal; | ||
ni_over_nt = 1.0f / _refractiveIndex; | ||
cosine = -dot(r.unitDirection, hitInfo.unitNormal); | ||
} | ||
|
||
const Vector3 refractedDir = Math::refract(r.unitDirection, outwardNormal, ni_over_nt); | ||
const Float reflectionProbability = dot(refractedDir, refractedDir) > 0 ? | ||
schlick(cosine, _refractiveIndex) : | ||
1.0f; | ||
scatteredRay.origin = hitInfo.p; | ||
if(Rnd::rand01() < reflectionProbability) { | ||
scatteredRay.unitDirection = Math::reflect(r.unitDirection, hitInfo.unitNormal); | ||
} else { | ||
scatteredRay.unitDirection = refractedDir.normalized(); | ||
} | ||
return true; | ||
} | ||
} } |
Oops, something went wrong.