Java – algorithm of 2D ray tracker

I want to write a programmed raytracer in Java for presentation purposes and give a general ray tracing presentation (3D is also mentioned. This 2D model should only be easier to understand and cultivate my general java knowledge)

My problem is, I don't know where to start the whole thing

The first thing I want to try is to use vectors to track every pixel on the screen at a given coordinate (such as the position of my mouse cursor) Then I will calculate whether the vector intersects the polygon, and then I will stop the vector there and draw it to this specific point

Maybe I can even draw some shadows by calculating normals and reflect vectors in the other direction with lower intensity

So is it a good idea to draw a vector from a = {every pixel on the screen} to a specific point P and calculate the intersection?

The finished version should look like this:

Solution

I'm afraid your proposed ray tracing application will be more misleading than using a real 3D ray tracker

>The 2D ray tracker is used slightly differently > this may confuse your audience

I will try to choose more native 2D ray tracing usage, such as:

>Optical simulation

This is used to simulate lens and mirror optics Here is an image from one of my ancient 2D ray tracing simulations:

>Store your world

You have a lens as a broken line in the form of a broken line diffraction index and a mirror You have the world diffraction index > Project R, G, B rays from a light source

Only cast important or all Use Snell's law to simulate optics

As you can see, the chromaticity error is visible (each color has its own wavelength, so the diffraction index is different) You can also use multiband rendering

If you added the drag & drop function of the optical lab, I'll use it to adjust the custom optical system > Wolfenstein demo

This Pseudo 3D game uses a 2D ray casting engine. See Wiki: Wolfenstein_ 3D_ engine. This picture comes from this link:

>First draw the floor and ceiling / sky as 2 and a half screens (screen divided by horizon) > then you get a 2D map of your maze / World (right)

Therefore, light is cast from the current position in all visible directions (similar to your image, but usually 60 degree view) Light must be done with sub - pixel (unit) accuracy The light hits the wall (on the map) to get the sub-pixel (cell) position It indicates which part of the wall texture was hit > draw the appropriate column (vertical line) on the screen for each shot hit

Its size and scale are determined by the distance from the ray origin If my memory service uses only the vertical distance (multiplied by cos (ray_angle player_angle)) to complete fisheye correction

Here is an example of how I had a good time just now:

It is completed in C using pure GDI (only bitmap scanlines), and there is no other third-party library at all It uses a single texture, diffuse ambient lighting, and 2D ray casting There are 2 bitmaps (screen, texture atlas) and a single 2D map Code less than 9 KB, including REMS It is only controlled by the keyboard (the mouse is used to edit the maze in the map sub window)

Here are examples of animated gifs:

If you are interested, please refer to the relevant quality assurance:

> Wolfenstein with variable height of cells

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>