Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Additive Blending


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
4 replies to this topic

#1 ~Helgon   Members   -  Reputation: 357

Like
0Likes
Like

Posted 23 November 2012 - 07:04 PM

Hi,

i have a problem with one of the exersises in "introduction to gameprogramming with directx 11" (for the ones who have the book, its chapter 10, exercise 7).

the task ist to draw an animation with additive blending. after trying some logic thinks i started "brute forcing" all possible variations but what ever i tried i never got the desired result.

i attached an img so u can see what is requested and what is my "best" result yet.

i hope that someone can give me a hint.. really tryed to solve it by my own for hours :(

(thats the blend desc which gives me the best result like on the screenshot (right))

D3D11_BLEND_DESC noBlack = {0};
noBlack.AlphaToCoverageEnable = false;
noBlack.IndependentBlendEnable = false;
noBlack.RenderTarget[0].BlendEnable = true;
noBlack.RenderTarget[0].SrcBlend	   = D3D11_BLEND_SRC_ALPHA;
noBlack.RenderTarget[0].DestBlend	  = D3D11_BLEND_SRC_ALPHA;
noBlack.RenderTarget[0].BlendOp	    = D3D11_BLEND_OP_ADD;
noBlack.RenderTarget[0].SrcBlendAlpha  = D3D11_BLEND_ONE;
noBlack.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ONE;
noBlack.RenderTarget[0].BlendOpAlpha   = D3D11_BLEND_OP_ADD;
noBlack.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
HR(device->CreateBlendState(&noBlack, &NoBlack));

boxTech->GetDesc( &techDesc );
for(UINT p = 0; p < techDesc.Passes; ++p)
    {
  md3dImmediateContext->IASetVertexBuffers(0, 1, &amp;mBoxVB, &amp;stride, &amp;offset);
  md3dImmediateContext->IASetIndexBuffer(mBoxIB, DXGI_FORMAT_R32_UINT, 0);
  // Draw the Cylinder
  XMMATRIX world = XMLoadFloat4x4(&amp;mCylWorld);
  XMMATRIX worldInvTranspose = MathHelper::InverseTranspose(world);
  XMMATRIX worldViewProj = world*view*proj;

  Effects::BasicFX->SetWorld(world);
  Effects::BasicFX->SetWorldInvTranspose(worldInvTranspose);
  Effects::BasicFX->SetWorldViewProj(worldViewProj);
  Effects::BasicFX->SetTexTransform(XMMatrixIdentity());
  Effects::BasicFX->SetMaterial(mBoltMat);
  Effects::BasicFX->SetDiffuseMap(bolts[currentBolt]);
  md3dImmediateContext->RSSetState(RenderStates::NoCullRS);
  md3dImmediateContext->OMSetBlendState(RenderStates::NoBlack, blendFactor, 0xffffffff);
  landAndWavesTech->GetPassByIndex(p)->Apply(0, md3dImmediateContext);
  md3dImmediateContext->DrawIndexed(mCylIndexCount, mCylIndexOffset, mCylVertexOffset);
  // Set per object constants.
  world = XMLoadFloat4x4(&amp;mBoxWorld);
  worldInvTranspose = MathHelper::InverseTranspose(world);
  worldViewProj = world*view*proj;

  Effects::BasicFX->SetWorld(world);
  Effects::BasicFX->SetWorldInvTranspose(worldInvTranspose);
  Effects::BasicFX->SetWorldViewProj(worldViewProj);
  Effects::BasicFX->SetTexTransform(XMMatrixIdentity());
  Effects::BasicFX->SetMaterial(mBoxMat);
  Effects::BasicFX->SetDiffuseMap(mBoxMapSRV);
  md3dImmediateContext->OMSetBlendState(0, blendFactor, 0xffffffff);
  boxTech->GetPassByIndex(p)->Apply(0, md3dImmediateContext);
  md3dImmediateContext->DrawIndexed(36, 0, 0);

}

i really hope that someone can help me because i get crazy

regards helgon

Attached Thumbnails

  • help.jpg

Edited by ~Helgon, 23 November 2012 - 07:06 PM.

from time to time i find time


Sponsor:

#2 Matias Goldberg   Crossbones+   -  Reputation: 3564

Like
0Likes
Like

Posted 24 November 2012 - 12:10 PM

Change your blend state a few bits:
noBlack.RenderTarget[0].SrcBlend	   = D3D11_BLEND_SRC_ALPHA;
noBlack.RenderTarget[0].DestBlend	  = D3D11_BLEND_INV_SRC_ALPHA;
This means the interpolation will be "finalOutput = colour1 * a + colour2 * (1-a);"

By looking at the screenshot, you're rendering your thunder effect before the terrain, but you have to do it after in order to get correct transparency (otherwise it will interpolate the colours with the sky).

Ordering front to back is a very good performance optimization due to something called Early Z. However when dealing with transparent objects (alpha blended objects) you have to sacrifice that performance and sort them back to front.

#3 ~Helgon   Members   -  Reputation: 357

Like
0Likes
Like

Posted 24 November 2012 - 05:12 PM

Hey, thank you very much for the awnser.

I changed the draw order. Now first terrain , then waves and then the bolt animation.
Then i changed the Blend values as u suggested but it still doesnt give the desired result

and an other question. i just get rid of the black parts of the texture if i clip them in hlsl. is it possible to do this through the blend state?
my solution for now is clip(texture.rgb - 0.1f); and it works but im not sure if this was the desired way of solving the problem.

and an other question. how should the material be for the bolt texture? i tried it with opaque 0 and 50% but both doesnt looked good :(

Hope that u can give me another hint :D

regards helgon

from time to time i find time


#4 JTippetts   Moderators   -  Reputation: 8575

Like
0Likes
Like

Posted 24 November 2012 - 05:37 PM

If the black is true black and you really are doing an additive blend, you don't need to worry about eliminating the black. Black is (R=0, G=0, B=0) and adding 0 to anything, of course, has no effect. If the black areas do cause an effect with an additive blend, it means the black isn't fully black. Your clipping trick leads me to believe that this might be the case. If the texture you are loading is .JPG format, there could be compression artifacts that are making the black not a true black. Or the texture could have been created incorrectly. In any case, make sure your black is really black, then if you do just a simple addition of the source against the destination (however this is done in DirectX; I'm an OpenGL guy) the black should have no effect but the blue/white areas should increase the brightness of the background.

#5 ~Helgon   Members   -  Reputation: 357

Like
0Likes
Like

Posted 24 November 2012 - 05:46 PM

Damn guys. I love u *nohomo*

I dont know what sh*t i tryed yesterday and the last minutes but now it works with this simple solution

noBlack.RenderTarget[0].BlendEnable = true;
noBlack.RenderTarget[0].SrcBlend	   = D3D11_BLEND_ONE;
noBlack.RenderTarget[0].DestBlend	  = D3D11_BLEND_ONE;
noBlack.RenderTarget[0].BlendOp	    = D3D11_BLEND_OP_ADD;
noBlack.RenderTarget[0].SrcBlendAlpha  = D3D11_BLEND_ONE;
noBlack.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ONE;
noBlack.RenderTarget[0].BlendOpAlpha   = D3D11_BLEND_OP_ADD;

how could i be so stupid not try to set all to add if its called "additive" blending.. damn im retarded.
and ur totally right jtippets. now that it is correct additive blending the black disappears without cliping

thank you both very much

p.s: for someone else if he has this problem. preventing the artefacts which comes up with additive blending solution: before drawing the object which uses the additive blending set depth stencil which is like:

D3D11_DEPTH_STENCIL_DESC mirrorDesc;
mirrorDesc.DepthEnable	  = true;
mirrorDesc.DepthWriteMask   = D3D11_DEPTH_WRITE_MASK_ZERO;
    mirrorDesc.DepthFunc	    = D3D11_COMPARISON_LESS;
    mirrorDesc.StencilEnable    = true;
    mirrorDesc.StencilReadMask  = 0xff;
    mirrorDesc.StencilWriteMask = 0xff;
    mirrorDesc.FrontFace.StencilFailOp	  = D3D11_STENCIL_OP_KEEP;
mirrorDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP;
mirrorDesc.FrontFace.StencilPassOp	  = D3D11_STENCIL_OP_REPLACE;
mirrorDesc.FrontFace.StencilFunc	    = D3D11_COMPARISON_ALWAYS;

and after drawing reset it

Edited by ~Helgon, 24 November 2012 - 05:47 PM.

from time to time i find time





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