Animated textures
by Rob Rendell · in Torque Game Engine · 03/03/2005 (4:23 pm) · 10 replies
Hi, all. I mentioned in another thread that I was writing an fxCloudLayer object, and was going to add the ability to generate fractal clouds based on Perlin noise. I've done that, and it looks good. However, Perlin noise can be extended into an arbitrary number of dimensions, so I thought, why not animate the 2d cloud texture by slowly moving through the z dimension?
So, I've done that, and it looks great :) However, I'm a bit worried that I'm potentially blowing away the texture cache, and/or creating memory leaks. It seems to run fine on my (moderately new) laptop, so I don't see any signs of problems, but I thought I'd post my texture animation code here and see if anyone who knows more about the internals of Torque will say "don't do it like that!"
In my object's header I declare a TextureHandle:
Then, when I want to update the texture, I do this:
So, is that the best way to do this?
So, I've done that, and it looks great :) However, I'm a bit worried that I'm potentially blowing away the texture cache, and/or creating memory leaks. It seems to run fine on my (moderately new) laptop, so I don't see any signs of problems, but I thought I'd post my texture animation code here and see if anyone who knows more about the internals of Torque will say "don't do it like that!"
In my object's header I declare a TextureHandle:
TextureHandle mCloudTexture;
Then, when I want to update the texture, I do this:
GBitmap *bitmap;
U8 *sky;
...
if (mCloudHandle)
bitmap = mCloudHandle.getBitmap();
else
bitmap = new GBitmap(mFractalSize, mFractalSize, false, GBitmap::RGBA);
sky = bitmap->getWritableBits(0);
/* ... update the contents of sky, which is an array of 4-byte chunks (RGBA)... */
mCloudHandle.set("fractalSky", bitmap, false);So, is that the best way to do this?
#2
As far as I know, the fractal cloud resource doesn't let you animate the clouds. They are still static, correct?
03/04/2005 (6:18 am)
Adam,As far as I know, the fractal cloud resource doesn't let you animate the clouds. They are still static, correct?
#3
It was also Sorich's question about how his fractal differed from Perlin noise that reminded me of the existance of Perlin noise, and prompted me to go off and read about it, get enthused about it, and implement it :)
@Stefan, yes, you're right. The fractal code in Josef's implementation is (as far as I can see) something similar to the plasma algorithm that I learned years ago... fixing the value of the centers of alternating squares and diamonds at increasingly smaller scales, with a random factor which also diminishes with time, so that by the end you're doing smoothing. It results in quite pleasing surfaces, and is quite efficient to compute, but I don't know of any way to extend it to 3d without generating the whole shebang in advance.
Perlin noise, on the other hand, can be implemetned as a pure function - you give it a floating point coordinate, and it gives you back the value at that coordinate, without you having to calculate the value anywhere else. Thus, if you create a 3d Perlin noise function, you can iterate over an aribitrary plane of the function to create the texture for a particular t (or z) without having to keep previous textures in memory.
Actually, you don't usually use raw Perlin noise, but rather take the sum of octaves (doubled frequency) of Perlin noise, so you do end up calculating multiple points... but the idea is the same. So, I did that :)
Screenshots... I'll see if I can take some and set them up tonight.
03/06/2005 (3:01 pm)
@Adam, yes, I grabbed Josef Jahn's fractal sky resource. It was the starting point for my own fractal cloud implementation, in that it was his code I read to see how to generate a new texture at run-time rather than reading it in from an image file.It was also Sorich's question about how his fractal differed from Perlin noise that reminded me of the existance of Perlin noise, and prompted me to go off and read about it, get enthused about it, and implement it :)
@Stefan, yes, you're right. The fractal code in Josef's implementation is (as far as I can see) something similar to the plasma algorithm that I learned years ago... fixing the value of the centers of alternating squares and diamonds at increasingly smaller scales, with a random factor which also diminishes with time, so that by the end you're doing smoothing. It results in quite pleasing surfaces, and is quite efficient to compute, but I don't know of any way to extend it to 3d without generating the whole shebang in advance.
Perlin noise, on the other hand, can be implemetned as a pure function - you give it a floating point coordinate, and it gives you back the value at that coordinate, without you having to calculate the value anywhere else. Thus, if you create a 3d Perlin noise function, you can iterate over an aribitrary plane of the function to create the texture for a particular t (or z) without having to keep previous textures in memory.
Actually, you don't usually use raw Perlin noise, but rather take the sum of octaves (doubled frequency) of Perlin noise, so you do end up calculating multiple points... but the idea is the same. So, I did that :)
Screenshots... I'll see if I can take some and set them up tonight.
#4
03/06/2005 (5:08 pm)
This sounds really cool.
#5
These were taking at erratic intervals on a single fxCloudLayer that was animating quite quickly (z changing 0.1 per second) with no movement (so I set the speed to 0.0).




