OpenGL Alpha Blend in DirectX?

This topic is 1804 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi everyone!

I'm drawing some squares in the same Z-coord in overlaped areas. The textures asociated are using alpha-channel.

I don't know how to do this in DirectX... I've done a drawing using OpenGL using this parameters:

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

[attachment=19814:OGL.png]

And the images display ok. But using this in DirectX:

m_d3ddev->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
m_d3ddev->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL);
m_d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
m_d3ddev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
m_d3ddev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

In the overlaped areas, none is displayed. Also textures shows weird colors. Any help for this?

[attachment=19815:DXerror.png]

Share on other sites
Disable z-writing when drawing the alpha shapes.

Share on other sites

What you've posted should be fine. I tried it with same states and my own texture. Must be something else going on, perhaps some other flags you're using or your vertex format.

When you say "drawing squares," what are you really doing?  Sprites? Vertex buffer with DrawPrimitive? Do you set the FVF? Does the FVF match your vertex buffer?

Post code snippets for "drawing squares" and for creating your "squares."

FYI, in future, you might use technical terms such as "from a vertex buffer" or "using a sprite" so others have a better idea what you're doing.

EDIT: Also, the last three states you set (alphablendenable, src- and dest-blend) are not default states for the device. You must set those states after every device reset. If you're setting those states before every render, you should be fine.

Edited by Buckeye

Share on other sites

Well... in DirectX I'm using Vertex buffer with FVF specifications and use DrawPrimitive. For drawing this "squares" I'm drawing triangles specifying 4 3D Vertices with its respective colors and U-V Vertices. All specifications must be ok because of this:

struct tri_vertex{
float x, y, z, w;
DWORD colour;
float tu,tv,tw;
};const DWORD tri_fvf=D3DFVF_XYZW|D3DFVF_DIFFUSE|D3DFVF_TEX1|D3DFVF_TEXCOORDSIZE3(0);

...

m_d3ddev->SetFVF(tri_fvf);

...

m_d3ddev->CreateVertexBuffer(size_vertex_buffer*sizeof(tri_vertex),
D3DUSAGE_WRITEONLY,//Usage
tri_fvf,           //FVF
D3DPOOL_MANAGED,
&d3dbuf,
NULL);

void* vb_vert;
hr = d3dbuf->Lock(0, 0, &vb_vert, 0);

memcpy(vb_vert, vertex_buffer.data(), size_vertex_buffer*sizeof(tri_vertex));
d3dbuf->Unlock();

m_d3ddev->SetStreamSource(0,     //StreamNumber
d3dbuf,   //StreamData
0,                   //OffsetInBytes
sizeof(tri_vertex)); //Stride

m_d3ddev->DrawPrimitive(prim_type, //PrimitiveType
0,           //StartVertex
nElem);      //PrimitiveCount

Well!. The problem maybe is in what you said in your last EDIT. The (alphablendenable, src- and dest-blend)  only is called in the begining. I'll try using this at every render.

EDIT: This is not working :( I used this:

HRESULT hr = m_d3ddev->Present(NULL,                            NULL,
NULL,
NULL );

if(FAILED(hr)) _printf(_T("Fallo al renderizar en DirectX\n"));

m_d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
m_d3ddev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
m_d3ddev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

Perhaps you need to post you all the code I've used. I'm just only drawing many squares in the same z-location but in diferent x-y-locations and the areas must overlap.

Edited by ckdur

Share on other sites

You're using transformed-clipped vertices! My test was with D3DFVF_XYZ and processed through the pipeline (transforming and clipping).

Also, as I mentioned previously, it's a better practice to set non-default device parameters (states, buffers, view, projection, etc.) after every device reset, or before each render.

Edited by Buckeye

Share on other sites

Okay! I'll try disabling Z-writing

EDIT: This is not helping. Maybe there's a collision in my libraries... I'll quit using DirectX.

Libraries: libpng, cairo, gtk+, directx (d3d), SDL (used for creating window) and OpenGL

I only load one graphics library per execution (DirectX/OpenGL)

Edited by ckdur

Share on other sites

The main difference from your two images is that the R and B channels have been swapped.

So the first thing I'd look at is the DWORD colour member in your tri_vertex struct. Maybe DirectX wants the channels in a different order.

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 14
• 46
• 22
• 27
• Forum Statistics

• Total Topics
634046
• Total Posts
3015219
×