如何绘制3D表面并隐藏隐形线?

时间:2022-01-04 11:59:50

does someone know how to draw 3D surfaces and hide the invisible lines?

有人知道如何绘制3D表面并隐藏不可见的线条吗?

I remember reading some code from an Apple II book, and it was only about 25 lines in BASIC, and it could plot some 3D surfaces, such as a mountain or a shape that looks like water ripples. The invisible lines were at first shown, but after adding a few lines of code, the invisible lines are gone too.

我记得从Apple II书中读过一些代码,BASIC中只有大约25行,它可以绘制一些3D表面,例如山峰或看起来像水波纹的形状。首先显示了不可见的线条,但在添加几行代码后,隐形线也消失了。

Does someone know how to do it or know of resources on the web for doing this?

有人知道如何做到这一点或知道网上的资源吗?

3 个解决方案

#1


This depends a little on how you are drawing the polygons. I would recommend reading a little about either OpenGL or DirectX as they are designed to solve many of these problems for you. If you don't want to use them directly there are many great engines built on top of these APIs already.

这取决于您如何绘制多边形。我建议您阅读一些关于OpenGL或DirectX的内容,因为它们旨在为您解决许多这些问题。如果您不想直接使用它们,那么已经有许多基于这些API构建的优秀引擎。

Assuming that you are drawing filled polygons (textured, solid etc) here are a couple of common algorithms you could use:

假设您正在绘制填充多边形(纹理,实体等),您可以使用以下几种常用算法:

  • Sort the polygons by distance from the camera and paint those furtherest away first, otherwise known as the Painters Algorithm. This method was used by many old 3D games like the classic Doom which also used a BSP Tree for efficient sorting.

    按照距离摄像机的距离对多边形进行排序,然后先将这些多边形绘制得最远,也称为“画家算法”。许多旧3D游戏都使用这种方法,比如经典的Doom,它也使用BSP树进行高效排序。

  • Store the closest distance from the camera to each pizel on the screen and don't draw anything further away, otherwise known as Z-Buffering. This method is well supported by modern 3D video cards, OpenGL and DirectX.

    将距离相机最近的距离存储在屏幕上的每个pizel上,不要画得更远,否则称为Z-Buffering。现代3D视频卡,OpenGL和DirectX很好地支持这种方法。

  • Use a ray tracing algorithm which naturally has hidden surface removal.

    使用自然具有隐藏表面移除的光线跟踪算法。

#2


I remember that book. If I recall correctly, these were surface plots, and the trick used was to plot the lines from front to back, keeping track of the min and max y plotted for each x. Probably the line drawer was a Bresenham, modified to skip the points drawn unless they were greater than max or less than min.

我记得那本书。如果我没记错的话,这些是表面图,使用的技巧是从前到后绘制线条,跟踪每个x的最小和最大y。可能是线条抽屉是Bresenham,经过修改以跳过绘制的点,除非它们大于最大值或小于最小值。

What's nice about the algorithm is you can do curves, too. Not just line segments. Probably the ripple picture was some sort of combination of trig functions with x and y as parameters.

这个算法有什么好处,你也可以做曲线。不只是细分市场。波纹图像可能是某种三角函数与x和y作为参数的组合。

I can still see those plots in my head. Would love to know what book that was from.

我仍然可以在脑海中看到那些情节。很想知道那是什么书。

#3


If you plot a triangle mesh, then you should do back-face culling of each triangle, and don't draw the edges of the triangles that have a back-facing normal. You calculate the normal as the cross product of two successive edges in the triangle.

如果绘制三角形网格,则应对每个三角形进行背面剔除,不要绘制具有背面法线的三角形边缘。您将法线计算为三角形中两个连续边的叉积。

If you want to plot only the outline, you plot only edges that go between a triangle with a front-facing normal, and a triangle with a back-facing normal.

如果只想绘制轮廓,则只绘制在具有正面法线的三角形和具有背面法线的三角形之间的边。

Or you use OpenGL, turn on the depth buffer, and forget about all the underlying math :-)

或者您使用OpenGL,打开深度缓冲区,忘记所有基础数​​学:-)

#1


This depends a little on how you are drawing the polygons. I would recommend reading a little about either OpenGL or DirectX as they are designed to solve many of these problems for you. If you don't want to use them directly there are many great engines built on top of these APIs already.

这取决于您如何绘制多边形。我建议您阅读一些关于OpenGL或DirectX的内容,因为它们旨在为您解决许多这些问题。如果您不想直接使用它们,那么已经有许多基于这些API构建的优秀引擎。

Assuming that you are drawing filled polygons (textured, solid etc) here are a couple of common algorithms you could use:

假设您正在绘制填充多边形(纹理,实体等),您可以使用以下几种常用算法:

  • Sort the polygons by distance from the camera and paint those furtherest away first, otherwise known as the Painters Algorithm. This method was used by many old 3D games like the classic Doom which also used a BSP Tree for efficient sorting.

    按照距离摄像机的距离对多边形进行排序,然后先将这些多边形绘制得最远,也称为“画家算法”。许多旧3D游戏都使用这种方法,比如经典的Doom,它也使用BSP树进行高效排序。

  • Store the closest distance from the camera to each pizel on the screen and don't draw anything further away, otherwise known as Z-Buffering. This method is well supported by modern 3D video cards, OpenGL and DirectX.

    将距离相机最近的距离存储在屏幕上的每个pizel上,不要画得更远,否则称为Z-Buffering。现代3D视频卡,OpenGL和DirectX很好地支持这种方法。

  • Use a ray tracing algorithm which naturally has hidden surface removal.

    使用自然具有隐藏表面移除的光线跟踪算法。

#2


I remember that book. If I recall correctly, these were surface plots, and the trick used was to plot the lines from front to back, keeping track of the min and max y plotted for each x. Probably the line drawer was a Bresenham, modified to skip the points drawn unless they were greater than max or less than min.

我记得那本书。如果我没记错的话,这些是表面图,使用的技巧是从前到后绘制线条,跟踪每个x的最小和最大y。可能是线条抽屉是Bresenham,经过修改以跳过绘制的点,除非它们大于最大值或小于最小值。

What's nice about the algorithm is you can do curves, too. Not just line segments. Probably the ripple picture was some sort of combination of trig functions with x and y as parameters.

这个算法有什么好处,你也可以做曲线。不只是细分市场。波纹图像可能是某种三角函数与x和y作为参数的组合。

I can still see those plots in my head. Would love to know what book that was from.

我仍然可以在脑海中看到那些情节。很想知道那是什么书。

#3


If you plot a triangle mesh, then you should do back-face culling of each triangle, and don't draw the edges of the triangles that have a back-facing normal. You calculate the normal as the cross product of two successive edges in the triangle.

如果绘制三角形网格,则应对每个三角形进行背面剔除,不要绘制具有背面法线的三角形边缘。您将法线计算为三角形中两个连续边的叉积。

If you want to plot only the outline, you plot only edges that go between a triangle with a front-facing normal, and a triangle with a back-facing normal.

如果只想绘制轮廓,则只绘制在具有正面法线的三角形和具有背面法线的三角形之间的边。

Or you use OpenGL, turn on the depth buffer, and forget about all the underlying math :-)

或者您使用OpenGL,打开深度缓冲区,忘记所有基础数​​学:-)