Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm for 2D Raytracer

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:

enter image description here

like image 244
Einstein Avatar asked Sep 22 '15 12:09

Einstein


People also ask

How does 2D ray tracing work?

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.

What is ray tracing algorithm?

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.

What is ray tracing examples?

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.


1 Answers

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.

  • 2D ray tracers are used a bit differently
  • and this may confuse your audience a lot

I would try to chose a more native 2D ray-trace usage like:

  1. Optic simulation

    This is used to simulate lens and mirrors optics. This image is from one of my ancient 2D ray-trace simulations:

    optic sim img

    1. 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

    2. 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.

  2. Wolfenstein demo

    This pseudo 3D game used a 2D ray casting engine. See Wiki: Wolfenstein_3D_engine. This image was taken from this link:

    wolf3d

    1. first draw the floor and ceiling/sky as 2 half screens (screen divided by horizon)
    2. 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

    3. 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:

    my Wolf3D

    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:

    Wolf3D

    If you're interested see this related QA:

    • Wolfenstein with variable height of cells
like image 107
Spektre Avatar answered Sep 20 '22 01:09

Spektre