Blending & Drawing Order

Objects are rendered one at a time

When blending, all that is known is the current polygon, and the results of previous rendering (frame buffer contents)

These are the source & destination colors of the blending formula

+ =





Blending & Drawing Order

Depth-buffering occurs before blending

To draw a transparent plus an opaque object, draw the opaque one first

Draw sphere, then cubeDraw cube, then sphere





Blending & Drawing Order

Multiple transparent objects need to be drawn in back-to-front order

The order can depend on the camera position






Depth Mask

Can prevent transparent objects occluding each other by disabling the depth mask:

glDepthMask(GL_FALSE)

Result can be incorrect, but errors are less noticeable






Backface Culling

Back faces are normally visible in transparent objects

glEnable(GL_CULL_FACE) prevents drawing back faces of objects

glDisable(GL_CULL_FACE) glEnable(GL_CULL_FACE)
      





Backface Culling

"Front-facingness" is determined by vertex order

When viewed from the front, vertices are in counter-clockwise order

Front facing Back facing
      





Multipass Rendering

Rendering the same objects more than once, with different modes


Render once with glCullFace(GL_FRONT), once with glCullFace(GL_BACK)






Texture Transformations

Transformations - translation, rotation, and scaling - can be applied to textures, similar to how they are applied to geometry.

The exact same OpenGL function calls are used. The only difference is that the matrix mode is changed to GL_TEXTURE.

    glMatrixMode(GL_TEXTURE)
     glTranslatef(0.1, 0.05, 0)
    glMatrixMode(GL_MODELVIEW)

Setting the matrix mode to GL_TEXTURE means that any subsequent transformation calls will be applied to texture coordinates, rather than vertex coordinates.

We switch the matrix mode back to the "default" of GL_MODELVIEW when we're done, so as to not adversely affect other code.






Texture Transformations

The same effect could be achieved by dynamically changing the texture coordinates directly.
The advantages are: GL transformation functions simplify things and take care of the math, and transformations can be applied to objects where we might not have direct control of the texture coordinates.






Texture Transformations

One trick that we can use a texture transformation for is to make sure that a texture is always applied at the same scale, as an object is transformed.

If we apply a texture to an object normally, and then scale the object, the texture appears to stretch:






Texture Transformations

By applying the same scaling to the texture coordinates as to the geometry, the texture "grows", and does not stretch:

    glMatrixMode(GL_TEXTURE)
     glLoadIdentity()
     glScalef(size, 1, 1)
    glMatrixMode(GL_MODELVIEW)
    glScalef(size, 1, 1)





Texture Transformations

Be aware that texture transformations are not applied to the texture per se, but to the texture coordinates.

This means that the visual result might be the opposite of what you expect - applying glRotate(30.0, 0, 0, 1) will cause the texture itself to appear to rotate 30 degrees clockwise, not counter-clockwise.

    





Texture Caching

Sometimes a program will use the same texture many times, on different objects.

For memory efficiency, each object should reference the same OpenGL texture, rather than making multiple copies.

This can be automated with a global "texture cache":

TextureCache.py:
    
    from Texture2D import *

    cachedTextures = { }

    def loadTexture(filename):
        global cachedTextures
        if not cachedTextures.has_key(filename):
            cachedTextures[filename] = Texture2D(filename)
        return cachedTextures[filename]
    import TextureCache

    tex = TextureCache.loadTexture("foo.jpg")

This technique can also be used with model files or other data.






Job Sites

creativeheads.net

SIGGRAPH site - job postings for video games, animation, special effects, software / tech development.



gamasutra.com

Game industry job postings, & articles



Creative Commons License
This document is by Dave Pape, and is released under a Creative Commons License.