Seeing Blending in Action

Let's write some code to help us visualize some commonly used blending effects. When you execute Listing 12-5, you will see a background image and another smaller image—of our friend fugu—that can be moved around with the mouse. The fugu image contains alpha information, but because no blending options have been set, you will see parts of the image that would normally be invisible. If you hit the 1 key on your keyboard, it will enable alpha blending and the background pixels of the fugu image will become invisible (see Figure 12-1). Pressing 2 selects additive blending, which will give the fugu a ghostly glow, and pressing 3 selects sub-tractive blending, which creates a dark shadow fugu.

Try changing the constants in the glBlendFunc and glBlendEquation to produce some more interesting effects. If there is a particular effect you want to implement that isn't covered here, you will likely be able to find the parameters on the Web.

Figure 12-1. Blending effects

Alpha blending

Figure 12-1. Blending effects

Listing 12-5. Demonstrating Blending Effects (blenddemo.py) SCREEN_SIZE = (800, 600)

from OpenGL.GL import * from OpenGL.GLU import *

import pygame from pygame.locals import *

def resize(width, height):

glViewport(0, 0, width, height)

glMatrixMode(GL_PROJECTION)

glLoadIdentity()

gluPerspective(45.0, float(width)/height, .1, 1000.)

glMatrixMode(GL_MODELVIEW)

glLoadIdentity()

glEnable(GL_TEXTURE_2D) glEnable(GL_BLEND) glClearColor(1.0, 1.0, 1.0, 0.0)

def upload_texture(filename, use_alpha=False):

# Read an image file and upload a texture if use_alpha:

format, gl_format, bits_per_pixel = 'RGBA', GL_RGBA, 4 else:

format, gl_format, bits_per_pixel = 'RGB', GL_RGB, 3

# Load texture and extract the raw data img_surface = pygame.image.load(filename)

data = pygame.image.tostring(img_surface, format, True)

# Generate and bind a texture id texture_id = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texture_id)

# Set texture parameters and alignment glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ) glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ) glPixelStorei(GL_UNPACK_ALIGNMENT, 1)

# Upload texture data width, height = img_surface.get_rect().size glTexImage2D( GL_TEXTURE_2D, 0, bits_per_pixel, width, height, 0, gl_format, GL_UNSIGNED_BYTE, data )

# Return the texture id, so we can use glBindTexture return texture_id def draw_quad(x, y, z, w, h):

# Send four vertices to draw a quad glBegin(GL_OUADS)

glEnd()

pygame.init()

screen = pygame.display.set_mode(SCREEN_SIZE, HWSURFACE|OPENGL|DOUBLEBUF)

resize(*SCREEN_SIZE) init()

# Upload the background and fugu texture background_tex = upload_texture('background.png') fugu_tex = upload_texture('fugu.png', True)

while True:

for event in pygame.event.get(): if event.type == QUIT: return if event.type == KEYDOWN: if event.key == K_1:

# Simple alpha blending glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glBlendEquation(GL_FUNC_ADD) elif event.key == K_2:

# Additive alpha blending glBlendFunc(GL_SRC_ALPHA, GL_ONE) glBlendEquation(GL_FUNC_ADD)

# Subtractive blending glBlendFunc(GL_SRC_ALPHA, GL_ONE) glBlendEquation(GL_FUNC_REVERSE_SUBTRACT)

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

# Draw the background glBindTexture(GL_TEXTURE_2D, background_tex)

# Draw the background texture to the screen glDisable(GL_BLEND)

draw_quad(0, 0, -SCREEN_SIZE[l], 600, 600) glEnable(GL_BLEND)

# Draw a texture at the mouse position glBindTexture(GL_TEXTURE_2D, fugu_tex) x, y = pygame.mouse.get_pos()

pygame.display.flip()

# Free the textures we used glDeleteTextures(background_tex)

glDeleteTextures(fugu_tex)

Was this article helpful?

0 0

Post a comment