Ray tracing for openFrameworks on Intel Embree 4: fast ray / mesh queries (nearest hit, shadows, mouse picking, millions of rays per second on all cores) and a progressive CPU ray tracer with shadows, ambient occlusion and reflections.
README
ofxEmbreeRayTracing
Ray tracing for openFrameworks on Intel Embree 4: fast ray / mesh queries (nearest hit, shadows, mouse picking, millions of rays per second on all cores) and a progressive CPU ray tracer with shadows, ambient occlusion and reflections.
<p align="center"><img src="docs/cornellbox.jpg" width="560" alt="example_cornellbox"></p>
Scene: ray queries
#include "ofxEmbreeRayTracing.h"
ofxEmbree::Scene scene;
unsigned box = scene.addMesh(ofMesh::box(100, 100, 100), node.getGlobalTransformMatrix());
unsigned ball = scene.addSphere({ 0, 200, 0 }, 50);
// nearest hit along a ray
ofxEmbree::Hit hit = scene.intersect(origin, direction);
if (hit) ofLog() << hit.geometry << " at " << hit.distance << ", normal " << hit.normal;
// what's under the mouse
hit = scene.pick(cam, { ofGetMouseX(), ofGetMouseY() });
// is anything in the way (shadow rays)
bool blocked = scene.occluded(ray);
// lots of rays on all cores
scene.intersect(rays, hits);addMesh(mesh, transform) | OF_PRIMITIVE_TRIANGLES, _TRIANGLE_STRIP or _TRIANGLE_FAN, indexed or not; returns the geometry id |
addSphere(center, radius) | an analytic sphere |
remove(id), clear(), commit() | queries commit changes themselves; commit() lets you choose when |
intersect(ray) / intersect(origin, direction) | nearest Hit: distance, position, normal, uv, geometry, primitive |
occluded(ray) | anything between near and far, faster than intersect |
intersect(rays, hits) | many rays, split across all cores |
pick(camera, screen[, viewport]), rayFromScreen(...) | mouse picking with any ofCamera / ofEasyCam |
getBounds(), getNumTriangles(), getRTCScene(), getDevice() | and the rest of the Embree API |
Renderer: a CPU ray tracer
ofxEmbree::Renderer renderer;
renderer.setMaterial(box, { ofFloatColor(0.8f, 0.2f, 0.2f) });
renderer.setMaterial(ball, { ofFloatColor(0.95f), 0.85f }); // mostly a mirror
// update(): one more pass every frame (reset() when the camera or scene changes)
renderer.render(scene, cam, width, height);
texture.loadData(renderer.getPixels());Every pass traces a jittered ray per pixel on all cores and adds to the image, so it refines over time (anti-aliased, with soft ambient occlusion).
settings: a directional or point light, shadows, ambient occlusion samples and distance, mirror bounces, ambient and background colours. Materials: colour, reflection and emission.Examples
| Example | |
|---|---|
example_cornellbox | a Cornell box with a mirror sphere, ray traced progressively; drag to orbit, toggle shadows / AO / reflections, save a PNG |
example_picking | of3dPrimitives with the shape under the mouse highlighted, the hit point and normal; space casts 1,000,000 rays in a batch |
example_tests | the addon's tests, headless: distances and normals against known shapes, transforms, ranges, strips / fans, batches, picking through a camera, and a render with a shadow check |
On an Apple M-series laptop: about 12 million rays/s in the tests' small scene and 45 million rays/s in the Cornell box (shadow and ambient occlusion rays included).
Embree
libs/embree is Embree 4.4.1, static, with Embree's internal task system (no TBB), one libembree.a per platform, built by the scripts in scripts/:| Folder | Platform, code path | Script |
|---|---|---|
lib/macos/embree.xcframework | macOS 10.15+ universal: arm64 NEON, x86_64 AVX2 (Intel Macs from 2013) iOS 15+: arm64 NEON iOS Simulator: arm64 NEON, x86_64 AVX2 | scripts/build_embree_apple.sh (Xcode, CMake, Ninja) |
lib/linux/64 | x86_64: SSE2, AVX and AVX2, picked at runtime | scripts/build_embree_linux.sh (Docker) |
lib/linux/arm64, lib/linux/aarch64 | 64-bit ARM NEON: ARM desktops and servers, and 64-bit Raspberry Pi OS (the same build) | scripts/build_embree_linux.sh arm64 |
- The Linux folders use openFrameworks'
lib/linux/<arch>layout (0.12.2 and later), built with-fPIC. - On macOS,
osxlinks the xcframework's macOS slice; on iOS the projectGenerator adds the xcframework. - Embree's static library allows one code path per architecture with Apple's clang, hence AVX2 only on
ROSETTA_ADVERTISE_AVX=1 so Embree sees AVX2. - Embree doesn't target iOS officially;
build_embree_apple.shnotes the two changes it makes. - Windows isn't included yet.
License
ofxEmbreeRayTracing: MIT, Dan Rosser. Embree: Apache 2.0, Intel (
libs/embree/license).
ofxAddons