This project is a simple ray tracer implemented in Python. It demonstrates the basics of ray tracing, including ray-sphere intersection, lighting, shadows, and reflections. The ray tracer generates an image of a scene containing spheres with different materials and lighting.
- Ray-Sphere Intersection: Determines where rays intersect with spheres in the scene.
- Lighting: Supports ambient, point, and directional lights.
- Shadows: Computes shadows cast by objects in the scene.
- Reflections: Implements reflective surfaces with configurable reflectivity.
- Rendering: Generates an image of the scene using ray tracing.
- Python 3.x
Pillowlibrary (for image manipulation)
-
Clone the repository:
git clone https://github.com/yourusername/raytracer.git cd raytracer -
Set up a virtual environment (optional but recommended):
python -m venv myenv source myenv/bin/activate # On Windows: myenv\Scripts\activate
-
Install the required dependencies:
pip install pillow
-
Run the ray tracer:
python main.py
-
View the output: The ray tracer will generate an image named
output.pngin the project directory. Open this file to view the rendered scene.
The project is organized into several files, each with a specific responsibility:
main.py: The entry point of the application. Initializes the scene and starts the rendering process.scene.py: Contains theSceneclass, which holds all the objects (spheres, lights, etc.) in the scene.vector.py: Contains theVector3class, which represents 3D vectors and provides vector operations.sphere.py: Contains theSphereclass, which represents a sphere in the scene.light.py: Contains theLightclass, which represents different types of lights (ambient, point, directional).renderer.py: Contains theRendererclass, which handles the rendering logic, including ray tracing, intersection tests, and lighting calculations.
Contributions are welcome! If you have any suggestions, bug reports, or feature requests, please open an issue or submit a pull request.
- Fork the repository.
- Create a new branch for your feature or bugfix.
- Commit your changes and push them to your fork.
- Submit a pull request to the main repository.
This project is licensed under the MIT License. See the LICENSE file for details.
-
Clone the Repository:
git clone https://github.com/yourusername/raytracer.git cd raytracer -
Set Up a Virtual Environment (optional but recommended):
python -m venv myenv source myenv/bin/activate # On Windows: myenv\Scripts\activate
-
Install Dependencies:
pip install pillow
-
Run the Ray Tracer:
python main.py
-
View the Output: The ray tracer will generate an image named
output.pngin the project directory. Open this file to view the rendered scene.
The default scene includes the following objects and lights:
-
Spheres:
- A red sphere at
(0, -1, 3)with a radius of1. - A blue sphere at
(2, 0, 4)with a radius of1. - A green sphere at
(-2, 0, 4)with a radius of1. - A large yellow sphere at
(0, -5001, 0)with a radius of5000.
- A red sphere at
-
Lights:
- An ambient light with intensity
0.2. - A point light at
(2, 1, 0)with intensity0.6. - A directional light with direction
(1, 4, 4)and intensity0.2.
- An ambient light with intensity
You can modify the scene by editing the main.py file.
