|
Post by citizen428 on Dec 20, 2019 18:00:41 GMT
First off, thanks for the great book Jamis, it's really a lot of fun working through it :-)
I'm doing my implementation in F#, and only got through the first two chapters so far. Still, the code might be interesting for people who want to see how to approach this in a functional language:
This is an interesting side project for me for several reasons:
* It's my first bigger F# project, so far I mostly did smaller coding challenges, puzzles, small bug fixes etc. * This is also my first real .NET project in general. I generally work on Linux/Mac, but this year I got into F# via .NET Core and am quite enjoying it. * I previously dabbled with a ray tracer in OCaml, and want to see how F# compares.
The only real tip I have so far is that the built-in Array2D type was very helpful in implementing the canvas. I also opted for keeping points and vectors as separate types. The benefits this provides via the type system (only allowing the correct overload for math operations for example. so you can't e.g. multiply 2 points) outweigh the minor code duplication in my opinion. I'll keep updating this thread as I work through the book.
|
|
|
Post by Jamis on Jan 6, 2020 20:52:08 GMT
Excellent! Thanks for sharing your progress. You'll probably find multiple places you'll need to adapt the pseudocode and algorithms for a functional language; when I worked through the book in OCaml there were places where I had to get creative. But it all came together in the end, and I look forward to hearing what your experience is like!
- Jamis
|
|
|
Post by citizen428 on Jan 13, 2020 15:17:46 GMT
Yes, there are definitely some tricky parts when trying to do this in a functional language. I'm blogging about it along the way, the first post is here. Also your OCaml implementation is super helpful when I get really stuck. For example I borrowed parts of your `Shape` abstraction (passing in the `intersect` and `normal` functions after doing it in a more OO style with interfaces first. Though I did modify it a bit to make use of F# features like type extensions, ending up with something like this: type ShapeType = | Sphere ...
type Shape = { Object : ShapeType Intersect : Shape -> Ray -> Shape Intersection list Transformation : Matrix ... } member x.InverseTransformation = Matrix.inverse x.Transformation
|
|