Algorithm for 2D Raytracer

♀尐吖头ヾ 提交于 2019-11-26 10:00:47

问题


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:


回答1:


I am afraid that this kind of ray trace app you are proposing is a bit more misleading then to use real 3D ray-tracer.

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

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

  1. Optic simulation

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

    1. Store your world

      You got lens in 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 got Optic Lab

  2. Wolfenstein demo

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

    1. first draw floor and ceiling/sky as 2 half screens (screen divided by horizon)
    2. then you got 2D map of your maze/world (right)

      So cast rays from you current position in all visible directions (similar to your image but usually 60 degrees 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 appropriate column (vertical line) on the screen for each Ray hit

      The size and scale of it is determinated by distance form 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 example of what I busted for fun just now:

    It is done in C++ with pure GDI (using the bitmap scan line only) no other 3th party libs at all. It uses single texture, diffuse+ambient lighting, 2D raycasting. Has 2 bitmaps (screen,texture-atlas) and 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 related QA:

    • Wolfenstein with variable height of cells



回答2:


To my understanding, the issue would be approached differently. Given some position (current position of the mouse or something similar), the environment would be traced from there in the range of some given angle (like 90 degrees, in your picture this looks like 360 degrees). In the desired resolution of the angle, rays from the initial position are intersected with the mentioned polygons. The nearest intersection points are rendered. This approach should render the points which are 'visible' from the given position.



来源:https://stackoverflow.com/questions/32716685/algorithm-for-2d-raytracer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!