Skip to content

FaraDuMatin/RayTracingImplementation

Repository files navigation

C++ Ray Tracer

A ray tracing renderer that creates photorealistic images from scene description files.

Compilation

To compile the project using g++:

g++ -o main main.cpp lodepng.cpp object.cpp parser.cpp raytracer.cpp

Or simply use the included Makefile:

make

Running the Ray Tracer

To render a scene, run the executable with a .ray scene file:

./main scenes/basic.ray

The program will generate an output PNG image in the same directory as the input scene file.

Example Scenes

The scenes/ folder contains several example scenes you can render:

Basic Scene

./main scenes/basic.ray

Basic Scene

Renders spheres with reflective materials, a plane, and a mesh object.

Teapot Scene

./main scenes/teapot.ray

Teapot Scene

Refraction Front View

./main scenes/scene_avec_refraction/refraction_front.ray

Refraction Front

Demonstrates light refraction through transparent objects from the front perspective.

Creating Your Own Scene

Scene files (.ray format) use a simple text-based syntax to define the rendered image. Here's how to create your own:

Basic Structure

# Set output image dimensions
Dimensions 640 480

# Camera perspective (like gluPerspective)
Perspective 30.0 1.33 3 20

# Camera position (like gluLookAt)
LookAt  1.2434  1.2533  9.8429  # eye position
        0.6500  0.3000 -0.9843  # focus position
       -0.0157  0.9921 -0.1243  # up vector

Defining Materials

Materials specify the appearance of objects:

Material "materialName"
    "ambient"   [r g b]         # Ambient color
    "diffuse"   [r g b]         # Diffuse color
    "specular"  [r g b]         # Specular highlight color
    "emission"  [r g b]         # Emissive color
    "shininess" 500.0           # Specular exponent
    "reflect"   0.8             # Reflection factor (0-1)

Example - Red Reflective Material:

Material "red"
    "ambient"   [1.0 0.0 0.0]
    "diffuse"   [1.0 0.0 0.0]
    "specular"  [1.0 1.0 1.0]
    "emission"  [0.0 0.0 0.0]
    "shininess" 500.0
    "reflect"   0.8

Adding Objects

Sphere

PushMatrix
    Translate x y z
    Rotate angle axis_x axis_y axis_z
    Scale sx sy sz
    Sphere radius "materialName"
PopMatrix

Example:

PushMatrix
    Translate -1.0 0.75 -1.75
    Sphere 1.75 "teal"
PopMatrix

Plane

PushMatrix
    Translate x y z
    Rotate angle axis_x axis_y axis_z
    Plane "materialName"
PopMatrix

Example - Ground Plane:

PushMatrix
    Translate 0 -1.25 0
    Rotate -90 1 0 0
    Plane "white"
PopMatrix

Mesh from OBJ File

PushMatrix
    Translate x y z
    Scale sx sy sz
    Rotate angle axis_x axis_y axis_z
    Mesh "materialName" "path/to/mesh.obj"
PopMatrix

Example:

PushMatrix
    Translate -1.25 1.75 2.25
    Scale 0.4 0.4 0.4
    Rotate 90 0 1 0
    Mesh "green" "meshes/icosahedron.obj"
PopMatrix

Mesh with Inline Vertices

Mesh "materialName" [numFaces] [vertex indices] "P" [
    x1 y1 z1
    x2 y2 z2
    x3 y3 z3
    ...
]

Example - Triangle:

PushMatrix
    Translate 1.75 -1.25 2.5
    Rotate -45 1 0 0
    Mesh "yellow" [3] [0 1 2] "P" [
        0 0 0
        1 0 0
        0 1 0
    ]
PopMatrix

Adding Lights

Point lights illuminate the scene:

PointLight x y z
    "ambient"     [r g b]           # Ambient light color
    "diffuse"     [r g b]           # Diffuse light color
    "specular"    [r g b]           # Specular light color
    "attenuation" [const lin quad]  # Light attenuation

Example:

PointLight 2 10 2
    "ambient"     [0.1 0.1 0.1]
    "diffuse"     [8.0 8.0 12.0]
    "specular"    [20.0 20.0 20.0]
    "attenuation" [1.8 3.0 0.0]

Transformations

Transformations work like OpenGL matrix operations:

  • PushMatrix / PopMatrix - Save/restore transformation state
  • Translate x y z - Move objects
  • Rotate angle axis_x axis_y axis_z - Rotate objects (angle in degrees)
  • Scale sx sy sz - Scale objects

License

Educational project from the course IFT3355 - 3D Computer graphics.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors