Skip to content

Commit

Permalink
Ray Tracer Part 2 (#4)
Browse files Browse the repository at this point in the history
See description of #4
  • Loading branch information
JstnMcBrd authored Mar 9, 2023
1 parent 78306d9 commit 18c6a82
Show file tree
Hide file tree
Showing 21 changed files with 869 additions and 240 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ FROM python:3

RUN pip install numpy
RUN pip install python-dotenv
RUN pip install tqdm

WORKDIR /ray-tracer

Expand Down
112 changes: 110 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

A simple [ray tracer](https://en.wikipedia.org/wiki/Ray_tracing_(graphics)) written in [Python](https://www.python.org/).

![example](https://user-images.githubusercontent.com/28303477/221353237-11327d55-0782-4e33-aa83-4f73434ee86a.png)
![program-6-scene-3](https://user-images.githubusercontent.com/28303477/224002637-4f6d5e4d-c5f9-428f-9237-80e46799bcb7.png)

## Licensing

Expand All @@ -12,7 +12,115 @@ Without a custom license, this code is the direct intellectual property of the o

Scenes are defined in `JSON` files. See a few examples in the [scenes](/scenes/) folder.

Any deviation from the schema will result in assertion errors.
Any deviation from the following type definitions will result in assertion errors.

```ts
/** Represents an RGB color. All elements must be between 0 and 1. */
type Color = Array<number>[3];

/** Represents a direction vector. Will throw warnings if not normalized. */
type Direction = Array<number>[3];

/** Represents a location in the scene. */
type Position = Array<number>[3];

/** Defines the entire scene. This is the schema you should use for your scene JSON files. */
class Scene {
/** The position the camera is looking towards. */
camera_look_at?: Position = [0, 0, 0];

/** The position the camera is looking from (the camera's position). */
camera_look_from?: Position = [0, 0, 1];

/** The "up" direction for the camera. */
camera_look_up?: Direction = [0, 1, 0];

/** The angle width of the camera's view. Must be between 0 and 359. */
field_of_view?: float = 90;

/** The direction to the directional light source. */
light_direction?: Direction = [0, 1, 0];

/** The color of the directional light. */
light_color?: Color = [1, 1, 1];

/** The color of the global ambient light. */
ambient_light_color?: Color = [1, 1, 1];

/** The color of the scene background. */
background_color?: Color = [0, 0, 0];

/** A list of all objects in the scene. */
objects?: Array<Object>;
};

/** The universal values shared by all objects. */
class Object {
/** The name of the object. Has no affect on the behavior of the object. */
name?: string;

/** The type of the object. Used to differentiate between different classes of objects.
* Must be one of the accepted values. See other object classes below. */
type: string;

/** The coefficient for ambient lighting. Must be between 0 and 1. */
ambient_coefficient?: number = 0;

/** The coefficient for diffuse lighting. Must be between 0 and 1. */
diffuse_coefficient?: number = 1;

/** The coefficient for specular lighting. Must be between 0 and 1. */
specular_coefficient?: number = 0;

/** The color for diffuse lighting. */
diffuse_color?: Color = [1, 1, 1];

/** The color for specular lighting. */
specular_color?: Color = [1, 1, 1];

/** The coefficient for specular glossiness. */
gloss_coefficient?: number = 4;

/** How reflective the object surface should be. Must be between 0 and 1. */
reflectivity?: number = 0;
};

/** The specific values necessary for Planes. */
class Plane extends Object {
/** Defines this object as a Plane. */
type: string = "plane";

/** The direction the plane faces. */
normal: Direction;

/** Any position on the plane. */
point: Position = [0, 0, 0];
};

/** The specific values necessary for Polygons. */
class Polygon extends Object {
/** Defines this object as a Polygon. */
type: string = "polygon";

/** A list of vertices in counterclockwise order. Must have at least 3. */
vertices: Array<Position>;
};

/** The specific values necessary for Spheres. */
class Sphere extends Object {
/** Defines this object as a Sphere. */
type: string = "sphere";

/** The central location of the Sphere. */
center: Position;

/** The radius of the Sphere. Must be greater than 0. */
radius: number;
};

// More types of objects can be added later.
// In the meantime, most kinds of objects can be modeled with Polygons.
```

## Running

Expand Down
5 changes: 3 additions & 2 deletions scenes/program-5-scene-1.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"camera_look_from": [0, 0, 1],
"camera_look_up": [0, 1, 0],
"field_of_view": 90,
"direction_to_light": [0.0, 1.0, 0.0],
"light_direction": [0.0, 1.0, 0.0],
"light_color": [1.0, 1.0, 1.0],
"ambient_light_color": [0.0, 0.0, 0.0],
"background_color": [0.2, 0.2, 0.2],
Expand All @@ -18,7 +18,8 @@
"specular_coefficient": 0.2,
"diffuse_color": [1.0, 0.0, 1.0],
"specular_color": [1.0, 1.0, 1.0],
"gloss_coefficient": 16.0
"gloss_coefficient": 16.0,
"reflectivity": 0.0
}
]
}
14 changes: 9 additions & 5 deletions scenes/program-5-scene-2.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"camera_look_from": [0, 0, 1],
"camera_look_up": [0, 1, 0],
"field_of_view": 60,
"direction_to_light": [1.0, 1.0, 1.0],
"light_direction": [1.0, 1.0, 1.0],
"light_color": [1.0, 1.0, 1.0],
"ambient_light_color": [0.1, 0.1, 0.1],
"background_color": [0.2, 0.2, 0.2],
Expand All @@ -18,7 +18,8 @@
"specular_coefficient": 0.1,
"diffuse_color": [1.0, 1.0, 1.0],
"specular_color": [1.0, 1.0, 1.0],
"gloss_coefficient": 4.0
"gloss_coefficient": 4.0,
"reflectivity": 0.0
},
{
"name": "red sphere",
Expand All @@ -30,7 +31,8 @@
"specular_coefficient": 0.3,
"diffuse_color": [1.0, 0.0, 0.0],
"specular_color": [1.0, 1.0, 1.0],
"gloss_coefficient": 32.0
"gloss_coefficient": 32.0,
"reflectivity": 0.0
},
{
"name": "green sphere",
Expand All @@ -42,7 +44,8 @@
"specular_coefficient": 0.2,
"diffuse_color": [0.0, 1.0, 0.0],
"specular_color": [0.5, 1.0, 0.5],
"gloss_coefficient": 64.0
"gloss_coefficient": 64.0,
"reflectivity": 0.0
},
{
"name": "blue sphere",
Expand All @@ -54,7 +57,8 @@
"specular_coefficient": 0.0,
"diffuse_color": [0.0, 0.0, 1.0],
"specular_color": [1.0, 1.0, 1.0],
"gloss_coefficient": 16.0
"gloss_coefficient": 16.0,
"reflectivity": 0.0
}
]
}
29 changes: 19 additions & 10 deletions scenes/program-5-scene-3.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"camera_look_from": [0, 0, 1],
"camera_look_up": [0, 1, 0],
"field_of_view": 90,
"direction_to_light": [0.0, 0.0, 1.0],
"light_direction": [0.0, 0.0, 1.0],
"light_color": [1.0, 1.0, 1.0],
"ambient_light_color": [1.0, 1.0, 1.0],
"background_color": [0.0, 0.0, 0.0],
Expand All @@ -18,7 +18,8 @@
"specular_coefficient": 0.2,
"diffuse_color": [1.0, 0.0, 0.0],
"specular_color": [1.0, 1.0, 1.0],
"gloss_coefficient": 16.0
"gloss_coefficient": 16.0,
"reflectivity": 0.0
},
{
"name": "pink",
Expand All @@ -30,7 +31,8 @@
"specular_coefficient": 0.2,
"diffuse_color": [1.0, 0.0, 0.6],
"specular_color": [1.0, 1.0, 1.0],
"gloss_coefficient": 16.0
"gloss_coefficient": 16.0,
"reflectivity": 0.0
},
{
"name": "purple",
Expand All @@ -42,7 +44,8 @@
"specular_coefficient": 0.2,
"diffuse_color": [0.5, 0.0, 1.0],
"specular_color": [1.0, 1.0, 1.0],
"gloss_coefficient": 16.0
"gloss_coefficient": 16.0,
"reflectivity": 0.0
},
{
"name": "blue",
Expand All @@ -54,7 +57,8 @@
"specular_coefficient": 0.2,
"diffuse_color": [0.0, 0.0, 1.0],
"specular_color": [1.0, 1.0, 1.0],
"gloss_coefficient": 16.0
"gloss_coefficient": 16.0,
"reflectivity": 0.0
},
{
"name": "aquamarine",
Expand All @@ -66,7 +70,8 @@
"specular_coefficient": 0.2,
"diffuse_color": [0.0, 1.0, 1.0],
"specular_color": [1.0, 1.0, 1.0],
"gloss_coefficient": 16.0
"gloss_coefficient": 16.0,
"reflectivity": 0.0
},
{
"name": "green",
Expand All @@ -78,7 +83,8 @@
"specular_coefficient": 0.2,
"diffuse_color": [0.0, 1.0, 0.33],
"specular_color": [1.0, 1.0, 1.0],
"gloss_coefficient": 16.0
"gloss_coefficient": 16.0,
"reflectivity": 0.0
},
{
"name": "lime",
Expand All @@ -90,7 +96,8 @@
"specular_coefficient": 0.2,
"diffuse_color": [0.5, 1.0, 0.0],
"specular_color": [1.0, 1.0, 1.0],
"gloss_coefficient": 16.0
"gloss_coefficient": 16.0,
"reflectivity": 0.0
},
{
"name": "yellow-orange",
Expand All @@ -102,7 +109,8 @@
"specular_coefficient": 0.2,
"diffuse_color": [1.0, 0.75, 0.0],
"specular_color": [1.0, 1.0, 1.0],
"gloss_coefficient": 16.0
"gloss_coefficient": 16.0,
"reflectivity": 0.0
},
{
"name": "middle sphere",
Expand All @@ -114,7 +122,8 @@
"specular_coefficient": 0.2,
"diffuse_color": [1.0, 1.0, 1.0],
"specular_color": [1.0, 1.0, 1.0],
"gloss_coefficient": 16.0
"gloss_coefficient": 16.0,
"reflectivity": 0.0
}
]
}
57 changes: 57 additions & 0 deletions scenes/program-6-scene-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"camera_look_at": [0, 0, 0],
"camera_look_from": [0, 0, 1],
"camera_look_up": [0, 1, 0],
"field_of_view": 60,
"light_direction": [0.0, 1.0, 0.0],
"light_color": [1.0, 1.0, 1.0],
"ambient_light_color": [0.0, 0.0, 0.0],
"background_color": [0.2, 0.2, 0.2],
"objects": [
{
"name": "reflective sphere",
"type": "sphere",
"center": [0.0, 0.3, -1.0],
"radius": 0.25,
"ambient_coefficient": 0.1,
"diffuse_coefficient": 0.0,
"specular_coefficient": 0.1,
"diffuse_color": [0.75, 0.75, 0.75],
"specular_color": [1.0, 1.0, 1.0],
"gloss_coefficient": 10.0,
"reflectivity": 0.9
},
{
"name": "blue triangle",
"type": "polygon",
"vertices": [
[0.0, -0.7, -0.5],
[1.0, 0.4, -1.0],
[0.0, -0.7, -1.5]
],
"ambient_coefficient": 0.1,
"diffuse_coefficient": 0.9,
"specular_coefficient": 1.0,
"diffuse_color": [0.0, 0.0, 1.0],
"specular_color": [1.0, 1.0, 1.0],
"gloss_coefficient": 4.0,
"reflectivity": 0.0
},
{
"name": "yellow triangle",
"type": "polygon",
"vertices": [
[0.0, -0.7, -0.5],
[0.0, -0.7, -1.5],
[-1.0, 0.4, -1.0]
],
"ambient_coefficient": 0.1,
"diffuse_coefficient": 0.9,
"specular_coefficient": 1.0,
"diffuse_color": [1.0, 1.0, 0.0],
"specular_color": [1.0, 1.0, 1.0],
"gloss_coefficient": 4.0,
"reflectivity": 0.0
}
]
}
Loading

0 comments on commit 18c6a82

Please sign in to comment.