views:

29

answers:

1

I have a hooked DirectX used in C++ code that draws text and sprite. I tested it and it drew well onto 2D application. However, when I tried it with 3D application (some complex game actually), only text was visible. From that I deduced the sprite is not being overdrawn by something else, hence the text would be too. I set flags

    SetVertexShader(0);
    SetPixelShader(0);
    SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);

    //SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
    SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); 
    SetRenderState(D3DRS_ZENABLE, false);
    SetRenderState(D3DRS_LIGHTING, FALSE);
    SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
    SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

    SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
    SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
    SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_DIFFUSE);
    SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
    SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE);

For some reason, ALPHABLEND caused the sprite not to be visible at all in any application, so I didn't enable it and it's commented out.

Does any fail-proof way to draw sprites over D3D exist? Otherwise, should I use different flags/values for them?

Also, which z-index should I set for the sprite in case it's really "under" some other sprites/objects?

(I'm not author of anything drawn to the device, I only have the hooked DirectX API and don't even know the code of application I draw over).

A: 

Firstly, the easiest way to render sprites to a D3D device is ID3DXSprite.

Secondly, i think there's a typo and you meant to set COLOROP to MODULATE, not ALPHAOP.

However, i think in both cases you would only end up with an invisible output if your alpha was 0. Could it be that the texture you're using is fully transparent?

Also, don't forget to restore all render states you changed (directly or indirectly) after you're done with your injected render code. A simple way to do this is to capture a state block and apply it after you're done with your own rendering.

smocoder
I'm not sure about the typo, it's possible it's wrong, but it doesn't work anyway. The texture loaded is jpg without any alpha at all. And as I said, it works when injected into another application, so I thing it's about flags and z-index. Thanks anyway, I will look further into alpha just to be sure.
Mikulas Dite
I've found the answer after all when I checked-out more flags. I was missing `SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS); // important for drawing sprites to the top, must be reset` and after custom render it needs rollback to `SetRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL);` again. Please post it as a new answer so I can accept it.
Mikulas Dite