Archived

This topic is now archived and is closed to further replies.

ShadowP13

I don't understand alpha testing apparently!!!

Recommended Posts

ShadowP13    122
Woo hoo!!! I always thought I did, but now that I''m actually trying to use it I relize I don''t. If someone can help me, this is what''s going on. I''ve made this function that will dig through a texture and replace one specified color with another color you specify. Now, I use TextureColorReplace( graphic_Cursor, D3DCOLOR_ARGB( 256, 0, 0, 0), D3DCOLOR_ARGB( 256, 0, 100, 0) ); I replace all of my opaque black pixels with opaque dark green ones (this is how I know my TextureColorReplace works) Infact, I have succesfully changed colors to colors several times with this function to ashure it works, but the above was an example. The proplem is that if I use TextureColorReplace( graphic_Cursor, D3DCOLOR_ARGB( 256, 0, 0, 0), D3DCOLOR_ARGB( 0, 0, 0, 0) ); I would then, in theory, have replace all of my opaque black with completely transparent black... Hower that is absolutely not what I''m doing (whatever it is that it is doing *shrugs*) I''ve been working on this for a long time and I can only think one thing is to blaim... My renderstates. I''ve changed my renderstates several time (referencing 4 books that I own) and I can not get this to work... here''s what there currently set too g_pDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, true); g_pDevice->SetRenderState( D3DRS_ALPHAREF, 0x01); g_pDevice->SetRenderState( D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL); g_pDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCCOLOR); g_pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCCOLOR); Needless to say I have no idea what values these should be set too. I''ve tried a lot of different values and nothing is really working. Does anyone know what''s going on here? (if you need/want more information about my code I''d be happy to supply it :/ )

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
How exactly are you swapping the colours inside TextureColorSwap()?

Share this post


Link to post
Share on other sites
ShadowP13    122
Hehehe, I was afraid that would come up as the code is very messy. I canabalized this from a program I wrote 2 years ago that edits surfaces.

I don''t know the code you use to put this into a textbox... So here it is.

void TextureColorReplace(int GraphicID, DWORD OldColor, DWORD NewColor )
{
if( GraphicID == -1 )
return;
if( !Graphic[GraphicID].Texture )
return;
if( !Graphic[GraphicID].Used )
return;

int OffsetX = 0, OffsetY = 0;

D3DLOCKED_RECT LockedTexture;

if( FAILED( Graphic[GraphicID].Texture->LockRect(0, &LockedTexture, NULL, 0 ) ) )
{
ReportError( "Could not lock during TextureColorReplace" );
return;
}

DWORD* pAlphaData = (DWORD*)LockedTexture.pBits;
LockedTexture.Pitch /=4;

int AlphaOffset = OffsetY * LockedTexture.Pitch + OffsetX;

for( int cy = 0 ; cy < Graphic[GraphicID].Height ; cy++ )
{
for( int cx = 0 ; cx < Graphic[GraphicID].Width ; cx++ )
{
if( pAlphaData[ AlphaOffset ] == OldColor )
{
pAlphaData[ AlphaOffset ] = NewColor;
}
AlphaOffset++;
}
AlphaOffset += LockedTexture.Pitch - Graphic[GraphicID].Width;
}

Graphic[GraphicID].Texture->UnlockRect(0);
}

Share this post


Link to post
Share on other sites
ShadowP13    122
In what I''ve been learning in this forum I could of also used the GetSurfaceLevel function to do this. I don''t think that is what the problem with my program is but I can use that method instead of this if it is.

Share this post


Link to post
Share on other sites
Endurion    5411
256 is not the maximum alpha. It''s one above, or bitwise the same as zero. That way your example with black would replace full transparent black with full transparent black.

Use D3DCOLOR_ARGB( 255 , r, g, b ).

Share this post


Link to post
Share on other sites
What format is your texture? Someone has this trouble a few months back. Their texture was X8R8G8B8, not A8R8G8B8.

SRCBLEND and DESTBLEND are not needed for alpha testing, just alpha blending.

If you have mipmaps, (the posts with GetSurfaceLevel you''ve seen mentioned them), you need to

1. modify ALL levels of the mipmap,
or
2. just level 0, then call D3DXFilterTexture to recreate the other miplevels.
or
3. Create/Load your texture with mipmaps disables (levels==1)

Share this post


Link to post
Share on other sites
DrunkenHyena    805
Alphatesting and alphablending are not the same. ALPHAREF and ALPHAFUNC are for alphatesting, so with the setup you''ve shown anything with an alpha of 0 would fail the test and not get written, BUT you haven''t enabled alphatest so it does nothing.
Alphatesting decides where a pixel will be written based on the alpha value.

Alphablending blends the source and destination together. How much of each is blended is partially controlled by the alpha value. Alphablending uses the SRCBLEND and DESTBLEND states, but you have them set to ignore alpha completely. Rather than SRCCOLOR you want SRCALPHA (INVSRCALPHA for DEST).



Stay Casual,

Ken
Drunken Hyena

Share this post


Link to post
Share on other sites
ShadowP13    122
I understand that alpha blending and testing are different, but I don't really care at the moment as I can't get either working. However, I'm working on everyone's advice at the moment and I'll see what I can get.

Ohh, and the person the mentioned the 256 and 255 thing, your right actually, however it doesn't work that way reasons I can't fathom in this program.

*later* you had a point with 256 = 0 as far as the bit shifts are concerned... that's even wierder (I knew 256 was wrong but it seemed to be working).

Apparently opaque black in my textures = 0,0,0,0 (!?!)
does that mean completly transparent alpha = 255? I thought 0 was the transparent.



[edited by - ShadowP13 on January 15, 2004 2:34:15 PM]

[edited by - ShadowP13 on January 15, 2004 2:34:43 PM]

Share this post


Link to post
Share on other sites
ShadowP13    122
Just changed my renderstates as recomemened. Most of these are things I''ve already had my renderstat set to. No luck. I''m bothered about somthing. When you load a bitmap, isn''t the default value for the alpha value of each pixel 255? If so, why doesn''t a search for 255,0,0,0 find anything, but 0,0,0,0 find all the black in my texture?

Share this post


Link to post
Share on other sites
ShadowP13    122
I use D3DXCreateTextureFromFile and don''t really know what color format my textures are in. I might want to temporarily start using the Ex version so that I have controll over what formats are used. But at the moment I have whatever is default :/

Share this post


Link to post
Share on other sites
ShadowP13    122
Namethatnobodyelsetook!! Your right! That''s all it was, I had to use the EX load inorder to get the color formated I want, after that it all made sence. Thanks.

Share this post


Link to post
Share on other sites