Show HN: I wrote a C++ ray tracer from scratch without AI
134 points by martiano 10 hours ago | 57 comments

ssenssei 5 hours ago
_" Btw this was initially coded without AI, but I've used it for the recent clean up and features. "_

???

reply
wavemode 4 hours ago
To be fair, the title is "I wrote a C++ ray tracer from scratch without AI", not "This C++ ray tracer, right now, contains no AI-written code"

It sounds to me like there is some point in this project's history when both of the following were true:

1) It was a C++ raytracer

2) It was entirely human-written

So technically there is no lie here.

reply
ezekg 4 hours ago
Wow, I also wrote a game 8 years ago and have been using AI to rebuild upon it. I'm excited to tell people that I wrote it from scratch without AI! Love these new rules!
reply
rameerez 4 hours ago
Look at the commit history.

Out of 557 total commits in the repo, 510 have been done before the past 2 weeks. All those (minus 5 in 2023-2024) have been done on or before July 2022, months before ChatGPT had even launched.

Out of the 47 commits in the last 2 weeks, 26 were README updates / CI / docs. The remaining 21 commits are clearly cleanups, speedups, bugfixes, and tangential features like Blender import/export. Which leaves us with 505/557 commits (90%) if we're not generous --or 531/557 commits (95%) in the best case-- of non-AI commits to the repo.

OP clearly wrote most of the project by hand and has just been cleaning things up for public release the last couple weeks. Exactly as he disclosed in his comment.

reply
Gud 60 minutes ago
So… it has been written with AI?
reply
ezekg 3 hours ago
I also wrote a huge chunk of my game by hand 8 years ago, but I wouldn't lead with 'it's built without AI' now because that would be disingenuous.
reply
hardex 55 minutes ago
It all boils down to what each of you individually understands as "made with AI", which could be anything between using autocompletions and braindead vibecode.
reply
martiano 10 hours ago
Hey HN,

5 years ago I was 17 and learning to code C/C++ in a coding bootcamp (42). One of the projects was a simple C ray tracer. I really enjoyed working on the project and always loved computer graphics, so I decided to create my own path tracer from scratch, in C++, without using any third-party libraries.

I ended up working on it consistently for over a year, then sporadically when CG excitement hit me again. Recently I polished it and completed some unfinished features and decided to make it public, finally. It's a C++20 Path Tracer with a CPU renderer. It is able to render good-looking images with reasonable performance and sample count.

Btw this was initially coded without AI, but I've used it for the recent clean up and features. This project is a personal favorite of mine, and it can improve a lot, so I'd love to hear your feedback.

reply
hresvelgr 7 hours ago
Great work, the examples look fantastic. I will say, it's misleading to put "without AI" in the title for you to then comment on your submission that you have in fact used it. While it may only be in a trivial capacity, you've still used it.
reply
martiano 5 hours ago
I get your point. I consider it fair with the disclaimer because the "manual" version was very similar to current but missing some love
reply
almostjazz 4 hours ago
Maybe you could set your manual version as the main branch and then have a separate AI-cleaned branch on the side? I think the manual version part is what is exciting people and drawing them in. Even if it is worse or doesn't work!
reply
smartmic 7 hours ago
> Btw this was initially coded without AI, but I've used it for the recent clean up and features

Then it makes sense to update the submission title. To me it reads as if the project was written completely without the help of AI (which might be a quality badge to some), but it is not 100% true then.

Anyhow, cool project ;)

reply
martiano 5 hours ago
Thanks! I get your point about AI, but I think it's fair to say it's almost 100% AI free. I worked on it for ~15 months, vs 1 week now with AI. Previous results were quite similar
reply
mywittyname 4 hours ago
> 1 week now with AI

1 week is enough to build out a full-featured application with AI if you know what you're doing and are using proper tooling.

You may want to use a better metric to quantify what AI tooling did in the project.

