PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Dissolve effect source?

Does anyone have source, preferably ready to go under GapiDraw, to do a dissolve effect? You know, like the dissolve transition you can do in PowerPoint, or in Internet Explorer, for example?

I'd like a version that can dissolve an image into view as well as out of view, but I'll take whatever I can get of course. TIA!
No idea, but try this.

Take an 8x8 matrix and seed it with numbers 1 through 64.

Then, randomly permute those numbers. (Change their order around, but keep every number from 1 to 64.)

Then, draw your image, but blank out every pixel in an 8x8 grid that had the number 1. (Just repeat that grid over your image.)

Now, again draw your image, but also blank out every pixel in an 8x8 grid that had the number 2.

And so on. 64 steps of dissolve.
Why not use a boolean mask and use that to decide what to draw. 1: first image, 0: 2nd image, then randomly switch all the 1's to 0's each frame.
I know that there's an actual algorithmic approach to this, I remember reading about it some time ago (but I can't seem to find anything useable now).

However, I'm going to try an approach almost identical to what mlepage suggests...

I'm going to pre-generate a list of unique x/y coordinates in a 40x40 block. I'm then going to create a "dissolve" surface, 40x40, filled in with a black color. Then, each ProcessNextFrame(), I'll draw my background image (a 240x320 image in this case) and then draw a series of "dissolve" surfaces over the background. Then, I'll simply draw a pixel in the transparent color on the "dissolve" surface, running through the list of unique coordinates to the end, and re-drawing.

Pretty much what mlepage said with a slight variation, and I think it'll work pretty well. I'll let you know...
DillRye's approach also would work, although you'd want to ensure that you actually make steady progress from one image to another.

My approach has the property of making 64 equal steps toward the other image, each step being 1/64 closer.

There's probably smarter ways to do it, but that's off the top of my head.
an easy way to create a personalized 'dissolve' effect is to have a grayscale image with a number of dissolving steps (usually 256.. grayscale image)
and use that image to dissolve the screen..

clear screen pixels equivalent to the image's top color (255) and go down until it reaches zero..

then u can just create your 'heightmap' with photoshop or any other the way you want..
the problem is having a extra image for each dissolve fx you wanna do..

hope it helps
That's my approach, except that my approach generates that "image" programmatically.
Just to let everyone that replied know how it turned out...

I wound up doing pretty much what I said I was going to in an above post...

I threw together a quick Excel macro (the place I usually go to do pre-calculated data generation-type things, quick and easy) and generated the list of all possible coordinates in a 40x40 block (1600 pairs obviously). That's my dissolve data. I just write it out to a file and import it as a resource (3.2K, not a big deal).

I create a dissolve surface of 40x40. Then, in my ProcessNextFrame(), to fade in I run through that dissolve data and set the pixels referenced to the transparent color in the dissolve surface. I render the image I want to fade in (a 240x320 image in this case) and then overlay 40 copies of the dissolve surface over it (6x8 grid). When the index into my dissolve data hits 3200 (1600x2), the fade is done.

To fade out, just set black pixels instead of transparent pixels, and it's the same thing otherwise.

The whole of the code is about 20-25 lines, if that. One thing I did find is that it was too slow a process to just do one set of coordinates per frame (obviously... 1600 frames at 24FPS!). So, I actually do 40 pixels per frame, and it works out pretty well, the dissolve takes 1-2 seconds total. Pretty nice effect.

The only thing I don't like about it is that the dissolve forms a pretty clear pattern. I'm thinking I may do a couple of different dissolve surfaces, maybe 1 or 2 more would probably be enough, each with a different randomization of the coordinate pairs. Then, when I'm rendering them I'll randomly choose one of the three for each dissolve surface rendered. I think that will make the dissolve appear a bit more random without taking up much more memory or anything.

So, thanks everyone for the suggestions, they were very helpful!

P.S. - If anyone is actually interested in the code, just let me know, I don't mind sharing it, trivial as it really is.
Oh yeah, vic... your suggestion about grayscale was a cool idea, but one thing to keep in mind is that currently, GD will not load a grayscale PNG (Johan said he will look into turning that support on). Obviously this doesn't matter if your not using PNGs, but it drove me nuts the other night because I didn't know this, and an image I had was being saved in grayscale mode by Paint Shop Pro's image optimizer because it only had gray component colors.

Otherwise, I think your idea is very interesting, I'd like to see how that looks working.
For the first second (30 frames) of my game, I fade from black to a mostly white logo screen.

I do this by drawing black, then drawing the logo screen overtop at an opacity (from fully transparent to fully opaque).

Then, I fade out in exactly the opposite way.

This looks great on the PC.

On my iPAQ 3970, the effect is the same, but I see a vertical line of tearing during the fade in and fade out.

I do the exact same fade in and fade out at another spot in my application, but to a different screen, mostly blue. The tearing is much less noticeable. I presume this is because of the colour.

