Skip to content

Commit

Permalink
Add RayTracing example
Browse files Browse the repository at this point in the history
  • Loading branch information
ttnghia authored and mosra committed May 9, 2020
1 parent 2fa4b8d commit ecbc805
Show file tree
Hide file tree
Showing 21 changed files with 1,246 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ cmake_dependent_option(WITH_MOTIONBLUR_EXAMPLE "Build Motion Blur example" OFF "
option(WITH_OVR_EXAMPLE "Build OVR example" OFF)
option(WITH_PICKING_EXAMPLE "Build Picking example" OFF)
option(WITH_PRIMITIVES_EXAMPLE "Build Primitives example" OFF)
option(WITH_RAYTRACING_EXAMPLE "Build Ray Tracing example" OFF)
option(WITH_SHADOWS_EXAMPLE "Build Shadow Mapping example" OFF)
option(WITH_TEXT_EXAMPLE "Build Text example (requires some TTF font plugin)" OFF)
cmake_dependent_option(WITH_TEXTUREDTRIANGLE_EXAMPLE "Build TexturedTriangle example (requires some TGA importer plugin)" OFF "NOT MAGNUM_TARGET_GLES" OFF)
Expand Down
2 changes: 2 additions & 0 deletions doc/building-examples.dox
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ integration libraries, see @ref building-plugins, @ref building-integration and
- `WITH_PICKING_EXAMPLE` --- Build the @ref examples-picking example.
- `WITH_PRIMITIVES_EXAMPLE` --- Build the @ref examples-primitives "Primitives"
example.
- `WITH_RAYTRACING_EXAMPLE` --- Build the @ref examples-raytracing "RayTracing"
example.
- `WITH_SHADOWS_EXAMPLE` --- Build the @ref examples-shadows example.
- `WITH_TEXT_EXAMPLE` --- Build the @ref examples-text example. Requires
the @ref Text library and some TTF font plugin such as
Expand Down
85 changes: 85 additions & 0 deletions doc/raytracing.dox
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


*/
}
Binary file added doc/raytracing.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions package/ci/appveyor-desktop-gles.bat
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ cmake .. ^
-DWITH_OVR_EXAMPLE=OFF ^
-DWITH_PICKING_EXAMPLE=OFF ^
-DWITH_PRIMITIVES_EXAMPLE=ON ^
-DWITH_RAYTRACING_EXAMPLE=ON ^
-DWITH_SHADOWS_EXAMPLE=OFF ^
-DWITH_TEXT_EXAMPLE=ON ^
-DWITH_TEXTUREDTRIANGLE_EXAMPLE=OFF ^
Expand Down
1 change: 1 addition & 0 deletions package/ci/appveyor-desktop-mingw.bat
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ cmake .. ^
-DWITH_OVR_EXAMPLE=OFF ^
-DWITH_PICKING_EXAMPLE=ON ^
-DWITH_PRIMITIVES_EXAMPLE=ON ^
-DWITH_RAYTRACING_EXAMPLE=ON ^
-DWITH_SHADOWS_EXAMPLE=ON ^
-DWITH_TEXT_EXAMPLE=ON ^
-DWITH_TEXTUREDTRIANGLE_EXAMPLE=ON ^
Expand Down
1 change: 1 addition & 0 deletions package/ci/appveyor-desktop.bat
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ cmake .. ^
-DWITH_OVR_EXAMPLE=ON ^
-DWITH_PICKING_EXAMPLE=ON ^
-DWITH_PRIMITIVES_EXAMPLE=ON ^
-DWITH_RAYTRACING_EXAMPLE=ON ^
-DWITH_SHADOWS_EXAMPLE=ON ^
-DWITH_TEXT_EXAMPLE=ON ^
-DWITH_TEXTUREDTRIANGLE_EXAMPLE=ON ^
Expand Down
1 change: 1 addition & 0 deletions package/ci/travis-desktop-gles.sh
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ cmake .. \
-DWITH_OVR_EXAMPLE=OFF \
-DWITH_PICKING_EXAMPLE=OFF \
-DWITH_PRIMITIVES_EXAMPLE=ON \
-DWITH_RAYTRACING_EXAMPLE=ON \
-DWITH_SHADOWS_EXAMPLE=OFF \
-DWITH_TEXT_EXAMPLE=ON \
-DWITH_TEXTUREDTRIANGLE_EXAMPLE=OFF \
Expand Down
1 change: 1 addition & 0 deletions package/ci/travis-desktop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ cmake .. \
-DWITH_OVR_EXAMPLE=OFF \
-DWITH_PICKING_EXAMPLE=ON \
-DWITH_PRIMITIVES_EXAMPLE=ON \
-DWITH_RAYTRACING_EXAMPLE=ON \
-DWITH_SHADOWS_EXAMPLE=ON \
-DWITH_TEXT_EXAMPLE=ON \
-DWITH_TEXTUREDTRIANGLE_EXAMPLE=ON \
Expand Down
4 changes: 4 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ if(WITH_PRIMITIVES_EXAMPLE)
add_subdirectory(primitives)
endif()

if(WITH_RAYTRACING_EXAMPLE)
add_subdirectory(raytracing)
endif()

if(WITH_SHADOWS_EXAMPLE)
add_subdirectory(shadows)
endif()
Expand Down
72 changes: 72 additions & 0 deletions src/raytracing/CMakeLists.txt
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)
76 changes: 76 additions & 0 deletions src/raytracing/Camera.h
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
91 changes: 91 additions & 0 deletions src/raytracing/Materials.cpp
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;
}
} }
Loading

0 comments on commit ecbc805

Please sign in to comment.