Reputation: 370
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:
Upvotes: 7
Views: 14017
Reputation: 51893
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:
Upvotes: 14
Reputation: 17605
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.
Upvotes: 0