OpenGL 法线未出现在三角形中

OpenGL Normals not appearing in triangles

我正在使用普通数组渲染普通数据集,如下所示:

glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer( GL_FLOAT, 0, nArray );

我的顶点渲染如下:

glEnableClientState( GL_VERTEX_ARRAY );
glVertexPointer( 3, GL_FLOAT, 0, vArray );
glDrawElements( GL_TRIANGLES, iSize, GL_UNSIGNED_INT, iArray );
glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState( GL_NORMAL_ARRAY );

为了这个例子,我按如下方式填充我的 nArray(正确计算法线时仍然会出现错误):

for(int i = 0; i < nSize * 3; ++i){
    nArray[i] = randomNumber();
}

其中 randomNumber() 返回一个介于 0 和 1 之间的随机浮点数。

我希望 nArray 应该填充 0 到 1 之间的随机浮点数,并且这些应该完全随机地显示在我生成的表面上。

当我渲染输出时,"normals" 显示如下。我不在乎它没有计算出正确的法线,但两个并排三角形的法线似乎是相同的。

下面是表面的线框,显示了单独的三角形:

我使用的遮光方式是:glShadeModel(GL_FLAT);

我预计每个单独的三角形会有完全不同的灰度值,但事实并非如此。感谢任何有关我出错的地方的帮助。

The shade method I am using is: glShadeModel(GL_FLAT);

到目前为止和你在一起。

When I render the output, the "normals" are displayed as follows. I do not care that it doesn't calculate a correct normal, but that it seems the normals for two side by side triangles are the same

是的,you requested that via GL_FLAT

GL primitives can have either flat or smooth shading. Smooth shading, the default, causes the computed colors of vertices to be interpolated as the primitive is rasterized, typically assigning different colors to each resulting pixel fragment. Flat shading selects the computed color of just one vertex and assigns it to all the pixel fragments generated by rasterizing a single primitive.

如果你想在所有三个顶点的法线之间进行插值,你需要使用 GL_SMOOTH