Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RayTracing example #76

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
7e5015c
Add ArcBallCamera example
ttnghia Jan 28, 2020
3718f6b
Address issues and implement scenegraph arcball camera
ttnghia Jan 29, 2020
7ff7662
Fix namespace
ttnghia Jan 30, 2020
33a53ac
Address new issues
ttnghia Jan 30, 2020
e8616a4
Update dox
ttnghia Jan 30, 2020
cfd45c3
Update ci
ttnghia Jan 30, 2020
71cc4bc
Add a necessary API
ttnghia Jan 30, 2020
d7e41e6
Fix deadlock
ttnghia Jan 31, 2020
ef80e97
Add RayTracing example
ttnghia Jan 30, 2020
8ad96e0
Attemp to fix the Travis CI build error
ttnghia Jan 31, 2020
aac74f7
Merge branch 'arcball-camera' into ray-tracing
ttnghia Jan 31, 2020
564b7c1
Remove camera lagging
ttnghia Jan 31, 2020
8eb6537
Add ci stuff
ttnghia Jan 31, 2020
b9c3293
Allow to generate random scene
ttnghia Jan 31, 2020
2697510
Some optimization and change scene generation
ttnghia Jan 31, 2020
d22e63a
Rewrite scene generator
ttnghia Jan 31, 2020
d69b4fc
Fix camera
ttnghia Feb 1, 2020
acfd7fc
Change frame timer to render block of pixels without delay
ttnghia Feb 1, 2020
b497a6b
Change variables and rewrite
ttnghia Feb 3, 2020
a072167
Merge branch 'master' into ray-tracing
ttnghia Feb 4, 2020
75d8248
Remove duplicate code, using code from the existing example
ttnghia Feb 4, 2020
2461fd1
Allow to toggle Depth-of-Field
ttnghia Feb 5, 2020
67a64a7
Update dox and camera
ttnghia Feb 5, 2020
9309efa
Address issues
ttnghia Feb 5, 2020
0207f8a
Using magnum math for reflect and refract rays
ttnghia Feb 7, 2020
bfebe01
Some improvements
ttnghia Feb 7, 2020
f8adc1e
Merge branch 'master' into ray-tracing
ttnghia Apr 2, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,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_RAY_TRACING_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 @@ -222,6 +222,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_RAY_TRACING_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_RAY_TRACING_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 @@ -119,6 +119,7 @@ cmake .. ^
-DWITH_OVR_EXAMPLE=OFF ^
-DWITH_PICKING_EXAMPLE=ON ^
-DWITH_PRIMITIVES_EXAMPLE=ON ^
-DWITH_RAY_TRACING_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 @@ -114,6 +114,7 @@ cmake .. ^
-DWITH_OVR_EXAMPLE=ON ^
-DWITH_PICKING_EXAMPLE=ON ^
-DWITH_PRIMITIVES_EXAMPLE=ON ^
-DWITH_RAY_TRACING_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 @@ -86,6 +86,7 @@ cmake .. \
-DWITH_OVR_EXAMPLE=OFF \
-DWITH_PICKING_EXAMPLE=OFF \
-DWITH_PRIMITIVES_EXAMPLE=ON \
-DWITH_RAY_TRACING_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 @@ -86,6 +86,7 @@ cmake .. \
-DWITH_OVR_EXAMPLE=OFF \
-DWITH_PICKING_EXAMPLE=ON \
-DWITH_PRIMITIVES_EXAMPLE=ON \
-DWITH_RAY_TRACING_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 @@ -103,6 +103,10 @@ if(WITH_PRIMITIVES_EXAMPLE)
add_subdirectory(primitives)
endif()

if(WITH_RAY_TRACING_EXAMPLE)
add_subdirectory(raytracing)
endif()

if(WITH_SHADOWS_EXAMPLE)
add_subdirectory(shadows)
endif()
Expand Down
69 changes: 69 additions & 0 deletions src/arcball-camera/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#
# 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(ArcBallCamera 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
MeshTools
Primitives
SceneGraph
Shaders
Sdl2Application)

set_directory_properties(PROPERTIES CORRADE_USE_PEDANTIC_FLAGS ON)

add_executable(magnum-arcball-camera WIN32
ArcBall.cpp
ArcBallCameraExample.cpp)
target_link_libraries(magnum-arcball-camera PRIVATE
Corrade::Main
Magnum::Application
Magnum::GL
Magnum::Magnum
Magnum::MeshTools
Magnum::SceneGraph
Magnum::Shaders)
target_include_directories(magnum-arcball-camera PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR})

install(TARGETS magnum-arcball-camera 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-arcball-camera)
65 changes: 65 additions & 0 deletions src/raytracing/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#
# 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
Sdl2Application)

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);

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
Loading