How putpixel works? and alpha

The Partridge Family were neither partridges nor a family. Discuss.
User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

How putpixel works? and alpha

Post by Asimov » June 11th, 2012, 6:01 pm

Hi Chilli,

Now I am trying to get alpha to work. I do store the alpha value on loading the image using

surface[ x + y * bitmap.GetWidth() ] = D3DCOLOR_ARGB(pixel.GetA(), pixel.GetR(), pixel.GetG(),pixel.GetB());

And then in put pixel the surface is sent via c here

((D3DCOLOR*)backRect.pBits)[ x + (backRect.Pitch >> 2) * y ] = c;

But I get no alpha tranparency on the screen

When putpixel is called it sends the colour value of the surface with this
surf[ x + y * width ]);

I don't really understand how this works. I know surf is just a surface stored in memory. I know what x and y are coordinates, but why * it by width. I really don't understand how it works.

Could you explain how
((D3DCOLOR*)backRect.pBits)[ x + (backRect.Pitch >> 2) * y ] = c;
puts a pixel onto the screen.

As far as I know because I haves saved the ARGB values in the surface and sent that surface colour to putpixel there should be transparency.

Could you let me know what I am missing?

I have tried googling backRect.pBits but I don't come up with any information on it, so I suspect it is a variable that could be called anything.

Any help would be appreciated. I have an animated sprite, but it is framed by the black doom LOL.

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

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 12th, 2012, 3:53 am

You have to do the alpha yourself Asimov. Basically, in the putpixel you have to examine the alpha value of the pixel you want to write, and if it is transparent, you do not copy the pixel. You will need to do bitmasking to isolate the alpha channel inside of the D3DCOLOR value.
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 12th, 2012, 9:03 am

Why was I mentioned : -D ?
ʕ •ᴥ•ʔ

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 12th, 2012, 10:24 am

I have no idea what you are talking about LuX.
Chili

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

Re: How putpixel works? and alpha

Post by Asimov » June 12th, 2012, 3:52 pm

Hi Chilli
You have to do the alpha yourself Asimov. Basically, in the putpixel you have to examine the alpha value of the pixel you want to write, and if it is transparent, you do not copy the pixel. You will need to do bitmasking to isolate the alpha channel inside of the D3DCOLOR value.
If I examine the pixel and don't write it, isn't that essentially turning the pixel off, whereas an alpha pixel would have different levels of transparencey from 0 to 255. Or should I have to read the background pixel and the foreground pixel and XOR them together or something. I am clutching at straws now LOL.

I spent the whole weekend googling for answers, with no success at all. The reason I am using a png rather than a jpg or a bmp is the soft transition from opaque to transparent.

Perhaps I should study Ghillies code. I think he said he had an alpha working properly.

I know it has to be possible to get an alpha to display properly as I did it with SDL and C++, although that is a game framework, so it isn't quite the same.

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

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

Re: How putpixel works? and alpha

Post by LuX » June 12th, 2012, 5:02 pm

I've seen a lot of tutorials for alpha mapping, where you have two images. One normal and the other has on a gray scale the transparency. But it's still not the same I guess...
ʕ •ᴥ•ʔ

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

Re: How putpixel works? and alpha

Post by Asimov » June 12th, 2012, 5:10 pm

Hi Lux,

I found this page
http://wiki.yoyogames.com/index.php/Fea ... ed_sprites

which looks interesting, but is only for smoothing edges
and this which is alphablend in the gdi
http://msdn.microsoft.com/en-us/library ... 85%29.aspx

Going to study this. I think you have to take the background pixel and mask it with the forground pixel, useing the alpha stored in the Get.A().

Not even sure how to read a screen pixel. I know I have the x,y coordinates anyway, so I am nearly there on a solution I think LOL.

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

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

Re: How putpixel works? and alpha

Post by Asimov » June 12th, 2012, 9:08 pm

Hi Chilli,

Another day of failed googling LOL. But I keep coming across the same bit of code, and I don't know if it is applicable at all

device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
device->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);
device->SetRenderState(D3DRS_ALPHAREF, (DWORD)8);
device->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);

I don't know exactly what it does or where it is supposed to go or if it would help with the problem or not.

I still think that maybe XORing the back pixel with the overlaying alpha might be the answer, but I haven't found anything like this.

Found this website, but I am not sure how to implement it
http://takinginitiative.net/2010/04/09/ ... -blending/

It also has an interesting equation
final color = src color * src alpha + dest color * (1-src alpha)

Oh and I am trying to understand this code too
http://www.directxtutorial.com/tutorial ... aspx#still
Looks the most promising.

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

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 13th, 2012, 3:39 am

Asimov wrote:Not even sure how to read a screen pixel. I know I have the x,y coordinates anyway, so I am nearly there on a solution I think LOL.
You can use the pBits pointer to read the pixels from the back buffer. It's exactly the reverse of putting a pixel.
Asimov wrote:If I examine the pixel and don't write it, isn't that essentially turning the pixel off, whereas an alpha pixel would have different levels of transparencey from 0 to 255. Or should I have to read the background pixel and the foreground pixel and XOR them together or something. I am clutching at straws now LOL.
I thought you were simply interested in transparency. If you want translucency, you need to read the back buffer and do alpha blending. Much more difficult and slower, but still very doable.
Asimov wrote:Found this website, but I am not sure how to implement it
http://takinginitiative.net/2010/04/09/ ... -blending/

It also has an interesting equation
final color = src color * src alpha + dest color * (1-src alpha)

That's the equation you need for alpha blending (more accurately, that is one equation that will get you what you want).
Asimov wrote:But I keep coming across the same bit of code, and I don't know if it is applicable at all

device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
device->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);
device->SetRenderState(D3DRS_ALPHAREF, (DWORD)8);
device->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
This will not get you anywhere right now. Later on when we are doing textured quads we will use this. Even later will will use something better than this (pixel shader).
Chili

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

Re: How putpixel works? and alpha

Post by Asimov » June 13th, 2012, 8:28 pm

Hi Chilli,

I have not been successful, but I am getting closer. 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.

Anyway I think you can get the RGB values with GetRValue(c);

Just for fun I put GetRValue into the alpha variable because I didn't know how to get the alpha out.
Anyway what I have done is masked it a little, as you can see the outline of the sprite.

I believe if I keep digging at this I will hopefully come up with a solution, it will be cool for effects anyway heh heh.

Here is my code so far. Yes I know it is so wrong, but that is how to learn I suppose heh heh.

Code: Select all

void D3DGraphics::PutPixelAlpha( int x,int y,D3DCOLOR c )
{	

	// this sent pixel to screen from sprite
	assert( x >= 0 );
	assert( y >= 0 );
	assert( x < 800 );
	assert( y < 600 );


	//int alpha;
	// Gets pixel from the backbuffer
	int backPixel;
	D3DCOLOR FinalColour;
	backPixel =((D3DCOLOR*)backRect.pBits)[ x + (backRect.Pitch >> 2) * y ];

	int alpha = GetRValue(c);// This line is totally wrong, but it gives a cool effect. I don't know how to get the alpha value yet
//This is the formula I pasted in the last  post
	FinalColour = c * alpha + backPixel * (1 - alpha);
	

	((D3DCOLOR*)backRect.pBits)[ x + (backRect.Pitch >> 2) * y ] = FinalColour;
}
Asimov
Attachments
alphamask.jpg
alphamask.jpg (11.03 KiB) Viewed 3845 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

Post Reply