Skip to content

A Minimal Working Example

Stefan Zellmann edited this page Aug 25, 2017 · 3 revisions

Content of this page: This page shows a minimal working example. Intended to demonstrate the basic principles for a brief overview.

Note that the current version of Visionaray is an early preview. At this stage, the framework, including the API, are likely to undergo frequent changes.

The following block of code extends the "Hello World" example shown before to a minimal working example that can be compiled and run. However, the example does nothing useful in particular but to initialize an array of floating points with 40,000 times the value "1.0".

#include <visionaray/math/math.h>
#include <visionaray/pinhole_camera.h>
#include <visionaray/scheduler.h>
#include <visionaray/simple_buffer_rt.h>

int main()
{

// 1.)
    // Visionaray's main namespace.
    using namespace visionaray;

    // Define some convenience types.
    typedef basic_ray<float>       ray_type;
    typedef simple_sched<ray_type> sched_type;
    typedef vector<4, float>       color_type;

// 2.)
    int w = 100;
    int h = 100;

    // A simple pinhole camera.
    pinhole_camera cam;
    cam.set_viewport(0, 0, w, h);
    float aspect = w / static_cast<float>(h);

    // Set camera projection like with gluPerspective:
    // 1. parameter: field of view angle in radians
    // 2. parameter: aspect ratio
    // 3. parameter: near and far clipping planes
    cam.perspective
    (
        45.0f * constants::pi<float>() / 180.0f,
        aspect,
        0.001f, 1000.0f
    );

// 3.)
    // The simple buffer render target provides storage
    // for the color buffer and depth buffer.
    simple_buffer_rt<PF_RGBA32F, PF_UNSPECIFIED> render_target;
    render_target.resize(100, 100);

// 4.)
    // Scheduler instance, probably a class member
    // in a real-world example.
    sched_type sched;
    auto sparams = make_sched_params
    (
        cam.get_view_matrix(),
        cam.get_proj_matrix(),
        render_target
    );

// 5.)
    // In this example, the kernel is passed to the
    // scheduler as a lambda. It specifies the code
    // path that a single ray or a ray packet executes.
    sched.frame([=](ray_type r) -> color_type
    {
        return color_type(1.0, 1.0, 1.0, 1.0);
    },
    sparams);
}

Code Section 1.) defines some shortcuts to types from namespace visionaray:

  • vector<Size, T> is a basic math vector primitive coming with visionaray.
  • basic_ray<T> contains vector<3, T>'s ori and dir which denote the ray's origin position and direction vector, respectively.
  • simple_sched<R> is the most basic ray scheduler, it iterates over the whole viewport in scanlines using a single CPU thread and outputs the ray tracing result to a 2-d array of pixels.

Code Section 2.) shows how to create a visionaray::camera object, which provides a simple pinhole camera. The projective camera matrix is set up just like in OpenGL by calling the set_viewport function and the perspective function of the camera object with self-explanatory parameters.

Code Section 3.) shows how to allocate storage for the color buffer and the depth buffer. Visionaray provides numerous render targets that wrap different types of memory.

Code Section 4.) wraps the camera and render target using a helper struct that wraps parameters that are passed to the scheduler.

Code Section 5.) shows how a simple kernel is passed to the scheduler's frame() method. In this particular case, the kernel ignores the incoming ray and simply returns the color white.