Giter VIP home page Giter VIP logo

ray-tracing's Introduction

Ray-Tracing

Ray tracing is a rendering technique for generating an image by tracing the path of light as pixels in an image plane. Basically, to generate an image of the environment you use view rays to calculate the color of each pixel. I have used several algebraic approaches to calculate ray-sphere and ray-plane intersections.

Ray-tracing-gif

Shading

Another problem to solve was calculating if the point is under a shadow or not. To check it, you need to generate a shadow ray to check if there are obstacles between the light source and the intersection point.
Ray-tracing-gif-2 Ray-tracing-gif-3

However, without a proper shading model results look pretty unrealistic. For example the image below consists of 2 spheres aligned one after another on light source's direction.

A-bad-example

Phong Model

To make it more realistic, I decided to use Phong Reflection Model without specular light. By the help of ambient and diffuse terms it became easier to generate a more realistic image. Phong-Model

Also I used a recursive ray tracing approach to add reflections:

Recursive-diffuse-color

Results

The final results looks more realistic: Result

How to run?

Input.txt format:

[Number of spheres]
For each sphere:

  • [colorR, colorG, colorB] → Comma seperated RGB values
  • [centerX, centerY, centerZ] → Comma seperated center coordinates
  • [radius]
  • [diffuse Coefficient]
  • [reflection Coefficient]

[Number of planes]
For each plane:

  • [colorR, colorG, colorB] → Comma seperated RGB values
  • [centerX, centerY, centerZ] → Comma seperated point coordinates
  • [normalX, normalY, normalZ] → Comma seperated normal vector
  • [diffuse Coefficient]
  • [reflection Coefficient]

You can check example input.txt

Run:

You need an environment that has python3 with numpy and pillow libraries installed. Then follow the steps:

  • Modify input.txt which is located in the same directory with ray_tracer.py
  • Use python ray_tracer.py to run the script.
  • Output.png or output.jpeg will be generated.

PS: It might take a few minutes to generate the output. Please be patient.

Contributing

Pull-requests, issues, comments and stars are always appreciated!

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.