Raptisoft
Raptisoft

Reputation: 213

OpenGL equivalent of DirectX renderstates

I'm dealing with a legacy system here, so shaders are not an option.

The code I need to modify has a render pipeline where a large part of the display is rendered to a texture, then that texture is drawn to the screen. It's expected to look exactly as if it was never rendered to texture in the first place.

It accomplishes this in DirectX by calling this function:

void RenderTargetChanged(bool isRenderToTexture)
{
    if (!isRenderToTexture)
    {
        gDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
        gDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
        gDevice->SetRenderState(D3DRS_SRCBLENDALPHA,D3DBLEND_SRCALPHA);
        gDevice->SetRenderState(D3DRS_DESTBLENDALPHA,D3DBLEND_INVSRCALPHA);
        gDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE,FALSE);
    }
    else
    {
        gDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
        gDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
        gDevice->SetRenderState(D3DRS_SRCBLENDALPHA, D3DBLEND_ONE);
        gDevice->SetRenderState(D3DRS_DESTBLENDALPHA, D3DBLEND_INVSRCALPHA);
        gDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
    }
}

(Notice, the only difference between the two branches is the values for D3DRS_SRCBLENDALPHA and D3DRS_SEPERATEALPHABLENDENABLE)

What is the OpenGL equivalent?

I've been driving myself crazy trying different combinations, and while I can get CLOSE, I never quite hit the point where it looks the same as if it wasn't rendered to texture in the first place-- the translucent areas either go dark or bright, whatever I try.

Upvotes: 0

Views: 724

Answers (1)

vallentin
vallentin

Reputation: 26157

Unless there's something I'm missing, then you should be able to compare the documentation for D3DBLEND enumeration and glBlendFunc().

  • D3DBLEND_SRCALPHA translates to GL_SRC_ALPHA
  • D3DBLEND_INVSRCALPHA translates to GL_ONE_MINUS_SRC_ALPHA
  • D3DBLEND_ONE translates to GL_ONE

When you want to separately specify the alpha components then you need to use glBlendFuncSeparate():

glEnable(GL_BLEND);

if (!isRenderToTexture) {
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glBlendEquation(GL_FUNC_ADD);
} else {
    glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
    glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);
}

Upvotes: 3

Related Questions