Can someone explain exactly to me why this effect is occuring? I thought the backbuffer was supposed to be flipped nicely to the display at the end of ProcessFrame. I am doing this in portrait (normal) mode. Is the iPAQ 3970 rotated or something?

How can I eliminate this tearing? It's such a simple effect, at only 30fps, and yet there's a huge blemish on it. And to think I paid a lot of money for my iPAQ 3970.
if you're willing to waste memory just load a non grayscale image which is still a grayscale image on a higher bitdepth and just load a color channel to compare values with..

i just made a working example here. ill put up a example, meanwhile just follow this code:

/////////////////////////////////////////////////////////////
// Dissolve effect
/////////////////////////////////////////////////////////////

WORD *texdata = (WORD *)pTex->GetData();
BYTE *b_mapdata = (BYTE *)pHeightMap->byteData;
WORD *w_mapdata = (WORD *)pHeightMap->GetData();

if( bDissolve )
{
int map_index = 0;
for( j=0; j<backbuf.dwHeight; j++ )
{
WORD *line = buffer;
for( i=0; i<backbuf.dwWidth; i++ )
{
if( b_mapdata[map_index] > h )
{
WORD pixel = *(texdata + i + j*pTex->GetWidth());
*line = pixel;
}

line += xpitch;
map_index++;
}

buffer += ypitch;
}

// effect is over
h++;
if( h == 255 ) bDissolve = FALSE;
}
http://www.nothing.scene.org/download/dissolvefx.zip (win32)
it uses a 24bit heightmap (bad thing to do) but i was lazy to change bitmap class (was a waster:)
Wow, that's a totally different type of dissolve, but I like that a lot better! I'm going to have to toy with it tonight...
what do you guys mean with `tearing“ ?
anything to do with the vsync problem, refresh rate?
You might want to check this out too... It's a disintigration effect.

Say you use 30 FPS, and you want to disintegrate the entire screen over 3 seconds. That gives us 90 frames to work with.

[pseudocode]
Create an integer "scrn_array" for the number of pixels/blocks you want to dissolve.

Assign every integer in the array a random number from 1-90.

Take a snapshot of the current screen.

Start the frame_counter at 1.

Loop through the scrn_array.

If our "scrn_array[x][y] == frame_counter", blank out that pixel/block in the snapshot.

Next

Increment frame_counter.

Update the screen with the modified snapshot.
[/pseudocode]

Looping through per pixel would be about 853 pixel changes per frame (76,800/90). That's a lot, but if we use 2x2 blocks, it's only 213 changes (19200/90). 4x4 blocks would be 53 changes (4800/90) per frame. And 8x8 blocks would yield 13 changes per frame (1200/90).

So, as always it's a speed vs. quality decision.

I'll have to try this myself sometime.
Just be sure that array of random numbers has all unique numbers. That's one of the problems with any approach aside from the grayscale approach, and the reason I pre-generated the list of numbers, in my case it was 1600 pairs of numbers where each pair had to be unique, but it's the same general problem.

The best way would be a completely random approach. For instance, if you have a 240x320 image to dissolve in three seconds, randomly choose 2560 pixels per frame (assuming 30 FPS) and set them black. Of course, completely random means your going to miss some. Also, ironically, cmopletely random yields an effect that doesn't look right anyway. I think the basis for the dissolves I was thinking of (like the slide transition in PowerPoint for example) must be working with some smaller block of randomness and plotting them over and over in a grid. The version I did doing that got me a result that looks very close to that, probably just need to adjust the block size, or have more than one block for a slightly more random look.
Right, that's a lot more difficult, as you have to ensure uniqueness. That's why I did it the other way.

Essentially the array we create is a virtual screen, and each element in the array (or "pixel") is assigned a frame number. When that frame number is reached by the program, the corresponding "pixel" on the real screen is turned off. (You can substitute "block" for "pixel".)

It just seems like a fast/easy way to ensure that every pixel is hit. You don't have to waste time figuring out the uniqueness stuff.

-John
Here's some sample code. It uses the PF GetDisplay() method and a made up random_number(min,max) function, but otherwise it should be completely adaptable for whatever you're working with.

Code:

// DEFINITION
#define DISSOLVE_BLOCK 1 // 1, 2, 4 or 8
#define DISSOLVE_FPS 30
#define DISSOLVE_TIME 3


// INITIALIZATION
int iDissolveFrame = 0;
int iDissolveX = (240/DISSOLVE_BLOCK);
int iDissolveY = (320/DISSOLVE_BLOCK);
int iDissolveBlocks = iDissolveX * iDissolveY;
int *iDissolveArray = new int[iDissolveBlocks];

for(int i=0; i<iDissolveBlocks; i++)
iDissolveArray[i] = random_number(0, DISSOLVE_FPS * DISSOLVE_TIME - 1);


// IMPLEMENTATION
if(iDissolveFrame < DISSOLVE_FPS * DISSOLVE_TIME)
{
for(int y=0; y<iDissolveY; y++)
for(int x=0; x<iDissolveX; x++)
if(iDissolveArray[y*iDissolveX+x] == iDissolveFrame)
GetDisplay()->FillRect(x*DISSOLVE_BLOCK,y*DISSOLVE_BLOCK,(x+1)*DISSOLVE_BLOCK,(y+1)*DISSOLVE_BLOCK,Color(0,0,0));
iDissolveFrame++;
}


// DESTRUCTION
delete iDissolveArray;
Presto wrote:
You might want to check this out too... It's a disintigration effect.


That's the same as my approach, except my array is a matrix, it is 8x8 tiled instead of image size, it has 64 steps, and every step is represented once per tile.

If you make my matrix the size of your image (i.e. 1 tile), change the steps to 90, and remove the requirement that each step is represented exactly once (well you have to if your screen is bigger than 90 pixels), then my approach is the same as yours.

I'm just explaining this so people can see the similarity.

I only suggested 8x8 tiles in case you want to conserve memory or something. The 8x8 tiles are quick to generate on the fly, they're just a random permutation.
vic wrote:
http://www.nothing.scene.org/download/dissolvefx.zip (win32)
it uses a 24bit heightmap (bad thing to do) but i was lazy to change bitmap class (was a waster:)


Nice. Ignoring the rotating image and focusing on the dissolve effect, it's the same as my approach with these changes.

Make my matrix the size of the image. Change it to have 256 levels. Change it so that the conditional probability of a pixel's value is dependent on the value of those beside it. (That's just fancy talk for saying that instead of totally random values, make values beside each other be about the same value.)

Again, I only mention this so people can see the similarity and difference.
fzammetti wrote:
Just be sure that array of random numbers has all unique numbers. That's one of the problems with any approach aside from the grayscale approach, and the reason I pre-generated the list of numbers, in my case it was 1600 pairs of numbers where each pair had to be unique, but it's the same general problem.


To ensure you have unique numbers, you do this.

You start out with every number in order, then you mix them up randomly. (The technical term is permutation. This is where your discrete math course comes into play.)

Think of playing cards. You want a full deck of shuffled cards. You don't randomly draw cards from a bin of 100 decks until you have a full deck. You just start out with a full deck, and shuffle it.

That's what you're doing. The permutation itself is quick. When you want a new random permutation, you can just permute from your previous random permutation. (Just like reshuffling the deck: you don't put them back in order first.)
But how do you quickly assure that the permutation is random? I mean, if I have 100 values in an array and I want to mix them up, I still have to choose 100 random numbers and use that list as the new access order of the array (rather than running through it linearly), but how do I asssure that a value isn't picked twice?

I don't really mean HOW do I do it, because it's a trivial matter, what I'm really getting at is that I still need a list of random numbers with no repeat values, so the same problem still applies, doesn't it?
Here's one more method. It's just another height-map approach.

Again, you use the approach of having a matrix represent the virtual screen with the frame numbers at which point the corresponding pixel will be dissolved.

(Or if not the frame number, a value by which the frame number is calculated. And if the matrix is itself just an offscreen image, that's just a representation thing, it doesn't change what it is.)

The difference here is, instead of having a totally random height-map, or drawing it, you can compute it by using fractal subdivision.

Do a search on how to do fractal subdivision. I prefer the "diamond" method. This is the fractal technique that generates mountainous terrain. So you're doing it in two dimensions to create your height-map.

The subdivision is what ensures that values at a particular pixel have a similar value to that of a nearby pixel (yet can be totally different from farther pixels, ensuring randomness).

I wrote a simple routine that calculates a fractal subdivision of any size using the diamond method. I can probably dig it up if I still have it lying around.
fzammetti wrote:
But how do you quickly assure that the permutation is random? I mean, if I have 100 values in an array and I want to mix them up, I still have to choose 100 random numbers and use that list as the new access order of the array (rather than running through it linearly), but how do I asssure that a value isn't picked twice?

I don't really mean HOW do I do it, because it's a trivial matter, what I'm really getting at is that I still need a list of random numbers with no repeat values, so the same problem still applies, doesn't it?


No you don't.

When you shuffle cards, you don't make a list of the new order, then find the cards and put them in that order.

You split the deck in a few places, maybe merge a few subsections.

Here is how you actually do the permutation.

You have 100 numbers in any order, and you want to randomize that order.

So you pick two indices randomly, and swap the numbers at those places.

Then you do it again. Do it 100 times.

So long as you are picking your swap indices from a uniform random distribution (not Gaussian or other), this effectively ensures that each number moves to a random location.
Now, if you really want me to blow your mind, I'll tell you how to choose a uniformly distributed random card from a sequence of cards, when you don't know how long the sequence is. ;-)
I hadn't thought of it that way. I guess I was over-thinking it.

That and the fact that I've always royaly sucked at math in general ;)

Yes, I see exactly what you mean now, it works very well (and is obvious after the fact). If nothing else that effectively allows me to remove my pre-generated coordinate list. I can generate it real-time now, no problem. Thanks for that! :)