• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
george7378

Alpha transparency help?

11 posts in this topic

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.

[attachment=13377: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
0

Share this post


Link to post
Share on other sites

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);
0

Share this post


Link to post
Share on other sites

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:

 

[attachment=13378: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.

0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites

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:

 

[attachment=13381:Direct3D first person camera 2013-01-27 17-29-11-66.png]

Edited by george7378
1

Share this post


Link to post
Share on other sites

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

2

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites

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!

0

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

@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?

0

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  
Followers 0