I want to do a programmatical RayTracer
in Java
for demo purpose while giving a presentation about Ray Tracing in general (also mentioning 3D, this 2D model should only be for easier understanding, and to train my general Java knowledge).
My problem is, that i dont know where to start this whole thing.
The first thing i would try is to use vectors to trace every pixel on the screen from a given coordinate (eg. the position of my mouse cursor). Then I would calculate if the vector intersects with a polygon and then i would stop the vector there and draw it only to this specific point.
Maybe i could even draw some shadows by calculating the normal and reflect the vector in the other direction with a lower intensity.
So would it be a good idea to draw a vector from A = {everypixelonthescreen}
to a specific Point P
and calculate the intersections?
The finished version should look somewhat like this:
Ray tracing performs a process called “denoising,” where its algorithm, beginning from the camera—your point of view—traces and pinpoints the most important shades of light and shadows. Using machine learning, it 'fills in the gaps' to form a photorealistic image.
Ray tracing generates computer graphics images by tracing the path of light from the view camera (which determines your view into the scene), through the 2D viewing plane (pixel plane), out into the 3D scene, and back to the light sources.
Top examples of ray tracing include early RTX demos, like Battlefield V, Shadow of the Tomb Raider, and Metro Exodus. More recent games like Control and MechWarrior 5: Mercenaries also look compelling. Stay in the Light is an indie horror game built using ray-traced shadows and reflections.
I am afraid that the kind of ray trace app you are proposing is a bit more misleading than to use a real 3D ray-tracer.
I would try to chose a more native 2D ray-trace usage like:
Optic simulation
This is used to simulate lens and mirrors optics. This image is from one of my ancient 2D ray-trace simulations:
Store your world
You’ve got a lens in the form of polylines + diffraction index and mirrors also as polylines. You have the world diffraction index
cast R,G,B rays from source of light
Cast important ones only or all of them. Use Snell's law to simulate optics
As you can see the chromatic error is visible (each color has its own wavelength so the diffraction index is different). You can also use MultiBand rendering.
I used this to tune custom optic systems. If you add drag & drop capability you’ve got Optic Lab.
Wolfenstein demo
This pseudo 3D game used a 2D ray casting engine. See Wiki: Wolfenstein_3D_engine. This image was taken from this link:
then you’ve got a 2D map of your maze/world (right)
So cast rays from your current position in all visible directions (similar to your image but usually a 60 degree view is used). Rays must be done with subpixel (cell) precision. Where your ray hit the wall (on map) obtain the subpixel (cell) position. It indicates which part of wall texture is hit
draw the appropriate column (vertical line) on the screen for each ray hit
The size and scale of it is determinated by the distance from the ray origin. The fish eye correction is applied — if my memory serves it was done by using only perpendicular distance (multiply distance by cos(ray_angle - player_angle)
).
Here’s an example of what I busted out for fun just now:
It was done in C++ with pure GDI (using the bitmap scan line only), no other 3th party libs at all. It uses a single texture, diffuse + ambient lighting, 2D raycasting. Has 2 bitmaps (screen, texture-atlas) and a single 2D map. The code is less then 9 KByte including rems. It is controlled by keyboard only (mouse is used to edit the maze in the map subwindow).
Here animated GIF example:
If you're interested see this related QA:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With