python - 在 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()
如果需要,我很乐意提供任何额外的信息。
解决方案
推荐阅读
- python - Python - 使用 Tkinter 的对象没有属性错误
- sql - 为什么“ORDER BY”可以在执行时使用不存在的列?
- reactjs - 使用 GraphQL 按字符串查询 Wordpress 帖子
- datasource - 如果 --mixed-- 类型的数据源之一关闭,则 Grafana 仪表板无法加载
- html - WebAIM 对比度检查器报告错误,但手动 WAVE 对比度检查通过就好了
- python - 用邻接矩阵表示图
- python - 如何在 Windows 上删除超过 14 天的文件
- python - 实例化列表类型之间的关系
- javascript - 查找两个日期 JSON 之间的时间
- excel - 如果“B1”>“B2”,则连续自动刷新一些查询,但每 2 分钟自动刷新所有查询