Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mratsim opened this issue May 21, 2020 · 1 comment
Closed

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

mratsim opened this issue May 21, 2020 · 1 comment

Comments

@mratsim
Copy link

mratsim commented May 21, 2020

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.

@hollasch
Copy link
Collaborator

Yes, good catch. The vec3 should be a point3, I believe.

@hollasch hollasch added this to the v3.1.2 milestone May 21, 2020
@hollasch hollasch self-assigned this May 21, 2020
hollasch added a commit that referenced this issue May 22, 2020
Book 1, we were computing the length of a point - vector, but that
doesn't make sense; it should have been computing the length of the
point - point.

Resolves #609
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants