Welcome! Log In Create A New Profile

Advanced

Question about libwiisprite transparency

Posted by Arikado 
Question about libwiisprite transparency
August 01, 2008 02:38AM
I'm working on my first game for wii. So I've decided to work with the libwiisprite library. So far I've got the menu for my game working. Anyways, I was wondering about how to implement the transparancy for my sprites. I know that there is a SetTransparency() for each sprite in the Sprite class but I can't figure out how it works. Is there just a color I can use in my png images that will show up transparent? If not, how do you use the SetTransparancy()?



Edited 2 time(s). Last edit at 08/01/2008 02:48AM by Arikado.
Re: Question about libwiisprite transparency
August 01, 2008 05:25AM
Quote from the Documentation:

Quote
libwiisprite
void wsp:: Sprite:: SetTransparency ( u8 alpha )

Sets the transparency of the sprite.

Parameters:

alpha Sets the transparency. Has a range from 0x00 (invisible) to 0xFF (fully visible)

This sets the alpha value for an image although i don't know if it automatically handles alpha blending, if you want alpha blending and this function doesn't support it, theres some simple math for calculating the pixel's colour channel values. Loosely, the alpha component of the RGBA quad represents the opaqueness of a surface. An alpha value of 0xFF (255) means the colour is completely opaque, and an alpha value of 0x00 (0) means the colour is completely transparent. Of course, the value of the alpha component is fairly meaningless unless you actually activate the alpha blending step. If you want, you can set things up a different way, such as having 0x00 (0) mean that the colour is completely opaque. The meaning of alpha is dependent on how you set up the alpha blending step. The equation that governs the behavior of the blending performed is defined as follows:

final colour source source blend factor destination destination blend factor

For example, say you want an alpha blending equation to do nothing to just draw the pixel from the triangle and not consider what was already there at all (this is the default behavior of the Direct3D Rasterizer). An equation that would accomplish this would be:

final color  source 1.0 destination 0.0

As you can see, the destination blending factor is 0 and the source blending factor is 1. This reduces the equation to:

final colour source

A second example would be if you wanted to multiply the source and destination components together before writing them to the frame buffer. This initially would seem difficult, as in the above equation they are only added together. However, the blending factors defined need not be constants; they can in fact be actual colour components (or inverses thereof). The equation setup would be:

final colour  source 0.0 destination source

In this equation, the destination blend factor is set to the source colour itself. Also, since the source blend factor is set to zero, the left-hand side of the equation drops away and you are left with:

final colour destination source

Hope that helped.



Edited 1 time(s). Last edit at 08/01/2008 05:27AM by WiiPhlex.
Re: Question about libwiisprite transparency
August 01, 2008 03:54PM
Oww. it is a little bit dizzy around my eye's.

But ontopic, I believe i'd hear Libwiisprite did support the png's alpha channel, I'm gonna to take a test today, I'll report it here.



Edited 1 time(s). Last edit at 08/01/2008 04:04PM by Dykam.
Re: Question about libwiisprite transparency
August 01, 2008 11:12PM
WiiPhlex:

Thank you for your help. I was wondering if you were implying that I can use your equations to make one color in a sprite rendered completely transparent when drawn. For example, back when I used to program with the Allegro game engine, all of the Pink (255, 0, 255) pixels in a sprite were drawn completely invisible. While I don't really care which color is used to achieve the same transparent ability, it would be nice to know if that functionality was possible. I'll be honest, until your post I really knew next to nothing about the u8 alpha parameter in SetTransparency(). I've been experimenting with it a bit today, and noticed that (like you said before) using:

sprite1->SetTransperancy(0); makes my entire Sprite transparent.

and using:

sprite1->SetTransperancy(0xFF); makes my entire Sprite fully visible.

