如何计算 openGL 4.5 中圆锥面的法线?
How can I calculate the normal of a cone face in openGL 4.5?
我是 openGL 4.5 的新手,我目前正在使用灯光,在几何绘图期间计算顶点法线时一切正常,但现在我必须计算圆锥体的面法线,但我不知道如何计算去做吧。我正在用 GL_TRIANGLE_STRIP 绘制它。我们可以在下面看到当前的片段:
float coneAngle = (float) Math.atan(radius / height);
coneCos = (float) Math.cos(coneAngle);
coneSin = (float) Math.sin(coneAngle);
// circleResolution * heightResolution * 3
for(int i = 0; i <= circleResolution; i++)
{
for(int j = 0; j <= heightResolution; j++)
{
angle = 2 * ((float) Math.PI) * (i / (float) circleResolution);
cos = ((float) Math.cos(angle));
sin = ((float) Math.sin(angle));
x = radius * cos;
z = radius * sin;
// Cone Bottom
vertices.add(x);
vertices.add(0.0f);
vertices.add(z);
// Cone Bottom Normal
vertices.add(coneCos * cos);
vertices.add(coneSin);
vertices.add(coneCos * sin);
// Cone Top
vertices.add(0f);
vertices.add(height);
vertices.add(0f);
// Cone Top Normal
vertices.add(0f);
vertices.add(0f);
vertices.add(0f);
}
}
// Center of Bottom Circle - Vertices
vertices.add(0.0f);
vertices.add(0.0f);
vertices.add(0.0f);
// Center of Bottom Circle - Normal
vertices.add(0.0f);
vertices.add(-1.0f);
vertices.add(0.0f);
// CircleResolution - Bottom Circle - TRIANGLE_FAN
for (int j = 0; j <= circleResolution; j++)
{
angle = (2 * ((float) Math.PI)) * ( j / (float) circleResolution );
x = (float) Math.cos(angle);
z = (float) Math.sin(angle);
vertices.add(radius * x);
vertices.add(0.0f);
vertices.add(radius * z);
// Normal
vertices.add(0.0f);
vertices.add(-1.0f);
vertices.add(0.0f);
}
近似圆锥体的多边形
直线或(矢量)的法向量可以通过将向量沿直线旋转 90° 来实现。
沿着圆锥体侧面的向量是 (radius
, -height
)。法向量为 (-(-height)
, radius
).
在您的例子中,底部 ("Cone Bottom Normal") 和顶部 ("Cone Top Normal") 顶点的圆锥侧面的法向量属性为:
// lenght of the flank of the cone
float flank_len = Math.sqrt(radius*radius + height*height);
// unit vector along the flank of the cone
float cone_x = radius / flank_len;
float cone_y = -height / flank_len;
.....
// Cone Bottom Normal
vertices.add(-cone_y * cos);
vertices.add( cone_x );
vertices.add(-cone_y * sin);
.....
// Cone Top Normal
vertices.add(-cone_y * cos);
vertices.add( cone_x );
vertices.add(-cone_y * sin);
我是 openGL 4.5 的新手,我目前正在使用灯光,在几何绘图期间计算顶点法线时一切正常,但现在我必须计算圆锥体的面法线,但我不知道如何计算去做吧。我正在用 GL_TRIANGLE_STRIP 绘制它。我们可以在下面看到当前的片段:
float coneAngle = (float) Math.atan(radius / height);
coneCos = (float) Math.cos(coneAngle);
coneSin = (float) Math.sin(coneAngle);
// circleResolution * heightResolution * 3
for(int i = 0; i <= circleResolution; i++)
{
for(int j = 0; j <= heightResolution; j++)
{
angle = 2 * ((float) Math.PI) * (i / (float) circleResolution);
cos = ((float) Math.cos(angle));
sin = ((float) Math.sin(angle));
x = radius * cos;
z = radius * sin;
// Cone Bottom
vertices.add(x);
vertices.add(0.0f);
vertices.add(z);
// Cone Bottom Normal
vertices.add(coneCos * cos);
vertices.add(coneSin);
vertices.add(coneCos * sin);
// Cone Top
vertices.add(0f);
vertices.add(height);
vertices.add(0f);
// Cone Top Normal
vertices.add(0f);
vertices.add(0f);
vertices.add(0f);
}
}
// Center of Bottom Circle - Vertices
vertices.add(0.0f);
vertices.add(0.0f);
vertices.add(0.0f);
// Center of Bottom Circle - Normal
vertices.add(0.0f);
vertices.add(-1.0f);
vertices.add(0.0f);
// CircleResolution - Bottom Circle - TRIANGLE_FAN
for (int j = 0; j <= circleResolution; j++)
{
angle = (2 * ((float) Math.PI)) * ( j / (float) circleResolution );
x = (float) Math.cos(angle);
z = (float) Math.sin(angle);
vertices.add(radius * x);
vertices.add(0.0f);
vertices.add(radius * z);
// Normal
vertices.add(0.0f);
vertices.add(-1.0f);
vertices.add(0.0f);
}
近似圆锥体的多边形
直线或(矢量)的法向量可以通过将向量沿直线旋转 90° 来实现。
沿着圆锥体侧面的向量是 (radius
, -height
)。法向量为 (-(-height)
, radius
).
在您的例子中,底部 ("Cone Bottom Normal") 和顶部 ("Cone Top Normal") 顶点的圆锥侧面的法向量属性为:
// lenght of the flank of the cone
float flank_len = Math.sqrt(radius*radius + height*height);
// unit vector along the flank of the cone
float cone_x = radius / flank_len;
float cone_y = -height / flank_len;
.....
// Cone Bottom Normal
vertices.add(-cone_y * cos);
vertices.add( cone_x );
vertices.add(-cone_y * sin);
.....
// Cone Top Normal
vertices.add(-cone_y * cos);
vertices.add( cone_x );
vertices.add(-cone_y * sin);