Skip to content

1st book - final scene, vec3 should be point3? #609

@mratsim

Description

@mratsim

Hello team,

I just finished the first book and implemented it in the Nim programming language.

https://github.com/mratsim/trace-of-radiance

I've used the opportunity to use one of Nim features that allows representing physics units in the type system and prevent compilation if there is an inconsistency.

For example I've represented Vec3 as a distinct type from Color and even UnitVector as a distinct type from a vector to ensure I don't pass a non-unit vector to the wrong functions.
Similarly I've created a distinct "Attenuation" type to ensure I don't multiply colors together.

In particular substracting 2 point3 give a vec3, adding a vec3and apoint3gives avec3but adding 2point3is a compile-time error as-is callinglengthon apoint3`.

func `-`*(a, b: Point3): Vec3 {.inline.}=
  ## Substracting points from one point to the other
  ## gives a vector
  result.x = a.x - b.x
  result.y = a.y - b.y
  result.z = a.z - b.z

template `+`*(p: Point3, v: Vec3): Point3 =
  ## Adding a vector to a point results in a point
  Point3(Vec3(p) + v)

template `-`*(p: Point3, v: Vec3): Point3 =
  ## Substracting a vector to a point results in a point
  Point3(Vec3(p) - v)

func `+`*(a, b: Point3): Point3 {.error: "Adding 2 Point3 doesn't make physical sense".}

And it seems like this caught an error in the book at chapter 13

image

In this expression (center - vec3(4, 0.2, 0)).length(), center is a point, substracting a vector gives a new point but points have no length (unles that was meant as distance from the origin).

So should vec3(4, 0.2, 0) be point3(4, 0.2, 0) instead?

Thank you very much for this excellent introduction to raytracing.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions