def LoadTexture(path): # запросим у OpenGL свободный индекс текстуры texture = glGenTextures(1) # сделаем текстуру активной glBindTexture(GL_TEXTURE_2D, texture) # загружаем изображение-текстуру image = Image.open(path) image = image.transpose(Image.FLIP_TOP_BOTTOM) img_data = image.convert("RGBA").tobytes() glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width, image.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img_data) glGenerateMipmap(GL_TEXTURE_2D) # установим параметры фильтрации текстуры - линейная фильтрация glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) # установим параметры "оборачиваниея" текстуры - отсутствие оборачивания glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) #GL_REPEAT glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) #GL_REPEAT # возвращаем текстуру return texture
def load_texture(self, img: Image) -> None: img_data = np.fromstring(img.tobytes(), np.uint8) width, height = img.size self._texture = glGenTextures(1) glPixelStorei(GL_UNPACK_ALIGNMENT, 1) glBindTexture(GL_TEXTURE_2D, self._texture) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) mode = GL_RGBA if img.mode == "RGB": mode = GL_RGB glTexImage2D( GL_TEXTURE_2D, 0, mode, width, height, 0, mode, GL_UNSIGNED_BYTE, img_data, ) glGenerateMipmap(GL_TEXTURE_2D)
def set_texture(m): texture_data = np.array(m.texture_image, dtype='int8') m.textureID = glGenTextures(1) glPixelStorei(GL_UNPACK_ALIGNMENT, 1) glBindTexture(GL_TEXTURE_2D, m.textureID) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texture_data.shape[1], texture_data.shape[0], 0, GL_BGR, GL_UNSIGNED_BYTE, texture_data.flatten()) glHint(GL_GENERATE_MIPMAP_HINT, GL_NICEST) # must be GL_FASTEST, GL_NICEST or GL_DONT_CARE glGenerateMipmap(GL_TEXTURE_2D)
def texturit(filn: str) -> int: """Import a Texture and return the texture buffer handle. Better by RGBA formatted, too.""" wa = glGenTextures(1) with Image.open(filn) as i: ix, iy, im = i.size[0], i.size[1], i.tobytes('raw', 'RGBA') glBindTexture(GL_TEXTURE_2D, wa) glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, im) glGenerateMipmap(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, 0) return wa
def get_textureid_with_text(text, fgcolor, bgcolor): if not hasattr(get_textureid_with_text, 'cache'): get_textureid_with_text.cache = {} import zlib uid = str(zlib.crc32(text)) + str(zlib.crc32(np.array(fgcolor))) + str( zlib.crc32(np.array(bgcolor))) if uid not in get_textureid_with_text.cache: from PIL import ImageFont from PIL import Image from PIL import ImageDraw font = ImageFont.truetype( os.path.join(os.path.dirname(__file__), "ressources", "Arial.ttf"), 100) imsize = (128, 128) bgarray = np.asarray(np.zeros((imsize[0], imsize[1], 3)), np.uint8) bgarray[:, :, 0] += bgcolor[0] bgarray[:, :, 1] += bgcolor[1] bgarray[:, :, 2] += bgcolor[2] img = Image.fromarray(bgarray) draw = ImageDraw.Draw(img) w, h = draw.textsize(text, font=font) text_pos = ((imsize[0] - w) / 2, (imsize[1] - h) / 2) draw.text(text_pos, text, fill=tuple(np.asarray(fgcolor, np.uint8)), font=font) texture_data = np.asarray( np.array(img.getdata()).reshape(img.size[0], img.size[1], 3) * 255, np.uint8) textureID = glGenTextures(1) glPixelStorei(GL_UNPACK_ALIGNMENT, 1) glBindTexture(GL_TEXTURE_2D, textureID) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texture_data.shape[1], texture_data.shape[0], 0, GL_BGR, GL_UNSIGNED_BYTE, texture_data.flatten()) glHint(GL_GENERATE_MIPMAP_HINT, GL_NICEST) # must be GL_FASTEST, GL_NICEST or GL_DONT_CARE glGenerateMipmap(GL_TEXTURE_2D) get_textureid_with_text.cache[uid] = textureID return get_textureid_with_text.cache[uid]
def createImage(self, qimagepath, layer, textureRect, drawRect, hidden=False, dynamicity=GL_STATIC_DRAW_ARB): ''' Creates an rggTile instance, uploads the correct image to GPU if not in cache, and some other helpful things. ''' #print "requested to create", qimagepath, layer, textureRect, drawRect, hidden layer = int(layer) texture = None found = False if qimagepath in self.qimages: qimg = self.qimages[qimagepath][0] if self.qimages[qimagepath][2] > 0: texture = self.qimages[qimagepath][1] found = True else: qimg = QImage(qimagepath) #print("created", qimagepath) if textureRect[2] == -1: textureRect[2] = qimg.width() if textureRect[3] == -1: textureRect[3] = qimg.height() if drawRect[2] == -1: drawRect[2] = qimg.width() if drawRect[3] == -1: drawRect[3] = qimg.height() image = tile(qimagepath, textureRect, drawRect, layer, hidden, dynamicity, self) if found == False: img = None if self.npot == 0: w = nextPowerOfTwo(qimg.width()) h = nextPowerOfTwo(qimg.height()) if w != qimg.width() or h != qimg.height(): img = self.convertToGLFormat(qimg.scaled(w, h)) else: img = self.convertToGLFormat(qimg) else: img = self.convertToGLFormat(qimg) texture = int(glGenTextures(1)) try: imgdata = img.bits().asstring(img.byteCount()) except Exception as e: print(e) print("requested to create", qimagepath, layer, textureRect, drawRect, hidden) for x in [0, 1, 2, 3]: f_code = _getframe( x ).f_code #really bad hack to get the filename and number print("Doing it wrong in " + f_code.co_filename + ":" + str(f_code.co_firstlineno)) print("Error: " + e) #print("created texture", texture) glBindTexture(self.texext, texture) if self.anifilt > 1.0: glTexParameterf(self.texext, GL_TEXTURE_MAX_ANISOTROPY_EXT, self.anifilt) if self.npot == 3 and self.mipminfilter != -1: glTexParameteri(self.texext, GL_TEXTURE_MIN_FILTER, self.mipminfilter) glTexParameteri(self.texext, GL_TEXTURE_MAG_FILTER, self.magfilter) elif self.npot == 2 and self.mipminfilter != -1: glTexParameteri(self.texext, GL_TEXTURE_MIN_FILTER, self.mipminfilter) glTexParameteri(self.texext, GL_TEXTURE_MAG_FILTER, self.magfilter) glTexParameteri(self.texext, GL_GENERATE_MIPMAP, GL_TRUE) else: glTexParameteri(self.texext, GL_TEXTURE_MIN_FILTER, self.minfilter) glTexParameteri(self.texext, GL_TEXTURE_MAG_FILTER, self.magfilter) glTexParameteri(self.texext, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) glTexParameteri(self.texext, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE) glTexParameteri(self.texext, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) glTexImage2D(self.texext, 0, GL_RGBA, img.width(), img.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, imgdata) if self.npot == 3 and self.mipminfilter != -1: glEnable(GL_TEXTURE_2D) glGenerateMipmap(GL_TEXTURE_2D) self.qimages[qimagepath] = [qimg, texture, 1] #texture, reference count else: self.qimages[qimagepath][2] += 1 image.textureId = texture if layer not in self.images: self.images[layer] = [] self.layers = list(self.images.keys()) self.layers.sort() image.createLayer = True self.images[layer].append(image) self.allimgs.append(image) return image