Jump to content
  • Advertisement
Sign in to follow this  

Solid rectangle in D3D

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

How can I make a solid filled rectangle in Direct3D? I made the code below but is generates a transparant rectangle (I do use alpha channel at full, 0xFF however). During my game I use the D3DXSprite interface and D3DXLine interface (I can make a rectangle from a series of lines though, but I don't think it's the fastest way). I should do it with an untextured quad I think, but how? I read that DrawPrimitiveUP is relatively slow so I should use a texture buffeR?
HRESULT DrawRectangle(const RECT* lpRectangle, D3DCOLOR Color)
{
    CUSTOM_VERTEX Vertices[4] =
    {
        (float) lpRectangle->left - 0.5f,  (float) lpRectangle->top - 0.5f,    0.5f, 1.0f, Color, 0, 0,
        (float) lpRectangle->right - 0.5f, (float) lpRectangle->top - 0.5f,    0.5f, 1.0f, Color, 0, 0, 
        (float) lpRectangle->right - 0.5f, (float) lpRectangle->bottom - 0.5f, 0.5f, 1.0f, Color, 0, 0, 
        (float) lpRectangle->left - 0.5f,  (float) lpRectangle->bottom - 0.5f, 0.5f, 1.0f, Color, 0, 0,
    };

	LPDIRECT3DDEVICE9 lpD3DDevice = m_Video->GetD3DDevice();

    m_Video->GetD3DSprite()->Flush();
     
    IDirect3DVertexDeclaration9* lpD3DVertexDeclaration = NULL;

    lpD3DDevice->GetVertexDeclaration(&lpD3DVertexDeclaration);

    lpD3DDevice->SetFVF(D3DFVF_CUSTOM_VERTEX);

    lpD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE );
    lpD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE );

    lpD3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, Vertices, sizeof(CUSTOM_VERTEX));

    lpD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
    lpD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);

    lpD3DDevice->SetVertexDeclaration(lpD3DVertexDeclaration);

    lpD3DVertexDeclaration->Release();

    return S_OK;
}

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by rmdeboer82
How can I make a solid filled rectangle in Direct3D? I made the code below but is generates a transparant rectangle (I do use alpha channel at full, 0xFF however). During my game I use the D3DXSprite interface and D3DXLine interface (I can make a rectangle from a series of lines though, but I don't think it's the fastest way). I should do it with an untextured quad I think, but how? I read that DrawPrimitiveUP is relatively slow so I should use a texture buffeR?
I'm assuming you mean "vertex buffer" on the last line of your post there.

ID3DXSprite just manages a dynamic vertex buffer internally, so it should be fast enough for your needs. I'd stick with it unless you need functionality it can't provide, or if profiling determines that it's a bottleneck.

You can disable alpha blending completely by setting the D3DRS_ALPHABLENDENABLE render state to FALSE, E.g.
pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE)
Just remember to flush your sprite before that.

It's also not usually a good idea to use a triangle fan, since most GPUs aren't very well optimised for them. Although I doubt it'll make too much difference in this case.

In all, I'd just use ID3DXSprite. You can pass various flags to it's Begin() member function to enable / disable alpha blending.

Share this post


Link to post
Share on other sites
Thank you, Evil Steve. It works now :) However it is red in stead of the white I gave it :S I wonder which flags I should toggle to have it white. It only appears in red no matter what color I choose. Can it have to do with shading or something?

What would you propose as an alternative for triangle fan?

You mentioned to use ID3DXSprite, is this for the rectangle also? Would it then be wise to use a solid colored (ie. white or black) image for that which I can recolor later just the way I can give vertices a color?

And I should not pass flasg to ID3DXSprite->Begin since it would then be applied to all my sprites as a result of my design.

[Edited by - rmdeboer82 on January 27, 2007 12:47:02 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!