繁体   English   中英

如何在pyglet中制作带有面部图像的立方体

[英]How to make a cube in pyglet with images on faces

我正在制作一个 3d 游戏,我需要在一个立方体上粘贴图像,但我找不到front / back / top / bottom / left / right纹理应该去哪里。

我刚刚开始使用pyglet,所以我对它不是很熟悉。 几天来我一直在翻转立方体的纹理部分,它仍然是一团糟。 下面是我的立方体函数代码:

def cuboid(self, x1,y1,z1, x2,y2,z2, tex):
    '''
    Draws a cuboid from x1,y1,z1 to x2,y2,z2 and covers each side with tex\n
    tex format:\n
        (front, back, left, right, top, bottom)
    Facing in the -z direction
    '''
    front = tex[0]
    back = tex[1]
    left = tex[2]
    right = tex[3]
    top = tex[4]
    bottom = tex[5]

    tex_coords = ("t2f", (0,0, 1,0, 1,1, 0,1))

    self.batch.add(4,GL_QUADS,back,('v3f',(x1,y1,z1, x1,y1,z2, x1,y2,z2, x1,y2,z1, )),tex_coords)
    self.batch.add(4,GL_QUADS,right,('v3f',(x2,y1,z2, x2,y1,z1, x2,y2,z1, x2,y2,z2, )),tex_coords)
    self.batch.add(4,GL_QUADS,top,('v3f',(x1,y1,z1, x2,y1,z1, x2,y1,z2, x1,y1,z2, )),tex_coords)
    self.batch.add(4,GL_QUADS,front,('v3f',(x1,y2,z2, x2,y2,z2, x2,y2,z1, x1,y2,z1, )),tex_coords)
    self.batch.add(4,GL_QUADS,bottom,('v3f',(x2,y1,z1, x1,y1,z1, x1,y2,z1, x2,y2,z1, )),tex_coords)
    self.batch.add(4,GL_QUADS,left,('v3f',(x1,y1,z2, x2,y1,z2, x2,y2,z2, x1,y2,z2, )),tex_coords)

我无法显示我所期望的内容,因为我没有可用的 3d 渲染程序。

我假设坐标 ( x1 , y1 , z1 ) 是立方体的最小值,坐标是最大值 ( x1 , y1 , z1 )。 例如:

self.cuboid(0, 0, 0, 1, 1, 1, self.tex)

如果立方体是在视图空间中绘制的,则:

x 轴指向左侧。 必须将left纹理放置在所有顶点的 x 坐标为x1 ,将right纹理放置在 x 坐标为x2

self.batch.add(4, GL_QUADS, right,  ('v3f',(x1,y1,z1, x1,y1,z2, x1,y2,z2, x1,y2,z1)), tex_coords)
self.batch.add(4, GL_QUADS, left,   ('v3f',(x2,y1,z2, x2,y1,z1, x2,y2,z1, x2,y2,z2)), tex_coords)

y 轴朝上。 所以bottom是 y 坐标是y1 ,顶部是 y 坐标是y2

self.batch.add(4, GL_QUADS, bottom, ('v3f',(x1,y1,z1, x2,y1,z1, x2,y1,z2, x1,y1,z2)), tex_coords)
self.batch.add(4, GL_QUADS, top,    ('v3f',(x1,y2,z2, x2,y2,z2, x2,y2,z1, x1,y2,z1)), tex_coords)

右手系中,z 轴指向视图外。 z 轴是 x 轴和 y 轴的叉积
这会导致front必须放置在 z 坐标为z2back必须放置在 z 坐标为z1

self.batch.add(4, GL_QUADS, back,   ('v3f',(x2,y1,z1, x1,y1,z1, x1,y2,z1, x2,y2,z1)), tex_coords)
self.batch.add(4, GL_QUADS, front,  ('v3f',(x1,y1,z2, x2,y1,z2, x2,y2,z2, x1,y2,z2)), tex_coords)

方法cuboid

def cuboid(self, x1,y1,z1, x2,y2,z2, tex):
    '''
    Draws a cuboid from x1,y1,z1 to x2,y2,z2 and covers each side with tex\n
    tex format:\n
        (front, back, left, right, top, bottom)
    Facing in the -z direction
    '''
    front  = tex[0]
    back   = tex[1]
    left   = tex[2]
    right  = tex[3]
    top    = tex[4]
    bottom = tex[5]

    tex_coords = ("t2f", (0,0, 1,0, 1,1, 0,1))

    self.batch.add(4, GL_QUADS, right,  ('v3f',(x1,y1,z1, x1,y1,z2, x1,y2,z2, x1,y2,z1)), tex_coords)
    self.batch.add(4, GL_QUADS, left,   ('v3f',(x2,y1,z2, x2,y1,z1, x2,y2,z1, x2,y2,z2)), tex_coords)
    self.batch.add(4, GL_QUADS, bottom, ('v3f',(x1,y1,z1, x2,y1,z1, x2,y1,z2, x1,y1,z2)), tex_coords)
    self.batch.add(4, GL_QUADS, top,    ('v3f',(x1,y2,z2, x2,y2,z2, x2,y2,z1, x1,y2,z1)), tex_coords)
    self.batch.add(4, GL_QUADS, back,   ('v3f',(x2,y1,z1, x1,y1,z1, x1,y2,z1, x2,y2,z1)), tex_coords)
    self.batch.add(4, GL_QUADS, front,  ('v3f',(x1,y1,z2, x2,y1,z2, x2,y2,z2, x1,y2,z2)), tex_coords)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM