我有一个奇怪的问题,当我渲染地形时,我的法线不起作用。我的地形渲染得很好,所以我遗漏了从高度图计算地形点的所有代码以及我如何计算索引。我知道我应该使用着色器,但我希望在继续之前先修复它。我假设问题来自我在法线生成代码中忽略的一些明显的问题,如下所示:
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];
}
下面是线框和三角形渲染问题的图片:
如果需要,我可以发布更多代码,但我只想保持这篇文章的简短,所以我试图找到我认为问题所在的位置。
答案 0 :(得分:1)
好吧,对于每个“黑暗”乐队,你都会意外地翻转法线,可能是因为表面切线向量以错误的顺序传递到交叉产品中
a × b = - (b × a)
如果您的地形是由三角形条带组成的,那么您就有了双向排序,这意味着您必须翻转操作数或否定每个奇数行的叉积结果。