How putpixel works? and alpha

The Partridge Family were neither partridges nor a family. Discuss.
User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: How putpixel works? and alpha

Post by chili » June 15th, 2012, 3:38 am

Asimov wrote:The big problem is this. I don't know how to get the alpha back out of the surface. I have managed to read the backbuffer in backPixel, but I don't know how to separate it into ARGB values.
You need to use bit masking and bit shifting to extract the individual channels. You said before that you studied binary notation, so it should not be that difficult for you. Just look up bitwise operators for c. The ARGB format is just as it is written: the high 8 bits are the alpha channel and the low 8 bits are the blue channel.
Chili

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: How putpixel works? and alpha

Post by Asimov » June 15th, 2012, 2:20 pm

Hi Chilli,

Well either the formula isn't working, or I am applying the formula wrong. I have tried quite a few methods, but the last 4 I tried I only got a better result from XORing, but that doesn't really solve my problem. The formula is supposed to apply the alpha channel, but all I get is a mess. Now first all I tried ANDing to get the Left most bit, but this produced a mess.

Code: Select all

D3DCOLOR backPixel;
D3DCOLOR FinalColour;
backPixel =((D3DCOLOR*)backRect.pBits)[ x + (backRect.Pitch >> 2) * y ];
int alpha = c & 0xFF000000;
FinalColour= (alpha * c + backPixel * (1 - alpha));
((D3DCOLOR*)backRect.pBits)[ x + (backRect.Pitch >> 2) * y ] = FinalColour;
Then I tried bit shifting. I don't actually understand bit shifting to get a result, but I have been told on a website >>24 produces the left most byte which should be the alpha. Right this made my turret totally invisible

Code: Select all

D3DCOLOR backPixel;
D3DCOLOR FinalColour;
backPixel =((D3DCOLOR*)backRect.pBits)[ x + (backRect.Pitch >> 2) * y ];
unsigned char alpha = c>>24;
FinalColour= (alpha * c + backPixel * (1 - alpha));
((D3DCOLOR*)backRect.pBits)[ x + (backRect.Pitch >> 2) * y ] = FinalColour;
Ok the most success I have had is actually XORing. The only thing with XORing is that it is not really doing what I want. It is combining my sprite with the background, so that it is part of the background rather than covering it up, but so far it looks the best. Also you can see the glow coming from the light.Of course the missile which is hidden behind my turret is showing through because XORing does that. I am pulling my hair out in frustration. I might use Xoring with my missiles as it would look cool if you can see the stars through them, but not for my solid turret object.

Code: Select all

D3DCOLOR backPixel;
D3DCOLOR FinalColour;
backPixel =((D3DCOLOR*)backRect.pBits)[ x + (backRect.Pitch >> 2) * y ];
FinalColour= c | backPixel;
((D3DCOLOR*)backRect.pBits)[ x + (backRect.Pitch >> 2) * y ] = FinalColour;
}
EDIT: Ok I am going to add to this post. Tried something new and that is to do the formula on individual RGB, and all I got was a black square LOL.

Code: Select all

int backPixel;
	int FinalColour;

	backPixel =((D3DCOLOR*)backRect.pBits)[ x + (backRect.Pitch >> 2) * y ];
	unsigned int alpha = c & 0xff000000;
	unsigned int R=alpha * (c & 0x00ff0000) + (backPixel & 0x00ff0000) * (1 - alpha);
	unsigned int G=alpha * (c & 0x0000ff00) + (backPixel & 0x0000ff00) * (1 - alpha);
	unsigned int B=alpha * (c & 0x000000ff) + (backPixel & 0x000000ff) * (1 - alpha);

	FinalColour=(R,G,B);
	((D3DCOLOR*)backRect.pBits)[ x + (backRect.Pitch >> 2) * y ] = FinalColour;
I am thinking that either the formula is wrong, or somehow I am applying the formula wrong.

Asimov
Attachments
XOR.jpg
Best result so far, but I don't really want my sprite merged with the background heh heh
XOR.jpg (7.37 KiB) Viewed 3474 times
Last edited by Asimov on June 15th, 2012, 4:32 pm, edited 1 time in total.
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: How putpixel works? and alpha

Post by chili » June 15th, 2012, 4:26 pm

I don't have the time to give a lesson on this right now, but post your solution and I will show you how to do it properly.
Chili

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: How putpixel works? and alpha

Post by Asimov » June 15th, 2012, 4:44 pm

Hi Chilli,

I presume by posting the solution you meant send the files heh heh, because I haven't got a solution that works hee hee.

Whenever I try something new I always copy the whole project folder and test stuff on that, so there may be stuff in this version which I have been testing.

The code in question is in the D3DGraphics::PutPixelAlpha.

I am really annoyed I couldn't work this out on my own. I am pretty sure I got the Hex right, and I used an AND to Mask out the bits that weren't needed.

Anyway I hope you can work it out.

Thanks.

Edit: After making this post I have been doing some research, and the correct format for And is to put a number at the end of the binary eg (c & 0x00ff0000)>>16 So I did a test. My surface is in c and that is fine so I tried to get the RGB values from c and then put them back together again. In theory my surface should look the same as using c, but obviously I am not getting the correct RGB values back, as my surface is messed up. If I use c my sprite is fine, except for transparency of course.

Code: Select all

        byte R= (c & 0x00ff0000)>>16  ;//tried using byte here instead of int, just in case
	byte G= (c & 0x0000ff00)>>8 ;//however it makes no difference
	byte B= (c & 0x000000ff);
	FinalColour=(R,G,B);
Asimov
Attachments
MCtestcopy.zip
test version of Missile command
(1008.86 KiB) Downloaded 173 times
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: How putpixel works? and alpha

