• 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
BlurEffect

Grass Rendering with DirectX 11 not working properly

9 posts in this topic

Hi guys,

 

I'm trying to get grass rendering to work with DirectX 11 and I'm pretty much following this GPU Gems article: http://http.developer.nvidia.com/GPUGems/gpugems_ch07.html. Basically, at the moment I have a mesh consisting of three interleaved quads that have grass textures with transparent parts on them. I'm animating these grass objects by moving the upper vertices of the quads in the vertex shader. The article says about the grass objects: "sort them back-to-front at runtime, use alpha blending, and enable z-testing/writing in the draw call". I thought I had this set up correctly but something isn't as I get some visual errors when rendering the animated grass. More precisely, when two quads of adjacent grass objects intersect, one of them gets hidden rather than being properly blended. Also when I generally render the grass objects, some parts (on the side opposite to the camera) are not visible although the occluding quad  should have transparent parts. I also don't get why some transparent parts of the textures are rendered in black instead of being actually transparent, they occlude other stuff in the application (like the grid you can see in the images below, although the grid is rendered after the grass). It's a bit difficult for me to explain, so I'm attaching three pictures to this post:

 

[attachment=21947:Grass1.png]

This image shows how the three quads making up the mesh of my grass objects are interleaved.

 

[attachment=21948:Grass2.png]

This image shows a single grass object. The more distant parts are not visible, it's pretty much just showing half the object.

 

[attachment=21949:Grass3.png]

This image shows how some parts of one grass object are occluded by the black parts of another one.

 

Here is the code, in which I set up the blending state and the depth-stencil-state for rendering of the grass.

D3D11_DEPTH_STENCIL_DESC m_grassStencilDesc;
ZeroMemory( &m_grassStencilDesc, sizeof( D3D11_DEPTH_STENCIL_DESC ) );

// Set up the description of the stencil state.
m_grassStencilDesc.DepthEnable = true;
m_grassStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
m_grassStencilDesc.DepthFunc = D3D11_COMPARISON_LESS; 

m_grassStencilDesc.StencilEnable = true;
m_grassStencilDesc.StencilReadMask = 0xFF;
m_grassStencilDesc.StencilWriteMask = 0xFF;

// Stencil operations if pixel is front-facing.
m_grassStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
m_grassStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
m_grassStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
m_grassStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;

// Stencil operations if pixel is back-facing.
m_grassStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
m_grassStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
m_grassStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
m_grassStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;

// Create the depth stencil state.
m_pD3d11Device -> CreateDepthStencilState(&m_grassStencilDesc, &m_grassStencilState);


D3D11_BLEND_DESC m_grassBlendingStateDesc;
ZeroMemory(&m_grassBlendingStateDesc, sizeof(D3D11_BLEND_DESC));

// Create an alpha enabled blend state description.
m_grassBlendingStateDesc.RenderTarget[0].BlendEnable = TRUE;
m_grassBlendingStateDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;//D3D11_BLEND_ONE;
m_grassBlendingStateDesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
m_grassBlendingStateDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
m_grassBlendingStateDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
m_grassBlendingStateDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
m_grassBlendingStateDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
m_grassBlendingStateDesc.RenderTarget[0].RenderTargetWriteMask = 0x0f;

// Create the blend state using the description.
m_pD3d11Device -> CreateBlendState( &m_grassBlendingStateDesc, &m_grassBlendingState );

I am sorting the grass object back to front, as the article suggests. To do this I compare the distance from the centre of each grass object to the camera position. When the objects are animated the quad of an object that is actually farther away from the camera might be moved towards the camera in a fashion that it is now actually placed in front of a grass object that is closer to the camera. Might this actually be the problem? Should I sort the single quads instead of the grass objects? But if so, how would I determine which quad is closer to the camera, as all three have their centre at the same position. Do you generally have any idea what's going wrong, maybe an error in the code above? I hope you guys can help me, thank you very much in advance.

0

Share this post


Link to post
Share on other sites

I'm not sure why that article suggests enabling z-write if you are already sorting back-to-front. Disabling it will make the grass look better, although you'll still have some more subtle alpha-blending order issues.

 

For your issue with single grass objects, you may need to break your 3 intersecting grass quads into 6 pieces (that all meet in the "center" of the grass object). Then those would be drawn in a back-to-front order.

 

As for the issue with adjacent grass objects intersecting, I would suggest placing them further apart (like the images in the article you linked)

Edited by phil_t
0

Share this post


Link to post
Share on other sites

