User Tools

Site Tools


tutorials:ffopengl_textures

Texture Mapping

When modifying your sample code to support texture mapping, you're going to need to include a way to read in images. In class, we looked at the PPM ASCII format. In this example, however, I'll show you how to use PIL (Python Image Library) which can open a wide variety of image formats. You will need to include one of the following imports at the top of your program. Some releases of Python require one while some require the other. Why? No idea. This was discovered via trial and error from other classes.

from PIL import Image
   or
import Image

You can now open images as follows. There are a couple things you'll want to have handy for when you send your texture to the GPU: image height, image width, and the image data in byte-array format (needed by OpenGL, a numeric list will not work).

        img=Image.open("myImage.png")
        width, height = img.size
        img_data=img.tobytes()

Next, you'll need to tell OpenGL how many textures you will be using. It will generate a unique ID number for each texture to be stored on the GPU. These numbers are generated by calling glGenTextures and providing it with the number of textures you want to use. If you request one texture, the function will return a single number, but if you request two or more, the function will return a list containing the IDs assigned to textures.

        self.textID=glGenTextures(1)

Once you've generated your textures IDs, you'll need to activate the texture for use. Every time you want to use or modify a texture, you'll have to make it active. This is done by binding it using the function glBindTexture. You will need to provide as input parameters GL_TEXTURE_2D (specifying the type of texture being used) and the texture ID.

        glBindTexture(GL_TEXTURE_2D, self.textID)

Now that we have activated our texture, we can send the image data to the GPU using the glTexImage2D function. Notice that you have to specify the color encoding for your image (GL_RGB in the example below). If you are using an image format that supports transparency (has an alpha channel), you will need to specify that using GL_RGBA. You will also need to specify the methods to be used for upscaling and downscaling your texture. This is done by specifying your texture magnification (GL_TEXTURE_MAG_FILTER) and minification (GL_TEXTURE_MIN_FILTER) filters using glTexParameterf.

        glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, img_data)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
        glEnable(GL_TEXTURE_2D)

Finally, you can use your textures! In the example below, we draw a quad and use glTexCoord2f to map the texture over the geometry.

        glTranslate(0.0, 0.0, -1.0)
        glBegin(GL_QUADS)
 
        glTexCoord2f(0.0, 1.0)
        glVertex3f(-0.5, -0.5, -1.0)
 
        glTexCoord2f(0.0, 0.0)
        glVertex3f(-0.5, 0.5, -1.0)
 
        glTexCoord2f(1.0, 0.0)
        glVertex3f(0.5, 0.5, -1.0)
 
        glTexCoord2f(1.0, 1.0)
        glVertex3f(0.5, -0.5, -1.0)
 
        glEnd()
tutorials/ffopengl_textures.txt · Last modified: 2021/08/01 00:52 by jones