如何在我的 3D 引擎中实现背面剔除?

问题描述 投票:0回答:1

我正在尝试在我的 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++ math 3d rendering sfml
1个回答
0
投票

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

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

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 */
  ...
}
© www.soinside.com 2019 - 2024. All rights reserved.