Giter Club home page Giter Club logo

neko_learn_ray_tracing's Introduction

Big Neko Learns Ray Tracing

This is a tutorial project created by a big cat (me) in order to learn ray tracing.

Chapter 0. How to create a figure.

This library is in RenderLib, with function to create a binary .ppm(P6) format. Following is an example of library generated by RenderLib. This is a circle in the square, with 256x256 pixel. This lib will be the basis of following ray tracing code.

test.png

Chapter 1. How to render a sphere.

First we need to build a background imag, the background is gradually changing from blue to white, which looks like follows (the image is 400 pixels height x 800 pixels width). background.png

Now that we have the background image, we can draw a sphere on the basis of it. The equation to describe a sphere looks like follows.

$$ (\mathbf{r} - \mathbf{r_c})^2 = r^2 $$

where $ \mathbf{r_c} = (x_c, y_c, z_c) $ being the coordinate of sphere cneter, and $r$ being the radius of sphere. A ray is a straight line, so it shall have the equestion of

$$ \mathbf{r }= \mathbf{r_0} + \mathbf{v} \cdot t$$

where $r = (x, y, z)$, $r_0$ is the starting point of the ray, $\mathbf{v}$ is the direction of the ray and $t$ a parameter describing the running of points on the straight line. With the two equations, it can be easily found that $t$ should satisfy (by constraining $v^2=1$):

$$(\mathbf{r_0}-\mathbf{r_c})^2 +2(\mathbf{r_0}-\mathbf{r_c})\cdot \mathbf{v} \cdot t + t^2 = r^2$$

We can simply judge if the ray has hit the surface by the following variable:

$$ \Delta = 4[(\mathbf{r_0}-\mathbf{r_c})\cdot\mathbf{v}]^2 - 4*[(\mathbf{r_0}-\mathbf{r_c})^2 - r^2]$$

We have hit, tangent to and away from sphere 3 situations, correponding to $ \Delta >0, \Delta =0, \Delta <0$ three cases. After knowing the hit situation of each ray, it is easy to draw a sphere as follows.

red_shpere.png

Chapter 2. Add a normal vector to the sphere

From the above chapter, we can have the intersection point between the sphere and the ray, therefore it is easy to computer the normal vector of hit point. By calculating the normal vector and give colors to pixels according to the normal vector of the sphere surface, we can render the surface as follows.

norm_sphere.png

Of course, we need a library that can handle multiple objects, so we design an abstract parent class geometry, which has two method, intercept and color, the first one return the parameter t where the ray intercepts with the geometry object, the second decide the surface color of object which hit a ray. This can be seen as follows. Here we place another green sphere as the background. So we can see a "green background".

norm_sphere_bg.png

Chapter 3. Anti-aliasing

We can see some aliasing in the above spheres, in oder eliminate this aliasing effects, we need to sample several rays for one pixel. This can be done by randomly add some real numbers between 0 and 1, and average the output of the rays. The more random number we sampled, the better anti-aliasing effect is. Owing to the large number sampling will slow down the rendering speed, here we only use a sampling number equals to 10. Following is the effect. We can observ improvement of rendering qualiies (especially the boundary of spheres).

anti_aliasing

Chapter 4. Diffuse material

Starting from this chapter, we are going to consider the material of sphere. The material of sphere will decide the appearance of a whole material. A simple class of material is called diffuse material. That means, if a light is shade on to the material, it will randomly bounces into any directions. Here we will use a uniform distribution to simulate this behavior. By simulating the ray bouncing many times, we can get the image as follows. We can find the ball is something like plastic, and we can find some shadow near the sphere.

diffuse_material

Chapter 5. Metal material

Then we can start with metal material. Then metal material is different from the diffuse material that it can reflect the light with some specified directions. We would like to specify a reflectivity and diffusity for this material. In the following graph, we can find two metal spheres reflecting to each other. metal_material

We can also add some diffusive effects on the metals, in the following graphs, the sphere in the left the the one with diffusity equals to 0.5, and the right one is the one with diffusity equals to 0.0 (mirror reflection). Notice here we have add an anti-aliasing x100, and it seems to be clearer than the usually x10 we use. metal_diffuse_material

Of course, we can have images with more sampling, see the image below. Now it looks super clear (x1000)! And we can also change the color to make the sphere looks like a mirror. metal_diffuse_material_super_resolution metal_diffuse_material_super_resolution2

Chapter 6. Dielectrics

Many materials has both reflection and refraction part. The reflection part can be described as follows, and the refraction part can be described with Snell's law, where n is the refraction indices (for air the value is near 1.0, and for glass the value is near 1.3-1.7)

$$n_1sin\left(\theta_1\right)=n_2sin\left(\theta_2\right)$$

The glass effect is as follows (the left sphere). glass_material

Chapter 7. Positional Camera

neko_learn_ray_tracing's People

Contributors

zxjzxj9 avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.