190 likes | 304 Views
3 . 2 . Graphics I. Alpha blending within games. Alpha Blending . An exploration of the use of alpha blending within games. How do we want to combine images?. In games there are two common ways in which images are combined.
E N D
3.2.Graphics I Alpha blending within games
Alpha Blending An exploration of the use of alpha blending within games
How do we want to combine images? In games there are two common ways in which images are combined... ‘Normal’ alpha blending, i.e. drawing a transparent or translucent image so that it shows what is underneath (e.g. drawing the pipes so that the background is seen) Additive blending, i.e. adding some image to the existing image (e.g. adding an effect image so that it brightens some region of the image)
Alpha blending (or more generally Alpha Compositing) Alpha blending is typically done in hardware and controls how two images are combined to produce a single composite image (one image is termed the source, the other the destination – the source image is added to the destination image). The most common blend function is: OutputColour = SourceColour * SourceBlend + DestinationColour * DesinationBlend Aside: Each channel (i.e. red, green, blue, alpha) is separately blended based on the blend function)
Alpha blending (blend functions) A wide range of different blend functions are defined, including those shown: Inverse functions are also defined, e.g. InverseSourceAlpha
Alpha blending (normal alpha blending) The combination of blend functions that provides ‘normal’ alpha blending, i.e. enabling translucency, is shown below: As SourceAlpha increases, SourceColour dominates, and DestinationColour reduces. OutputColour = SourceColour * SourceAlpha + DestinationColour * InverseSourceAlpha
Alpha blending (getting the right order) Aside: In XNA use SpriteBatchSpriteSortModeBackToFront to draw back to front. The order in which images are combined is important if using this form of blending, i.e. Images that are ‘behind’ a transparent image must be drawn first before the transparent image is drawn Pillar (50% transparency) Player (transparent background) If player is to be drawn behind pillar, player must be drawn before pillar
Alpha blending (additive blending) A less common, but popular, means of alpha blending is additive blending: OutputColour = SourceColour * SourceAlpha + DestinationColour * One Additive blending is commutative, i.e. It does not matter the order in which images are combined, the end result is the same. Using this form of blending there is no need to sort by depth order when combining images (although it can be more difficult to ‘visualise’ the output)
Alpha blending (in Java) AlphaComposite alpha = AlphaComposite.getInstance( AlphaComposite.SRC_OVER,0.5f); graphics2d.setComposite(alpha); graphics2d.drawImage(image,…); The java.awt.AlphaComposite class uses what are called the Porter-Duff compositing rules to implement alpha compositing. Mostly commonly the SRC_OVER rule is used (equivalent to normal alpha blending). Aside: The code repository uses normal alpha blending by default.
Alpha blending (in XNA) BlendStateblendState= new BlendState(); blendState.ColorSourceBlend= Blend.DestinationColor; blendState.ColorDestinationBlend= Blend.SourceColor; GraphicsDevice.BlendState= blendState; SpriteBatch supports both normal alpha blending and additive blending, via the BlendState(which can be AlphaBend, Additive, or None). Other forms of additive blending can be directly specified by changing the render state within the sprite batch. Example: for 2x multiplicative blending (which lightens/ darkens the destination based on the source colour – good for applying lighting effects) Aside: For more info see: http://msdn.microsoft.com/en-us/library/bb976070.aspx
Summary: Steps in Your Game Design Image loading, management and rendering summary
Summary: Image loading and management Q1: What graphics need to be loaded? In what order? All at once? Q2: How are assets managed? (The default managers will likely suffice) In your game design you should consider the following separate-but-dependent processes. Load graphics Game objects Asset Manager Request / receive graphics To do: Develop design Q3: Which graphics are used by each game object? How/when do game objects obtain graphics from the manager.
Summary: Image drawing Game objects Drawing a layer Visible objects Layers Draw ordering Drawing To do: Develop design Q1: Which layers should be drawn? In what order? Q2: Which layer objects are visible and should be drawn? Q3: In what order should visible objects be drawn? Q4: How will each object be drawn?
Developing Your Game Design Having another stab at producing your own game design
Graphics in your game… Using your own game idea, take a game screen (I’d suggest the main game layer) and: Briefly identify key game objects within the layer Decide if any visibility culling is possible, and if so, what. Decide the order in which the game objects should be drawn. To do: Develop design Question Clinic : This process should result in questions. Feel free to ask (and/or include in the Question Clinic) Start 1 min 2 mins 3 mins 4 mins 5 mins 7 mins 30 sec 8 mins 9 mins 10 mins Finished 6 mins
Extracting Images Making a transparent / translucent image
Extracting images Most popular image editing programs, e.g. GIMP, will enable you to make/edit transparent/ translucent images. To do this you may need to make sure the image has an alpha channel The ‘magic wand’ selection tool can be used to select regions (it may be necessary to play with the selection threshold). Once selected, unwanted regions can be cut.
It may be necessary to use the selection tool a number of times to refine the cut. • Translucency can often be varied in a number of different ways. Simply changing the layer opacity is often easiest. • Images shown below can be used to practice (easy/medium/hard cut)
Summary Today we explored: • The theory and uses of alpha blending • Summary of key questions when using graphics • Started to plan how graphics will be selected, drawn in your game. To do: • Complete Question Clinic • Complete/iterate the design for loading, selecting, ordering and drawing images in your game. • Write some code that loads and displays images.