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:
And this is what the sphere's texture looks like:
I will be happy to provide any extra information if it is required.