Thank you very much for your answer. I actually tried disabling z-write before but as you mentioned there are still some order issues as the image below shows. However, I don't get the other problems with intersections or parts of the grass object not being drawn. I guess in order to solve the issue present in the image below (it's a bit difficult to spot though), I would have to order the quads back to front and not the grass objects as a whole. At the moment with all quads centred in the same spot, I have no idea how you could sort them properly. However, as I read your post, I thought that I could split up each quad into two quads (just as you suggested to solve one of the problems), who would then all have their centre in different spots. I will try that out. If you or someone else know of other things I could try, it would be great if you could share them, too.

Edited by BlurEffect
0

Share this post


Link to post
Share on other sites


At the moment with all quads centred in the same spot, I have no idea how you could sort them properly.

 

I think you could avoid complex sorting with some clever tricks. Assuming all your 6-quad grass objects are oriented the same way in the world, I think there are probably only 6 different "sets of orders" that you would ever need (maybe less), depending on camera angle. So you could have 6 different index buffers that define the different order of the grass quads within a bunch, and you choose one of them based on camera angle. I could diagram if I'm not explaining clearly.

 

Also, to avoid all these sorting problems you could use alpha-test instead of alpha blending. The upside is that you no longer need to sort, and you no longer need to render your grass after all other objects, as the depth buffer will be correct. The downside is that visual quality will suffer - your grass will have jaggy edges.

0

Share this post


Link to post
Share on other sites

How would I actually use alpha-testing in DirectX 11. I couldn't really find anything.

 

Edit: Apparently you can no longer enable Alpha-Testing in DirectX 11 but you have to do it manually in a shader by discarding pixels with alpha values below a user-defined threshold. I tried it, and yeah the edges become quite jaggy, but as the camera in the game will be at a bit of a distance from the grass at all times, I might get away with it. It's still annoying though that I can't really get it to work as it was described in that article.

Edited by BlurEffect
0

Share this post


Link to post
Share on other sites


It's still annoying though that I can't really get it to work as it was described in that article.

 

What about the other suggestions I made? i.e.:

 


I think you could avoid complex sorting with some clever tricks. Assuming all your 6-quad grass objects are oriented the same way in the world, I think there are probably only 6 different "sets of orders" that you would ever need (maybe less), depending on camera angle. So you could have 6 different index buffers that define the different order of the grass quads within a bunch, and you choose one of them based on camera angle. I could diagram if I'm not explaining clearly.
0

Share this post


Link to post
Share on other sites

So, I could solve the issue that I had for single grass objects by using single quads and sorting them back to front (instead of just sorting the grass objects). However, I still couldn't figure out any working solution to prevent visual errors as soon as quads intersect. But that's not really a problem of the sorting as during the intersections there is one part of a specific quad in front of the other one while the rest of it is behind it. So you simply can't sort that properly. I don't understand why that alpha-blending is not working as it seems to do in the article. Or maybe there are really no intersections at all in the article approach...

0

Share this post


Link to post
Share on other sites

As mentionned earlier, try alpha testing : use clip / discard in your shader to clip at certain alpha threshold. In the same time, enable depth testing and depth writes. 

 

That is the easiest solution which will produce output without visual errors. The edges of the grass blades will be more jagged, but it may or may not be a concern - some antialiasing techniques might improve the image quality.

 

You may also look in to OIT (order independent transparency) in order to draw the grass blades with alpha blending. There is plenty of research about OIT, but it isn't the easiest subject. 

 

The problem with the traditional alpha blending is that all the surfaces must be draw in back to front order, and it is very difficult to produce a perfect ordering with a triangle soup - and reordering the triangles with a modern GPU is not a good idea. Some solutions sort the objects in back to front order and with a good geometry (such as a screen aligned quad) the visual errors are pretty low. 

 

Cheers!

Edited by kauna
0

Share this post


Link to post
Share on other sites

This article gave me an idea: http://www.sjbaker.org/steve/omniv/alpha_sorting.html

 

Your case falls into the "Disabling Z-write for Translucent Polygons.", where it says: "If all the translucent polygons have the same colour, then this does actually work".

 

I think the steps required to get it working properly are:

1) enable z-writing and z-testing; clear the z-buffer

2) draw all of your non-grass objects (like the blue grid)

3) disable z-writing, but leave z-testing on

4) draw all of your grass objects in back-to-front order with alpha-blending, as described in the GPU Gems article

 

AFAIKT, the point that the GPU Gems article is trying to make is that you do not need perfect order-independent transparency in order to draw the grass. Order-independent alpha blending would induce a huge performance hit. You're over-analysing the individual grass mesh, but have you tried maybe adding 100 to 1000 of them to your scenery? If that looks ok, then you should stick with it.

Edited by tonemgub
0

Share this post


Link to post
Share on other sites

I'm using alpha testing for now and will see how it works within the actual game (in my test application it seems to work and look fine). Depending on that I might consider some of the other advice given. Thanks guys!

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