Alpha blending to draw buffer?

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Alpha blending to draw buffer?

Post by ne0h »

It's that possible?
That's the function that I'm using:

Code: Select all

void XgeFillScreenRect(XgeColor Color, int x0, int y0, int width, int height)
{
    int skipX = PSP_LINE_SIZE - width;
    int x, y;
    XgeColor* data = XgeGetVramDrawBuffer() + x0 + y0 * PSP_LINE_SIZE;
    for&#40;y = 0; y < height; y++, data += skipX&#41;
    &#123;
        for&#40;x = 0; x < width; x++, data++&#41;
            *data = Color;
    &#125;
&#125;
There's a way to do the same thing but with alpha?
Thanks,

ne0h
a_noob
Posts: 97
Joined: Sun Sep 17, 2006 8:33 am
Location: _start: jr 0xDEADBEEF

Post by a_noob »

Its possible but it would be much better to use the GU.

alpha blending would be something like.

Code: Select all

inline unsigned int RGBA&#40;unsigned int R, unsigned int G, unsigned int B, unsigned int A&#41;
&#123;
	return &#40;unsigned int&#41;&#40;&#40;A & 0xFF&#41; << 24 | 
						  &#40;R & 0XFF&#41; << 16 | 
						  &#40;G & 0XFF&#41; << 8 |
						  &#40;B & 0XFF&#41;&#41;;
&#125;

unsigned int alphaBlend&#40;unsigned int a, unsigned int b&#41;&#123;
	float aa = 255.f/&#40;a>>24&0xff&#41;;// A colors alpha channel as a float 0 - 1
	float ba = 255.f/&#40;b>>24&0xff&#41;;// B colors alpha channel as a float 0 - 1
	
	char ar = aa * &#40;a&0xff&#41;;//alpha influenced
	char ag = aa * &#40;a>>8&0xff&#41;;
	char ab = aa * &#40;a>>16&0xff&#41;;
	
	char br = ba * &#40;b&0xff&#41;;//alpha influenced
	char bg = ba * &#40;b>>8&0xff&#41;;
	char bb = ba * &#40;b>>16&0xff&#41;;
	
	return RGBA&#40;ar+br,ag+bg,ab+bb,&#40;a+b&#41;*255&#41;;
	
&#125;

Code: Select all

.øOº'ºOø.
'ºOo.oOº'
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

Excuse me, I didn't explain my problem as I wanted...
When I use the function with alpha like this:

Code: Select all

XgeFillScreenRect&#40;0x50FF0000, X, Y, Width, Height&#41;;
i won't get an rectange with trasparency....
carl0sgs
Posts: 33
Joined: Thu Dec 10, 2009 3:51 am
Contact:

Post by carl0sgs »

I think the function that a_noob wrote is for blending two colors.
Like this:

Code: Select all

mixedColor = alphaBlend&#40;RGB&#40;100,200,50&#41;, RGB&#40;200,100,150&#41;&#41;;
Should return the resulting color.

So you would have this function (i think):

Code: Select all

unsigned int RGBA&#40;unsigned int R, unsigned int G, unsigned int B, unsigned int A&#41;
&#123;
   return &#40;unsigned int&#41;&#40;&#40;A & 0xFF&#41; << 24 |
                    &#40;R & 0XFF&#41; << 16 |
                    &#40;G & 0XFF&#41; << 8 |
                    &#40;B & 0XFF&#41;&#41;;
&#125;

unsigned int alphaBlend&#40;unsigned int a, unsigned int b&#41;&#123;
   float aa = 255.f/&#40;a>>24&0xff&#41;;// A colors alpha channel as a float 0 - 1
   float ba = 255.f/&#40;b>>24&0xff&#41;;// B colors alpha channel as a float 0 - 1
   
   char ar = aa * &#40;a&0xff&#41;;//alpha influenced
   char ag = aa * &#40;a>>8&0xff&#41;;
   char ab = aa * &#40;a>>16&0xff&#41;;
   
   char br = ba * &#40;b&0xff&#41;;//alpha influenced
   char bg = ba * &#40;b>>8&0xff&#41;;
   char bb = ba * &#40;b>>16&0xff&#41;;
   
   return RGBA&#40;ar+br,ag+bg,ab+bb,&#40;a+b&#41;*255&#41;;
   
&#125; 

void XgeFillScreenRect&#40;XgeColor Color, int x0, int y0, int width, int height&#41;
&#123;
    int skipX = PSP_LINE_SIZE - width;
    int x, y;
    XgeColor* data = XgeGetVramDrawBuffer&#40;&#41; + x0 + y0 * PSP_LINE_SIZE;
    for&#40;y = 0; y < height; y++, data += skipX&#41;
    &#123;
        for&#40;x = 0; x < width; x++, data++&#41;
            *data = alphaBlend&#40;Color,data&#41;;//We put the mixed color
    &#125;
&#125;
I hope that works!
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

O_O
omg...
Anyway, I'll try...
carl0sgs
Posts: 33
Joined: Thu Dec 10, 2009 3:51 am
Contact:

Post by carl0sgs »

OK, it works!
Thanks to both of you, you helped me a lot :-P
Now I can blend alpha images in multitasking :-)

@ne0h Good luck with Xplora, it is a really useful program :-D

Thanks again,
Carlos
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

Nevermind, resolved...
But now I get something like:
Image
There's a way to do something like this funct but with only one color?

Code: Select all

sceGuTexImage
Like a monocrome texture?
carl0sgs
Posts: 33
Joined: Thu Dec 10, 2009 3:51 am
Contact:

Post by carl0sgs »

OK
You have to mix two colors because you want to put one color over the other one.

I don't know why that is not working, I let you this piece of code:

Code: Select all

