基于pygame._sdl2的3D渲染器添加透视畸变实现方案咨询
为自定义3D渲染器添加透视畸变效果
我正在基于pygame._sdl2库开发自定义3D渲染器,目前已实现基础功能,但缺少透视畸变效果(如基于距离的深度缩放等)。以下是我的Python代码,寻求添加该效果的可行方法,同时附上当前渲染效果截图:
@cython.inline def get_position(length, angle_degrees): angle_radians = math.radians(angle_degrees) x = length * math.cos(angle_radians) y = length * math.sin(angle_radians) return (x, y) import cv2 @cython.inline def project2d(points, rotation, position,global_rotation,global_position): points=numpy.add([[0,0,0]]*len(points),points) rotation=numpy.add([0,0,0],rotation) position=numpy.add([0,0,0],position) global_rotation=numpy.add([0,0,0],global_rotation) global_position=numpy.add([0,0,0],global_position) points2=[] local_center=numpy.mean(numpy.array(points),axis=0) local_rotation=numpy.add(rotation,global_rotation) @cython.inline def rotate(a0,axis=0): point2d=[a0[axis-1],a0[axis]] center2d=[local_center[axis-1],local_center[axis]] local_angle=math.degrees(math.atan2(*numpy.subtract(center2d,point2d))) local_distance=abs(math.dist(point2d,center2d)) npoint2d=get_position(local_distance,local_angle+local_rotation[axis-1]) a0[axis-1],a0[axis]=reversed(npoint2d) return a0 x_rotated_points=map( lambda x: rotate(x,axis=0),points) y_rotated_points=map( lambda x: rotate(x,axis=2),x_rotated_points) z_rotated_points=map( lambda x: rotate(x,axis=1),y_rotated_points) """ x_rotated_points = numpy.array(list(map(lambda x: rotate(x, axis=0), points))).T y_rotated_points = numpy.array(list(map(lambda x: rotate(x, axis=2), x_rotated_points))).T z_rotated_points = numpy.array(list(map(lambda x: rotate(x, axis=1), y_rotated_points))) @cython.inline def transpose(a0,axis=0): point2d=[a0[axis-1],a0[axis]] local_position1=[global_position[axis-1],global_position[axis]] local_distance=abs(math.dist(point2d,local_position1)) local_angle=math.degrees(math.atan2(*numpy.subtract(local_position1,point2d))) ndistance=get_position(local_distance,local_angle+local_rotation[axis-1]) a0[axis-1],a0[axis]=reversed(numpy.add(local_position1,ndistance)) return a0 x_transposed_points=map( lambda x: transpose(x,axis=0),z_rotated_points) y_transposed_points=map( lambda x: transpose(x,axis=2),x_transposed_points) z_transposed_points=map( lambda x: transpose(x,axis=1),y_transposed_points) """ return z_rotated_points
当前渲染效果
- 左上角视角:

- 正面视角:

内容的提问来源于stack exchange,提问作者Roberto E. Torres
相关产品推荐
相关产品推荐

