如何在 3D 引擎中实现 Backface Culling?

How do I implement Backface Culling into my 3D engine?

提问人:Dylan Angius 提问时间:8/7/2023 更新时间:8/7/2023 访问量:77

问:

我正在尝试在我的 3d 引擎中实现背面剔除,但它似乎不起作用 我关注了 javidx9 的视频,这是我唯一能找到的关于背面剔除的视频, 但它没有用,我不知道为什么。

唯一正确完成的事情(或者我认为是这样) 是正常计算 所以除非我的计算是错误的,否则没有必要添加。 我现在唯一需要的是允许绘制三角形的条件

我有点了解它是如何工作的以及背后的公式,但我无法对其进行编码。 我希望得到什么作为答案,向我展示如何使用代码实现它

这是所需的代码

triangle &listtri = tridrawlist[i]; // the triangle that is going through the culling process
vec3 norm; // the surface normal of the triangle
 
line = listtri.vpos2 - listtri.vpos; // vpos means view space (camera space) position if you were wondering
line2 = listtri.vpos3 - listtri.vpos;
norm = line.cross(line2); // cross() is a cross product function (which works) and in this case the first vector is line and the second is lin2

// here is where you write the condition / variables needed for you to solve the problem
C++ 数学 3D 渲染 SFML

评论

1赞 Mike 'Pomax' Kamermans 8/7/2023
为什么你的帖子读起来像是在问家庭作业?(请注意,就其本身而言,询问家庭作业并没有错,但是如果您要显示具有“这是您编写条件的地方”之类的代码,您确实需要明确它是否是错误的。我们不必写任何东西,不过你显然需要)
0赞 Spektre 8/8/2023
查看 stackoverflow.com/a/67176014/2521214

答:

1赞 fana 8/7/2023 #1

看来您首先应该做的是确认正常向量计算结果。 例如,渲染它并检查法线是指向内方向还是外方向。

如果您确认所有法向量都正确指向外部方向,则“剔除”只是检查法向量是否指向相机侧,例如:

vec3 CameraFrontDirectionalVector;  //e.g. (0,0,1) if Z is front
vec3 norm = (Normal Vector of the Triangle)

if( DotProduct( norm, CameraFrontDirectionalVector ) < 0 )
{//norm directs to camera side
  /* Draw the triangle */
  ...
}