渲染地形法线时出现奇怪的“条纹”问题。

3

我遇到了一个奇怪的问题,当我渲染地形时,我的法线不起作用。尽管我的地形可以正常渲染,但我省略了所有计算高度图的地形点以及计算索引的代码。我知道我应该使用着色器,但我想先解决这个问题。我假设问题来自于我在法线生成代码中忽视了一些明显的东西,如下所示:

for (currentind = 0; currentind < indices.size() - 3; currentind+=3)
{

indtopt = indices[currentind] * 3;
point1.vects[0]=terrainpoints[indtopt];//x
point1.vects[1]=terrainpoints[indtopt+1];//y
point1.vects[2]=terrainpoints[indtopt+2];//z


indtopt = indices[currentind+1] * 3;
//second indice

//points of that indice
point2.vects[0]=terrainpoints[indtopt];//x
point2.vects[1]=terrainpoints[indtopt+1];//y
point2.vects[2]=terrainpoints[indtopt+2];//z

indtopt = indices[currentind+2] *3;
//third indice

//points of that indice
point3.vects[0]=terrainpoints[indtopt];//x
point3.vects[1]=terrainpoints[indtopt+1];//y
point3.vects[2]=terrainpoints[indtopt+2];//z

//--------------------------------------------------
point4.vects[0]=(point2.vects[0]-point1.vects[0]);
point4.vects[1]=(point2.vects[1]-point1.vects[1]);
point4.vects[2]=(point2.vects[2]-point1.vects[2]);

point5.vects[0]=(point3.vects[0]-point2.vects[0]);
point5.vects[1]=(point3.vects[1]-point2.vects[1]);
point5.vects[2]=(point3.vects[2]-point2.vects[2]);
//-------------------------------------------------

//cross product
point6.vects[0]=point4.vects[1]*point5.vects[2] - point4.vects[2]*point5.vects[1];
point6.vects[1]=point4.vects[2]*point5.vects[0] - point4.vects[0]*point5.vects[2];
point6.vects[2]=point4.vects[0]*point5.vects[1] - point4.vects[1]*point5.vects[0];

point6 = point6.normalize();
ternormals[currentind]=point6.vects[0];
ternormals[currentind+1]=point6.vects[1];
ternormals[currentind+2]=point6.vects[2];
}

以下是线框图和三角形渲染中问题的图片: 地形渲染条纹问题 如果需要,我可以发布更多代码,但我只是想让这篇文章保持简短,所以我试图找到可能存在问题的地方。
1个回答

1
每个“黑色”条带都是意外地翻转了法线,可能是因为表面切向量以错误的顺序传递到叉积中。
a × b = - (b × a)

如果你的地形是由三角带构成的,那么你就有了一个双向排序,这意味着你必须为每个奇数行翻转操作数或否定叉积的结果。

我认为这可能是情况。然而,地形实际上是由三角形渲染的,因此不需要双向排序。 - user2446445
好的,有些东西正在翻转你的法线。作为一个简单的解决方法,我建议你只需测试法线是否向下,如果是,则翻转法线;此外,你还应该翻转三角形的绘制顺序,否则它会在启用剔除时被剔除。 - datenwolf

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接