Ejemplo n.º 1
0
    def __init__(self, img_file, texture_renderer):
        self.renderer = texture_renderer
        self.image = PIL.Image.open(img_file).convert("RGBA")

        self.tw = 1
        while self.tw < self.width:
            self.tw *= 2
        self.th = 1
        while self.th < self.height:
            self.th *= 2

        self.teximage = PIL.Image.new("RGBA", (self.tw, self.th), (0, 0, 0, 0))
        self.teximage.paste(self.image, (0,0), self.image)
        self.teximage = self.teximage.convert("RGBa") # premultiply alpha

        self.texid = gl.glGenTextures(1)
        gl.glBindTexture(gl.GL_TEXTURE_2D, self.texid)
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR_MIPMAP_LINEAR)
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR)
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_EDGE)
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_EDGE)

        try:
            blob = self.teximage.tobytes()
        except AttributeError:
            blob = self.teximage.tostring()

        if glu is not None:
            glu.gluBuild2DMipmaps(gl.GL_TEXTURE_2D, 4, self.tw, self.th,
                gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, blob)
        else:
            gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA, self.tw, self.th,
                            0, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, blob)
            gl.glGenerateMipmap(gl.GL_TEXTURE_2D)
Ejemplo n.º 2
0
    def __init__(self, img_file, background=(0, 0, 0)):
        self.image = PIL.Image.open(img_file)

        self.tw = 1
        while self.tw < self.width:
            self.tw *= 2
        self.th = 1
        while self.th < self.height:
            self.th *= 2

        r, g, b = background
        self.teximage = PIL.Image.new("RGBA", (self.tw, self.th), (r, g, b, 0))
        self.teximage.paste(self.image, (0, 0), self.image)

        self.texid = gl.glGenTextures(1)
        gl.glBindTexture(gl.GL_TEXTURE_2D, self.texid)
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER,
                           gl.GL_LINEAR_MIPMAP_LINEAR)
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER,
                           gl.GL_LINEAR)
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP)
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP)

        try:
            blob = self.teximage.tobytes()
        except AttributeError:
            blob = self.teximage.tostring()

        glu.gluBuild2DMipmaps(gl.GL_TEXTURE_2D, 4, self.tw, self.th,
                              gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, blob)
Ejemplo n.º 3
0
def LoadTextures():
    global textures
    image = Image.open(os.path.join("Data", "Crate.bmp"))
    
    sizeX = image.size[0]
    sizeY = image.size[1]

    textures = gl.glGenTextures(3) #Create The Texture

    #get the raw image pixel converted in openGL coordinates (point (0,0) is bottom, left)
    image = image.convert("RGBX").tobytes("raw", "RGBX", 0, -1)
    
    # Create Nearest Filtered Texture
    gl.glBindTexture(gl.GL_TEXTURE_2D, textures[0])
    gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST)
    gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_NEAREST)
    gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, 3, sizeX, sizeY, 0, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, image)

    # Create Linear Filtered Texture
    gl.glBindTexture(gl.GL_TEXTURE_2D, textures[1])
    gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR)
    gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR)
    gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, 3, sizeX, sizeY, 0, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, image)

    # Create MipMapped Texture
    gl.glBindTexture(gl.GL_TEXTURE_2D, textures[2])
    gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR)
    gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR_MIPMAP_NEAREST)
    glu.gluBuild2DMipmaps(gl.GL_TEXTURE_2D, gl.GL_RGBA, sizeX, sizeY, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, image)
Ejemplo n.º 4
0
    def _load_texture(self, filename):
        """
        Loads an image as texture
        """
        if not os.path.exists(filename):
            raise ValueError("Texture file not found: " + filename)
            sys.exit()
        # Load the image
        image = Image.open(filename)
        ix = image.size[0]
        iy = image.size[1]
        image = image.tostring("raw", "RGBX", 0, -1)

        # Create Texture
        ind = gl.glGenTextures(1)
        gl.glBindTexture(gl.GL_TEXTURE_2D, ind)

        gl.glTexParameterf(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP)
        gl.glTexParameterf(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP)
        gl.glTexParameterf(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S,
                           gl.GL_REPEAT)
        gl.glTexParameterf(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T,
                           gl.GL_REPEAT)

        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER,
                           gl.GL_LINEAR)
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER,
                           gl.GL_LINEAR_MIPMAP_NEAREST)

        glu.gluBuild2DMipmaps(gl.GL_TEXTURE_2D, 3, ix, iy, gl.GL_RGBA,
                              gl.GL_UNSIGNED_BYTE, image)

        return ind
Ejemplo n.º 5
0
    def _load_texture(self, filename):
        """
        Loads an image as texture
        """
        if not os.path.exists(filename):
            raise ValueError("Texture file not found: " + filename)
            sys.exit()
        # Load the image
        image = Image.open(filename)
        ix = image.size[0]
        iy = image.size[1]
        image = image.tobytes("raw", "RGBX", 0, -1)

        # Create Texture
        ind = gl.glGenTextures(1)
        gl.glBindTexture(gl.GL_TEXTURE_2D, ind)

        gl.glTexParameterf(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP)
        gl.glTexParameterf(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP)
        gl.glTexParameterf(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S,
                           gl.GL_REPEAT)
        gl.glTexParameterf(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T,
                           gl.GL_REPEAT)

        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER,
                           gl.GL_LINEAR)
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER,
                           gl.GL_LINEAR_MIPMAP_NEAREST)

        glu.gluBuild2DMipmaps(gl.GL_TEXTURE_2D, 3, ix, iy, gl.GL_RGBA,
                              gl.GL_UNSIGNED_BYTE, image)

        return ind
