Ejemplo n.º 1
0
def loadPNGTexture(filename, *a, **kw):
    try:
        w, h, ndata = loadPNGFile(filename)
        tex = glutils.Texture(name=os.path.basename(filename), image=ndata.ravel(),
                              width=w, height=h, *a, **kw)
        return tex
    except Exception as e:
        print ("Exception loading ", filename, ": ", repr(e))
        return glutils.Texture()
Ejemplo n.º 2
0
def loadPNGTexture(filename, *a, **kw):
    try:
        w, h, ndata = loadPNGFile(filename)

        tex = glutils.Texture(functools.partial(loadTextureFunc, w, h, ndata), *a, **kw)
        tex.data = ndata
        return tex
    except Exception as e:
        print ("Exception loading ", filename, ": ", repr(e))
        return glutils.Texture()
Ejemplo n.º 3
0
    def texturePath(self, value):
        self._texturePath = value
        if value is not None:
            log.info("Got texture path: %s", value)
            try:
                w, h, modelImage = loadPNGFile(value)
                modelImage = modelImage[::-1]
                # modelTex = loadPNGTexture(value)
                if h == 32:
                    w, h, modelImage = fixupTextureImage(modelImage)

                tex = glutils.Texture(name=os.path.basename(value),
                                      image=modelImage.ravel(),
                                      width=w,
                                      height=h)
            except Exception as e:
                log.warn("Error while loading player texture: %r", e)
                return

            if self.entityNode:
                self.removeChild(self.entityNode)

            self.entityNode = entityModelNode(self.playerRef,
                                              cookedModels['MCEDIT_Player'],
                                              tex)
            self.entityNode.addState(Enable(GL.GL_ALPHA_TEST))
            self.addChild(self.entityNode)
        else:
            log.info("Did not get texture path.")
Ejemplo n.º 4
0
def _makeLightTexture(dayTime=1.0, minBrightness=1.0):
    def _loadLightTexture():
        pixels = generateLightmap(dayTime)
        pixels.clip(int(minBrightness * 255), 255, pixels)
        GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, 16, 16, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, pixels.ravel())

    return glutils.Texture(_loadLightTexture)
Ejemplo n.º 5
0
    def load(self):
        if self._terrainTexture is not None:
            return
        maxLOD = min(4, self._maxLOD)

        if self.overrideMaxSize is None:
            if maxLOD:
                minFilter = GL.GL_NEAREST_MIPMAP_LINEAR
            else:
                minFilter = None
            self._terrainTexture = glutils.Texture(name="TextureAtlas",
                                                   image=self.textureData.ravel(),
                                                   width=self.width, height=self.height,
                                                   minFilter=minFilter, maxLOD=maxLOD)
            self._terrainTexture.load()
        else:
            self._terrainTexture = object()

        if self._lightTexture is None:
            self._lightTexture = LightTexture(self.dayTime, self.minBrightness)
            self._lightTexture.load()

        log.info("GL resources loaded for TextureAtlas for %s", util.displayName(self._filename))
Ejemplo n.º 6
0
    def load(self):
        if self._terrainTexture:
            return

        if self.overrideMaxSize is None:
            maxSize = getGLMaximumTextureSize()
        else:
            maxSize = self.overrideMaxSize

        maxLOD = min(4, self._maxLOD)
        if not bool(GL.glGenerateMipmap):
            maxLOD = 0
        if maxLOD:
            borderSize = 1 << (maxLOD - 1)
        else:
            borderSize = 0

        slots = []
        atlasWidth = 0
        atlasHeight = 0
        self._rawTextures.sort(key=lambda (_, w, h, __): max(w, h),
                               reverse=True)

        for path, w, h, data in self._rawTextures:
            w += borderSize * 2
            h += borderSize * 2
            for slot in slots:
                if slot.addTexture(path, w, h, data):
                    log.debug("Slotting %s into an existing slot", path)
                    break
            else:
                if atlasHeight < 24 * atlasWidth and atlasHeight + h < maxSize:
                    # Prefer to lay out textures vertically, since animations are vertical strips
                    slots.append(
                        TextureSlot(0, atlasHeight, max(atlasWidth, w),
                                    atlasHeight + h))
                    atlasWidth = max(atlasWidth, w)
                    atlasHeight = atlasHeight + h
                else:
                    slots.append(
                        TextureSlot(atlasWidth, 0, atlasWidth + w,
                                    max(atlasHeight, h)))
                    atlasWidth = atlasWidth + w
                    atlasHeight = max(atlasHeight, h)

                if atlasWidth > maxSize or atlasHeight > maxSize:
                    raise ValueError(
                        "Building texture atlas: Textures too large for maximum texture size. (Needed "
                        "%s, only got %s", (atlasWidth, atlasHeight),
                        (maxSize, maxSize))

                if not slots[-1].addTexture(path, w, h, data):
                    raise ValueError("Building texture atlas: Internal error.")

                log.debug("Slotting %s into a newly created slot", path)

        self.textureData = texData = numpy.zeros((atlasHeight, atlasWidth, 4),
                                                 dtype='uint8')
        self.textureData[:] = [0xff, 0x0, 0xff, 0xff]
        self.texCoordsByName = {}
        b = borderSize
        for slot in slots:
            for name, left, top, width, height, data in slot.textures:
                log.debug("Texture %s at (%d,%d,%d,%d)", name, left, top,
                          width, height)
                texDataView = texData[top:top + height, left:left + width]
                if b:
                    texDataView[b:-b, b:-b] = data

                    # Wrap texture edges to avoid antialiasing bugs at edges of blocks
                    texDataView[-b:, b:-b] = data[:b]
                    texDataView[:b, b:-b] = data[-b:]

                    texDataView[:, -b:] = texDataView[:, b:2 * b]
                    texDataView[:, :b] = texDataView[:, -b * 2:-b]
                else:
                    texDataView[:] = data
                self.texCoordsByName[
                    name] = left + b, top + b, width - 2 * b, height - 2 * b

        def _load():
            GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, atlasWidth,
                            atlasHeight, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE,
                            self.textureData.ravel())

        if self.overrideMaxSize is None:
            if maxLOD:
                minFilter = GL.GL_NEAREST_MIPMAP_LINEAR
            else:
                minFilter = None
            self._terrainTexture = glutils.Texture(_load,
                                                   minFilter=minFilter,
                                                   maxLOD=maxLOD)
            self._terrainTexture.load()
        else:
            self._terrainTexture = object()

        self.width = atlasWidth
        self.height = atlasHeight

        totalSize = atlasWidth * atlasHeight * 4
        usedSize = sum(
            sum(width * height for _, _, _, width, height, _ in slot.textures)
            for slot in slots) * 4
        log.info("Terrain atlas created for world %s (%d/%d kB)",
                 util.displayName(self._filename), usedSize / 1024,
                 totalSize / 1024)
        self.blockModels.cookQuads(self)