Search code examples
pythonpyglet

How to make a cube in pyglet with images on faces


I'm making a 3d game, and i need to paste images on a cube, but I can't find out where the front/back/top/bottom/left/right textures should go.

I just started using pyglet, so I'm not very familiar with it. I've been flipping the texture part of the cube for a few days now, and it's still a mess. Here's my code for the cube function below:

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)

I cannot show what I was expecting because I don't have a working 3d rendering program.


Solution

  • I assume that the coordinates (x1, y1, z1) are the minimum of the cube and the coordinates are the maximum (x1, y1, z1). e.g.:

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

    If the cube is drawn in view space, then:

    The x-axis points to the left. The left texture has to be placed where the x coordinates of all the vertices are x1 and the right texture where the x coordinates are 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)
    

    The y-axis point up. So bottom is where the y coordinates are y1 and top where the y coordinates are 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)
    

    In a Right-handed system the z axis points out of the view. The z-axis is the Cross product of the x-axis and y-axis.
    This causes that front has to be placed where the z-coordinates are z2 and back where the z-coordinates are 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)
    

    The method 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)