03/06/2005 (10:40 pm)
Ok, screenshots. Hopefully these are large enough to be able to see the progression, without being obnoxiously large.These were taking at erratic intervals on a single fxCloudLayer that was animating quite quickly (z changing 0.1 per second) with no movement (so I set the speed to 0.0).




#6
03/06/2005 (11:00 pm)
WOW!!!!!
#7
03/06/2005 (11:02 pm)
Damn, nice work, man! Definitely one of those things I've thought about and wished I had for a long time. Any chance you might put it out as a resource?
#8
03/06/2005 (11:57 pm)
Awesome! That looks DAMN nice! This would make an exelent resource(hint....hint). :)
#9
Yes, I'm definitely going to make a resource of the fxCloudLayer. I had an issue with drawing priorities due to my complete lack of understanding of how torque (and OpenGL in general) did its 3D rendering, which I asked newbie questions about over here, but I think I've got a handle on it now, and have decided that I'm doing it the only way it can be done (sanely). Unfortunately, it requires a small patch on sceneGraph/sceneState.h, but otherwise it's a drop-in object.
So, I just have a few more features I'd like to add, and then I'll create one.
* The ability to nominate a cloud colour
* The ability to nominate the range over which the cloud goes from an alpha of 0% to 100%.
* Sending a seed for the RNG to the clients, so networked players get the same sky.
* Allow a few types of update. Currently, you can only send all the parameters in one hit from the server, which regenerates the arrays (re-randomising the clouds).
* For changes for which it makes sense, have the clouds morph to the new value.
I'd also like to add the ability to have a cloud layer "roll in" and "roll out". The current sky implementation has all the storm stuff to fade clouds in and out, but it does that by alpha-ing the clouds to 0% over time... there's a bit of a sweep across the sky, but it's nothing like seeing a big black storm front build on the horizon and then sweep overhead :)
I have some ideas on how to do it, but I won't delay the resource until I get it working, because it might take a while. If/when I get it working, I can update the resource.
(I'm fairly sure that you can update a resource after posting it? I haven't done one before, but I think some of the ones I've seen have comments to that effect...)
03/07/2005 (3:49 pm)
Thanks :)Yes, I'm definitely going to make a resource of the fxCloudLayer. I had an issue with drawing priorities due to my complete lack of understanding of how torque (and OpenGL in general) did its 3D rendering, which I asked newbie questions about over here, but I think I've got a handle on it now, and have decided that I'm doing it the only way it can be done (sanely). Unfortunately, it requires a small patch on sceneGraph/sceneState.h, but otherwise it's a drop-in object.
So, I just have a few more features I'd like to add, and then I'll create one.
* The ability to nominate a cloud colour
* The ability to nominate the range over which the cloud goes from an alpha of 0% to 100%.
* Sending a seed for the RNG to the clients, so networked players get the same sky.
* Allow a few types of update. Currently, you can only send all the parameters in one hit from the server, which regenerates the arrays (re-randomising the clouds).
* For changes for which it makes sense, have the clouds morph to the new value.
I'd also like to add the ability to have a cloud layer "roll in" and "roll out". The current sky implementation has all the storm stuff to fade clouds in and out, but it does that by alpha-ing the clouds to 0% over time... there's a bit of a sweep across the sky, but it's nothing like seeing a big black storm front build on the horizon and then sweep overhead :)
I have some ideas on how to do it, but I won't delay the resource until I get it working, because it might take a while. If/when I get it working, I can update the resource.
(I'm fairly sure that you can update a resource after posting it? I haven't done one before, but I think some of the ones I've seen have comments to that effect...)
#10
03/07/2005 (3:53 pm)
BTW, I'm going to presume, since no-one has shrieked at my animated texture code at the start of this thread, that it's not disasterous to do what I'm dong...
Torque 3D Owner Adam
Adam deGrandis
Anyway... Id love to see a screen if you have time. The skyboxes are something I mess around with quite a bit. :)