reply
socalgal2 2 hours ago
My experience is 1 week of AI can = a year of by hand development.
reply
gspr 5 hours ago
> think it's fair to say it's almost 100% AI free.

So write "almost without AI" in the title, then!

reply
applfanboysbgon 4 hours ago
I am interested in hand-crafted software, and it feels deceptive when you put "no AI" in the title only to reveal there was. If you try to minimise its impact after, one wonders, if the impact was so minimal, why it was necessary. If you worked on it for 15 months without generated code, vs. 1 week with, why not finish the job? What was the purpose of introducing generated code in the last week, and could you not have taken a little more time to do it by hand given you already invested so much time into it?
reply
pixelesque 7 hours ago
JFYI: Your inverse ray direction calculation is not NaN-safe: if rays are completely axis-parallel in one dimension, so the direction value is 0.0 for that axis, you'll be doing the val / 0.0 which results in a NaN...

Also, as you're using full double/f64-precision all the time, you're leaving a fair bit of performance on the table: transcendentals (sin(), cos(), etc) in particular - can be a lot slower than when using f32, and generally double precision can be special-cased to particular areas of the renderer that need it (curve, sphere intersection, and some situations where volume scattering produces very small distances).

reply
adrian17 5 hours ago
> Also, as you're using full double/f64-precision all the time, you're leaving a fair bit of performance on the table

There's another issue that popped up on my quick naive profiling run: std::shared_ptr<Material> in the HitRecord/HittableLightSample is assigned/copied and destroyed a lot, and somehow these refcount operations show up as half of all samples on my profile (presumably because even if there's no hit and the pointer stays nullptr, the smart pointer still must check if there's anything to deallocate).

reply
pixelesque 5 hours ago
Yeah, passing std::shared_ptr by value in a multi-threaded setup can have a lot over overhead due to them being copied and destroyed a lot, and the fact that the atomic ref count value modifications effectively cause a write back to cache and can cause contention.

Should pass them by const refs really to avoid this.

reply
cyber_kinetist 4 hours ago
Or for a better alternative, just use plain old indices rather than shared pointers.

The scene is only going to be loaded / unloaded all at once, you can just load the data into contiguous arrays and index from them. No need to use shared_ptr since lifetimes aren't that complex.

reply
pixelesque 4 hours ago
Or just raw pointers, indeed.

