Jump to content

  • Log In with Google      Sign In   
  • Create Account


Alpha transparency help?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 george7378   Members   -  Reputation: 1185

Like
0Likes
Like

Posted 27 January 2013 - 06:15 AM

Hi everyone,

I'm trying to get my Direct3D scene to render trees using .png images with in-built alpha transparency for the leaves, and I'm running into some problems. At the moment, I have just created a plane with my leaf texture on it before I get into creating an actual tree, and as you can see, it's looking rather weird! I should say that I created the leaf object and applied the texture in Blender, and I then exported it as a .x file. I enabled all the relevant alpha options in my blender material/texture.

leaftest.png

I should also mention that the bright blue colour which shows up where the plane should be transparent is the same colour that I clear my background and z-buffer to when I render the frame (i.e. d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(20, 170, 220), 1.0f, 0);). The only thing I have done to my code to enable the alpha transparency is this:
d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);    d3ddev->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);    d3ddev->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);    d3ddev->SetTextureStageState(0,D3DTSS_ALPHAARG1,D3DTA_TEXTURE);

So - can anyone see an obvious issue with my method?

Thankyou very much :)

Edited by george7378, 27 January 2013 - 06:17 AM.


Sponsor:

#2 belfegor   Crossbones+   -  Reputation: 2557

Like
0Likes
Like

Posted 27 January 2013 - 07:26 AM

DId you render your leaves last?

Also you may need to sort leaves back to front.

 

Or use alpha test instead, witch i think is much better solution.

device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
device->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);
device->SetRenderState(D3DRS_ALPHAREF, (DWORD)10); // render pixel with alpha greater then 10 (0 - 255)
device->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);


#3 george7378   Members   -  Reputation: 1185

Like
0Likes
Like

Posted 27 January 2013 - 07:58 AM

I used your suggestion (with some modifications because for some reason it made my translucent floor texture become fully opaque) and I now get this:

 

Direct3D first person camera 2013-01-27 13-55-46-19.png

 

So it seems to be working fine! By the way, I didn't render my trees last - they come second in the list. I just enabled alpha testing for that specific part of the rendering, and left it out for the rest.



#4 belfegor   Crossbones+   -  Reputation: 2557

Like
0Likes
Like

Posted 27 January 2013 - 08:03 AM

Are you doing something like this? You may need to disable some states after you finish drawing transparent objects.
...
DrawSky();
DrawTerrain();
 
device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
... // other states
 
DrawLeaves();
 
device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); // disable alpha blend
device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE); // i am not sure maybie this one needs to be disabled also

I would like to advise you to take a look at tutorial on http://www.cgtextures.com/ under "Tutorial" menu "Alpha mask background trick".

Edited by belfegor, 27 January 2013 - 08:06 AM.


#5 george7378   Members   -  Reputation: 1185

Like
1Likes
Like

Posted 27 January 2013 - 11:25 AM

At the moment, yes I do. For my semi-transparent water texture on the floor (which is drawn on a plane defined in the code rather than on a mesh) I also have to do this:

 

d3ddev->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA); //Set up alpha blending for the imported water texture:
d3ddev->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
//Draw water plane
d3ddev->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_ONE); //Reset the aplha blending to be used differently elsewhere:
d3ddev->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_ZERO);

 

I am also defining these things when I set up Direct3D:

 

d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); //Turn off culling (can see both sides of shapes)
d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE); //Activate the z-buffer
d3ddev->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE); //Handle normals in scaling
d3ddev->SetRenderState(D3DRS_ALPHAREF, (DWORD)10); //Render pixel with alpha greater then 10 (0 - 255) when alpha test enabled
d3ddev->SetTextureStageState(0,D3DTSS_ALPHAARG1,D3DTA_TEXTURE); //Take alpha from texture
d3ddev->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL); //Set the aplha test mode to only draw if alpha >= specified value
d3ddev->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE); //Turn on alpha test mode

 

This combination seems to work for me at the moment! Here it is with actual trees:

 

Direct3D first person camera 2013-01-27 17-29-11-66.png


Edited by george7378, 27 January 2013 - 11:37 AM.


#6 L. Spiro   Crossbones+   -  Reputation: 13222

Like
2Likes
Like

Posted 27 January 2013 - 05:01 PM

You are setting yourself up for some future hurt.

Render states should not be set once during initialization.  They should be set per-object or per-pass.

Culling should be disabled except for on the leaves for performance reasons.

 

Drawing them in random order works now because your leaves are either (almost) fully opaque or fully transparent.

When about when a car drives through with glass windows?  You have to draw transparent objects in a second pass and they must be sorted far-to-near.

That includes those leaves.  You won’t be able to avoid it for long so you may as well implement it now.

 

 

