Sign in to follow this  

Transparency on non-textured meshes

This topic is 4746 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

Ello all, I'm trying to implement transparency on my very simple completely untextured meshes. All they have is a material so they're rendered with some colour but that's it. My problem is that when I turn on alpha blending, the mesh that I set to be transparent is simply rendered black. I've no idea why, I'm following textbook examples as best I can. Here's the drawing code:
void DuncEngineMesh::PhysicalCylinderMesh::draw(IDirect3DDevice9* device) {
	
	if (!renderMe) return;

	if (transparent) { 
		device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);	
		device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE);
		device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
		device->SetRenderState(D3DRS_SRCBLEND,  D3DBLEND_SRCALPHA);
		device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
	}

	compositeTransform = *physicalObject->getTransformMatrix();
	position = physicalObject->getPosition();

	//newton defines cylinders along x, but dx defines along z, correct
	D3DXMATRIX r;
	D3DXMatrixRotationYawPitchRoll(&r,3.1416f*0.5f,0.0f,0.0f);
	compositeTransform = r * compositeTransform;
		
	device->SetMaterial(&material);
	device->SetTransform(D3DTS_WORLD, &compositeTransform);

	this->mesh->DrawSubset(0);		

	if (transparent) {
		device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
	}
}

The colour of the material used on this particular mesh is "const D3DXCOLOR TRANSP_ORANGE( D3DCOLOR_ARGB(150, 255, 20, 147) );", this is the colour that's set for the Ambient, Diffuse and Specular components of the material. Emissive is set to BLACK and Power to 2.0f (changing BLACK makes no difference, it still doesn't work). Any ideas what I'm doing wrong? Cheers for any replies, Duncan

Share this post


Link to post
Share on other sites
I thought it might have something to do with WHEN the mesh was being rendered (i.e. it was at the front of the data structure I use to hold all entities to be rendered, so it didn't have anything rendered in the background by the time it was rendered). So I moved it to the back of the data structure (so it was the last thing rendered), and now it renders totally invisible! Again, regardless of the settings I provide.

Any ideas?

Share this post


Link to post
Share on other sites
Is the texture set to NULL?
try setting some or all of these states before rendering

device->SetTexture(0, NULL);
device->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0);
device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
device->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2);
device->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);

the problem could also be with your light source. Check the alpha value of your light color, I'm not sure how d3d calculates the light, but I think light.diffuse.a would need to be set to 1.0f or at least non-zero.


dave

Share this post


Link to post
Share on other sites
Hi, cheers for the reply.

I tried those states, and I set my lights to having some alpha value, all to no avail.

It seems I can get transparency working with the flags:

device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCCOLOR);
device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVDESTCOLOR);

I.e. and other COLOR based flags... strangely it seems that if I try to use ALPHA based flags at all, the mesh just goes invisible! So I can't use alpha at all for some reason. (My back buffer format is A8R8G8B8 so its not that).

Share this post


Link to post
Share on other sites
Are you using any pixel or vertex shaders? The alpha value would need to be set into the transformed vertex and I pretty sure the transparency will work with those settings if the vertex has an alpha value less than 255. Just out of curiosity, try drawing a single triangle defined in screen coords where you are drawing your mesh and make sure the vertex colors have alpha values less than 255, this might shorten the list of possible problems.

dave

Share this post


Link to post
Share on other sites
Hi, it does sound like the alpha is not getting output properly from the pixel pipeline, which would be consistent with you seeing black when you render your object before the rest of the scene and nothing when rendered after.

If all you want is a constant alpha of your object, I suggest you use the texturefactor for your alpha, something like this:

device->SetRenderState( D3DRS_TEXTUREFACTOR, 0x7f000000 );
device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);

Also, when rendering alpha blended objects you would generally render them after all opaque objects in the scene so that the blending will occur with the objects that are in the framebuffer.

hope this helps,
svein

Share this post


Link to post
Share on other sites
Cheers for those replies guys.

I'm not using any sharders - yet. How do I set the value of the alpha in the *transformed* vertex? All the tutorials I've seen just set the alpha value in the diffuse component of a material, and that does the trick.

That hack works perfectly sveinm, I can get my objects alpha blended now. I was wondering if there was a straight forward way to just set the amount of alpha arbitrarily. However, do you know if this is a particularly expensive operation? I.e. is using real textures/materials alpha channel faster in terms of framerate than this method?

Share this post


Link to post
Share on other sites
Quote:
All the tutorials I've seen just set the alpha value in the diffuse component of a material, and that does the trick.

Did those tuts set a vertex shader in the setup? I'm thinking the default pipeline doesn't touch the material or light alpha values (someone correct me if I'm wrong) That would be the best way to set the alpha in the transformed vertices. You could also do a software transform and lighting stage to accomplish this, but would probably be slower. Look into vertex shaders, they are pretty easy to use and there are samples in the dx sdk.

Quote:
However, do you know if this is a particularly expensive operation? I.e. is using real textures/materials alpha channel faster in terms of framerate than this method?

Is constant alpha (D3DRS_TEXTUREFACTOR) faster than per vertex alpha? If the alpha were just pulled from the material they should be about equal.

dave

Share this post


Link to post
Share on other sites

This topic is 4746 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.

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