{"id":3331,"date":"2015-07-28T11:13:42","date_gmt":"2015-07-28T11:13:42","guid":{"rendered":"http:\/\/anthroponaute.fr\/blog-informatique\/?p=3331"},"modified":"2015-12-03T22:04:41","modified_gmt":"2015-12-03T22:04:41","slug":"une-classe-alphapass-pour-specifier-la-transparence","status":"publish","type":"post","link":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/?p=3331","title":{"rendered":"Une classe AlphaPass pour activer la transparence (fa\u00e7on RAII)"},"content":{"rendered":"<div class=\"entry-content\">\n<p><a href=\"https:\/\/anthropoya.cluster014.ovh.net\/blog-informatique\/wp-content\/uploads\/2015\/07\/alpha.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-3333\" src=\"https:\/\/anthropoya.cluster014.ovh.net\/blog-informatique\/wp-content\/uploads\/2015\/07\/alpha.png\" alt=\"alpha\" width=\"197\" height=\"197\" srcset=\"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/wp-content\/uploads\/2015\/07\/alpha.png 330w, https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/wp-content\/uploads\/2015\/07\/alpha-150x150.png 150w, https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/wp-content\/uploads\/2015\/07\/alpha-300x300.png 300w\" sizes=\"(max-width: 197px) 100vw, 197px\" \/><\/a><\/p>\n<p><strong>Intro :<\/strong><\/p>\n<p>Parfois on veut afficher certaines mod\u00e8les tout en activant la transparence.<\/p>\n<p>Ici je pr\u00e9sente une fa\u00e7on simple de g\u00e9rer cela avec le principe du RAII.<\/p>\n<p><strong>Pr\u00e9requis :<\/strong><\/p>\n<p>&#8211; Comprendre le principe du RAII<\/p>\n<p>&#8211; Savoir initialiser DirectX 10 \u00e0 travers la classe <em>D3D10Renderer<\/em><\/p>\n<p><strong>Explications :<\/strong><\/p>\n<p>Voici le code du fichier AlphaPass.h :<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#ifndef ALPHA_PASS_H\r\n#define ALPHA_PASS_H\r\n\r\n#include &lt;d3dx10math.h&gt;\r\n\r\nclass AlphaPass\r\n{\r\npublic:\r\n\u00a0\u00a0 \u00a0AlphaPass();\r\n\u00a0\u00a0 \u00a0virtual ~AlphaPass();\r\n\r\nprotected:\r\n\u00a0\u00a0 \u00a0ID3D10BlendState* m_pOldBlendState;\r\n\u00a0\u00a0 \u00a0float m_OldBlendFactor[4];\r\n\u00a0\u00a0\u00a0 unsigned int m_OldSampleMask;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0ID3D10BlendState* m_pCurrentBlendState;\r\n};\r\n\r\n#endif\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Voici le fichier AlphaPass.cpp :<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &quot;AlphaPass.h&quot;\r\n\r\nAlphaPass::AlphaPass() :\r\nm_pOldBlendState(nullptr),\r\nm_pCurrentBlendState(nullptr)\r\n{\r\n\u00a0\u00a0 \u00a0D3D10_RENDERER-&gt;GetDevice()-&gt;OMGetBlendState(&amp;m_pOldBlendState, m_OldBlendFactor, &amp;m_OldSampleMask);\r\n\u00a0\u00a0 \u00a0m_pCurrentBlendState = nullptr;\r\n\r\n\u00a0\u00a0 \u00a0D3D10_BLEND_DESC BlendState;\r\n\u00a0\u00a0 \u00a0ZeroMemory(&amp;BlendState, sizeof(D3D10_BLEND_DESC));\r\n\r\n\u00a0\u00a0 \u00a0BlendState.BlendEnable[0] = TRUE;\r\n\u00a0\u00a0 \u00a0BlendState.SrcBlend = D3D10_BLEND_SRC_ALPHA;\r\n\u00a0\u00a0 \u00a0BlendState.DestBlend = D3D10_BLEND_INV_SRC_ALPHA;\r\n\u00a0\u00a0 \u00a0BlendState.BlendOp = D3D10_BLEND_OP_ADD;\r\n\u00a0\u00a0 \u00a0BlendState.SrcBlendAlpha = D3D10_BLEND_ZERO;\r\n\u00a0\u00a0 \u00a0BlendState.DestBlendAlpha = D3D10_BLEND_ZERO;\r\n\u00a0\u00a0 \u00a0BlendState.BlendOpAlpha = D3D10_BLEND_OP_ADD;\r\n\u00a0\u00a0 \u00a0BlendState.RenderTargetWriteMask[0] = D3D10_COLOR_WRITE_ENABLE_ALL;\r\n\r\n\u00a0\u00a0 \u00a0D3D10_RENDERER-&gt;GetDevice()-&gt;CreateBlendState(&amp;BlendState, &amp;m_pCurrentBlendState);\r\n\u00a0\u00a0 \u00a0D3D10_RENDERER-&gt;GetDevice()-&gt;OMSetBlendState(m_pCurrentBlendState, 0, 0xffffffff);\r\n}\r\n\r\nAlphaPass::~AlphaPass()\r\n{\r\n\u00a0\u00a0 \u00a0D3D10_RENDERER-&gt;GetDevice()-&gt;OMSetBlendState(m_pOldBlendState, m_OldBlendFactor, m_OldSampleMask);\r\n\r\n\u00a0\u00a0 \u00a0SAFE_RELEASE(m_pCurrentBlendState);\r\n\u00a0\u00a0 \u00a0SAFE_RELEASE(m_pOldBlendState);\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-270\" src=\"https:\/\/anthropoya.cluster014.ovh.net\/blog-informatique\/wp-content\/uploads\/2015\/02\/warning.png\" alt=\"warning\" width=\"120\" height=\"100\" \/><\/p>\n<p>Vos entit\u00e9s transparentes doivent \u00eatre affich\u00e9es en dernier lors de le boucle de rendu et tout en d\u00e9sactivant le Z-Buffer.<\/p>\n<p><strong>R\u00e9sum\u00e9 :<\/strong><\/p>\n<p>Nous avons \u00e9tabli un objet AlphaPass qui permet d&rsquo;activer, lors de sa cr\u00e9ation <strong>sur la pile<\/strong>, la transparence dans le rendu. La transparence est restaur\u00e9 lors de la destruction de l&rsquo;objet en sortant du bloc de code.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Intro : Parfois on veut afficher certaines mod\u00e8les tout en activant la transparence. Ici je pr\u00e9sente une fa\u00e7on simple de g\u00e9rer cela avec le principe du RAII. Pr\u00e9requis : &#8211; Comprendre le principe du RAII &#8211; Savoir initialiser DirectX 10 \u00e0 travers la classe D3D10Renderer Explications : Voici le code du fichier AlphaPass.h : &nbsp; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/index.php?rest_route=\/wp\/v2\/posts\/3331"}],"collection":[{"href":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3331"}],"version-history":[{"count":31,"href":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/index.php?rest_route=\/wp\/v2\/posts\/3331\/revisions"}],"predecessor-version":[{"id":4311,"href":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/index.php?rest_route=\/wp\/v2\/posts\/3331\/revisions\/4311"}],"wp:attachment":[{"href":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}