如何在立方体上获得平坦的法线 [英] How to get flat normals on a cube
问题描述
我正在使用不推荐使用的OpenGL,并且我的光照计算是在片段着色器上完成的.所以,我正在做平滑着色.
I am using OpenGL without the deprecated features and my light calculation is done on fragment shader. So, I am doing smooth shading.
我的问题是,当我绘制一个立方体时,我需要平面法线.平面法线是指在面部中生成的每个片段都具有相同的法线.
My problem, is that when I am drawing a cube, I need flat normals. By flat normals I mean that every fragment generated in a face has the same normal.
到目前为止,我的解决方案是为每个面生成不同的顶点.因此,现在我有了24(6 * 4)个顶点,而不是有8个顶点.
My solution to this so far is to generate different vertices for each face. So, instead of having 8 vertices, now I have 24(6*4) vertices.
但这对我来说似乎是错误的,复制了顶点.有没有更好的方法来获得平坦的法线?
But this seems wrong to me, replicating the vertexes. Is there a better way to get flat normals?
更新:我使用的是OpenGL版本3.3.0,我还不支持OpenGL 4.
Update: I am using OpenGL version 3.3.0, I do not have support for OpenGL 4 yet.
推荐答案
如果在摄影机空间中进行照明,则可以使用dFdx/dFdy从顶点的摄影机空间位置计算面部法线.
If you do the lighting in camera-space, you can use dFdx/dFdy to calculate the normal of the face from the camera-space position of the vertex.
因此片段着色器看起来像这样.
So the fragment shader would look a little like this.
varying vec3 v_PositionCS; // Position of the vertex in camera/eye-space (passed in from the vertex shader)
void main()
{
// Calculate the face normal in camera space
vec3 normalCs = normalize(cross(dFdx(v_PositionCS), dFdy(v_PositionCS)));
// Perform lighting
...
...
}
这篇关于如何在立方体上获得平坦的法线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!