Exemplo n.º 1
0
def load_texture(fname):
    """
    TODO: allow texture parameters to be set through arguments.
          allow mipmap creation.
          add a texture cache.
    """
    texture_id = gl.genTextures(1)[0]

    gl.bindTexture(gl.TEXTURE_2D, texture_id) # All upcoming GL_TEXTURE_2D operations now have effect on our texture object

    # Set our texture parameters
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)   # Set texture wrapping to GL_REPEAT
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)

    # Set texture filtering
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
    # Load, create texture and generate mipmaps

    img = PIL.Image.open(fname)
    img = PIL.ImageOps.flip(img)
    width, height = img.size
    data = img.convert("RGBA").tobytes("raw", "RGBA")
    # unsigned char* image = SOIL_load_image(FileSystem::getPath("resources/textures/container.jpg").c_str(), &width, &height, 0, SOIL_LOAD_RGB);

    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, data)
    gl.generateMipmap(gl.TEXTURE_2D)
    gl.bindTexture(gl.TEXTURE_2D, 0)  # Unbind texture when done, so we won't accidentily mess up our texture.

    return texture_id
Exemplo n.º 2
0
def load_texture(fname, min_filter = gl.LINEAR_MIPMAP_LINEAR, mag_filter=gl.LINEAR, texture_wrap_s=gl.REPEAT, texture_wrap_t=gl.REPEAT):
    img = PIL.Image.open(fname)
    img = PIL.ImageOps.flip(img)
    width, height = img.size
    data = img.convert("RGBA").tobytes("raw", "RGBA")

    texture_id = gl.genTextures(1)[0]
    gl.bindTexture(gl.TEXTURE_2D, texture_id) # All upcoming GL_TEXTURE_2D operations now have effect on our texture object
    
    # Set our texture parameters
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, texture_wrap_s)   # Set texture wrapping to GL_REPEAT
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, texture_wrap_t)

    # Set texture filtering
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, min_filter)
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, mag_filter)

    # create texture and generate mipmaps
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, data)
    gl.generateMipmap(gl.TEXTURE_2D)

    # Unbind texture when done, so we won't accidentily mess up our texture.
    gl.bindTexture(gl.TEXTURE_2D, 0) 

    return texture_id