pycraft icon indicating copy to clipboard operation
pycraft copied to clipboard

Make textures clearly belong to blocks

Open traverseda opened this issue 8 years ago • 3 comments

Enhancement for #2

Right now, there are like 3 different places where we import/define textures.

It will involve some digging into pyglet, but we definitely need to de-duplicate that.

I'm imagining a "texture" object, that has textures for more then one block.

Blocks return coordinates and a reference to their texture in it.

I think heightmaps could lead to some very interesting effects, and in the future we might want to support dynamic textures (maps) or layered textures (signs).

But we don't need a perfect solution off the bat. Just a quick system for defining a texture in one place, and having the renderer read them.

traverseda avatar Apr 19 '16 19:04 traverseda

Would it be useful to have blocks 'owning' their textures via having the textures split into separate files? I'm not sure what difficulties that could bring into the rendering setup logic, but I know minecraft uses separate files for each texture and allows blocks and items to 'inherit' the textures and models of other blocks/items.

I'm not suggesting we take the same organizational route minecraft has taken (in fact, I'd argue vehemently against it), but splitting textures into separate files seems like a win for modularity.

PythonJedi avatar May 28 '16 16:05 PythonJedi

True.

r58Playz avatar Jun 17 '19 22:06 r58Playz

This may be useful:

class Block(object):
    tex = []
    files = []
    colorizeable = False

    def get_tex(self,file):
        tex = pyglet.image.load(file).get_texture()
        glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST)
        glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST)
        return pyglet.graphics.TextureGroup(tex)

    def __init__(self):
        for nme in self.files:
            self.tex.append(self.get_tex(nme))
            
    def draw(self, position, batch):
        x, y, z = position
        return self.cuboid(*position, self.tex, batch)
    
    def cuboid(self, x, y, z, textures, batch):
        '''
        Draws a cuboid from x1,y1,z1 to x2,y2,z2 and covers each side with tex
        tex format:
            (side, top, bottom)
        Facing in the -z direction
        '''
        tex = textures
        front = tex[0]
        back = tex[0]
        left = tex[0]
        right = tex[0]
        top = tex[1]
        bottom = tex[2]

        tex_coords = ("t2f", (0, 0, 1, 0, 1, 1, 0, 1))
        x1, y1, z1 = x, y, z
        x2, y2, z2 = x + 1, y + 1, z + 1
        if self.colorizeable:
            color = self.colorizer.colorize(y)
        else:
            color = None
        if color:
            cube = (batch.add(4, GL_QUADS, right, ('v3f', (x1, y1, z1, x1, y1, z2, x1, y2, z2, x1, y2, z1)), tex_coords),
                batch.add(4, GL_QUADS, left, ('v3f', (x2, y1, z2, x2, y1, z1, x2, y2, z1, x2, y2, z2)), tex_coords),
                batch.add(4, GL_QUADS, bottom, ('v3f', (x1, y1, z1, x2, y1, z1, x2, y1, z2, x1, y1, z2)), tex_coords),
                batch.add(4, GL_QUADS, top, ('v3f', (x1, y2, z2, x2, y2, z2, x2, y2, z1, x1, y2, z1)), tex_coords, color),
                batch.add(4, GL_QUADS, back, ('v3f', (x2, y1, z1, x1, y1, z1, x1, y2, z1, x2, y2, z1)), tex_coords),
                batch.add(4, GL_QUADS, front, ('v3f', (x1, y1, z2, x2, y1, z2, x2, y2, z2, x1, y2, z2)), tex_coords))
        else:
            cube = (batch.add(4, GL_QUADS, right, ('v3f', (x1, y1, z1, x1, y1, z2, x1, y2, z2, x1, y2, z1)), tex_coords),
                batch.add(4, GL_QUADS, left, ('v3f', (x2, y1, z2, x2, y1, z1, x2, y2, z1, x2, y2, z2)), tex_coords),
                batch.add(4, GL_QUADS, bottom, ('v3f', (x1, y1, z1, x2, y1, z1, x2, y1, z2, x1, y1, z2)), tex_coords),
                batch.add(4, GL_QUADS, top, ('v3f', (x1, y2, z2, x2, y2, z2, x2, y2, z1, x1, y2, z1)), tex_coords),
                batch.add(4, GL_QUADS, back, ('v3f', (x2, y1, z1, x1, y1, z1, x1, y2, z1, x2, y2, z1)), tex_coords),
                batch.add(4, GL_QUADS, front, ('v3f', (x1, y1, z2, x2, y1, z2, x2, y2, z2, x1, y2, z2)), tex_coords))
        return cube

r58Playz avatar Jun 26 '20 03:06 r58Playz