Sign in to follow this  
fervent

[mdx] Fading out meshes

Recommended Posts

Hi, I have several meshes that I'd like to have fade out (to transparent) in my managed direct 3d game. I can't seem to find out how to do this :) My draw function for my mesh class looks like this: public void Draw() { for (int i = 0; i < materials.Length; i++) { device.Material = materials[i]; device.SetTexture(0, textures[i]); mesh.DrawSubset(i); } } I'd like to have something like: Draw(float percentageTransparent) { // code... } Any ideas?

Share this post


Link to post
Share on other sites
i believe i once had a problem similar to this...
the thing is, directx mesh objects dont come with a colour variable in their vertex definition. this means you need to create a FVF with a value for colour and assign it to all loaded meshes.
now while this may seem like a fair bit of work just to get a colour tinge on your meshes, its worth it [wink].

first off you need to declare the FVF you're going to use:
const DWORD D3DFVF_VERTEX = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1;

or something to that effect. in case you're not familiar, D3DFVF_XYZ means the structure your vertices will use contains x, y and z values (positions...), D3DFVF_DIFFUSE is the one that specifies a colour value and D3DFVF_TEX1 gives room for 1 set of texture coordinates.

next you need a struct that will be used for each vertex's information:

typedef struct CUSTOMVERTEX {
float x, y, z;
DWORD colour;
float tu, tv;
};




this has variables corresponding to the FVF that was specified before (position, colour then texture coordinates).

so far so good, eh? now comes the tricky part - like i said before you need to tell every mesh object to use this format (D3DFVF_VERTEX) so that you can give colour to them.
im going to assume you use D3DXLoadMeshFromX or something similar to load meshes and to get their pointers.
here's how i went about applying this FVF:

LPD3DXMESH tempmesh = NULL;
LPD3DXMESH finalmesh = NULL;
LPDIRECT3DDEVICE9 pd3ddev; //initialised device

D3DXLoadMeshFromX(filename,D3DXMESH_SYSTEMMEM,pd3ddev,NULL,&matbuff,NULL,&num_mats,&tempmesh);
tempmesh->CloneMeshFVF(D3DXMESH_SYSTEMMEM,D3DFVF_VERTEX,pd3ddev,&finalmesh);




this takes the loaded mesh (tempmesh) and "clones" it but with the FVF supplied - D3DFVF_VERTEX - then it outputs a pointer to a new mesh (finalmesh).
now the mesh thats loaded will have a colour variable in its vertex definition. to use this you need to lock the vertex buffer for the mesh and manually change all colour values for each vertex:

CUSTOMVERTEX *verts = NULL;
colour = D3DCOLOR_ARGB(alpha,red,green,blue);

mesh->LockVertexBuffer(0,(void**)&verts);

for (int i = 0; i < mesh->GetNumVertices(); ++i)
verts[i].colour = colour;

mesh->UnlockVertexBuffer();




this assigns the value "colour" to all vertices in the mesh "mesh". now when you render your mesh it should be with the colour value that you specify. this means that if you have textures, the colour will be applied over the top; so for no colour change to be applied, make the "colour" value = ARGB(255,255,255,255) or white.
as for your question, if you want transparency in percentage youd do something like this:
colour = D3DCOLOR_ARGB(alpha/100.0*255,255,255,255);

where "alpha" is a value between 0 and 100 representing a percent of visibility (0% meaning totally transparent).

hope that all helps! [smile]

[Edited by - the_moo on May 23, 2006 8:40:30 PM]

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