Smooth Sprite Animation using 3D Textures in OpenGL
Sprite animations can be rendered in various ways. More commonly used techniques use 2D textures and discretely display one frame at a time (for example on a quad). This is usually done by loading each frame of the animation into a seperate 2D texture and binding them as required, or by putting all frames into a single 2D texture and moving texture coordinates. Smooth animations require a high number of frames this way.
Alternatively, you can use a 3D texture, which gives you linear filtering between frames, i.e. frames are blended into each other. A 3D texture can increase visual quality and create smooth animations with fewer frames. The following describes the idea and shows a few examples.
How It Works
Imagine the 3D texture as a stack of 2D textures where the animation frames are layered on top of each other in the order of the animation. You then progress through the animation by moving the R-texture coordinate from 0 to 1. While 2D textures only apply linear filtering between pixels in the S and T dimension, a 3D texture also filters in the R dimension. Moving from one frame to next does not just display 100% of frame 1 and then 100% of frame 2. It dispays 100% of frame 1, then maybe 80% of frame 1 and 20% of frame 2, then 50% of both and so on. The previous frame slowly fades out while the next fades in.
2D vs. 3D
3D textures are not suitable for all kinds of animations. Fast moving animations usually look bad and start flickering. This is because between two frames, both are barely visible and only overlapping parts are fully visible.

Top: animation state of a rotating goblet with discrete frames
Bottom: animation state somewhere between two frames in a 3D texture

With regular discrete 2D textures the animation looks much clearer.

Organic animations, like fire, on the other hand look very choppy and static if a single frame is displayed at a time.
This is where 3D textures can shine. They are great for animations like explosions, fire, pulsating slime and other special effects. While the previous frame fades out, the next one fades in and the animation look very smooth. The effect is hard to capture in an animated GIF, so you best see it rendered in realtime.
Click here to launch the Java Web Start comparison of 2D vs. 3D texture animation
(requires: Java, OpenGL 1.2, Win/Linux/Mac OS X/Solaris)

