Sign in to follow this  
bitterbanana

A simple transparent filled rectangle

Recommended Posts

I just completed a directx hook for Halo. But I can't get a simple semi-transparent rectangle to put behind my text. I tried this:
SetRect(&txtFrame,g_pPP->BackBufferWidth-200,50,g_pPP->BackBufferWidth-10,250);
g_pDevice->GetRenderTarget(0,&g_pSurface);
g_pDevice->ColorFill(g_pSurface,&txtFrame,D3DXCOLOR(0.5f,0.5f,0.5f,0.5f));


It ended up being a solid rectangle, no transparency. I'm just wondering why this doesn't work. An alternate method would be great too. Thank you.

Share this post


Link to post
Share on other sites
You need to make sure that the Renderstate has transparency enabled.

Make sure these renderstates are enabled:

// Enable alpha blending vertex colors.
m_pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
m_pDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
m_pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

//As for enabling transparency in textures, which sorta looks like
//the way you're doing this, you also need to use:
m_pDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
m_pDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_CURRENT);



You might want to store the current values away and set them back after you've done your rendering, or you could potentially mess up the rendering for Halo itself.

If you've done all this already, then I'm actually somewhat stumped.

Share this post


Link to post
Share on other sites
Rather than even using a texture and adding to the texture pool, you could skip that and merely use a vertex colored quad, since it seems like you're just using a single color anyway, rather than an actual texture.

If you decide to do only that, then the code I gave you will no doubt work.

Share this post


Link to post
Share on other sites

// pre-render code
vertexBuffer->Lock(0, 0, (void**)&vertices, NULL);
vertices[0].color=D3DXCOLOR(0,0,0,0.7f);
vertices[0].x=g_pPP->BackBufferWidth-100;
vertices[0].y=50;
vertices[0].z=0;
vertices[0].rhw=1.0f;
vertices[0].u=0;
vertices[0].v=0;
vertices[1].color=D3DXCOLOR(0,0,0,0.7f);
vertices[1].x=g_pPP->BackBufferWidth;
vertices[1].y=50;
vertices[1].z=0;
vertices[1].rhw=1.0f;
vertices[1].u=1;
vertices[1].v=0;
vertices[2].color=D3DXCOLOR(0,0,0,0.7f);
vertices[2].x=g_pPP->BackBufferWidth-100;
vertices[2].y=250;
vertices[2].z=0;
vertices[2].rhw=1.0f;
vertices[2].u=1;
vertices[2].v=1;
vertices[3].color=D3DXCOLOR(0,0,0,0.7f);
vertices[3].x=g_pPP->BackBufferWidth;
vertices[3].y=250;
vertices[3].z=0;
vertices[3].rhw=1.0f;
vertices[3].u=0;
vertices[3].v=1;
vertexBuffer->Unlock();

// render code
g_pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
g_pDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
g_pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
g_pDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);

g_pDevice->SetRenderState(D3DRS_AMBIENT,RGB(255,255,255));
g_pDevice->SetRenderState(D3DRS_LIGHTING,false);
g_pDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);


g_pDevice->SetStreamSource(0, vertexBuffer, 0, sizeof(TLVERTEX));
g_pDevice->DrawPrimitive (D3DPT_TRIANGLESTRIP, 0, 2);



The render code is executed every frame. The rectangle doesn't show up in the main menu of halo. But in a game, it shows up PERFECTLY for about 20 seconds, then it loses transparency and it turns to a solid random color. It's the same color as the walls around me, so that probably has a lot to do with it. Is there a command that I'm missing?

Thanks again.

Share this post


Link to post
Share on other sites
That's actually kind of odd it takes 20 seconds before it screws up...

Are you going into the same area around that 20 second mark each time, or are you able to reproduce this every 20 seconds even if you stay still?

I'm not entirely sure how Halo does a lot of it's rendering, after all, I wasn't a developer for it, however it could be that once you get to a certain area it changes something in it's rendering (think going from outdoors to indoors in some games, the renderer is changed completely for the new environment) and it changes something that your code doesn't like.

I'm actually kind of stumped on that one.

One question however, when you set your render states, do you store the old ones so you can set them back when you're done with your rendering? Program's don't change renderstates if they don't have to. You might end up confusing Halo that way.

Share this post


Link to post
Share on other sites
I pretty much stayed in the same place when it stopped working. I found something that saves and restores renderstates:

// text frame
// save halo's render states
g_pDevice->CreateStateBlock( D3DSBT_ALL, &pStateBlock );

// do my own
g_pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
g_pDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
g_pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
g_pDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
g_pDevice->SetRenderState(D3DRS_AMBIENT,RGB(255,255,255)); //maximum ambient light ( NEW )
g_pDevice->SetRenderState(D3DRS_LIGHTING,false); //lighting disabled ( NEW )
g_pDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE); //don't cull backside ( NEW )
g_pDevice->SetStreamSource(0, vertexBuffer, 0, sizeof(TLVERTEX));
g_pDevice->DrawPrimitive (D3DPT_TRIANGLESTRIP, 0, 2);

// restore halo's states
pStateBlock->Apply();



It slows down the framerate a lot. And it did the same exact thing anyway. I'll have to do more research on what Halo is doing. Thanks for the help anyway Flimflam.

EDIT:
I forgot to add a couple lines to it. The box is turning transparent now. But the color is changing to the current texture I look at... Odd.

Share this post


Link to post
Share on other sites
Just a bit of friendly advice, you can cut down dramatically on the performance hit if you just save the states that you change using GetRenderState() :)

...

Wait, wow, how stupid of me. Before you render your box, go:
m_pDevice->SetTexture(0, NULL);

Halo is setting the texture to the texture in front of you for rendering purposes, and it doesn't clear the texture because wasteful changing textures can be costly on performance. However, you will need to do this. I'm sorry I didn't realize this sooner. My mind isn't quite with me today. Since you're not using your own texture, you need to make sure Halo isn't using it's last rendered texture, either :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this