Skip to content

Latest commit

 

History

History
105 lines (88 loc) · 5.14 KB

raymarch-dev.md

File metadata and controls

105 lines (88 loc) · 5.14 KB

Raymarching Distance Fields Development Description

Contents

  • The raymarching algorithm
  • Orthographic scene
  • Transformations and perspective
  • Shading
  • Displaying the results
  • Resources

The raymarching algorithm

The raymarching algorithm being used is inspired by the work of Inigo Quilez in the demoscene. The algorithm can be formulated as such:

###Problem:### For each pixel in the result image, we wish to cast a ray into the world and find the closest surface intersection.

###Solution:### Instead of analytically solving the ray intersection problem, we look for an approximation. First, we define our scene using distance functions that, for any given point in the world, return the closest distance from the point to a surface.

For example, the (signed) distance function for a sphere centered at the origin would be:

float sdSphere(vec3 p, float radius) {
	return length(p) - radius;
}

The distance function for the composed scene will then be the minimum of all the individual distance functions.

To solve for intersection, we march along the ray in variable steps. That is: for any point along the ray, we find the smallest distance to any surface in the scene and step forward this distance. Repeat until the distance is below a given epsilon. This way we can speed up the raymarching, and prevent missing surfaces

Displaying the results

Using OpenGL we create a quadrilateral and a texture to be rendered onto that quad. This allows us to render at a resolution lower than the window and simply scale the quad in the vertex shader. The raymarching algorithm will be implemented in the fragment shader.

Perspective

An advantage of ray-casting-based rendering approaches is that changing from an orthographic view to a perspective view does not change the behaviour of the rays.

A perspective view can be modeled by positioning the eye somewhere in the world, and defining the orientation of the image plane and the distance from the eye to the plane.

We define the orientation of the image plane using a right-vector and an up-vector. The crossproduct of these gives us the forward-vector. If we wanted an orthographic view, all the rays would be cast parallell to this direction.

Image plane position = s = eye + focalLength * (right x up) + u * right + v * up Ray direction = d = normalize(s - eye) = focalLength * (right x up) + u * right + v * up

Dependencies

References

###OpenGL###

###Raymarching###

###Lighting###

Resources

Raytracing