• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
hpdvs2

Alpha Masked copy is getting weird results.

5 posts in this topic

I have some code that adds a texture to another texture using the alpha mask of another texture.  But I'm getting really wonky results.
 
First, All images are 64x64 PNG files, stored with no compression.  
 
I have 4 masks, used for the 4 corners that would make up an image (The image is all white, but the blue shows the alpha.  It was the easiest way to show that here.
03.09.2013-07.49.png
 
Next, I have 4 textures to mess with: (dirt, grass, concrete and pavement)
03.09.2013-07.52.png
 
I put together a C# App to Create images.  I start with a black 64x64 image, and start adding the pieces I want.  For instance, mixing NE with 2 gives me this:
03.09.2013-07.56.png
It looks pretty accurate, but for some reason much brighter.
 
When I combine 3 & NW, I get this:
03.09.2013-08.02.png
Again hard to notice, but some of the tones are brighter.
 
Now for the real kicker.  Now I take the first image (green in the top right corner) and call the same method to add the concrete to the top left corner, and I get this:
03.09.2013-08.05.png
 
Obviously, my code is doing something very wrong, but I'm not sure what, because it starts out so close.  
Here is my code (C#, which doesn't support AND/OR on Bytes, which is pretty dumb, so everything is cast to int and back.)
 

 

        public void Paint(Bitmap canvas, Bitmap mask, Bitmap newLayer)
        {
            var rect = new Rectangle(0, 0, newLayer.Width, newLayer.Height);
            var bitsMask = mask.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
            var bitsInput = newLayer.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
            var bitsOutput = canvas.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
            unsafe
            {
                for (int y = 0; y < newLayer.Height; y++)
                {
                    byte* ptrMask = (byte*)bitsMask.Scan0 + y * bitsMask.Stride;
                    byte* ptrInput = (byte*)bitsInput.Scan0 + y * bitsInput.Stride;
                    byte* ptrOutput = (byte*)bitsOutput.Scan0 + y * bitsOutput.Stride;
                    for (int x = 0; x < newLayer.Width; x++)
                    {
                        int msk = (int)ptrMask[4 * x + 3];
                        //blue
                        ptrOutput[4 * x + 0] = (byte)
                        (
                            (int)ptrOutput[4 * x + 0] | 
                            (msk & (int)ptrInput[4 * x + 0])
                        );
                        //green
                        ptrOutput[4 * x + 1] = (byte)
                        (
                            (int)ptrOutput[4 * x * 1] | 
                            (msk & (int)ptrInput[4 * x + 1])
                        );
                        //red
                        ptrOutput[4 * x + 2] = (byte)
                        (
                            (int)ptrOutput[4 * x * 2] | 
                            (msk & (int)ptrInput[4 * x + 2])
                        );
                        //mask
                        ptrOutput[4 * x + 3] = 255;
                    }
                }
            }
            mask.UnlockBits(bitsMask);
            newLayer.UnlockBits(bitsInput);
            canvas.UnlockBits(bitsOutput);


        }
 

The general logic is canvas = canvas OR (mask AND cover)

Edited by Dan Violet Sagmiller
0

Share this post


Link to post
Share on other sites

I should include that I also tried casting to uint and back as well, but that had no difference.  The negative flip isn't held where I'm translating to int and back to byte.

 

I should also include that speed is not important here.  This is for an editing tool to put the textures together.  if it takes 20 minutes for 525 permutations, I'm ok with that.  I just need it to work.

Edited by Dan Violet Sagmiller
0

Share this post


Link to post
Share on other sites

Some things i noticed as unexerienced guy, don't hang me if i am totaly off:

 

Format is specified as Format32argb but it seems you are writing bgra, is the reversed order necessary?

Following your example, wouldn't it be possible that: ((INT)Byte) & ((INT)Byte) > Byte? In that case your cast back to byte would not give 11111111 which it should.

 

In general i am not sure you could use the same code for masking and for imageblending.

 simple add would probably result in very bright colors in the overlapping area..

0

Share this post


Link to post
Share on other sites

You have an inconsistency in your code here:

 

(int)ptrOutput[4 * x * 1] should be: (int)ptrOutput[4 * x + 1] (for both the green and the blue channels.)

 

...but the real problem is that you can't use bitwise masking to perform alpha blending (only 1-bit alpha testing).

 

Consider the color value 170 (10101010) and an alpha value of 85 (1010101). AND-ing them together would result in the value 0 (00000000), while the expected value (of a multiplication of the two) would be 170 * 85 / 255 = 56. An OR operation would result in the value 255 (11111111).

 

 

More correct would be: newcolor = ((basecolor * (255 - alpha)) + (covercolor * alpha)) / 255.

Edited by eppo
0

Share this post


Link to post
Share on other sites

(int)ptrOutput[4 * x * 1] should be: (int)ptrOutput[4 * x + 1] (for both the green and the blue channels.)

 

Thanks!  i had the misplaced multiple, instead of a +.  that fixed the singular approaches.    

03.10.2013-11.27.png

So now, the only issue is its combination of 2 areas.  It turns pink/bright.  Additive, which I suppose it is.  

 

Which then brings me on to your next part: 

the real problem is that you can't use bitwise masking to perform alpha blending

 

Which is what I am seeing.

 

And then your solution:

newcolor = ((basecolor * (255 - alpha)) + (covercolor * alpha)) / 255

 

Which solved this for me.  Thanks!  

Here is the end result of having 4 different tiles corners stamped together.

03.10.2013-11.37.png

0

Share this post


Link to post
Share on other sites

Format is specified as Format32argb but it seems you are writing bgra, is the reversed order necessary?

 

Yes, it does appear blanked out, but I noticed piece by piece that when using the "A" I was getting red, "R" I was getting blue, etc... So I marked them the way I say them.  I'm not sure why It came through this way, but that's how it worked.  I agree it is messed up, and if anyone knows why, that would be great.  It might have to do with locked bits or something.

 

In general i am not sure you could use the same code for masking and for imageblending.
 simple add would probably result in very bright colors in the overlapping area..

 

 - Bingo!  as noted in my previous post.  

 

Thanks for your help as well.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0