算法2D光线跟踪器算法、光线、跟踪器

2023-09-11 22:43:55 作者:萬物不相爭

我想在的Java A的程序化光线跟踪为演示目的而提供有关雷一presentation跟踪一般(提的还有3D,这个二维模型应该只是为了更容易理解,并培养我一般的Java知识)。

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.

所以会是一个好主意,吸取 A = {everypixelonthescreen} 到一个特定的 P点并计算交点?

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:

推荐答案

恐怕这样的光线跟踪应用程序,你的建议是更多的误导那么一点用真正的3D光线追踪。

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射线示踪剂用于有点不同 ,这可能会混淆你的观众很多

我会尝试选择更天然的2D光线跟踪使用情况,如:

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

光学模拟

这是用来模拟透镜和反射镜光学器件。从我的古2D光线跟踪模拟一个在这里的图像:

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

存储你的世界

Store your world

您在折线+衍射指数和镜子也为折线形式得到了镜头。你拥有世界上的衍射指数

You got lens in form of polylines + diffraction index and mirrors also as polylines. You have the world diffraction index

投R,从浅源G,B光

只投或全部重要的。使用斯涅耳定律来模拟光学

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

重返德军总部演示

这个伪3D游戏的2D光线投射引擎看到百科:Wolfenstein_3D_engine 。这张照片是从这个链接采取:

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

在第一次抽签地板和天花板/天为2个半屏(屏幕由水平线划分)

那么,你有你的迷宫/世界2D地图(右) first draw floor and ceiling/sky as 2 half screens (screen divided by horizon)

then you got 2D map of your maze/world (right)

因此​​,从你施放的光线当前位置中的所有可见的方向(类似于您的形象,但通常为60度视图时)。光线必须以子像素(单元)precision来完成。在您的光线打在墙上(地图)获得子像素(小区)的位置。它表明这墙纹理的一部分被击中

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

在屏幕每条射线上画出相应的列(垂直线)按

draw appropriate column (vertical line) on the screen for each Ray hit

它的大小和规模是距离形式射线起源determinated。如果我没记错的它仅使用垂直距离(以乘距离COS(ray_angle-player_angle))。

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:

这是在做C ++纯GDI(仅使用位图扫描线)没有其他届党库都没有。它采用单一材质,弥漫性+环境照明,2D光线投射。有2位图(屏幕,纹理图谱)和单二维地图。在code是小于9 K字节包括雷姆。它仅由键盘控制(使用鼠标编辑的地图子窗口迷宫)。

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