在 OpenGL 中将圆坐标转换为方坐标以进行纹理映射

问题描述 投票:0回答:0

我在 OpenGL 中绘制了一个圆柱体,需要为其添加纹理。要为圆柱体的顶部和底部圆执行纹理映射,我必须将圆坐标 (u, v) 正确转换为方形坐标 (x, y)。但是,对于 x 和 y,我需要转换成的方坐标范围在 [0, 1] 范围内,而不是像 这篇文章 中那样的 [-1, 1]
我已经尝试过了,但这道数学题似乎超出了我的能力范围。希望有人能帮我解决这个问题。谢谢你来看看。
有关更多信息,这是我用来绘制圆柱体的代码:

vertex_data = []

# draw_cylinder(r: radius, h: height, n: num_slices)
def draw_cylinder(r, h, n):
    global vertex_data

    circle_pts = []
    for i in range(n + 1):
        angle = 2 * math.pi * (i / n)
        x = r * math.cos(angle)
        y = r * math.sin(angle)
        if i == n:
            x = r
            y = 0
        pt = (x, y)
        circle_pts.append(pt)

    vertex_data.append([0, 0, h / 2.0])
    for (x, y) in circle_pts:  # vertices for top circle
        z = h / 2.0
        vertex_data.append([x, y, z])

    vertex_data.append([0, 0, -h / 2.0])
    for (x, y) in circle_pts:  # vertices for bottom circle
        z = -h / 2.0
        vertex_data.append([x, y, z])

    for (x, y) in circle_pts:  # vertices for tube
        z = h / 2.0
        vertex_data += [[x, y, z], [x, y, -z]]

    return vertex_data
math opengl texture-mapping coordinate-systems coordinate-transformation
© www.soinside.com 2019 - 2024. All rights reserved.