MiniRT
My MiniRT notes.
MiniRT
Sources
Knowledge
Wiki
Blogs
Textures
Tools
Tricks
The following union is used for the vectors. It has 3 datatypes. The float arrray, the struct with 4 float members and the float vector_size attribute. All these members of the union need to be the exact same size for vector_size attribute to work propperly. The struct is just so we can access seperate elements easily with vec3.x. The array is so we could do the same with vec3.array[0] for easy array indexing. The vector_size is so we can do math operations on each of the struct members with easy syntaxing. vec3a.v = vec3a.v * vec3b.v; This wil not give you the dot or cross product. This will just multyply every struct member with the other respective stuct member. but this saves on a lot of smaller helper functions to do so.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/// @brief union for adaptable vector operations
/// @param float v for +-*/ operations on vector -> vec3_1.v - vec3_2.v
/// @param struct for x y z access -> vec3.x
/// @param float array[4] for array like access -> vec3.array[1]
typedef union u_vec3
{
float v __attribute__ ((vector_size(16)));
struct
{
float x;
float y;
float z;
float w;
};
float array[4];
} t_vec3;
Design
Scene Hierarchy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Scene
│
├── Ambient (unique)
│ ├─ ratio: float
│ └─ color: Color
│
├── Camera (unique)
│ ├─ position: Vector3
│ ├─ orientation: Vector3 (normalized)
│ └─ fov: float
│
├── Lights (list, one or more)
│ └─ Light
│ ├─ position: Vector3
│ ├─ brightness: float
│ └─ color: Color
│
└── Objects (list, one or more)
└─ Object (versatile template)
├─ type: enum {SPHERE, PLANE, CYLINDER, ...}
├─ data: void* → points to Sphere/Plane/Cylinder
└─ function pointers (optional):
• intersect(ray, obj)
• normal_at(point, obj)
Shared Building Blocks
1
2
3
4
5
6
7
8
9
Vector3
├─ x: float
├─ y: float
└─ z: float
Color
├─ r: float (0–1 normalized)
├─ g: float
└─ b: float
Specific Objects
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Sphere
├─ center: Vector3
├─ diameter: float
└─ color: Color
Plane
├─ point: Vector3
├─ normal: Vector3 (normalized)
└─ color: Color
Cylinder
├─ center: Vector3
├─ axis: Vector3 (normalized)
├─ diameter: float
├─ height: float
└─ color: Color