L. Spiro


It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#7 EWClay   Members   -  Reputation: 659

Like
0Likes
Like

Posted 27 January 2013 - 06:06 PM

Agreed, mostly. Culling should be disabled and alpha test enabled only for the objects that need it, i.e the leaves.

But you don't need to sort alpha tested polygons! Drawing them after the solid pass may be advantageous because they will affect hierarchical z on the GPU, but they can always be drawn in a random order. Even if you add true transparent objects later, you can just draw those objects last, and sort only those.

#8 george7378   Members   -  Reputation: 1185

Like
0Likes
Like

Posted 27 January 2013 - 06:55 PM

OK, thanks for the advice, I changed the render states to only apply to certain objects and I was sure to reset them afterwards. I can certainly see what you mean about culling - it is only necessary for the objects I need to see the inside of. I also had to turn it off for the skybox and, for some reason, my floor disappears when I don't disable it (it's defined as a plane in the program itself using vertices so maybe I am looking at the bottom of it or something).

Also, at the moment, I can see leaves through eachother when they are overlapping and I can also see them through other transparent surfaces (like my translucent water floor) so maybe they'll be OK.

Edited by george7378, 27 January 2013 - 06:55 PM.


#9 george7378   Members   -  Reputation: 1185

Like
0Likes
Like

Posted 30 January 2013 - 07:54 PM

I've run into another small problem when I'm trying to render one surface behind another where the front surface is a textured plane with an in-built alpha channel in the texture, and the other surface is a mesh which doesn't have a texture, but only has a material applied to it instead. The water looks OK, but the untextured mesh which sits behind it doesn't show up unless I bring it to the foreground. So basically, the water is blocking the untextured mesh from view even though it's supposed to be transparent. I've tried moving their order around in the pipeline, but this doesn't solve the problem - if I set the untextured mesh to render before the water, the water simply won't show up. Can anyone offer me some advice?

 

I'm not using alpha test mode for this - here's a part of my code:

 

d3ddev->SetStreamSource(0, floor_buffer, 0, sizeof(CUSTOMVERTEX)); //Take vertices from floor_buffer
d3ddev->SetIndices(i_buffer); //Take indices from i_buffer
d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); //Enable alpha blending
d3ddev->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA); //Set up alpha blending for the imported water texture:
d3ddev->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
d3ddev->SetTexture(0, waterTex); //Apply the water texture
d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 4, 0, 2); //Draw the water layer (a plane defined in the program itself)
renderdev->SetMaterial(&meshMaterial);    //Set the material to the untextured mesh's material
renderdev->SetTexture(0, NULL); //No texture here
//Draw the untextured mesh here

 

That shows the general structure of the problem I'm having.

 

Thankyou!



#10 Tispe   Members   -  Reputation: 1010

Like
0Likes
Like

Posted 31 January 2013 - 07:36 AM

You are setting yourself up for some future hurt.

Render states should not be set once during initialization.  They should be set per-object or per-pass.

Culling should be disabled except for on the leaves for performance reasons.

 

Drawing them in random order works now because your leaves are either (almost) fully opaque or fully transparent.

When about when a car drives through with glass windows?  You have to draw transparent objects in a second pass and they must be sorted far-to-near.

That includes those leaves.  You won’t be able to avoid it for long so you may as well implement it now.

 

 

L. Spiro

 

Will changing states per-object hurt performance? Like if you have to change the states 100 times, 1000 times or even 10,000 times per frame?



#11 L. Spiro   Crossbones+   -  Reputation: 13222

Like
0Likes
Like

Posted 31 January 2013 - 09:19 AM

Redundant render-state changes are a different responsibility of yours.

It doesn’t matter what system you implement, with or without sorting objects etc.  In any case, you never access the graphics API calls directly but instead always go through wrappers.

Firstly wrapper are necessary for multi-platform support.  Then they are necessary for multi-threaded and single-threaded support.  No matter what, you always issue rendering commands through custom wrappers.

 

With that in place, it is fairly easy to eliminate redundant state changes.  If depth-testing is enabled twice in a row you already know that it is enabled and the second call should do nothing.

 

No matter the order of your rendering, this system guarantees the minimal state-changes necessary for each render.  Now all you need to maximize this is to sort objects by state so that the existing system will be maximized.

 

 

L. Spiro


It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#12 belfegor   Crossbones+   -  Reputation: 2557

Like
0Likes
Like

Posted 31 January 2013 - 11:16 AM

@L. Spiro

Is, for example, GetRenderState cheaper then SetRenderState?

Then there is a problem with PURE device witch doesn't provide Get functions. How to get default states then? Are they always the same on all hardware?






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS