OpenGL ES 2.0 Sphere [英] OpenGL ES 2.0 Sphere

查看:222
本文介绍了OpenGL ES 2.0 Sphere的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我特别想知道如何计算顶点。

在OpenGL ES 2.0中使用GL_TRIANGLES绘制纹理球体的最简单方法是什么?

解决方案

有多种三角化球体的方法。受欢迎的,不受欢迎的,好的,不太好的。不幸的是,最广泛使用的方法不是很好。

球面坐标



这可能是最广泛的采用的方法。您在两个嵌套循环中遍历球形坐标系中的两个角度,并为每个角度生成点一对角度。使用 theta -pi / 2 pi / 2 0 2 * pi 迭代,c>和 phi >和球体半径 r ,每个点的计算公式为:

  x = r * cos(theta)* cos(phi)
y = r * cos(theta)* sin(phi)
z = r * sin(theta)

如果需要,计算可以更高效,但我会跳过这个答案。镶嵌的级别(精度)由角度的细分数决定。



这种方法的主要优点是易于实现,易于操作理解。您可以将细分图像作为地球上的经线和纬线。



尽管如此,它不会产生非常好的三角测量。赤道周围的三角形在所有方向上具有相似的尺寸,但接近南/北极的三角形越来越窄。在北/南极你有很多很窄的三角形在一个点上相遇。好的三角形具有所有非常相似的大小的三角形,而这个不是。



八面体的递归细分



这种方法,你从一个普通的八面体开始,给你8个三角形。然后您递归地将每个三角形细分为4个子三角形,如下所示:

  / \ 
/ \
/ ____ \
/ \ / \
/ \ / \
/ ____ \ / ____ \


精度)的细分由递归细分中的层数决定。它从0级八面体的8个原始三角形开始,在1级产生32个三角形,在2级产生128个,在3级产生512个等等。通常你会在3级左右获得相当好看的球体。



这种方法导致更加规则的三角测量,因此优于球坐标方法。

主要缺点是它看起来可能更复杂。点的计算其实很简单。如果你想使用索引顶点而不是重复公共顶点,它会变得更加棘手。如果你想建立漂亮的三角形条,那就更加痛苦了。不是非常困难,但它需要一些工作。



这是我最喜爱的绘画领域的方法。



其他多面体



你可以做同样的事情,我描述的八面体从其他多面体开始。由三角形组成的正多面体特别合适,这使得三面体和二十面体成为自然候选。八面体是最有吸引力的恕我直言,因为初始坐标很容易列举。使用二十面体可能会导致更加规则的三角测量,并且可以查找顶点坐标。

细分立方体



我不确定是否有人真的使用这个。但我最近尝试过,而且很有趣。 :)这个想法是,你把一个以原点为中心的立方体,并将六边中的每一边细分成更小的平方。然后你可以通过标准化每个描述顶点的矢量来将立方体变成一个球体。

这种方法的优点是非常简单,包括构建三角形带子。三角测量的质量似乎相当好。我认为它不像递归细分的八面体那样规则,但肯定比(广泛使用的)球坐标方法更好。


What is the easiest way to draw a textured Sphere in OpenGL ES 2.0 with GL_TRIANGLES?

I'm especially wondering how to calculate the vertices.

解决方案

There are various ways of triangulating spheres. Popular ones, less popular ones, good ones, and not so good ones. Unfortunately the most widely used approach isn't very good.

Spherical Coordinates

This might be the most widely used approach. You iterate through the two angles in a spherical coordinate system in two nested loops, and generate points for each pair of angles. With angle theta iterating from -pi/2 to pi/2 and angle phi iterating from 0 to 2*pi, and sphere radius r, each point is calculated as:

x = r * cos(theta) * cos(phi)
y = r * cos(theta) * sin(phi)
z = r * sin(theta)

The calculation can be made more efficient if necessary, but I'll skip that aspect for this answer. The level (precision) of the tessellation is determined by the number of subdivisions of the angles.

The main advantage of this approach is that it's simple to implement, and easy to understand. You can picture the subdivision as the lines of latitude and longitude on a globe.

It does not result in a very good triangulation, though. The triangles around the equator have similar dimensions in all directions, but the triangles closer to the north/south pole get increasingly narrow. At the north/south pole you have a large number of very narrow triangles meeting in a single point. Good triangulations have all very similar sized triangles, and this one does not.

Recursive Subdivision of Octahedron

With this approach, you start with a regular octahedron, giving you 8 triangles. You then recursively subdivide each triangle into 4 sub-triangles, as illustrated here:

     /\
    /  \
   /____\
  /\    /\
 /  \  /  \
/____\/____\

So each triangle is subdivided by calculating 3 additional vertices that are midway between two of the existing vertices, and 4 triangles are formed from these 6 vertices. For calculating the midway point between two input points, you calculate the sum of the two vectors, and normalize the result to get the point back on the sphere.

The level (precision) of the tessellation is determined by the number of levels in the recursive subdivision. It starts with the 8 original triangles of the octahedron at level 0, results in 32 triangles at level 1, 128 at level 2, 512 at level 3, etc. You normally get a reasonably good looking sphere around level 3.

This approach results in a much more regular triangulation, and is therefore superior to the spherical coordinate approach.

The main disadvantage is that it might seem more complex. The calculation of the points is in fact very simple. It gets slightly more tricky if you want to use indexed vertices, instead of repeating common vertices. And even more painful if you want to build nice triangle strips. Not terribly difficult, but it takes some work.

This is my favorite approach of drawing spheres.

Other Polyhedra

You can do the same thing I described for the octahedron starting with other polyhedra. Regular polyhedra that consist of triangles are particularly suitable, which makes the tethrahedron and the icosahedron natural candidates. The octahedron is the most attractive IMHO because the initial coordinates are so easy to enumerate. Using an icosahedron would probably result in an even more regular triangulation, and the vertex coordinates can be looked up.

Subdivided Cube

I'm not sure if anybody is actually using this. But I tried it recently, and it was kind of fun. :) The idea is that you take a cube centered at the origin, and subdivide each of the six sides into smaller sub-squares. You can then turn the cube into a sphere by simply normalizing each of the vectors that describe a vertex.

The advantage of this approach is that it's very simple, including building triangle strips. The quality of the triangulation seems reasonably good. I don't think it's as regular as the recursively subdivided octahedron, but definitely better than the (much too) widely used spherical coordinate approach.

这篇关于OpenGL ES 2.0 Sphere的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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