Post by chili » June 16th, 2012, 1:12 am

Asimov wrote:Anyway I hope you can work it out.
Asameshi mae BABY; was there ever any doubt? 8-)
Attachments
Alpha.zip
(1009.09 KiB) Downloaded 184 times
Chili

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: How putpixel works? and alpha

Post by Asimov » June 16th, 2012, 9:44 am

Hi Chilli,

As usual you are a genius.

I was very close to the solution, but also a million miles away. For a start the formula I was using was obviously wrong, because my formula I found didn't have 255 in, and also I realise something else. I will explain. Remember in my last email I did a test. I will show you my test again.

Code: Select all

        int R= (c & 0x00ff0000)>>16;
	int G= (c & 0x0000ff00)>>8;
	int B= (c & 0x000000ff);
	FinalColour=(R,G,B);
	((D3DCOLOR*)backRect.pBits)[ x + (backRect.Pitch >> 2) * y ] = FinalColour;
This test did not work. In fact I was thinking it should not look any different to the original, but you know what it did. Now after looking at your code I did this.

Code: Select all

        int R= (c & 0x00ff0000)>>16;
	int G= (c & 0x0000ff00)>>8;
	int B= (c & 0x000000ff);
	//FinalColour=(R,G,B);
	((D3DCOLOR*)backRect.pBits)[ x + (backRect.Pitch >> 2) * y ] = D3DCOLOR_XRGB(R,G,B);
And this reproduces c properly. Can you tell me why the first code failed? Is it because I cannot pass a D3DCOLOR to an int in this way?

So the first problem with my code wsa that FinalColour=(R,G,B); was not storing my colour properly.
My second problem with my code was that my formula was way incorrect. I will paste your formula for other people with the same problem.

Thanks for the help. Now I can work on building a robust sprite system. At the moment my sprite code is in 3 different places and really I want it all in one place. 1) to keep it tidy and 2) so I can re use it in future programs.

By the way I originally set up all my colours like you did as ints, but I deleted that one in a rage of temper LOL, and started again.

Code: Select all

        finalRed = (destRed * (255 - alpha) + sourceRed * alpha) / 255;
	finalGreen = (destGreen * (255 - alpha) + sourceGreen * alpha) / 255;
	finalBlue = (destBlue * (255 - alpha) + sourceBlue * alpha) / 255;
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: How putpixel works? and alpha

Post by LuX » June 16th, 2012, 10:46 am

Sweet! I tested it out by putting some red glow around your turret and looks really nice!

On other matters, I finally figured out a, MUCH, faster way to calculate sight in my older shooting game. And I mean a lot faster. I was able to run the sight check tens of thousands of times without it getting much slower, so I might continue on that project. And if I figure out how to use these alpha pixels I could make all kinds of sweet exploding effects and shit!

Looking forward to your next tutorial, chili. Pretty sure with the amounts of info being loaded into the game it wouldn't hurt to know how to handle the memory better...
ʕ •ᴥ•ʔ

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: How putpixel works? and alpha

Post by chili » June 16th, 2012, 11:23 am

Encoding as we speak LuX. ;)
Fucking GPU isn't working with my encoder though, so it's rendering about half as fast as I'm used to. :x

I looked at your old code for the sight calculation, it was crazy. :lol: Calculating for each pixel basically. It would work better if you generated geometry for the viewing region and then used that as a mask or something, but it would be a real challenge. How did you achieve the speedup you mentioned? There is a lot of room for optimisation, even before touching hardware accel. One easy speedup is creating a separate framebuffer in system memory (just a D3DCOLOR surface array) and then working on that buffer and copying it to the hardware backbuffer at the end of every frame. Big speedup there when I tried it, and if using alpha the speedup would be even huger. 8-)
Chili

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: How putpixel works? and alpha

Post by LuX » June 16th, 2012, 12:08 pm

My new calculation doesn't use much geometry. Or I mean it only has to calculate four times using cos and sin, but those only need to be done once for each frame, rather than multiple times for each new sight check. The new one calculates four points in an X formation, then using vector cross point calculation I calculate if the target is on the right side of both "lines" in the X, and if it is, it's in sight.

For walls I use an all new calculation which is a lot shorter and faster and doesn't cause an exception when the player is on the left side of the wall. Basically I just calculate the players position on the wall. Eg. the eight possible spots you could be around the wall, and then assume the correct left and right corners for the walls. Then without having to use geometry I just take the wall corners for my calculations.

The old one calculated the angle of all corner and then assumed the smallest and largest ones to be the "edge" corners. This of course made it a problem if you were on the left side of the wall where the angles would be the ones in between.

All in all the amount of calculations needed is a lot smaller and I pretty much use only subtractions and multiplying which makes it a lot faster. To check sight without walls its two simple calculations! And for each wall its three calculations more and all calculations are light in terms of calculation types.
Attachments
Sight.png
Sight.png (15.61 KiB) Viewed 3456 times
ʕ •ᴥ•ʔ

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: How putpixel works? and alpha

Post by Asimov » June 16th, 2012, 12:16 pm

Hi Lux,

That looks terribly complicated LOL. I will be needing to check at a pixel level on mine soon. When the missiles are coming down, they get destroyed by the white explosions. The game will start with a few missiles coming down and then slowly increase. There are also bombs which get pushed away from the white explosion rather than getting destroyed. The only way to destroy bombs is to fire missiles either side.

It will be slow going, as I am supposed to be building a huge town in 3D for a game, and I have neglected it since the programming LOL.

Once I have got my sprite class working it will be able to run single sprites, animated sprites and animated sprites that only play once, as in explosions, or timed animation.

Asimov
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

Post Reply