gpt4 book ai didi

c++ - 计算顶点法线OpenGL

转载 作者:太空狗 更新时间:2023-10-29 20:46:24 29 4
gpt4 key购买 nike

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

enter image description here

我不确定我是否将正确的 vector 传递给 calcNormal() 函数,或者是否有其他错误。

这是我的挥手代码

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;
}

有什么想法吗?

最佳答案

那些没有意义:

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

由此您可以计算位置 vector 的叉积。但您真正想要的是局部差异的叉积,即差异。

即(wave2 - wave1) × (wave3 - wave1)

关于c++ - 计算顶点法线OpenGL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8301106/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com