The top two quads are rendered with 2D textures and the bottom two quads are rendered with a 3D texture. Key W speeds the animation up and S slows it down. Space bar toggles between displaying both, 2D-only and 3D-only. There are three animations which can be switched by pressing keys 1, 2 and 3:
- 1 is a 16 frame fire
- 2 is 32 frame explosion
- 3 is 19 frame rotating goblet
On an interesting side note, rendering the 3D-textured quads is not slower than the 2D alternative, at least on my computer.
Code example (in Java/LWJGL)
Using 3D textures to animate your sprites is very easy. The following steps are written in Java and use the Lightweight Java Game Library (LWJGL), but they are essentially the same in any OpenGL library.
-
If you want to use animations in which the number of frames is not a power of two, you have to check whether the OpenGL driver supports the extension GL_ARB_texture_non_power_of_two. This is necessary because the number of levels - the R dimension, the animation frames - in a 3D texture follows the same rules as the S and T dimensions.
ContextCapabilities capabilities = GLContext.getCapabilities(); if (!capabilities.GL_ARB_texture_non_power_of_two) { ... // Don't use animations with non-POT frame-count } -
Load the frames into a ByteBuffer that will be passed to OpenGL's glTexImage3D. Simply take the data of each frame and append it to one buffer in the order of the animation:
ByteBuffer threeDTextureBuffer = null; for (int i = 1; i < frames + 1; i++) { ByteBuffer currentFrameData = ... // Load frame i if (threeDTextureData== null) { threeDTextureData = BufferUtils.createByteBuffer(currentFrameData.limit() * frames); } threeDTextureBuffer.put(imageBuffer); } threeDTextureBuffer.rewind(); -
Create a 3D texture:
IntBuffer texNameBuffer = BufferUtils.createIntBuffer(1); GL11.glGenTextures(texNameBuffer); int threeDTexture = texNameBuffer.get(0); GL11.glBindTexture(GL12.GL_TEXTURE_3D, threeDTexture); GL11.glTexParameteri(GL12.GL_TEXTURE_3D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); GL11.glTexParameteri(GL12.GL_TEXTURE_3D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR); GL12.glTexImage3D(GL12.GL_TEXTURE_3D, 0, GL11.GL_RGBA, frameWidth, frameHeight, frames, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, threeDTextureBuffer); GL11.glEnable(GL12.GL_TEXTURE_3D);
Setting the filter to GL_LINEAR is important to achieve the desired effect.
-
Animate texture coordinate R:
r = (r + rIncreasePerMillisecond * timeSinceLastFrame) % 1.0f;
If you have a fixed frame rate, you can just advance r by a fixed value.
-
Draw the texture on a quad:
GL11.glBegin(GL11.GL_QUADS); GL11.glTexCoord3f(0.0f, 0.0f, r); GL11.glVertex3f(0.0f, 0.0f, 0.0f); GL11.glTexCoord3f(0.0f, 1.0f, r); GL11.glVertex3f(0.0f, 1.0f, 0.0f); GL11.glTexCoord3f(1.0f, 1.0f, r); GL11.glVertex3f(1.0f, 1.0f, 0.0f); GL11.glTexCoord3f(1.0f, 0.0f, r); GL11.glVertex3f(1.0f, 0.0f, 0.0f); GL11.glEnd();
That is all you have to do.
zhjchv0201
Replica Handbags
Replica Handbags
Replica Handbags
Replica Handbags
Replica Handbags
Dior Handbags
Chloe handbags
Thomas Wylde Handbags
Replica Handbags
Valentino handBags
replica Hermes Belts
Marni Handbags
Replica Belts
Replica Handbags
Louis Vuitton handbags
Loewe Handbags
Bottega Veneta Handbags
Replica Versace Handbags
Gucci handbags
Prada Handbags
Hermes Handbags
Jimmy Choo Handbags
Fianl Sales Handbags
Fake handbags
Versace Handbags
Miu Miu handbags
Replica Belts
Balenciaga Handbags
Lancel Handbag
Louis Vuitton handbags
- reply
Submitted by lange (not verified) on 1. February 2010 - 4:31.aqian100221
Chopard Sunglasses
Gucci Scarf
Police Sunglasses
Alexander McQueen Scarves
|Replica Sunglasses|
|Replica Scarf|
|Designer Replica Sunglasses|
|Wholesale scarves|
|Women's Scarves|
|Cheap Sunglasses|
|Fashion scarves|
|Hermes Handbags|
replica Sunglasses
|Gucci Sunglasses
mens Gucci Sunglasses
Chanel Sunglasses
Discount Chanel Sunglasses
Dior Sunglasses
Bvlgari Sunglasses
Louis Vuitton Sunglasses
Marc Jacobs Sunglasses
Chloe Sunglasses
Fendi Sunglasses
Prada Sunglasses
Givenchy Sunglasses
Ferragamo Sunglasses
Fendi Scarf
Armani Sunglasses|
Wholesale scarves
|Burberry Scarves
Cheap Burberry Scarf
Burberry Neck Scarves
Louis Vuitton Scarf
Replica LV Scarf
Hermes Scarves
Replica Hermes Scarf
Paul Smith Scarf
Paul Smith Neck Scarves
Gucci Replica Scarf
Neck Scarves
Coach Scarf
Coach Scarf For Women
Neck Scarves
Chanel Scarf For Women
Chanel Scarves
Shawls Scarf
MiuMiu Scarf
Related Blog
Honeyreplica Louis Vuitton Handbags
yanrikun
hermes handbags
hermes handbags
hermes handbags
honeyreplica
hermes handbags
hermes handbags
hermes handbags
replica handbags
Honeyreplica
Ugg boots|
- reply
Submitted by ReplicaScarf (not verified) on 21. February 2010 - 2:59.Very interesting, i like this website.
Look my website : Odżywki
- reply
Submitted by odżywki (not verified) on 1. February 2010 - 15:46.It's not so simply to bring a professional written essays, preferably if you are intent. I consult you to notice buy essays and to be spare from discredit that your work will be done by professionals
- reply
Submitted by LaraVu (not verified) on 7. February 2010 - 0:51.10luoyuejun0209
Replica Louis Vuitton
Replica Watches
replica handbags
Louis vuitton handbags
Replica Handbags
replica watches
Wholesale Replica Handbags
Omega Replica Watches
replica Omega
Omega Classic Watches
Omega CO AXIAL Watches
Omega CONSTELLATION Watches
Omega DEVILLE Watches
Omega Museum Watches
Omega OLYMPIC COLLECTION Watches
Omega OMEGAMANIA Watches
Omega Railmaster Watches
Omega Seamaster Watches
Omega SPEEDMASTER Watches
Chanel Handbags
Replica Chanel
Chanel bags
Chanel Replica Handbags
Replica Chanel Bags
Chanel Purses
Cheap Chanel Bags
Chanel Watches
Chanel Wallets
Chanel Sunglasses
Chanel Jewelry
Chanel Replica Jewelry
Chanel Scarves
Chanel Silk Scarf
Cheap Chanel Scarves
replicat tiffany
tiffany jewelry
Replica Bags
Hermes Handbags
Replica Purses
Replica Mobile
replica scarf
Burberry scarves
Hermes scarves
Ugg boots
Ugg boots
Replica handbags
Replica Gucci
Gucci Handbags
Chanel handbags
Hermes handbags
adidas shoes, air jordans
Wholesale nike shoes, adidas shoes Blogs
air jordans, jordan fusions
air jordans, jordan fusions
jordan fusions,Chanel Shoes
Chanel Shoes, Christian Louboutin Shoes
Replica Fashion Handbags Blogs
Christian Louboutin Shoes, Manolo Blahnik Shoes
Manolo Blahnik Shoes, Shoe Trends
This week in shoes, spring 21 shoes
- reply
Submitted by replica handbags (not verified) on 9. February 2010 - 5:49.10luoyuejun0221
Replica Louis Vuitton
Replica Watches
replica handbags
Louis vuitton handbags
Replica Handbags
replica watches
Wholesale Replica Handbags
Omega Replica Watches
replica Omega
Omega Classic Watches
Omega CO AXIAL Watches
Omega CONSTELLATION Watches
Omega DEVILLE Watches
Omega Museum Watches
Omega OLYMPIC COLLECTION Watches
Omega OMEGAMANIA Watches
Omega Railmaster Watches
Omega Seamaster Watches
Omega SPEEDMASTER Watches
Chanel Handbags
Replica Chanel
Chanel bags
Chanel Replica Handbags
Replica Chanel Bags
Chanel Purses
Cheap Chanel Bags
Chanel Watches
Chanel Wallets
Chanel Sunglasses
Chanel Jewelry
Chanel Replica Jewelry
Chanel Scarves
Chanel Silk Scarf
Cheap Chanel Scarves
replicat tiffany
tiffany jewelry
Replica Bags
Hermes Handbags
Replica Purses
Replica Mobile
replica scarf
Burberry scarves
Hermes scarves
Ugg boots
Ugg boots
Replica handbags
Replica Gucci
Gucci Handbags
Chanel handbags
Hermes handbags
adidas shoes, air jordans
Wholesale nike shoes, adidas shoes Blogs
air jordans, jordan fusions
air jordans, jordan fusions
jordan fusions,Chanel Shoes
Chanel Shoes, Christian Louboutin Shoes
Replica Fashion Handbags Blogs
Christian Louboutin Shoes, Manolo Blahnik Shoes
Manolo Blahnik Shoes, Shoe Trends
This week in shoes, spring 21 shoes
- reply
Submitted by replica handbags (not verified) on 21. February 2010 - 5:28.The topics just about this good topic are offered by the research paper writing services thus, we would buy essay and already written essay about this.
- reply
Submitted by buy essays (not verified) on 22. February 2010 - 1:23.The proper way to have a good grade was to get the superb quality custom essay or stuff about this good post, or just get know just about become our writer selecting the essays writing company.
- reply
Submitted by essay writing topics (not verified) on 22. February 2010 - 4:03.Do you see that you make great research about this good post. Keep on performing it! I buy an essay just about it using the essay writers.
- reply
Submitted by buy essay (not verified) on 22. February 2010 - 4:26.Thank you a lot for a kind of intellectual stuff relating with this good topic ! You have to ground your own buy dissertation service, we opine. Just because a lot of thesis service do it and you should accomplish good dissertation form also.
- reply
Submitted by dissertation service (not verified) on 22. February 2010 - 8:13.per dimagrire
sfondi gratis
giochi mario
- reply
Submitted by Anonymous (not verified) on 9. March 2010 - 21:13.The thesis writing service will propose you with the thesis example but the professional article about this post scholars could find on this page only.
- reply
Submitted by dissertation writing (not verified) on 22. February 2010 - 8:30.