I would like to use a mosaic effect in my game, but am frankly at a loss as to how to implement such an effect. Has anyone implemented a mosaic effect before? Do you have any suggestions?
]]>No idea, but here's how I would take a stab at it (changing a graphic to mosaic)
1) Define how your mosaic pieces should look. Typically, a single piece is dark on the border and the same color elsewhere (the middle).
2) Define how big your pieces are (could be passed into the function that does the dirty work)
3) Move along the graphic and group any pixels that are close in color (what that means is questionable) to form your mosaic pieces
4) Change each group to the average color (any pixel not this color in the group force to this color)
5) Output all your mosaic pieces
Of course, there's plenty of gotcha's in the above algorithm, but I did say, "no idea" at the beginning of the post.
Actually, I did the above method for a game I was making earlier this year called Cosmos. Only, instead of grouping by color difference, I grouped by height difference. To see the progress of that, you can look at the pictures in this thread.
Hope I've been of some help...
]]>What kind of mosaic are we talking here? If you mean the pixelation thing, that should be very straightforward.
]]>Or do you mean something a bit more advanced?
{"name":"Mosaic1.JPG","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/c\/0c28df5b2a9d48e56986b0c53e88a7bd.jpg","w":300,"h":254,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/c\/0c28df5b2a9d48e56986b0c53e88a7bd"}
Or do you want a real collage-style mosaic?
]]>Here's some mock-up code I just wrote. There's probably much more efficient ways to do this, but since I've never done this before I figured this was the simplest method:
1 | void Bitmap2Mosaic (BITMAP *bitmap, int xpixel, int ypixel) |
2 | { |
3 | // xpixel and ypixel define the size of each pixel in the mosaic. |
4 | |
5 | int xp = xpixel - 1; |
6 | int yp = ypixel - 1; |
7 | int c; |
8 | int z, zz; |
9 | |
10 | if (bitmap == NULL) return; |
11 | |
12 | for (z = 0; z < bitmap->w; z += xpixel) |
13 | for (zz = 0; zz < bitmap->h; zz += ypixel) |
14 | { |
15 | c = getpixel(bitmap,z,zz); |
16 | rectfill(bitmap,z,zz,z+xp,zz+yp,c); |
17 | } |
18 | } |
If your game only uses one colour depth you could make this routine a little faster by replacing getpixel() with one of the _getpixel() commands. Or another change could be to add a second bitmap reference to the prototype which would become the target bitmap. (And the first would be the source.)
Presuming that's the effect you want. I've heard people call trails left by objects a mosaic effect. If that's what you mean then the process is obviously entirely different and can be done in a number of ways depending on exactly the effect you're going for.
--- Kris Asick (Gemini)
--- http://www.pixelships.com