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];
}

下面是线框和三角形渲染中存在的问题的图片:

地形渲染条纹问题

如果需要,我可以发布更多代码,但我只是想保持这篇文章简短,所以我试图找到我认为问题可能出在哪里。

4

1 回答 1

1

好吧,对于每个“暗”带,您都会意外地翻转法线,可能是因为表面切线向量以错误的顺序传递到叉积中

a × b = - (b × a)

如果您的地形由三角形条带组成,那么您将获得双向排序,这意味着您必须翻转操作数或否定每个奇数行的叉积结果。

于 2013-08-11T10:09:45.410 回答