有谁知道怎么做? 我希望能够在Python中将纹理映射到3d立方体。
到目前为止,这是我的代码,
from pygame import gfxdraw
import pygame, sys, math
def rotate2d(pos,rad): x,y=pos; s,c = math.sin(rad),math.cos(rad); return x*c-y*s,y*c+x*s
class Cam:
def __init__(self,pos=(0,0,0),rot=(0,0)):
self.pos=list(pos)
self.rot=list(rot)
def events(self,event):
if event.type == pygame.MOUSEMOTION:
x,y = event.rel
x/=200;y/=200
self.rot[0]+=y; self.rot[1]+=x
def update(self,dt,key):
s=dt*10
if key[pygame.K_q]: self.pos[1]+=s
if key[pygame.K_e]: self.pos[1]-=s
x,y = s*math.sin(self.rot[1]),s*math.cos(self.rot[1])
if key[pygame.K_w]: self.pos[0]+=x;self.pos[2]+=y
if key[pygame.K_s]: self.pos[0]-=x;self.pos[2]-=y
if key[pygame.K_a]: self.pos[0]-=y;self.pos[2]+=x
if key[pygame.K_d]: self.pos[0]+=y;self.pos[2]-=x
class Cube:
vertices = (-1,-1,-1),(1,-1,-1),(1,1,-1),(-1,1,-1),(-1,-1,1),(1,-1,1),(1,1,1),(-1,1,1)
edges = (0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),(0,4),(1,5),(2,6),(3,7)
faces = (0,1,2,3),(4,5,6,7),(0,1,5,4),(2,3,7,6),(0,3,7,4),(1,2,6,5)
colors = (255,0,0),(0,255,0),(0,0,255),(255,255,0),(255,0,255),(0,255,255)
def __init__(self,pos=(0,0,0)):
x,y,z = pos
self.verts = [(x+X/2,y+Y/2,z+Z/2) for X,Y,Z in self.vertices]
pygame.init()
w,h = 400,400; cx,cy = w//2,h//2; fov = min(w,h)
screen = pygame.display.set_mode((w,h))
clock = pygame.time.Clock()
cam=Cam((0,0,-5))
pygame.event.get(); pygame.mouse.get_rel()
pygame.mouse.set_visible(0); pygame.event.set_grab(1);
i1 = pygame.image.load("Untitled.png")
cubes = [Cube((0,0,0)),Cube((-2,0,0)),Cube((2,0,0))]
while True:
dt = clock.tick()/1000
event = pygame.event.poll()
if event.type == pygame.QUIT: pygame.quit();sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE: pygame.quit();sys.exit()
cam.events(event)
screen.fill((0,0,0))
face_list = []; face_color = []; depth = []
for obj in cubes:
vert_list = []; screen_coords = []
for x,y,z in obj.verts:
x-=cam.pos[0]
y-=cam.pos[1]
z-=cam.pos[2]
x,z = rotate2d((x,z),cam.rot[1])
y,z = rotate2d((y,z),cam.rot[0])
vert_list += [(x,y,z)]
f=fov/z
x,y = x*f,y*f
screen_coords+=[(cx+int(x),cy+int(y))]
for f in range(len(obj.faces)):
face = obj.faces[f]
on_screen = False
for i in face:
x,y = screen_coords[i]
if vert_list[i][2]>0 and x>0 and x<w and y>0 and y<h: on_screen = True; break
if on_screen:
coords = [screen_coords[i] for i in face]
face_list += [coords]
face_color += [obj.colors[f]]
depth += [sum([sum(vert_list[j][i] for j in face)**2 for i in range(3)])]
order = sorted(range(len(face_list)),key=lambda i: depth[i],reverse=1)
for i in order:
try:
x-=cam.pos[0]
y-=cam.pos[1]
z-=cam.pos[2]
x,z = rotate2d((x,z),cam.rot[1])
y,z = rotate2d((y,z),cam.rot[0])
vert_list += [(x,y,z)]
f=fov/z
x,y = x*f,y*f
#x2 = str(x)
#x2 = int(x2.split('.')[0])
#{
# HERE IS THE TEXTURED POLYGON PART!!!!! VVVVVVVV
pygame.gfxdraw.textured_polygon(screen, face_list[i], i1, 0, 0) # !!!
#}
except:
pass
pygame.display.flip()
key=pygame.key.get_pressed()
cam.update(dt/10,key)
到目前为止,我正在使用gfxdraw.textured_polygon,但这意味着2d。所以它看起来很蒙蔽,很奇怪。我尝试使用x,y值而不是偏移部分中的0,0。
我按照本教程执行此操作:https://www.youtube.com/watch?v=g4E9iq0BixA
任何需要的帮助。
如果您还有其他需求,请询问。