首页 > 解决方案 > 在 OpenGL 中使用纹理数组对球体进行纹理处理

问题描述

我之前已经问过一个类似的问题(OpenGL 中的纹理数组),但这仅适用于立方体。我想知道,我怎么能用它来纹理一个球体?我有一个生成球体顶点和边缘的函数,以及一个处理球体绘制的类,但是我如何将纹理应用于所述球体?我希望能够创建一个纹理类来存储纹理的所有数据。这是我目前拥有的:

球体顶点和边的生成:

def uv_sphere_vertices(radius, stackcount : int, sectorcount : int, position = (0,0,0)):
    verts = []
    edges = []
    sectorstep = 2 * math.pi / sectorcount
    stackstep = math.pi / stackcount
    for num in range(stackcount+1):
        stackangle = math.pi / 2 - num * stackstep
        for num2 in range(sectorcount+1):
            sectorangle = num2 * sectorstep
            x = radius * math.cos(stackangle) * math.cos(sectorangle) + position[0]
            y = radius * math.cos(stackangle) * math.sin(sectorangle) + position[1]
            z = radius * math.sin(stackangle) + position[2]
            verts.append([x,y,z])

    for num in range(stackcount):
        cstack = num * (sectorcount + 1)
        nstack = cstack + sectorcount + 1
        for num2 in range(sectorcount):
            #if num != 0:
            edges.append([cstack, nstack, cstack + 1])
            if num != stackcount - 1:
                edges.append([cstack + 1, nstack, nstack + 1])
            cstack += 1
            nstack += 1

    return verts,edges

球类:

class UV_Sphere(Mesh):
    def __init__(self, position, radius, stackcount, sectorcount):
        """
        BUILT-IN MESH
        """
        verts,uedges = uv_sphere_vertices(radius, stackcount, sectorcount)
        super().__init__(position,verts,None,(1,1,1),uedges)
        self.vertexArray = []
        for edge in self.edges:
            for vertex in edge:
                self.vertexArray += self.vertices[vertex]  # <--- flat list

        array = (GLfloat * len(self.vertexArray))(*self.vertexArray)
        self.vbo = glGenBuffers(1)
        glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
        glBufferData(GL_ARRAY_BUFFER, array, GL_STATIC_DRAW)
        glBindBuffer(GL_ARRAY_BUFFER, 0)

    def draw_edges(self):
        """Draws the sphere's edges"""
        glPushMatrix()
        glTranslate(self.position[0], self.position[1], self.position[2])
        glRotate(self.rotation[3],self.rotation[0],self.rotation[1],self.rotation[2])
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)

        glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
        glVertexPointer(3, GL_FLOAT, 0, None)
        glBindBuffer(GL_ARRAY_BUFFER, 0)

        glEnableClientState(GL_VERTEX_ARRAY)
        glDrawArrays(GL_TRIANGLES, 0, len(self.vertexArray) // 3)
        glDisableClientState(GL_VERTEX_ARRAY)

        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
        glPopMatrix()

    def draw_faces(self):
        """Draws the sphere's edges"""
        glPushMatrix()
        glTranslate(self.position[0], self.position[1], self.position[2])
        glRotate(self.rotation[3],self.rotation[0],self.rotation[1],self.rotation[2])

        glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
        glVertexPointer(3, GL_FLOAT, 0, None)
        glBindBuffer(GL_ARRAY_BUFFER, 0)

        glEnableClientState(GL_VERTEX_ARRAY)
        glDrawArrays(GL_TRIANGLES, 0, len(self.vertexArray) // 3)
        glDisableClientState(GL_VERTEX_ARRAY)

        glPopMatrix()

这就是我希望球体的样子: 纹理球体

这就是球体的纹理: 球体纹理

如果需要,我很乐意提供任何额外的信息。

标签: pythonopengltexturespyopengl

解决方案


推荐阅读