r/Python • u/jrmiranda • May 03 '20
I Made This I made a raytracer in Python from scratch!
9
u/hamoliciousRUS May 03 '20
I am actually quite jealous... been trying to make mine work for about 2 weeks now, keep coming back to it to then get pissed off and abandon it lol... looks amazing!, going to go give mine another shot :/
1
u/jrmiranda May 04 '20
Check out the references in the Github repo(https://github.com/jrmiranda/raytracer). There are many good explanations there. It helped me a lot.
4
u/bloopietybloopbloop May 03 '20
github?
5
u/hamoliciousRUS May 03 '20
it never worked so there was nothing to upload, this time this is the best i could get to before having a brain aneurysm and closing it lol. (btw, in the above image, i am rendering a single sphere, just the 1, in the middle of the screen)
3
u/bloopietybloopbloop May 03 '20
it's fine dude. if you've put it up anywhere, I'd like to see. I don't have a lot of experience but i could see where it went wrong
1
u/hamoliciousRUS May 04 '20 edited May 04 '20
Well, gave it a shot yesterday and managed to get it working : )
Here is a 1 and a half hour render I made yesterday overnight
SUPER basic right now but at least it worked
But thanks for offering
2
3
6
2
u/dusktreader May 03 '20
Did you overlay phong effect with true reflection here? Otherwise, I'm wondering what's reflected; it's clearly not a luminaire by the shadows.
2
2
u/diamondketo May 03 '20
You've developed the raytracing itself from scratch? That's amazing! Would you mind showing me your source code?
1
3
May 03 '20
raytracing is pretty simple conceptually, it just is very processor intensive and gives awesome results. This is a good free tutorial showing how to do GPU accelerated raytracing https://devblogs.nvidia.com/accelerated-ray-tracing-cuda/
0
May 04 '20
[deleted]
3
u/cbarrick May 04 '20
The Raytracing in One Weekend series is usually cited as the easiest way to get into raytracing. The books use C++, but it wouldn't be hard to use Python instead. The code in the books also uses simple loops, but you could vectorize a lot of it with numpy for better performance, maybe even better than the original C++. Or maybe use PyTorch to get it GPU accelerated.
The Nvidia book above is based on that series IIRC.
2
u/florinandrei May 03 '20
Is there any limit to how many times you're bouncing rays between the balls?
1
2
2
u/cylonlover May 04 '20
That's very impressive. I'd like to know how that works, mathematically. How do you go about doing that.
Is there something odd about he shadows, though?
It seems they are diffferent on the plan than in the reflections, and in the blu-shine the red-shine seem to float. Is that a trick on the eye, just?
2
u/jrmiranda May 04 '20
There's a point(the camera) and in front of it there's a grid of pixels. The job is to calculate de color of each one of those pixels. You do it by casting a light ray from the camera to a pixel and following its diretion to see in which objects it intersects.
1
256
u/ZDRuX1 May 03 '20
... show-off
Fine, maybe it's better than my rock, paper, scissors.
110
u/Resolt May 03 '20
Strictly speaking his raytracer probably won't let you play rock paper scissors, so in that way your programs are just different :)
54
May 03 '20
who needs rock, paper, scissors, when you can play sphere, sphere sphere, sphere, sphere, sphere, sphere, sphere, sphere, sphere, sphere?
7
2
May 04 '20
Strictly speaking his raytracer probably won't let you play rock paper scissors, so in that way your programs are just different :)
Oh yeah, but do your rock, paper or scissors go pew, pew, pew?
1
u/Resolt May 04 '20
Probably not, but then again neither does the raytracer. My point was merely that just because a project is smaller or less complex it doesn't necessarily mean it should receive less appreciation.
1
8
u/prettyanonymousXD May 04 '20
It’s cool that he used python, but why? I mean it’s a great and fun project, but I’d imagine that it must be painfully slow. Regardless, nice job OP!
3
u/blackRNA May 04 '20
Maybe if he moves it to the gpu (depending on his implementation) it might be able to run at usable speeds
2
u/prettyanonymousXD May 04 '20
It’s possible that combined with the CUDA SDK, it’s performance would significantly improve. I still have my doubts about it being usable, but that isn’t the point. The point is not whether it’s usable under optimal conditions. The point is that if it was written in a compiled language, it would be far more usable. However, I realize that the point of the project is to better understand raytracers and have fun making one, so in that sense the OP is successful. However, I would challenge them to make a compiled rewrite and observe the significant performance improvements.
1
May 04 '20
Python compiles down to bytecode that is then run in a VM, similar idea to Java. The bytecode is cached and only needs re-compiling when something changes, like eruba pointed out. For sure if you are doing all the rendering via CUDA you could make something "usable" in Python.
1
u/prettyanonymousXD May 05 '20 edited May 05 '20
Yes, I’m aware. I’m not arguing that it wouldn’t be usable. I am mostly arguing that why be inefficient when you don’t have to? The OP did a great job making a cool ray tracing tool, but I would challenge him in the future to recreate it in a more efficient language so they can play around with optimization.
1
May 05 '20
You are right, but I think we can safely assume the guy has different priorities to you.
Python is a pretty nice language for learning things and making prototypes. If he's just doing it for fun then optimisation isn't much of a concern yet.
Plus, if he wants to get any kind of significant optimisation then he should offload everything to the GPU, in which case the environment the base app is running in doesn't matter much at all, he'd be doing all the heavy lifting in HLSL or whatever.
1
u/prettyanonymousXD May 06 '20
Yes, I agree with that. That is exactly what python is for.
All I’m saying is that since he has already gone through the trouble of making a raytracer, I would challenge him to also create a compiled version and play with optimization since there’s a lot to be learned there.
1
u/eruba May 04 '20 edited May 04 '20
The point is that if it was written in a compiled language, it would be far more usable.
But python can actually compile programs. It's only interpreting code that was changed recently.
1
u/prettyanonymousXD May 05 '20
Base python is interpreted and slower on average than most non-interpreted languages. Yes Cython exists and you can compile python code into .pyx bytecode, but even using those tools arte not as efficient as a c++ or rust rewrite. However, I maintain that the OP did a great job making this cool raytracer, I would just challenge them to make a more efficient version and play around with optimization.
1
u/eruba May 06 '20
The standard cpython is also creating compiled .pyc files, not only cython. Although I agree about the performance issues.
1
1
-2
u/__xor__ (self, other): May 04 '20
C'mon, people have been doing ray tracing forever. So played out.
2
May 04 '20
sure, but consumer hardware has only recently started to be able to do realtime ray tracing at a decent resolution. So it's a good time for people to get more familiar with it surely? I especially like path tracing, gives a nice grainy feel like a camera running with a high ISO setting (which technically it kind of is) https://www.youtube.com/watch?v=BRCAfdBMe2Y
0
162
u/Synaptic_Verse May 03 '20
I thought pokeballs when I first seen it
31
u/Thecrawsome May 03 '20
Seriously! OP managed to use a reflection on the bottom of balls, and their own reflections of each other to almost perfectly resemble a PokeBall.
32
u/Hardvig May 03 '20
Isn't something wrong with the shadows..?
It looks like the reflection of the light source places the light source behind the camera, but the shadows seem to indicate that the light source is above the point of view..?
Nevertheless, amazing job!
8
u/johnnySix May 03 '20
I think there are two light sources. The ‘ring light from the camera POV and a top/rim light which casts the light on the ground, and gives a rim brightness.
5
u/dusktreader May 03 '20
There is only one light source or you would see multiple shadows. I think this is phong reflection layered over true reflection
2
u/johnnySix May 03 '20
He says it’s a raytracer, so it shouldn’t be Fong. But perhaps. But if one of the light sources is at the cameras position, which is what it looks like here, you wouldn’t see shadows from that light. If one of the light sources is at the same position as the camera, the shadows would all be hidden, and all you would get is illumination, similar to a ring light in traditional photography You can see a reflected light source in The middle of the center sphere which shows that the main source is at the camera position. If I am missing something, please draw me a diagram. Thanks!
6
u/zero_iq May 03 '20
Phong shading in raytracers refers to the Phong reflection model, rather than Phong interpolation used in rasterization. It used to be pretty common in early raytracers, and often features in people's first coding experiments, because it's pretty simple and gives pretty results. (Or Blinn-Phong, which is a modified version for higher performance).
2
u/Hamoodzstyle May 03 '20
Blinn Phong shading model is how you judge lighting (except refraction iirc) in ray tracing. It factors in specular reflection, diffuse reflection, ambient lighting, and shading.
39
u/sysintegra May 03 '20
Cool but you shouldn't use it for pool Billard. Build Battlefield or something!
26
u/x3r0x_x3n0n May 03 '20
In python?!!! Wouldnt that drastically slow the game down.
33
May 03 '20
He’s joking, making battlefield in Python would be insane and I don’t even know how you’d go about starting that.
7
May 03 '20
I'd start by trying OpenGL. I doubt it would be that insane since Python code compiles, and the rendering and physics etc could still all be hardware accelerated.
edit: not saying it's an optimal solution of course. I started messing around with OpenGL 20 years ago, but I just use UE4 any time I need to do 3D graphics these days
3
May 04 '20
Maybe it’s because i’m kinda new to programming that it would seem so crazy. I haven’t heard of OpenGL or UE4 but which one would you recommend if I want to get started in 3D and maybe try to make a small game?
3
2
2
u/socialistvegan May 04 '20
I’d give Unity a look, I think it’s the most popular game engine these days, especially for small projects and beginners. They have a ton of tutorials too.
3
1
May 04 '20
if you are wanting to make a game rather than mess specifically with graphics, then for sure I recommend Unreal Engine. Unity is also another popular one.
You can't use Python in either by the way. Unreal Engine has a system called Blueprints where you can wire nodes together to create code. It's actually pretty cool for exploring the API and throwing together ideas. You can also code things up in C++ if you want more speed or complexity, but blueprints will definitely be enough to play around with game ideas quickly, and is a much nicer way to explore Unreal functionality (it can sometimes be hard to find the right syntax on the C++ side when they change the API between versions for example)
1
2
May 04 '20
The irony of this statement is that a good chunk of the game and mods for BF2 were in python.
1
May 05 '20
That’s cool! I’m a beginner so I was just saying it seems insane to someone at my skill level :)
0
May 03 '20
[deleted]
6
May 03 '20
Everything would have to route through a single python thread at a time... Wild guess, but I have to imagine it would be impressive to even get 1/100th of the performance of a better suited architecture. Ray tracing in battlefield renders at real time -- even if you could make use of the same hardware, OPs pokeballs probably take a few seconds to render a single frame.
11
7
5
u/HankDeTank05 May 03 '20
I made a raycasting engine with python, using the Arcade library primarily. I’d be curious to know how you made this. I can show you my code, if you’d like
3
2
u/mysterd2006 May 03 '20
Reminds me so much of the first tutorials for Persistence of Vision, ray tracing software on my Atari ST :)
2
u/photonymous May 03 '20
I hope you used dictionaries as your fundamental data type. There's nothing like hashing pixel coordinates to get the blood moving in the morning.
2
1
u/WiggleBooks May 04 '20
I don't get it. What do you mean regarding implementation?
1
u/photonymous May 04 '20
I'm sorry. I was really just kidding around. Dictionaries are pretty much Python's fundamental data type. But they are horrendously inefficient for numerical algorithms when all you really need is linear addressing. Heavy core usage of dictionaries is part of the reason why Python is slow compared to other languages. Of course if you avoid them and use efficient numerical libraries such as Numpy then it's not too big of an issue.
2
2
2
u/shartoo May 07 '20
nice!
1
u/nice-scores May 07 '20
𝓷𝓲𝓬𝓮 ☜(゚ヮ゚☜)
Nice Leaderboard
1.
u/RepliesNiceat 7136 nices2.
u/spiro29at 6246 nices3.
u/GreenAppleCZat 5081 nices...
270115.
u/shartooat 1 niceI AM A BOT | REPLY !IGNORE AND I WILL STOP REPLYING TO YOUR COMMENTS
3
u/CaffeinatedQuant May 03 '20
Did you follow rtiaw or trtc?
1
u/parkerSquare May 04 '20
I know what RTIAW is (RayTracer in a Weekend FWIW) but what is TRTC? Google just brings me back to your comment.
2
2
1
1
1
1
1
1
1
May 04 '20
That is pretty cool I'll have to see how it works and I noticed the reflection on the floor below the green ball reminds me of the knight's face from the hollow knight game. How long did it take to make it?
1
u/yagooba May 04 '20
I never played past the fire red gen of Pokémon and thought raytracer was one of the newer Pokémon
1
1
u/yaboytomsta May 04 '20
Honest question: what does from scratch mean? I assume you used some visual module? I always get confused when people say from scratch. (No hate btw this is really cool!)
1
u/TheBaconator523 May 04 '20
It means from nothing, he built it ground up
1
1
1
0
0
49
u/RisingRhymer May 03 '20
Will you release the source code, I keep getting stuck building a render engine and learning how this works may help.