forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Alpha blending to draw buffer?

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
ne0h



Joined: 21 Feb 2008
Posts: 386

PostPosted: Fri Dec 18, 2009 6:48 am    Post subject: Alpha blending to draw buffer? Reply with quote

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

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(y = 0; y < height; y++, data += skipX)
    {
        for(x = 0; x < width; x++, data++)
            *data = Color;
    }
}

There's a way to do the same thing but with alpha?
Thanks,

ne0h
_________________
Get Xplora!
Back to top
View user's profile Send private message
a_noob



Joined: 17 Sep 2006
Posts: 97
Location: _start: jr 0xDEADBEEF

PostPosted: Fri Dec 18, 2009 10:36 am    Post subject: Reply with quote

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

alpha blending would be something like.

Code:

inline unsigned int RGBA(unsigned int R, unsigned int G, unsigned int B, unsigned int A)
{
   return (unsigned int)((A & 0xFF) << 24 |
                    (R & 0XFF) << 16 |
                    (G & 0XFF) << 8 |
                    (B & 0XFF));
}

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

_________________
Code:
.øOº'ºOø.
'ºOo.oOº'
Back to top
View user's profile Send private message AIM Address MSN Messenger
ne0h



Joined: 21 Feb 2008
Posts: 386

PostPosted: Sat Dec 19, 2009 1:59 am    Post subject: Reply with quote

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

XgeFillScreenRect(0x50FF0000, X, Y, Width, Height);

i won't get an rectange with trasparency....
_________________
Get Xplora!
Back to top
View user's profile Send private message
carl0sgs



Joined: 10 Dec 2009
Posts: 41

PostPosted: Sat Dec 19, 2009 3:30 am    Post subject: Reply with quote

I think the function that a_noob wrote is for blending two colors.
Like this:
Code:
mixedColor = alphaBlend(RGB(100,200,50), RGB(200,100,150));


Should return the resulting color.

So you would have this function (i think):

Code:
unsigned int RGBA(unsigned int R, unsigned int G, unsigned int B, unsigned int A)
{
   return (unsigned int)((A & 0xFF) << 24 |
                    (R & 0XFF) << 16 |
                    (G & 0XFF) << 8 |
                    (B & 0XFF));
}

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

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(y = 0; y < height; y++, data += skipX)
    {
        for(x = 0; x < width; x++, data++)
            *data = alphaBlend(Color,data);//We put the mixed color
    }
}


I hope that works!
Back to top
View user's profile Send private message Visit poster's website
ne0h



Joined: 21 Feb 2008
Posts: 386

PostPosted: Sat Dec 19, 2009 3:53 am    Post subject: Reply with quote

O_O
omg...
Anyway, I'll try...
_________________
Get Xplora!
Back to top
View user's profile Send private message
carl0sgs



Joined: 10 Dec 2009
Posts: 41

PostPosted: Sat Dec 19, 2009 4:06 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Visit poster's website
ne0h



Joined: 21 Feb 2008
Posts: 386

PostPosted: Sat Dec 19, 2009 4:37 am    Post subject: Reply with quote

Nevermind, resolved...
But now I get something like:

There's a way to do something like this funct but with only one color?
Code:

sceGuTexImage

Like a monocrome texture?
_________________
Get Xplora!
Back to top
View user's profile Send private message
carl0sgs



Joined: 10 Dec 2009
Posts: 41

PostPosted: Sat Dec 19, 2009 5:13 am    Post subject: Reply with quote

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:
#define RGBA(r, g, b, a) ((r)|((g)<<8)|((b)<<16)|((a)<<24)) //Use this instead of the other function

void blitAlphaImageToImage(int sx, int sy, int width, int height, Image* source, int dx, int dy, Image* destination)//With alpha
{
   // TODO Blend! - Done! xD
   Color* destinationData = &destination->data[destination->textureWidth * dy + dx];
   int destinationSkipX = destination->textureWidth - width;
   Color* sourceData = &source->data[source->textureWidth * sy + sx];
   int sourceSkipX = source->textureWidth - width;
   int x, y;
   for (y = 0; y < height; y++, destinationData += destinationSkipX, sourceData += sourceSkipX) {
      for (x = 0; x < width; x++, destinationData++, sourceData++) {
         Color color = *sourceData;
         *destinationData = alphaBlend(*destinationData,color);
      }
   }
}


This is what works for me, I haven't tested the rectangle function, sorry :-S
Back to top
View user's profile Send private message Visit poster's website
carl0sgs



Joined: 10 Dec 2009
Posts: 41

PostPosted: Sat Dec 19, 2009 5:16 am    Post subject: Reply with quote

ne0h wrote:
Nevermind, resolved...
But now I get something like:

There's a way to do something like this funct but with only one color?
Code:

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
Back to top
View user's profile Send private message Visit poster's website
ne0h



Joined: 21 Feb 2008
Posts: 386

PostPosted: Sat Dec 19, 2009 5:20 am    Post subject: Reply with quote

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
_________________
Get Xplora!
Back to top
View user's profile Send private message
carl0sgs



Joined: 10 Dec 2009
Posts: 41

PostPosted: Sat Dec 19, 2009 5:31 am    Post subject: Reply with quote

I think you should change the alpha values
Back to top
View user's profile Send private message Visit poster's website
a_noob



Joined: 17 Sep 2006
Posts: 97
Location: _start: jr 0xDEADBEEF

PostPosted: Sat Dec 19, 2009 6:55 am    Post subject: Reply with quote

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

unsigned int alphaBlend(unsigned int a, unsigned int b){
   float aa = 255.f/(a>>24&0xff);// A colors alpha channel as a float 0 - 1
   float ba = 1.f-(255.f/(b>>24&0xff));// B colors alpha channel as a float 0 - 1
   
   unsigned char ar = aa * (a&0xff);//alpha influenced
   unsigned char ag = aa * (a>>8&0xff);
   unsigned char ab = aa * (a>>16&0xff);
   
   unsigned char br = ba * (b&0xff);//alpha influenced
   unsigned char bg = ba * (b>>8&0xff);
   unsigned char bb = ba * (b>>16&0xff);
   
   return RGBA(ar+br,ag+bg,ab+bb,(a+b)*255);
   
}
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:
unsigned int RGBA(unsigned int R, unsigned int G, unsigned int B, unsigned int A)
{
   return (unsigned int)((A & 0xFF) << 24 | (B & 0XFF) << 16 | (G & 0XFF) << 8 | (R & 0XFF));
}

_________________
Code:
.øOº'ºOø.
'ºOo.oOº'
Back to top
View user's profile Send private message AIM Address MSN Messenger
carl0sgs



Joined: 10 Dec 2009
Posts: 41

PostPosted: Sat Dec 19, 2009 7:44 am    Post subject: Reply with quote

Good!
Thanks for the updated function ;-)
Back to top
View user's profile Send private message Visit poster's website
Raphael



Joined: 17 Jan 2006
Posts: 646
Location: Germany

PostPosted: Sat Dec 19, 2009 8:06 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Visit poster's website
ne0h



Joined: 21 Feb 2008
Posts: 386

PostPosted: Sun Dec 20, 2009 3:48 am    Post subject: Reply with quote

I would like to use sceGu, but how?
I've never used very much sceGu...
_________________
Get Xplora!
Back to top
View user's profile Send private message
Raphael



Joined: 17 Jan 2006
Posts: 646
Location: Germany

PostPosted: Sun Dec 20, 2009 5:30 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Visit poster's website
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Sun Dec 20, 2009 1:38 pm    Post subject: Reply with quote

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/source/browse/trunk/lockdowningame/main.c
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group