如何定位在OpenGL一个二十面体的面貌吗? [英] How to orient the faces of an Icosahedron in opengl?

查看:169
本文介绍了如何定位在OpenGL一个二十面体的面貌吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到下面的code从链接 HTTP做一个二十面体:// WWW .glprogramming.com /红/ chapter02.html

I saw the following code to make an icosahedron from the link http://www.glprogramming.com/red/chapter02.html

#define X .525731112119133606 
#define Z .850650808352039932

static GLfloat vdata[12][3] = {    
   {-X, 0.0, Z}, {X, 0.0, Z}, {-X, 0.0, -Z}, {X, 0.0, -Z},    
   {0.0, Z, X}, {0.0, Z, -X}, {0.0, -Z, X}, {0.0, -Z, -X},    
   {Z, X, 0.0}, {-Z, X, 0.0}, {Z, -X, 0.0}, {-Z, -X, 0.0} 
};
static GLuint tindices[20][3] = { 
   {0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1},    
   {8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3},    
   {7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6}, 
   {6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11} };
int i;

glBegin(GL_TRIANGLES);    
for (i = 0; i < 20; i++) {    
   /* color information here */ 
   glVertex3fv(&vdata[tindices[i][0]][0]); 
   glVertex3fv(&vdata[tindices[i][1]][0]); 
   glVertex3fv(&vdata[tindices[i][2]][0]); 
}
glEnd();

数组tindices [] []将告诉您如何连接顶点,使三角形。例如,第一个三角形是从零,第四和第一个顶点进行。如果你把顶点的三角形中给定的顺序,所有的三角形具有相同的方向。

The array tindices[][] tells how to link the vertices to make triangles. For example, the first triangle is made from the zeroth, fourth, and first vertex. If you take the vertices for triangles in the order given, all the triangles have the same orientation.

但我怎么能知道所有的三角形是否会仅仅通过观察code具有相同的方向?对于具有相同的方向,其中所有三角形制造方式必须是顺时针或逆时针全部,对不对?然后,我shouldn`t弥补我心中的复杂结构,只知道它们是否具有相同的方向?即使我观察到的方向是在code给予同样的,我怎么能形成(写)code对方向自己吗?

But how can I know whether or not all the triangles will have the same orientation just by observing the code? For having the same orientation, the way in which all triangles are made must be clockwise or all anticlockwise, right?Then, shouldn`t I make up a complex structure in my mind just to know whether or not they have the same orientation? Even if I observed that the orientation are the same given in the code, how can I form(write) code for the orientation myself?

推荐答案

有关比较简单的形状,您可以绘制它们放到一个三维坐标系统上的一张纸。标记每个顶点与一个索引,并启动列举三角形,确保它们在寻找来自外部的形状时是逆时针方向所有

For relatively simple shapes, you can sketch them into a 3D coordinate system on a piece of paper. Label each vertex with an index, and start enumerating the triangles, making sure that they are all counter-clockwise when looking at the shape from the outside.

在数学上,这也很容易让你的榜样,因为它以原点为中心的凸起形状。所以,如果你计算面法线每个三角形的,它需要指向远离原点。如果坐标3角矢量为 V0 =(X0,Y0,Z0) V1 =(X1,Y1,Z1) V2 =(X2,Y2,Z2),面法线可以计算为 V1的跨产品 - V0 V2 - V0

Mathematically, it's fairly easy for your example because it's a convex shape centered at the origin. So if you calculate the face normal of each triangle, it needs to point away from the origin. If the coordinate vectors of the 3 corners are v0 = (x0, y0, z0), v1 = (x1, y1, z1) and v2 = (x2, y2, z2), the face normal can be calculated as the cross product of v1 - v0 and v2 - v0:

[ x1 - x0 ]   [ x2 - x0 ]   [ (y1 - y0) * (z2 - z0) - (z1 - z0) * (y2 - y0) ]
[ y1 - y0 ] x [ y2 - y0 ] = [ (z1 - z0) * (x2 - x0) - (x1 - x0) * (z2 - z0) ]
[ z1 - z0 ]   [ z2 - z0 ]   [ (x1 - x0) * (y2 - y0) - (y1 - y0) * (x2 - x0) ]

要搞清楚,如果这矢量点远离原点,我们计算的点积的顶点之一,例如 V0 。此点积将必须积极为三角形是逆时针

To figure out if this vector points away from the origin, we calculate the dot product with one of the vertices, e.g. v0. This dot product will have to be positive for the triangle to be counter-clockwise:

x0 * ((y1 - y0) * (z2 - z0) - (z1 - z0) * (y2 - y0)) +
y0 * ((z1 - z0) * (x2 - x0) - (x1 - x0) * (z2 - z0)) +
z0 * ((x1 - x0) * (y2 - y0) - (y1 - y0) * (x2 - x0))

我会从乘以了条件,并结合他们的简单的代数运算拯救你。他们中的大多数最终退学,并简化的结果是:

I'll save you from the simple algebra of multiplying out the terms and combining them. Most of them end up dropping out, and the simplified result is:

  x0 * y1 * z2
- x0 * y2 * z1
+ x2 * y0 * z1
- x1 * y0 * z2
+ x1 * y2 * z0
- x2 * y1 * z0

如果这看起来很熟悉,这绝非偶然。这确实是矩阵的行列式:

If this looks familiar, that's no accident. It's indeed the determinant of the matrix:

[ x0 y0 z0 ]
[ x1 y1 z1 ]
[ x2 y2 z2 ]

这是有道理的,因为这个决定的符号告诉你,如果向量 V0 V1 V2 有一个惯用右手的方向,而这正是我们所期待的摆在首位。

This makes sense, because the sign of this determinant tells you if the vectors v0, v1, v2 have a right-handed orientation, which is exactly what we were looking for in the first place.

应用到您的示例的第一个三角形,它由顶点0,4,1:

Applying this to the first triangle of your example, which consists of vertices 0, 4, 1:

    [ -X  0.0 Z ]
det [ 0.0 Z   X ] = - X * Z * Z - X * Z * Z = - 2 * X * Z * Z
    [ X   0.0 Z ]

第二个三角形,由顶点0,9,4:

Second triangle, consisting of vertices 0, 9, 4:

    [ -X  0.0 Z   ]
det [ -Z  X   0.0 ] = - X * X * X - Z * Z * Z
    [ 0.0 Z   X   ]

三三角形,由顶点9,5,4:

Third triangle, consisting of vertices 9, 5, 4:

    [ -Z  X 0.0 ]
det [ 0.0 Z -X  ] = - X * Z * Z - X * Z * Z = - 2 * X * Z * Z
    [ 0.0 Z X   ]

所有这些因素都负值,所以至少你的第3个三角形都有顺时针方向。

All these determinants have negative values, so at least your first 3 triangles all have clockwise orientation.

这篇关于如何定位在OpenGL一个二十面体的面貌吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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