std::shared_ptrs can also (because they're implicitly for sharing) alias, so the compiler has to assume the worst and emit loads in other cases, and there's no way (unless a newer C++ version has introduced it and I haven't noticed?) to use '__restrict__' with shared ptrs.

reply
adrian17 2 hours ago
[dead]
reply
deliveryboyman 6 hours ago
What's the proper way to handle a zero in the direction vector when calculating the reciprocal direction? Should it evaluate to infinity?
reply
pixelesque 6 hours ago
Inverse is still 0.0 technically, but yes, there is a trick you can use with Inf and SIMD to mask them out, so Inf is sometimes used.

However, I'd just condition it for the moment.

so:

invDirX = dirX != 0.0 ? 1.0 / dirX : 0.0; etc, etc for each dimension.

Obviously doing the != 0.0 comparison is not great, as it suffers from potential issues again (especially if you have denormals), but you can generally get away with it I've found in most cases.

reply
MyHonestOpinon 3 hours ago
Amazing. What happened with your professional career ? Did this exercise help you out professionally ?

One of my worries about AI is that doing these deep dives are a lot harder to justify.

reply
martiano 3 hours ago
I just launched the project, so can't say it helped me professionally yet. Almost all of the time invested in this was pre-AI. I agree it's harder to justify those deep dives but still worth it if you do it alongside AI IMO.
reply
MyHonestOpinon 2 hours ago
Even if the project didn't become popular. Have you been able to use it as part of your portfolio to get a job, or move into academia ? Perhaps what you learned in the project was useful in some other way ?
reply
shinycode 8 hours ago
Congrats on doing 42 and to have worked and shared your project, very nice results !
reply
manoji 7 hours ago
Hey ! Great work , I wanted to try something like this as well to begin my journey into games and computer graphics . I would love to know what resources you used to learn.
reply
martiano 6 hours ago
The greatest resource I've found on the internet is the Ray Tracing in One Weekend series. (https://raytracing.github.io/) You can start there and go pretty far. Also you can mix random papers you'll find and eventually just testing and experimenting yourself.
reply
pjmlp 7 hours ago
Congratulations on achieving it.
reply
ttoinou 8 hours ago
Congrats ! Results look stunning
reply
jasonjmcghee 4 hours ago
This is (or at least used to be) a right of passage in the graphics world.

I think many people go through the very popular https://raytracing.github.io/

There was a big influx of this when Sebastian Lague did his video series on building a ray tracer.

reply
pixelesque 4 hours ago
And if you've decided you want to take it a bit more seriously, move on to:

https://www.pbrt.org/

which is effectively the bible, and has been for years (I wrote mine and moved into the VFX industry when the Second edition was still out! - I feel old now)

reply
evilturnip 5 hours ago
Ray tracing is one of those problems that is conceptually so simple, yet continues to take so much mindshare because of all the challenges to implementation.
reply
ivanjermakov 5 hours ago
Me too, but in TS/WebGPU: https://github.com/ivanjermakov/moonlight

Very fun! Packing data for GPU-side BVH was quite tricky.

reply
martiano 5 hours ago
Wow, pretty good. Why web?
reply
ivanjermakov 5 hours ago
Thanks! Wanted to tackle WebGPU for making browser games.
reply
socalgal2 2 hours ago
why not web? 1000x easier than native and 1000x easier to share
reply
Phelinofist 6 hours ago
"Without AI" is the new "Written in Rust", SCNR
reply
cultofmetatron 8 hours ago
for anybody else interested in this undertaking, I recommend this book https://pragprog.com/titles/jbtracer/the-ray-tracer-challeng...
reply
eleventen 6 hours ago
A C++ ray tracer from scratch was the course project for my computer graphics class in 2016. I enjoyed the exercise immensely. Not nearly as robust as yours of course.
reply
Quarrel 6 hours ago
I basically was ready to come on and make a snarky comment like this. "I wrote one in the '90s!".

and then I saw the examples, and the feature set. I particularly like the blender-to-Luz export.

It seems great. Good luck to OP.

reply
Alifatisk 6 hours ago
> without AI

Now this is how you catch attention

reply
glouwbug 6 hours ago
I expect similar headlines like “I saved on token cost by hiring juniors” to come in soon too
reply
gspr 5 hours ago
Indeed. Until it turns out not to be true: https://news.ycombinator.com/item?id=48541543
reply
Alifatisk 4 hours ago
Bummer. I fell for clickbait as usual.
reply
cute_boi 2 hours ago
I just flagged this post. With AI i can write in 1 day, so what is the point here....?
reply
sharpfuryz 5 hours ago
Have you considered rewriting it in Rust? Not for any technical reason (I say it reflexively now)
reply
martiano 5 hours ago
Not really, but it would be fun. I really like Rust as well
reply
ddtaylor 4 hours ago
HN ongoing support and disdain for AI is perplexing.
reply
rajhphuyal 4 hours ago
Great work on such a technically challenging project.
reply
brianolson 3 hours ago
yup. me too. once upon a time that was a 2 week assignment in graphics class in college
reply
deadbabe 2 hours ago
I think these kind of projects fail to impress these days, with or without AI.

You need a second order effect, like “I did X using this thing I built Y”, where X is the actual impressive part and the Y is just an implementation detail. Maybe like that Roman Empire Names thing.

reply
throwpoaster 3 hours ago
Nice, I once built a ray tracer without using an SQL database.
reply
py93 4 hours ago
Very cool!
reply
itsthecourier 6 hours ago
for the love of the game, very refreshing good ol' coding
reply
tuinorizn 5 hours ago
[dead]
reply