#define RGBA&#40;r, g, b, a&#41; &#40;&#40;r&#41;|&#40;&#40;g&#41;<<8&#41;|&#40;&#40;b&#41;<<16&#41;|&#40;&#40;a&#41;<<24&#41;&#41; //Use this instead of the other function

void blitAlphaImageToImage&#40;int sx, int sy, int width, int height, Image* source, int dx, int dy, Image* destination&#41;//With alpha
&#123;
	// TODO Blend! - Done! xD
	Color* destinationData = &destination->data&#91;destination->textureWidth * dy + dx&#93;;
	int destinationSkipX = destination->textureWidth - width;
	Color* sourceData = &source->data&#91;source->textureWidth * sy + sx&#93;;
	int sourceSkipX = source->textureWidth - width;
	int x, y;
	for &#40;y = 0; y < height; y++, destinationData += destinationSkipX, sourceData += sourceSkipX&#41; &#123;
		for &#40;x = 0; x < width; x++, destinationData++, sourceData++&#41; &#123;
			Color color = *sourceData;
			*destinationData = alphaBlend&#40;*destinationData,color&#41;;
		&#125;
	&#125;
&#125;
This is what works for me, I haven't tested the rectangle function, sorry :-S
carl0sgs
Posts: 33
Joined: Thu Dec 10, 2009 3:51 am
Contact:

Post by carl0sgs »

ne0h wrote:Nevermind, resolved...
But now I get something like:
Image
There's a way to do something like this funct but with only one color?

Code: Select all

sceGuTexImage
Like a monocrome texture?
No idea of how did you got that :-S
It is not possible to do that alpha blending without using two colors, maybe with the GU, but that would do the same thing really.

Sorry :-S
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

I don't use images but fillscreenrect only for memory purposes...
I want to find a way to draw an alpha rectangle on the screen,
just like sceGuTexImage, but without loading something like 1MB of image...
Anyway sorry for misunderstanding with the two colors and thanks both.

ne0h
carl0sgs
Posts: 33
Joined: Thu Dec 10, 2009 3:51 am
Contact:

Post by carl0sgs »

I think you should change the alpha values
a_noob
Posts: 97
Joined: Sun Sep 17, 2006 8:33 am
Location: _start: jr 0xDEADBEEF

Post by a_noob »

oh its my bad for the b alpha you need to do 1 - the alpha. So

Code: Select all

unsigned int alphaBlend&#40;unsigned int a, unsigned int b&#41;&#123; 
   float aa = 255.f/&#40;a>>24&0xff&#41;;// A colors alpha channel as a float 0 - 1 
   float ba = 1.f-&#40;255.f/&#40;b>>24&0xff&#41;&#41;;// B colors alpha channel as a float 0 - 1 
    
   unsigned char ar = aa * &#40;a&0xff&#41;;//alpha influenced 
   unsigned char ag = aa * &#40;a>>8&0xff&#41;; 
   unsigned char ab = aa * &#40;a>>16&0xff&#41;; 
    
   unsigned char br = ba * &#40;b&0xff&#41;;//alpha influenced 
   unsigned char bg = ba * &#40;b>>8&0xff&#41;; 
   unsigned char bb = ba * &#40;b>>16&0xff&#41;; 
    
   return RGBA&#40;ar+br,ag+bg,ab+bb,&#40;a+b&#41;*255&#41;; 
    
&#125; 
should do the trick.

I do advise switching to the GU as this is terribly slow on the CPU, you need to do this calculation for each pixel :/ where as the GPU has built in hardware blending procedures which are extremely fast.

-=EDIT=-

Oh crap and the psp uses 0xAABBGGRR not 0xAARRGGBB so this is the updated RGBA func

Code: Select all

unsigned int RGBA&#40;unsigned int R, unsigned int G, unsigned int B, unsigned int A&#41; 
&#123; 
   return &#40;unsigned int&#41;&#40;&#40;A & 0xFF&#41; << 24 | &#40;B & 0XFF&#41; << 16 | &#40;G & 0XFF&#41; << 8 | &#40;R & 0XFF&#41;&#41;; 
&#125; 

Code: Select all

.øOº'ºOø.
'ºOo.oOº'
carl0sgs
Posts: 33
Joined: Thu Dec 10, 2009 3:51 am
Contact:

Post by carl0sgs »

Good!
Thanks for the updated function ;-)
User avatar
Raphael
Posts: 646
Joined: Tue Jan 17, 2006 4:54 pm
Location: Germany
Contact:

Post by Raphael »

Just for note: It should be 1-aalpha, if anything, for the common alpha blending behaviour.
Still, this software solution is stupid for anything else but learning how alpha blending works.
<Don't push the river, it flows.>
http://wordpress.fx-world.org - my devblog
http://wiki.fx-world.org - VFPU documentation wiki

Alexander Berl
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

I would like to use sceGu, but how?
I've never used very much sceGu...
User avatar
Raphael
Posts: 646
Joined: Tue Jan 17, 2006 4:54 pm
Location: Germany
Contact:

Post by Raphael »

pspsdk/samples/gu is your first source of information. Afterwards, there are lots of open source graphical engines available, that you can learn from, like OSlib or triEngine to name only two.
Using gu is very simple. It's just a matter of a little time investment to learn the concepts of a state machine and a very basic graphics api. If you have some pre-knowledge on OpenGL it's easy as cake.
<Don't push the river, it flows.>
http://wordpress.fx-world.org - my devblog
http://wiki.fx-world.org - VFPU documentation wiki

Alexander Berl
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

For software, you need to check the display mode and calculate accordingly.

Use the getdisplaymode or whatever first to read the pixelformat. See the DrawImage() function.

http://code.google.com/p/lockdownpack/s ... ame/main.c
Post Reply