计算四边形的顶点法线 [英] Calculating the vertex normals of a quad

查看:203
本文介绍了计算四边形的顶点法线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有以下数组:

Lets say that i have the following array :

float QuadVertices[4 * 2];
float QuadNormals[4 * 2];

我以这种方式填写:

//Fill vertices for a 2d quad
Renderer->FillVertices(QuadVertices,GL_QUADS,x,y,width,height);

现在,一切正常,我可以渲染四边形,对其进行纹理处理,将其拉伸等等.

Now at this point everything is ok i can render a quad , texture it , stretch it and all that.

但是现在我要计算四边形的法线:

But now i want to calculate the normals of the quad :

for (int i = 0; i < 8;i++)
{
    QuadNormals[i] = ??
}

但是我不知道我应该如何计算一个简单的2d顶点数组的法线,该数组包含GL_QUADS的4个顶点或GL_TRIANGLES的6个顶点....

BUT i can't figure out how on earth i am supposed to calculate the normals of a simple 2d vertice array that contains either 4vertices of GL_QUADS or 6vertices of GL_TRIANGLES....

推荐答案

如果您有此-

   v1        v2
    +---------+
    |         | 
    |         |
    +---------+
    v3        v4

其中v1..v4是四边形的顶点,然后要计算v1的法线,则应沿着其顶点的两个边计算向量,然后计算这些顶点的叉积.

Where v1..v4 are the vertices of your quad then to calculate the normal at v1 you should calculate the vectors along the two edges it is on, and then calculate the cross product of those vertices.

因此,v1的法线是

CrossProduct((v2-v1), (v3-v1))

您可以为每个顶点重复此操作,尽管如果四边形是平坦的",它们将全部相同

You can repeat this for each vertex, although they will all be the same if the quad is "flat"

如果还有其他四边形连接到该四边形,则可能需要计算每个四边形的法线,然后将所有连接的四边形的平均值指定为该顶点的法线

If you have other quads connected to this one, you may want to calculate the normal for each quad, and then assign the average of all the connected quads to be the normal for that vertex

这篇关于计算四边形的顶点法线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