Any further help in achieving the effect of making only one color in a sprite completely transparent would be much appreciated.
Re: Question about libwiisprite transparency
August 02, 2008 03:24AM
The method you described that you used in the Allegro is called a colour key, it checks each pixels RGB value (8bit channels usually) and if that pixel has a value of (255,0,255) for example (this is a common colour as it isn't often used in games) then the video card will not render that part of the texture/sprite/image etc.
Now I've been looking at the libwiisprite documentation and in the wsp:: Sprite Class Reference there are several functions that relate to pixel locations on the sprite, its difficult to tell exactly what they do as they description is less than indepth to say the least, but if you can get a pixel and its information then a brute force method could be employed, heres some psuedo code

int pink[3] = { 255, 0, 255 }

void colourKey(pointerTo *image, image.pixelCoord(), image.pixelsRGBValue[ ])
{
for (int i = 0; i < image.numberOfPixelsOnImage; ++i)
{
if image.pixelsRGBValue [ ] != pink[ ]
{
continue
}

elseif image.pixelsRGBValue[ ] == pink[ ]
{
image.pixelCoord()->SetTransperancy(0);
moveToNextPixel()
}
}
}

This method would be very slow as its brute force, there are far better ways of going about doing it you may want to look into them as well as being able to get values like the first pixels coordinate (top left) then move right across the top of the image, then down to the next row on the left and do it again until you've finished.Sorry I couldn't give you a solid answer on the matter, I'm sure if you look carefully through the documentation and make some of your own functions then you should be able to get it.
Re: Question about libwiisprite transparency
August 02, 2008 04:50AM
WiiPhlex:

Thank you once again and I'll do some more research. If I write a working function for this purpose I'll post it here.

UPDATE: I found Allegro's source code here I'm surgically picking through it and attempting to port its transparency functions.

UPDATE 2: I just finished making a test program in which you guide a sprite over a multi-colored background. So far the pixels I want to be transparent still are visible. But at least I'm making progress. If anyone has any ideas, please feel free to post.

UPDATE 3: Actually, Allegro is way to complicated to just port some of it over. There are way to many files, and unfortunately to many classes and structs as well. It was made for a PC and unless someone else wants to devote time to it, thats where its going to stay. Having said that, I've learned a lot about transparency by looking at its source. Time for the brute coding part I guess. I'm gonna do just a little bit of search engine research on my own before getting started though. I'll post the working functions here when they're done however. If anyone can beat me to it, I would thank you to do the same. If anyone knows of any good places full of RGB and transparency info, please post the links here. Well... Time to get to work!



Edited 3 time(s). Last edit at 08/02/2008 11:23PM by Arikado.
Re: Question about libwiisprite transparency
August 03, 2008 05:22AM
It has just occured to me that if you are only using a 2D plane, theres little point in using a colour key (that I can see). Why not just delete the pixels you don't want? This would save a few bytes of memory, a few cycles printing the pixels, as well as all the hastle an computation of iterating through all of the pixels and testing them to see if its colour matches when deleting them will have exactly the same effect as making its alpha value == to 0. If you wanted a colour key for any particular reason thats cool, but other wise it is likely a waste of time and energy.
Re: Question about libwiisprite transparency
August 03, 2008 05:28AM
//Function Prototypes

void MakeTransparent(Image* image, int r, int g, int b);
bool CheckPixel(Image* image, int x, int y, int r, int g, int b);

//Actual Functions (or most of them anyway)

void MakeTransparent(Image* image, int r, int g, int b){

   int width = image->GetWidth();

   int height = image->GetHeight();

    for(int y = 0; y <= height; y++){

    for(int x = 0; x <= width; x++){

     if(CheckPixel(image, x, y, r, g, b))
     //Make that pixel on the image transparent for the rest of the program

     }
    }
   }

bool CheckPixel(Image* image, int x, int y, int r, int g, int b){

 //Find the coordinte on the image (determined by x and y)
 //Determine the numeric value of its Red, Blue, and Green hues
 
 //If they are same as r, g, and b
 return true;

 else

 return false;

 }

 //Example of calling the function

 MakeTransparent(hero, 255, 0, 255);
 //This would make all Pink pixels in the Image hero transparent


Looking through the libwiisprite documentation, I've discovered that the image class does hold pixel data (see image.h), but I can't figure out how to manipulate it. Also I think I may need some additional functions. Any assistance on supplying more information or helping me turn the commented lines into actual code would be truly appreciated.



Edited 2 time(s). Last edit at 08/03/2008 04:44PM by Arikado.
Re: Question about libwiisprite transparency
August 03, 2008 06:01AM
Read my previous reply then consider if you want to go ahead with it.
Re: Question about libwiisprite transparency
August 03, 2008 03:53PM
I like the idea behind your previous reply, but I don't see a function for it in libwiisprite. Unfortunately, I believe the method you described would also be rather tedious for large sprite sheets. Having said that, it may just be easier to just delete the pixels if the color checks out. If I can get my function working properly, you need only call it once and you can make any color of pixels transparent on an image before assigning the image to your sprite.

UPDATE: I'm doing a lot of hard coding right now. Its no longer a matter of "if" but "when"



Edited 2 time(s). Last edit at 08/03/2008 10:39PM by Arikado.
Re: Question about libwiisprite transparency
August 04, 2008 04:10AM
I've only ever encounted colour key's whilst programming in 3D, so it would almost definately be easier to delete the pixels, but i know what its like to want to do something for the challenge of it. Hope to see a release of the function soon, if you need help with optimisation algorithms there are several functions in the DirectSDK that would be useful to look at.
Re: Question about libwiisprite transparency
August 04, 2008 05:36AM
Really? I'll take a look at it thank you.

EDIT: Oh, and I e-mailed chaosteil what I've got so far. I'm hoping for some friendly correspondance and maybe a word or two of advice. Actually, I'm really just hoping he sends me the source for libwiisprite.



Edited 1 time(s). Last edit at 08/04/2008 05:42AM by Arikado.
Re: Question about libwiisprite transparency
August 04, 2008 05:42AM
Its in this function

HRESULT D3DXCreateTextureFromFileEx(LPDIRECT3DDEVICE9 pDevice,
LPCTSTR pSrcFile,
UINT Width,
UINT Height,
UINT MipLevels,
DWORD Usage,
D3DFORMAT Format,
D3DPOOL Pool,
DWORD Filter,
DWORD MipFilter,
D3DCOLOR ColorKey,
D3DXIMAGE_INFO* pSrcInfo,
PALETTEENTRY* pPalette,
LPDIRECT3DTEXTURE9* ppTexture);

Notice the D3DCOLOR ColorKey bit? well it would be filled in as such to make pink 'invisible'

D3DXCreateTextureFromFileEx(d3ddev, // the device pointer
L"BlueCircle.png", // the file name
D3DX_DEFAULT, // default width
D3DX_DEFAULT, // default height
D3DX_DEFAULT, // no mip mapping
NULL, // regular usage
D3DFMT_A8R8G8B8, // 32-bit pixels with alpha
D3DPOOL_MANAGED, // typical memory handling
D3DX_DEFAULT, // no filtering
D3DX_DEFAULT, // no mip filtering
D3DCOLOR_XRGB(255, 0, 255), // the hot-pink color key
NULL, // no image info struct
NULL, // not using 256 colors
&texture_1); // load to texture_1


If you can find some info about that function and the D3DCOLOR parameter it would help you out a lot.
Re: Question about libwiisprite transparency
August 04, 2008 05:45AM
So would I need to bother with its hexadecimal equivalent (FF00FF)? That was what I thought I needed to use to perform the "check". That or its binary, at least.

Actually, my brain has been running around in circles all day. I'm this close to blowing out the little lightbulb in my brain. The function you showed me looks a little complex, but it can't possibly be worse than what I've been trying. I'll give it a shot.



Edited 1 time(s). Last edit at 08/04/2008 05:53AM by Arikado.
Re: Question about libwiisprite transparency
August 04, 2008 05:53AM
Depends on the function, in the example shown above, direct3d (9 in this case), D3DCOLOR is declared as a 32 bit colour display, 8 bits R, 8 bits green, 8 bits blue and 8 bits unused alpha (thats what the X is in place of). Because of this parameter it is automatically converted for you, something that I don't know you will be able to do. You could pass an int array with 3 member to represent r,g,b then just use a conversion formula to do the rest, then pass that to the display in a 32 bit channel (make sure to not lose 8 bits alpha along the way though.
Easier to go hex direct though.
Re: Question about libwiisprite transparency
August 04, 2008 05:56AM
What I'm working on now, is writing something that returns the red, blue, and green hue values of a pixel. Or something (like an algorithm) that returns that returns a pixels hexadecimal color.

EDIT: I just remembered; don't Direct3d and Directx only work on the xbox, xbox360 and windows operating system?



Edited 1 time(s). Last edit at 08/04/2008 05:59AM by Arikado.
Re: Question about libwiisprite transparency
August 04, 2008 05:58AM
What do you do with that information once you have it?
Re: Question about libwiisprite transparency
August 04, 2008 06:14AM
I compare the RGB or hexadecimal with the one originally passed through calling the function. If they are the same, I make the pixel invisible for the rest of the program. It sounds simple in theory, but I'm really stumped on writing the part that retrieves the pixels data.
Re: Question about libwiisprite transparency
August 04, 2008 06:17AM
Depending on the image format, it could be easy or damn near impossible. There are several functions in libwiisprite in the sprite class that are to do with testing pixels, not sure if they will be any help but I recall seeing them a couple days ago in the doc's.
Re: Question about libwiisprite transparency
August 04, 2008 06:27AM
The format is .png I'll look at those docs again too. Hopefully chaosteil will send me his librarys' source soon. I know that the Image class contains pixel data, but there are no functions to do anything with it. With the source, I could hopefully change that.
Sorry, only registered users may post in this forum.

Click here to login