# Making a Surface Transparent

## Recommended Posts

I have a bitmap for crosshairs that I am drawing to the screen using a "LPDIRECT3DSURFACE9" device. I got the backbuffer and loaded the surface with a transparency color. The only problem is when I draw it, no matter what the transparency color is, the part that should be transparent turns up black and blotches out all of the geometry behind it. How can I make it transparent?

##### Share on other sites
The techniques you are looking for are called alpha test and alpha blending.

##### Share on other sites
Oh. I didn't know you could do that with surfaces. Ok thanks I'll try it.

EDIT:

I just tried it and it doesn't change the results. Here is my code:

//Draw the HUD	g_pD3DDevice->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);	g_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);	g_pD3DDevice->SetRenderState(D3DRS_ALPHAREF, 0x01);	//-/Target	crossRect.top=height/2-16;	crossRect.left=width/2-16;	crossRect.bottom=height/2+16;	crossRect.right=width/2+16;	g_pD3DDevice->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO, &backbuffer);	g_pD3DDevice->StretchRect(cross, NULL, backbuffer, &crossRect, D3DTEXF_NONE);	//Done rendering HUD	g_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);	g_pD3DDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);

##### Share on other sites
Sorry big misunderstanding. I thought you use the surface interface to fill a texture and then render a quad as this is the more correct way to render something.

StretchRect should only used in some special cases and not for rendering a HUD.

##### Share on other sites
Oh. Okay. What should I use then? I was going to use sprites but they didn't seem right because the coordinates were in 3d and it seemed like it would be too hard to calculate a D3DXVECTOR3 for each HUD element. If you are supposed to use a sprite, how do I do it?

##### Share on other sites
Quote:
 Original post by Programmer101Oh. Okay. What should I use then? I was going to use sprites but they didn't seem right because the coordinates were in 3d and it seemed like it would be too hard to calculate a D3DXVECTOR3 for each HUD element. If you are supposed to use a sprite, how do I do it?
Personally, I'd draw a series of textured quads (Same the D3DXSprite does, really) in screen space. You can do that by using transformed vertices (D3DFVF_XYZRHW if you're using the fixed function pipeline), or by using an orthogonal projection matrix (Which means the viewport will be like a CAD-style window where objects with a greater Z value aren't drawn smaller - See D3DXMatrixOrthoLH)

##### Share on other sites
Thanks for the replies. They are very much appreciated.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627667
• Total Posts
2978533

• 10
• 10
• 12
• 22
• 13