【发布时间】:2013-12-28 12:05:32
【问题描述】:
在python中,我写了一个3D渲染程序。 Y 旋转工作正常,但 X 旋转由于一些模糊的原因放大。我没看出来,所以放在这里。
def plotLine(W, H, (x, y, z), (x2, y2, z2), rotX, rotY, FOV=1.0):
try:
x = float(x)
y = float(y)
z = float(z)
x2 = float(x2)
y2 = float(y2)
z2 = float(z2)
if z == 0:
z = 0.01
if z2 == 0:
z2 = 0.01
x, y, z = rotateY((x, y, z), rotY)
x, y, z = rotateX((x, y, z), rotX)
x2, y2, z2 = rotateY((x2, y2, z2), rotY)
x2, y2, z2 = rotateX((x2, y2, z2), rotX)
scX = (x/z)*FOV
scY = (y/z)*FOV
scX *= min(W, H)
scY *= min(W, H)
scX += W/2
scY += H/2
scX2 = (x2/z2)*FOV
scY2 = (y2/z2)*FOV
scX2 *= min(W, H)
scY2 *= min(W, H)
scX2 += W/2
scY2 += H/2
pygame.draw.aaline(display, (0, 255, 0), (scX, scY), (scX2, scY2))
except (OverflowError, ZeroDivisionError):
return
def rotateY((x, y, z), degrees): # Looking left and right.
x, y, z = float(x), float(y), float(z)
rads = math.radians(degrees)
newX = (math.cos(rads)*x)+(math.sin(rads)*z)
newY = y
newZ = (-math.sin(rads)*x)+(math.cos(rads)*z)
return (newX, newY, newZ)
def rotateX((x, y, z), degrees):
x, y, z = float(x), float(y), float(z)
rads = math.radians(degrees)
newX = x
newY = (math.cos(rads)*y)+(math.sin(rads)*z)
newZ = (math.sin(rads)*y)+(math.cos(rads)*z)
return (newX, newY, newZ)
任何帮助将不胜感激!
顺便说一句,我在 Wikipedia 上查找了矩阵旋转。要么维基百科弄错了矩阵,要么我把矩阵乘错了,这不太可能。我已经看了好几遍了。
【问题讨论】:
标签: python 3d rotation rendering