Ejemplo n.º 6
0
    def __init__(self, img_file, texture_renderer):
        self.renderer = texture_renderer
        self.image = PIL.Image.open(img_file).convert("RGBA")

        self.tw = 1
        while self.tw < self.width:
            self.tw *= 2
        self.th = 1
        while self.th < self.height:
            self.th *= 2

        self.teximage = PIL.Image.new("RGBA", (self.tw, self.th), (0, 0, 0, 0))
        self.teximage.paste(self.image, (0,0), self.image)
        self.teximage = self.teximage.convert("RGBa") # premultiply alpha

        self.texid = gl.glGenTextures(1)
        gl.glBindTexture(gl.GL_TEXTURE_2D, self.texid)
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR_MIPMAP_LINEAR)
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR)
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_EDGE)
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_EDGE)

        try:
            blob = self.teximage.tobytes()
        except AttributeError:
            blob = self.teximage.tostring()

        if glu is not None:
            glu.gluBuild2DMipmaps(gl.GL_TEXTURE_2D, 4, self.tw, self.th,
                gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, blob)
        else:
            gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA, self.tw, self.th,
                            0, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, blob)
            gl.glGenerateMipmap(gl.GL_TEXTURE_2D)
Ejemplo n.º 7
0
 def makeTextureFromSurface(self, surface):
     texture = GL.glGenTextures(1)
     GL.glBindTexture(GL.GL_TEXTURE_2D, texture)
     GLU.gluBuild2DMipmaps(GL.GL_TEXTURE_2D, GL.GL_RGBA,
             surface.get_width(), surface.get_height(),
             GL.GL_RGBA, GL.GL_UNSIGNED_BYTE,
             pygame.image.tostring(surface, 'RGBA'))
     GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER,
             GL.GL_NEAREST_MIPMAP_NEAREST)
     GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER,
             GL.GL_NEAREST)
     return texture
Ejemplo n.º 8
0
	def __init__(self, img_file):
		self.image = PIL.Image.open(img_file).convert("RGBA")
		self.texid = gl.glGenTextures(1)

		gl.glBindTexture(gl.GL_TEXTURE_2D, self.texid)
		gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR_MIPMAP_NEAREST);
		gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR);
		gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP);
		gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP);

		glu.gluBuild2DMipmaps(gl.GL_TEXTURE_2D, 4, self.width, self.height, gl.GL_RGBA,
				      gl.GL_UNSIGNED_BYTE, self.image.tostring())
Ejemplo n.º 9
0
 def createTextureFromSurface(self, surface, has_alpha=True):
     texture = GL.glGenTextures(1)
     modeFlag = GL.GL_RGB
     modeString = "RGB"
     if has_alpha:
         modeFlag = GL.GL_RGBA
         modeString = "RGBA"
     GL.glBindTexture(GL.GL_TEXTURE_2D, texture)
     GLU.gluBuild2DMipmaps(GL.GL_TEXTURE_2D, modeFlag, surface.get_width(),
                           surface.get_height(), modeFlag,
                           GL.GL_UNSIGNED_BYTE,
                           pygame.image.tostring(surface, modeString))
     GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER,
                        GL.GL_NEAREST_MIPMAP_NEAREST)
     GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER,
                        GL.GL_NEAREST)
     return texture
Ejemplo n.º 10
0
def loadTexture(path, type, clamp, mipmap=False):
    img = sdl2.SDL_LoadBMP(path)
    if not img:
        print('Unable to load bitmap :', sdl2.SDL_GetError())
        return 0

    width = img.contents.w
    height = img.contents.h

    # converts from BGR to RGB
    if (type == gl.GL_RGB) or (type == gl.GL_RGBA):
        if type == gl.GL_RGB:
            offset = 3
        else:
            offset = 4
        imagesize = width * height * offset
        b = ctypes.cast(img.contents.pixels, ctypes.POINTER(ctypes.c_byte))
        for i in range(0, imagesize, offset):
            b[i], b[i + 2] = b[i + 2], b[i]  # slow...

    index = int(gl.glGenTextures(1))
    gl.glBindTexture(gl.GL_TEXTURE_2D, index)
    gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, clamp)
    gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, clamp)
    gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER,
                       gl.GL_LINEAR)

    if mipmap:
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER,
                           gl.GL_LINEAR_MIPMAP_LINEAR)
        bytes = ctypes.cast(img.contents.pixels, ctypes.POINTER(ctypes.c_byte))
        glu.gluBuild2DMipmaps(gl.GL_TEXTURE_2D, type, width, height, type,
                              gl.GL_UNSIGNED_BYTE, bytes)
    else:
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER,
                           gl.GL_LINEAR)
        bytes = ctypes.cast(img.contents.pixels, ctypes.POINTER(ctypes.c_byte))
        gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, type, width, height, 0, type,
                        gl.GL_UNSIGNED_BYTE, bytes)
    sdl2.SDL_FreeSurface(img)
    return index
Ejemplo n.º 11
0
    def __init__(self, img_file, background=(0,0,0)):
        self.image = PIL.Image.open(img_file)

        self.tw = 1
        while self.tw < self.width:
            self.tw *= 2
        self.th = 1
        while self.th < self.height:
            self.th *= 2

        r, g, b = background
        self.teximage = PIL.Image.new("RGBA", (self.tw, self.th), (r, g, b, 0))
        self.teximage.paste(self.image, (0,0), self.image)

        self.texid = gl.glGenTextures(1)
        gl.glBindTexture(gl.GL_TEXTURE_2D, self.texid)
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR_MIPMAP_LINEAR)
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR)
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP)
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP)

        glu.gluBuild2DMipmaps(gl.GL_TEXTURE_2D, 4, self.tw, self.th, gl.GL_RGBA,
                      gl.GL_UNSIGNED_BYTE, self.teximage.tostring())