Skip to content

Ray tracing is a rendering technique for generating an image by tracing the path of light.

License

Notifications You must be signed in to change notification settings

safakozdek/Ray-Tracing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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!