slightknack/keikan

Implementing Different Objects

Opened this issue · 3 comments

Tloru commented

Right now, keikan supports:

  • Planes
  • Spheres
  • and Mandelulbs (a fractal).

This is quite a small number of objects. At the least, the following primitives should be implemented:

  • Triangles
  • Rectangular Prisms
  • Cones

To implement a new object, go to objects/ and make a new file with a data structure that implements the March and/or the Trace traits.

I am intrested in contributing. But I dont know much about rendering engine in general. So cant gurantee. And cargo build is failing.

Tloru commented

Hey @mustafapc19, thanks for the interest. In light of your failed cargo build, I recently cloned and rebuilt the project myself. It turns out that my most recent changes hadn't saved before commiting, so I've saved them and pushed to master - the project should be buildable now.

Computer graphics is a fun subject, and there are lots of resources to get started.

The way keikan currently works is simple: A scene is a list of objects with a camera. At render time, the camera asks each object to provide information about it. This information is used to calculate light bounces, which renders the final scene.

Currently, keikan supports two rendering techniques, ray-tracing and ray-marching. Ray-marching uses the distance to objects to calculate collisions, whereas ray-tracing uses direct intersections to calculate collisions. Currently, all objects are in the /objects subfolder. Each object is a structure that implements at either of the Trace or March trait, or both.

The March trait requires you implement a function that returns the distance to the nearest point on that object given a point in 3D space. For example, the distance to the nearest point on a sphere is given by the equation:

length(point - sphere_position) - sphere_radius

So to implement that for Keikan you just need to translate that to Rust and wrap it into a neat little function:

impl March for Sphere {
    // ... snip

    fn march(&self, point: Vec3) -> f64 {
        (point - self.position).length() - self.radius
    }
}

There are a lot of cool fractals you can make this way. If you want to see the distance functions for some more shapes, check out Inigo Quilez's website: https://iquilezles.org/www/articles/distfunctions/distfunctions.htm

Ray-tracing is a bit more involved. Given a ray, a ray tracer must determine:

  1. Whether the object was hit (i.e. intersects the ray)
  2. How far away the collision was
  3. And the normal of the collision (which way the face of the object was facing)

There are a lot of well-documented ray-intersection tests online, just google one for the specific shape you're trying to implement. If you need any help getting started, feel free to reach out. Thanks again for the interest.

Thanx. I will look into it. But this a new thing for me and I dont know if I have time to actually contribute. But If I can I surely will.