简体   繁体   English

圆锥的OpenGL / GLUT曲面法线

[英]OpenGL/GLUT surface normals of cone

I created a cone with GL_TRIANGLE_FAN 我用GL_TRIANGLE_FAN创建了一个圆锥GL_TRIANGLE_FAN

// draw the upper part of the cone
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0, 0, height);
for (int angle = 0; angle < 360; angle++) {
    glVertex3f(sin(angle) * radius, cos(angle) * radius, 0);
}
glEnd();

// draw the base of the cone
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0, 0, 0);
for (int angle = 0; angle < 360; angle++) {
    // normal is just pointing down
    glNormal3f(0, -1, 0);
    glVertex3f(sin(angle) * radius, cos(angle) * radius, 0);
}
glEnd();

How do I get the surface normals? 我如何获得表面法线? For the bottom am I right to say normal is just pointing down? 对于底部我是正确的说正常只是指向下方?

UPDATE UPDATE

I tried using 我试过用

for (int angle = 0; angle < 360; angle++) {
    glNormal3f(sin(angle), cos(angle), 0);
    glVertex3f(sin(angle) * radius, cos(angle) * radius, 0);
}

But it looks weird in some angles ... 但从某些角度看它看起来很怪异......

在此输入图像描述

在此输入图像描述

2nd image looks like just 1 solid color? 第二张图片看起来只有1个纯色?

Assuming your cone has height h and radius r and its as standing (say its tip points into +Y direction) the lateral face normal depends on two angles: the angle of the circular ground area and the angle of the cone's tip (lets call it cone angle or α). 假设你的锥体有高度h和半径r并且它的直立(比如它的尖端指向+ Y方向),侧面法线取决于两个角度:圆形地面区域的角度和锥形尖端的角度(让我们称之为锥角或α)。 This cone angle in turn depends on the ratio of h and r . 该锥角又取决于hr的比率。

Looking at the crossection of the cone we see basically a right angle triangle, whose one cathedus has length h and the other one r . 看着锥体的横截面,我们基本上看到了一个直角三角形,其中一个神像的长度为h ,另一个为r Lets assume that the h cathetus goes straight up the Y axis from the origin and the r cathetus one does the same along the X axis. 让我们假设h cathetus从原点沿Y轴直线上升,而r cathetus沿X轴做同样的事。 Now we want to calculate the normal of the hypothenuse point outwards. 现在我们想要向外计算hypothenuse点的法线。

角度校正和法线坐标

Doing some angle math on the triangle we can see that the normal of the hyponetuse has following form: 在三角形上做一些角度数学,我们可以看到hyponetuse的法线有以下形式:

(cos(coneAngle), sin(coneAngle))

with

coneAngle = atan(r / h)

This is of course only in 2D and we need the 3D equivalent. 这当然只在2D中,我们需要3D等价物。 At first we need the normal of a circle in the XZ plane. 首先,我们需要XZ平面中的圆的法线。 This can be written as 这可以写成

(cos(circleAngle), 0, sin(circleAngle))

And now we can combine these two to one normal equation. 现在我们可以将这两个结合到一个正规方程中。 Our slope normal has a horizontal and a vertical part. 我们的斜率法线有一个水平和一个垂直部分。 The vertical part goes directly into the Y coordinate, while the horizontal part contributes to both horizontal direction (X and Z): 垂直部分直接进入Y坐标,而水平部分直接进入水平方向(X和Z):

(cos(coneAngle) * cos(circleAngle), sin(coneAngle), cos(coneAngle) * sin(circleAngle))

Basically there are two vectors: The up vector which points to the tip of the cone and the horizontal vector, which is the one generated by the circle normal. 基本上有两个向量:指向锥体尖端的向上向量和水平向量,即由圆形法线生成的向量。 These two vectors form a base and what I have done here is to apply a linear transformation from the XY 2D space (of the cone normal) into the space spanned by the circle normal and the up vector (Y axis). 这两个向量形成一个基础,我在这里所做的是应用从XY二维空间(锥法线)到圆形法线和向上矢量(Y轴)跨越的空间的线性变换。 To do this transformation you multiply the components of the XY space vector with the respective base vectors of the other space and sum the result together, so you basically calculate: 要进行此转换,您需要将XY空间矢量的分量与其他空间的相应基矢量相乘,并将结果相加,因此您基本上计算:

cos(coneAngle) * (cos(circleAngle), 0, sin(circleAngle)) + sin(coneAngle) * (0, 1, 0)

Update 更新

I just noticed that the two triangles in the hypothenuse normal image are similar, which means, that one can calculate the normal without trigonometric functions: Given the length of the hypothenuse c = sqrt(h * h + r * r) we know from the similarity of the triangles that: 我只是注意到,hypothenuse正常图像中的两个三角形是相似的,这意味着,可以计算没有三角函数的法线:给定长度的长度c = sqrt(h * h + r * r)我们从三角形的相似性:

n_x / 1 = n_x = h / c

and

n_y / 1 = n_y = r / c

Therefore, the hypothenuse normal is: 因此,hypothenuse正常是:

1/c * (h, r)

Incidentally, multiplying by the factor 1/c is a mere normalization of the vector (h, r) . 顺便提及,乘以因子1/c仅仅是矢量(h, r)归一化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM