Title: Texture Tricks
1Texture Tricks
This section concerns itself with using polygons
and nice textures to fake out the viewer in
interesting ways. These tricks can really spice
up your project with very little additional
processing load.
2Billboards
- Billboards are textured polygons (usually quads)
- The quads are always rendered so that they are
facing the Viewer. - IE as the viewer moves about the world the
polygon tracks the viewer keeping its normal
pointing toward the viewer. - See tutorials at Lighthouse3d and NeHe
3 Example Tree Texture
The black background will be made
transparent! This background must be all ONE
color in order to do this.
4 What are they used for
- Scenery
- Trees, grass, spectators
- Mesh simplification
- Replace far-away objects with billboards
- Non-polygonal objects
- Fire, smoke, clouds, particles
5 Billboard basics
- When drawing the textured polygon we need to
remove texture background using either - Masking
- Alpha blending
6Buildboards using Masking
- Each texel (texture element) has alpha value 0 or
1 - Only draw texels with alpha value 1
void glAlphaFunc(GLenum func, GLclampf ref)
func when to render the texel GL_NEVER,
GL_LESS, GL_EQUAL, etc ref value to compare
texel alpha with
glAlphaFunc(GL_GREATER, 0.5f) glEnable(GL_ALPHA,T
EST)
7Billboards using Blending
- Each texel has an alpha value in the range from 0
to 1 - Alpha value is used to blend each texel with the
background - Use glBlendFunc() to do this
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA
) - glEnable(GL_BLEND)
- Must turn off depth buffer testing
- glDisable(GL_DEPTH_TEST)
8 Using Blending Cont.
- Advantages
- Smooth blending with background
- Partially transparent objects possible
- Disadvantages
- Takes longer to render
- Rendering must be done in depth sorted order
(front-to-back or back-to-front) - Blending is generally render order dependent
9 Use multiple Billboards
Use one or more intersecting polygons to
construct a complicated image.
10 Make a forest
- Here we draw a large number of billboarded trees
in random locations. - We can add variation by
- Scaling the billboards slightly
- Changing the overshading of each billboard
- Rotation around the axis of symmetry.
- Multitexturing tricks
- Use several tree textures
11 Here is one
12 Some code
- for(vectorltTreegtconst_iterator ti
forest.begin() ti ! forest.end() ti) -
- glPushMatrix()
- // Place the tree and scale it
- glTranslatef(ti-gtx, 0, ti-gtz)
- glScalef(ti-gtwidthscale, ti-gtheightscale,
ti-gtwidthscale) - // With the GL_MODULATE texture mode the texture
colours are multiplied by //the glColor values.
This is an easy way to make a texture lighter or
darker, or // even give it a tint. - glColor3f(ti-gtlightness, ti-gtlightness,
ti-gtlightness) - glRotatef(ti-gtrot, 0,1,0)
- glBegin(GL_QUADS)
- glTexCoord2f(0, 1) glVertex3f(-1, 0, 0)
- glTexCoord2f(0, 0) glVertex3f(-1, 2, 0)
- glTexCoord2f(1, 0) glVertex3f( 1, 2, 0)
- glTexCoord2f(1, 1) glVertex3f( 1, 0, 0)
- glEnd()
- glPopMatrix()
Should these be objects in our scene graph?
13 Aligning billboards
- Billboard polygons obvious when viewing nearly
edge-on - Possible solution lock billboard rotation axes
to camera rotation - Billboard always facing camera
- Need only one billboard per object
- Works best for symmetric objects in a crowded
scene (trees in forest, people in a stadium) - Often only lock heading, not pitch/bank
14 Align with projection plane
- Set billboard rotation such that it faces the
cameras projection plane
- Ways to do this
- 1. Set billboard rotation of locked axes equal to
camera rotation - 2. Render billboards in camera coordinates
without rotation
Projection plane
15 Align with camera position
- Billboard rotated so that normal points at camera
position
Projection plane
16 Align with camera position
- Compute vector from billboard to camera
- Heading/pitch of vector gives rotation for locked
heading/pitch - Construct coord system for billboard using camera
to centre of billboard vector, and an up vector - Put in matrix and multiply with modelview matrix
- Dont use a wide field of view
17What are the camera coordinates.
- Recall that the modelview matrix moves the
eye(camera) coordinate system to 0 looking along
the z axis. - So what was the coordinate system before we
transformed it. - We can find this out by taking the inverse of the
modelview matrix(Why)
18Inverse of ModelView matrix
- First get the matrix
- GLfloat viewMatrix16
- glGetFloatv(GL_MODELVIEW_MATRIX,viewMatrix)
- Recall that the modelview matrix looks like
Right-gt Up -gt
Matrix M transpose(M)
19Extract the up and right vectors
- Vector right(viewMatrix0,viewMatrix4,viewMatri
x8) - Vector up(viewMatrix1,viewMatrix5,viewMatrix9
) - Now let centerpoint be the location of the quad
that you want to draw. - The four corners can be found by
- Pointctrpointupht_scale rightwd_scale
20Specifically we have
up
- //bottom left
- glTexCoord2f(0.0,0.0)
- glVertex3fv(point(rightup)-size)
- //bottom right
- glTexCoord2f(0.0,0.0)
- glVertex3fv(point(right-up)size)
right
21Display a lot of Cacti
- void DrawCacti()
-
- // make sure the random numbers we generate are
the same every time - srand(100)
- // make sure the transparent part of the
texture isn't drawn - glEnable(GL_BLEND)
-
- // glBlendFunc(SOURCE, DESTINATION). Recall that
the Source fragment is - // written on top of the Destination(ie frame
buffer) - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA
) - // This compares and incoming alpha value with a
reference value. Fragment - // is accepted or rejected based on this value.
- glEnable(GL_ALPHA_TEST)
- glAlphaFunc(GL_GREATER, 0) // accept if
greater than 0 -
22Continued
- // get the modelview matrix
- float mat16
- glGetFloatv(GL_MODELVIEW_MATRIX, mat)
- // get the right and up vectors
- vector3_t right(mat0, mat4, mat8)
- vector3_t up(mat1, mat5, mat9)
- // select the cactus texture
- glBindTexture(GL_TEXTURE_2D, g_cactus)
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
GL_REPLACE) -
23Continued
- // draw all cacti
- glBegin(GL_QUADS)
- for (int n 0 n lt NUM_CACTI n)
-
- // randomly size the cactus
- float size 5.0f FRAND 3.0f
- // pick a random position on the map
- vector3_t pos(RAND_COORD((MAP_X - 1)
MAP_SCALE), 0.0, -RAND_COORD((MAP_Z - 1)
MAP_SCALE)) - pos.y GetHeight(pos.x, pos.z) size -
0.5f -
24Continued
- // bottom left corner
- glTexCoord2f(0.0, 0.0) glVertex3fv((pos
(right up) -size).v) - // bottom right corner
- glTexCoord2f(1.0, 0.0) glVertex3fv((pos
(right - up) size).v) - // top right corner
- glTexCoord2f(1.0, 1.0) glVertex3fv((pos
(right up) size).v) - // top left corner
- glTexCoord2f(0.0, 1.0) glVertex3fv((pos
(up - right) size).v) -
- glEnd()
- glDisable(GL_ALPHA)
- glDisable(GL_BLEND)
- // end DrawCacti()
25Check out Tree Companies
- Marlin Studios Tree Farm
- Realworld Imagery Trees etc
- A comparison of SpeedTree Max vrs natFX
- TreeGenerator website (Builds 3D trees)
26 Billboard Clouds( Siggraph 2003)
- Just recently a new method for extreme
simplification has appeared on the scene. - Siggraph 2003 contains a paper entitled
Billboard Clouds for Extreme Model
Simplification by Decoret, Durand, Sillion and
Dorsey.  - 3D models are simplified onto a set of planes
with texture and transparency maps.
27(No Transcript)
28Imposters
- An imposter is a billboard created on the fly to
cache rendered imagery - Once rendered, cost of rendering and imposter is
just a single textured quad - Can use for a few frames before updating
- Can use for a few instances of the object.
29 Skyboxes
- Here we create a cube that is centered at the
location of the camera. - The cube moves with the viewer but does not
rotate( ie it stays axis aligned) - The textures are pre-calculated to map the inside
of the cube at the distance given.
30 Drawing methods
- Draw skybox first
- Turn off depth buffer writing
- Draw cube
- Note that the sky box looks the same no matter
what size it is. - Use fog to hide the edge of the terrain
- Skybox not affected by fog, as it is actually
draw very near the camera. - On the other hand you could turn off fog for the
skybox to keep it from being fogged out, so to
speak. - Opengl1.3 has a special cube-mapping texture mode
for texturing skyboxs using one texture.
31 Example skybox
32Point sprites
- A point sprite is a screen-aligned textured quad
placed by rendering a single vertex. - Nice for particle systems
- When GL_POINT_SPRITE_NV is enabled
- Point antialiasing state ignored all points
become quads - Points rendered with point width as usual
- Texture coords of points can be replaced by
special point sprite tex coords s,t,r between
0,1 - Tex coord r is special, can set to zero (default)
or use to play through an animation stored in
3D text - For hardware acceleration on GF3, set r t0 0
(default) and enable coordinate replacement for
tex unit 3 only.
33 Example
Each little fuzzy image is a point sprite.