我需要创建一个立方体的 3D 模型,在一个面的中心打一个圆孔,完全穿过立方体到另一侧。我能够为面和孔生成顶点。
其中四个面(未受孔影响)可以建模为单个三角形条带。孔的内部也可以建模为单个三角形条带。
如何为有孔的面生成索引缓冲区?是否有标准算法来做到这一点?
我正在使用 Direct3D,但欢迎来自其他地方的想法。
要生成所需的索引缓冲区,您可以这样做。在 2D 中思考,所讨论的脸是一个顶点为 (±1, ±1) 的正方形,而洞是中间的一个圆。
(x/M,y/M)
,其中M
是max(abs(x),abs(y))
。M
是最大坐标的绝对值,所以这将缩放(x,y)
,使最大坐标为±1。这是一个示例,将圆细分为 64 段,每条射线分为 8 段。您可以选择符合您要求的数字。
替代文字 http://pici.se/pictures/AVhcssRRz.gif
下面是一些演示这一点的 Python 代码:
from math import sin, cos, pi
from itertools import izip
def pairs(iterable):
"""Yields the previous and the current item on each iteration.
"""
last = None
for item in iterable:
if last is not None:
yield last, item
last = item
def circle(radius, subdiv):
"""Yields coordinates of a circle.
"""
for angle in xrange(0,subdiv+1):
x = radius * cos(angle * 2 * pi / subdiv)
y = radius * sin(angle * 2 * pi / subdiv)
yield x, y
def line(x0,y0,x1,y1,subdiv):
"""Yields coordinates of a line.
"""
for t in xrange(subdiv+1):
x = (subdiv - t)*x0 + t*x1
y = (subdiv - t)*y0 + t*y1
yield x/subdiv, y/subdiv
def tesselate_square_with_hole((x,y),(w,h), radius=0.5, subdiv_circle=64, subdiv_ray=8):
"""Yields quads of a tesselated square with a circluar hole.
"""
for (x0,y0),(x1,y1) in pairs(circle(radius,subdiv_circle)):
M0 = max(abs(x0),abs(y0))
xM0, yM0 = x0/M0, y0/M0
M1 = max(abs(x1),abs(y1))
xM1, yM1 = x1/M1, y1/M1
L1 = line(x0,y0,xM0,yM0,subdiv_ray)
L2 = line(x1,y1,xM1,yM1,subdiv_ray)
for ((xa,ya),(xb,yb)),((xc,yc),(xd,yd)) in pairs(izip(L1,L2)):
yield ((x+xa*w/2,y+ya*h/2),
(x+xb*w/2,y+yb*h/2),
(x+xc*w/2,y+yc*h/2),
(x+xd*w/2,y+yd*h/2))
import pygame
def main():
"""Simple pygame program that displays the tesselated figure.
"""
print('Calculating faces...')
faces = list(tesselate_square_with_hole((150,150),(200,200),0.5,64,8))
print('done')
pygame.init()
pygame.display.set_mode((300,300))
surf = pygame.display.get_surface()
running = True
while running:
need_repaint = False
for event in pygame.event.get() or [pygame.event.wait()]:
if event.type == pygame.QUIT:
running = False
elif event.type in (pygame.VIDEOEXPOSE, pygame.VIDEORESIZE):
need_repaint = True
if need_repaint:
print('Repaint')
surf.fill((255,255,255))
for pa,pb,pc,pd in faces:
# draw a single quad with corners (pa,pb,pd,pc)
pygame.draw.aalines(surf,(0,0,0),True,(pa,pb,pd,pc),1)
pygame.display.flip()
try:
main()
finally:
pygame.quit()
您想查找 Tessellation,这是处理 MizardX 所显示内容的数学领域。 3D Graphcs 中的人们必须一直处理这个问题,并且有多种 tessellation 算法来获取带孔的面部并计算渲染它所需的三角形。
现代硬件通常无法正确渲染凹多边形。
具体来说,通常甚至没有办法定义带孔的多边形。
您需要以某种方式在孔周围找到平面的三角剖分。最好的方法可能是创建从孔的顶点到矩形面最近顶点的三角形。这可能会创建一些非常薄的三角形。如果这不是问题,那么你就完成了。如果是,那么您将需要一些网格整流罩/优化算法来创建漂亮的三角形。
阿尔法混合是不可能的吗?如果没有,只需使用中间具有透明度的纹理对带有孔的侧面进行纹理处理。你必须做更多的多边形渲染,因为你不能利用从前到后绘制和忽略覆盖面的优势,但它可能比拥有很多小三角形更快。
我想象有 4 个三角形风扇从广场的 4 个角落飞来。
只是一个想法 -
如果你喜欢作弊(就像在游戏中多次做的那样),你总是可以构造一个普通的立方体,但是你想要的两个面的纹理有一个洞(alpha = 0),然后你可以在着色器中剪辑它,或混合它(在这种情况下,您需要使用 Z 排序进行渲染)。您可以通过构造一个朝内且没有盖子的内圆柱体来获得内孔。
当然,这仅在几何对您不重要时才有效。