计算顶点法线OpenGL

发布于 2024-12-18 13:32:13 字数 2117 浏览 2 评论 0原文

我正在尝试计算波的顶点法线,但我得到的是方格效果,而不是所需的平滑阴影。

在此处输入图像描述

我不确定是否将正确的向量传递到 calcNormal()< /code> 功能或其他错误。

这是我的波形代码

Vector3 wave1(Sea::seaGrid[i][j].x, Sea::sinVals[k], Sea::seaGrid[i][j].z);
Vector3 wave2(Sea::seaGrid[i][j+1].x, Sea::sinVals[k], Sea::seaGrid[i][j+1].z);
Vector3 wave3(Sea::seaGrid[i+1][j+1].x, Sea::sinVals[nextK], Sea::seaGrid[i+1][j+1].z);
Vector3 wave4(Sea::seaGrid[i+1][j].x, Sea::sinVals[nextK], Sea::seaGrid[i+1][j].z);

Vector3 waveNorm1 = wave1.calcNormal(wave2);
Vector3 waveNorm2 = wave2.calcNormal(wave3);
Vector3 waveNorm3 = wave3.calcNormal(wave4);
Vector3 waveNorm4 = wave4.calcNormal(wave1);

//rest of wave
glBegin(GL_POLYGON);    
glNormal3f(waveNorm1.x, waveNorm1.y, waveNorm1.z);glTexCoord2f(0.0, 1.0);glVertex3f(Sea::seaGrid[i][j].x, Sea::sinVals[k], Sea::seaGrid[i][j].z);
glNormal3f(waveNorm2.x, waveNorm2.y, waveNorm2.z);glTexCoord2f(0.0, 0.0);glVertex3f(Sea::seaGrid[i][j+1].x, Sea::sinVals[k], Sea::seaGrid[i][j+1].z);
glNormal3f(waveNorm3.x, waveNorm3.y, waveNorm3.z);glTexCoord2f(1.0, 0.0);glVertex3f(Sea::seaGrid[i+1][j+1].x, Sea::sinVals[nextK], Sea::seaGrid[i+1][j+1].z);
glNormal3f(waveNorm4.x, waveNorm4.y, waveNorm4.z);glTexCoord2f(1.0, 1.0);glVertex3f(Sea::seaGrid[i+1][j].x, Sea::sinVals[nextK], Sea::seaGrid[i+1][j].z);
glEnd();

,这是 calcNormal() 函数

Vector3& Vector3::calcNormal(const Vector3 &other){

Vector3 normal = crossProduct(*this, other);

//normalises vector
float vecLength = sqrt(pow(normal.x,2)+pow(normal.y,2)+pow(normal.z,2));// length();

normal.x = normal.x / vecLength;
normal.y = normal.y / vecLength;
normal.z = normal.z / vecLength;

return normal;
}

,这是 crossProduct() 函数,

Vector3 Vector3::crossProduct( const Vector3 &v1,  const Vector3 &v2 )
{
Vector3 vCrossProduct;

vCrossProduct.x = v1.y * v2.z - v1.z * v2.y;
vCrossProduct.y = v1.z * v2.x - v1.x * v2.z;
vCrossProduct.z = v1.x * v2.y - v1.y * v2.x;

return vCrossProduct;
}

有什么想法吗?

I'm trying to calculate the vertex normals for a wave, but I'm getting a chequered effect instead of the desired smooth shading.

enter image description here

I'm not sure if I'm passing the correct vector into the calcNormal() function or if something else is wrong.

Here is my wave code

Vector3 wave1(Sea::seaGrid[i][j].x, Sea::sinVals[k], Sea::seaGrid[i][j].z);
Vector3 wave2(Sea::seaGrid[i][j+1].x, Sea::sinVals[k], Sea::seaGrid[i][j+1].z);
Vector3 wave3(Sea::seaGrid[i+1][j+1].x, Sea::sinVals[nextK], Sea::seaGrid[i+1][j+1].z);
Vector3 wave4(Sea::seaGrid[i+1][j].x, Sea::sinVals[nextK], Sea::seaGrid[i+1][j].z);

Vector3 waveNorm1 = wave1.calcNormal(wave2);
Vector3 waveNorm2 = wave2.calcNormal(wave3);
Vector3 waveNorm3 = wave3.calcNormal(wave4);
Vector3 waveNorm4 = wave4.calcNormal(wave1);

//rest of wave
glBegin(GL_POLYGON);    
glNormal3f(waveNorm1.x, waveNorm1.y, waveNorm1.z);glTexCoord2f(0.0, 1.0);glVertex3f(Sea::seaGrid[i][j].x, Sea::sinVals[k], Sea::seaGrid[i][j].z);
glNormal3f(waveNorm2.x, waveNorm2.y, waveNorm2.z);glTexCoord2f(0.0, 0.0);glVertex3f(Sea::seaGrid[i][j+1].x, Sea::sinVals[k], Sea::seaGrid[i][j+1].z);
glNormal3f(waveNorm3.x, waveNorm3.y, waveNorm3.z);glTexCoord2f(1.0, 0.0);glVertex3f(Sea::seaGrid[i+1][j+1].x, Sea::sinVals[nextK], Sea::seaGrid[i+1][j+1].z);
glNormal3f(waveNorm4.x, waveNorm4.y, waveNorm4.z);glTexCoord2f(1.0, 1.0);glVertex3f(Sea::seaGrid[i+1][j].x, Sea::sinVals[nextK], Sea::seaGrid[i+1][j].z);
glEnd();

This is the calcNormal() function

Vector3& Vector3::calcNormal(const Vector3 &other){

Vector3 normal = crossProduct(*this, other);

//normalises vector
float vecLength = sqrt(pow(normal.x,2)+pow(normal.y,2)+pow(normal.z,2));// length();

normal.x = normal.x / vecLength;
normal.y = normal.y / vecLength;
normal.z = normal.z / vecLength;

return normal;
}

and this is the crossProduct() function

Vector3 Vector3::crossProduct( const Vector3 &v1,  const Vector3 &v2 )
{
Vector3 vCrossProduct;

vCrossProduct.x = v1.y * v2.z - v1.z * v2.y;
vCrossProduct.y = v1.z * v2.x - v1.x * v2.z;
vCrossProduct.z = v1.x * v2.y - v1.y * v2.x;

return vCrossProduct;
}

any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落叶缤纷 2024-12-25 13:32:13

这些没有意义:

Vector3 waveNorm1 = wave1.calcNormal(wave2);
Vector3 waveNorm2 = wave2.calcNormal(wave3);
Vector3 waveNorm3 = wave3.calcNormal(wave4);
Vector3 waveNorm4 = wave4.calcNormal(wave1);

这样您就可以计算位置向量的叉积。但你真正想要的是局部微分的叉积,即差异。

即(波2 - 波1)×(波3 - 波1)

Those make no sense:

Vector3 waveNorm1 = wave1.calcNormal(wave2);
Vector3 waveNorm2 = wave2.calcNormal(wave3);
Vector3 waveNorm3 = wave3.calcNormal(wave4);
Vector3 waveNorm4 = wave4.calcNormal(wave1);

By that you're calculating the cross product of the position vectors. But what you actually want are the cross products of the local differentials, i.e. differences.

I.e. (wave2 - wave1) × (wave3 - wave1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文