0

I've already asked a similar question before (Texture arrays in OpenGL) but that only works for cubes. I was wondering, how could I use this to texture a sphere? I have a function that generates a sphere's vertices and edges, and a class that handles the drawing of the sphere, but how would I apply textures to said sphere? I want to be able to make a texture class that stores all of the data for the texture. This is what I currently have:

Sphere Vertices and Edges Generation:

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

Sphere Class:

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()

This is what I want the sphere to look like: Textured Sphere

And this is what the sphere's texture looks like: Sphere Texture

I will be happy to provide any extra information if it is required.

User-92
  • 374
  • 3
  • 13
  • What do you want the sphere to look like? – user253751 Oct 05 '20 at 13:15
  • @user253751 I've added an image of what I want the sphere to look like. – User-92 Oct 05 '20 at 13:24
  • And what does the texture file look like? (or what do you want it to look like?) – user253751 Oct 05 '20 at 13:32
  • 2
    What does this have to do with array textures? I only see a single image being used, so just use a single image from the array. – Nicol Bolas Oct 05 '20 at 13:38
  • Similar questions (for c++) [How to map texture to sphere that rendered by parametric equation using points primitive](https://stackoverflow.com/questions/48380127/how-to-map-texture-to-sphere-that-rendered-by-parametric-equation-using-points-p/48388897#48388897) or [Sphere Calculations](https://stackoverflow.com/questions/49080505/sphere-calculations/49081855#49081855) – Rabbid76 Oct 05 '20 at 13:41
  • @NicolBolas If you meant textures for different spheres, I will be using many other textures than just the one provided. If you mean different textures for one sphere, I probably wont be doing that, so just don't use texture arrays if you don't want. – User-92 Oct 05 '20 at 13:42
  • @Rabbid76 Thank You! I'll try to solve my problem with information you linked. – User-92 Oct 05 '20 at 13:43
  • @Rabbid76 The thing with the links you provided is that it doesn't explain how it works. It would be nice if you were able to explain it to me. – User-92 Oct 05 '20 at 19:16
  • @User-92 What do you mean by *"how it works"*. Theres is nothing complicated about it. You just have to setup the mesh and warp the texture around it. All you have to do is to specify (3D) vertex coordinates and (2D )texture coordinates. – Rabbid76 Oct 05 '20 at 19:17
  • @User-92 It is like wrapping a checkered paper around the sphere. The §D vertex coordinates formt the sphere. The checkered paper is the 2D texture, with the 2D texture coordinates arranged in a grid. Each texture coordinate has to be associated to a point on the sphere (a vertex coordinate). – Rabbid76 Oct 05 '20 at 19:31

0 Answers0