在 OpenGL 中近似球体

发布于 2024-10-13 06:18:17 字数 1409 浏览 1 评论 0原文

我正在尝试使用 http://local 中的说明来近似球体。 wasp.uwa.edu.au/~pbourke/miscellaneous/sphere_圆筒/,但它看起来根本不对。这是我的代码:

def draw_sphere(facets, radius=100):
    """approximate a sphere using a certain number of facets"""

    dtheta = 180.0 / facets
    dphi = 360.0 / facets

    global sphere_list
    sphere_list = glGenLists(2)
    glNewList(sphere_list, GL_COMPILE)

    glBegin(GL_QUADS)

    for theta in range(-90, 90, int(dtheta)):
        for phi in range(0, 360, int(dphi)):
            print theta, phi
            a1 = theta, phi
            a2 = theta + dtheta, phi
            a3 = theta + dtheta, phi + dphi
            a4 = theta, phi + dphi

            angles = [a1, a2, a3, a4]

            print 'angles: %s' % (angles)


            glColor4f(theta/360.,phi/360.,1,0.5)

            for angle in angles:
                x, y, z = angle_to_coords(angle[0], angle[1], radius)
                print 'coords: %s,%s,%s' % (x, y, z)
                glVertex3f(x, y, z)



    glEnd()

    glEndList()


def angle_to_coords(theta, phi, radius):  
    """return coordinates of point on sphere given angles and radius"""

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

    return x * radius, y * radius, z * radius

似乎有些四边形并不简单,即边缘交叉,但改变顶点的顺序似乎没有任何区别。

I am trying to approximate a sphere using the instructions at http://local.wasp.uwa.edu.au/~pbourke/miscellaneous/sphere_cylinder/, but it doesn't look right at all. This is my code:

def draw_sphere(facets, radius=100):
    """approximate a sphere using a certain number of facets"""

    dtheta = 180.0 / facets
    dphi = 360.0 / facets

    global sphere_list
    sphere_list = glGenLists(2)
    glNewList(sphere_list, GL_COMPILE)

    glBegin(GL_QUADS)

    for theta in range(-90, 90, int(dtheta)):
        for phi in range(0, 360, int(dphi)):
            print theta, phi
            a1 = theta, phi
            a2 = theta + dtheta, phi
            a3 = theta + dtheta, phi + dphi
            a4 = theta, phi + dphi

            angles = [a1, a2, a3, a4]

            print 'angles: %s' % (angles)


            glColor4f(theta/360.,phi/360.,1,0.5)

            for angle in angles:
                x, y, z = angle_to_coords(angle[0], angle[1], radius)
                print 'coords: %s,%s,%s' % (x, y, z)
                glVertex3f(x, y, z)



    glEnd()

    glEndList()


def angle_to_coords(theta, phi, radius):  
    """return coordinates of point on sphere given angles and radius"""

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

    return x * radius, y * radius, z * radius

It seems that some of the quads aren't simple, i.e. the edges are crossing, but changing the order of the vertices doesn't seem to make any difference.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青衫负雪 2024-10-20 06:18:17

我这里没有可以同时运行 Python 和 OpenGL 的系统,但无论如何我都可以看到一些问题:

您在 dphidtheta 中舍入>范围语句。这意味着面将始终从整度数开始,但是当您添加未舍入的增量值时,远边缘不能保证这样做。这可能是造成重叠的原因。

最好让范围值从 0 ..facets-1 开始,然后将这些索引精确乘以 360/facets(或纬度线的 180)。这可以避免舍入错误,例如:

dtheta = 180.0 / facets
dphi = 360.0 / facets

for y in range(facets):
    theta = y * dtheta - 90
    for x in range(facets):
        phi = x * dphi
        ...

另外,您是否在其他地方转换为弧度? Python 的默认三角函数采用弧度而不是度数。

I don't have a system here that can run Python and OpenGL together, but I can see a few issues anyway:

You're rounding dphi and dtheta in the range statements. This means that the facets will always start on a whole degree, but then when you add the unrounded delta values the far edge isn't guaranteed to do so. This is the probable cause of your overlaps.

It would be better to have your range values go from 0 .. facets-1 and then multiply those indices by 360 / facets (or 180 for the latitude lines) exactly. This would avoid the rounding errors, e.g.:

dtheta = 180.0 / facets
dphi = 360.0 / facets

for y in range(facets):
    theta = y * dtheta - 90
    for x in range(facets):
        phi = x * dphi
        ...

Also, are you converting to radians somewhere else? Python's default trig functions take radians rather than degrees.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文