Exemplo n.º 1
0
def load_texture(fname):
    img = QImage(fname)
    ptr = c_void_p(int(img.constBits()))

    tex_id = gl.glGenTextures(1)
    gl.glBindTexture(gl.GL_TEXTURE_2D, tex_id)
    gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA8, img.width(), img.height(), 0, gl.GL_BGRA, gl.GL_UNSIGNED_BYTE, ptr)
    gl.glGenerateMipmap(gl.GL_TEXTURE_2D)
    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)
    return tex_id
Exemplo n.º 2
0
 def loadFromFile(cls, path, internalFormat=GL_RGBA):
     image = QImage(path)
     image = QGLWidget.convertToGLFormat(image)
     dataFormat = { QImage.Format_ARGB32: GL_RGBA }.get(image.format(), None)
     dataType = GL_UNSIGNED_BYTE
     data = [0] * image.numBytes()
     bits = image.constBits()
     bits.setsize(image.numBytes())
     for i in range(image.numBytes()):
         data[i] = bits[i][0]
     return Texture2D(image.width(), image.height(), internalFormat, dataFormat, dataType, data)
Exemplo n.º 3
0
    def create_font_array(self, char_height=62, pixel_margin=1, font_family='Courier', font_weight=50):
        # Load font and get the dimensions of one character (assuming monospaced font)
        f = QFont(font_family)
        f.setPixelSize(char_height)
        f.setWeight(font_weight)
        fm = QFontMetrics(f, QImage())

        char_width = char_height = 0
        char_y = 999

        for i in range(32, 127):
            bb = fm.boundingRect(chr(i))
            char_width = max(char_width, bb.width())
            char_height = max(char_height, bb.height())
            char_y = min(char_y, bb.y())

        imgsize = (char_width + 2 * pixel_margin, char_height + 2 * pixel_margin)
        self.char_ar = float(imgsize[1]) / imgsize[0]

        # init the image and the painter that will draw the characters to each image
        img = QImage(imgsize[0], imgsize[1], QImage.Format_ARGB32)
        ptr = c_void_p(int(img.constBits()))
        painter = QPainter(img)
        painter.setFont(f)
        painter.setPen(QColor(255, 255, 255, 255))

        # Set-up the texture array
        self.tex_id = gl.glGenTextures(1)
        gl.glBindTexture(gl.GL_TEXTURE_2D_ARRAY, self.tex_id)
        gl.glTexImage3D(gl.GL_TEXTURE_2D_ARRAY, 0, gl.GL_RGBA8, imgsize[0], imgsize[1], 127 - 32, 0, gl.GL_BGRA, gl.GL_UNSIGNED_BYTE, None)
        gl.glTexParameteri(gl.GL_TEXTURE_2D_ARRAY, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR)
        gl.glTexParameteri(gl.GL_TEXTURE_2D_ARRAY, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR)
        gl.glTexParameterf(gl.GL_TEXTURE_2D_ARRAY, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_BORDER)
        gl.glTexParameterf(gl.GL_TEXTURE_2D_ARRAY, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_BORDER)
        # We're using the ASCII range 32-126; space, uppercase, lower case, numbers, brackets, punctuation marks
        for i in range(32, 127):
            img.fill(0)
            painter.drawText(pixel_margin, pixel_margin - char_y, chr(i))
            gl.glTexSubImage3D(gl.GL_TEXTURE_2D_ARRAY, 0, 0, 0, i - 32, imgsize[0], imgsize[1], 1, gl.GL_BGRA, gl.GL_UNSIGNED_BYTE, ptr)

        # We're done, close the painter, and return the texture ID, char width and char height
        painter.end()