{"id":5146,"date":"2016-07-17T06:28:29","date_gmt":"2016-07-17T06:28:29","guid":{"rendered":"http:\/\/anthroponaute.fr\/blog-informatique\/?p=5146"},"modified":"2016-08-28T15:52:31","modified_gmt":"2016-08-28T15:52:31","slug":"une-classe-texturescreenquad-pour-les-effets-de-post-traitement","status":"publish","type":"post","link":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/?p=5146","title":{"rendered":"Une classe TextureScreenQuad pour afficher vos effets shaders de Post-Traitement"},"content":{"rendered":"<p><a href=\"https:\/\/anthropoya.cluster014.ovh.net\/blog-informatique\/wp-content\/uploads\/2016\/07\/fig-1-war-and-peace1.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-5161\" src=\"https:\/\/anthropoya.cluster014.ovh.net\/blog-informatique\/wp-content\/uploads\/2016\/07\/fig-1-war-and-peace1.png\" alt=\"fig-1-war-and-peace\" width=\"601\" height=\"375\" srcset=\"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/wp-content\/uploads\/2016\/07\/fig-1-war-and-peace1.png 1024w, https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/wp-content\/uploads\/2016\/07\/fig-1-war-and-peace1-300x187.png 300w, https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/wp-content\/uploads\/2016\/07\/fig-1-war-and-peace1-624x389.png 624w\" sizes=\"(max-width: 601px) 100vw, 601px\" \/><\/a><\/p>\n<p><strong>Intro :<\/strong><\/p>\n<p>Dans un jeu, nous avons besoin d&rsquo;afficher une texture \/ image qui <strong>prend la place de tout l&rsquo;\u00e9cran<\/strong>. Cette image peut \u00eatre modifi\u00e9e pour appliquer un rendu de post-effet avec un fichier shader .fx.<\/p>\n<p><strong>Explications :<\/strong><\/p>\n<p>Nous nous servons d&rsquo;un tampon de <strong>vertices<\/strong> formant un<strong> carr\u00e9<\/strong> ou d&rsquo;un <strong>quad<\/strong>.<\/p>\n<p>Voici le fichier TextureScreenQuad.h :<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#ifndef TEXTURE_QUAD_H\r\n#define TEXTURE_QUAD_H\r\n\r\n#include &lt;d3dx10.h&gt;\r\n#include &lt;string&gt;\r\n\r\n#include &quot;VertexDeclarations.h&quot;\r\n#include &quot;Renderable.h&quot;\r\n#include &quot;RenderTarget.h&quot;\r\n#include &quot;MeshSceneNode.h&quot;\r\n\r\nclass TextureScreenQuad : public MeshSceneNode\r\n{\r\npublic:\r\n\u00a0\u00a0 \u00a0TextureScreenQuad();\r\n\u00a0\u00a0 \u00a0virtual ~TextureScreenQuad();\r\n\r\n\u00a0\u00a0 \u00a0bool Initialize();\r\n\u00a0\u00a0 \u00a0virtual void OnRender(float fTimeSinceLastFrame) override;\r\n\r\n\u00a0\u00a0 \u00a0void SetTransparency(float fValue);\r\n\u00a0\u00a0 \u00a0void SetColor(const D3DXCOLOR&amp; color);\r\n\u00a0\u00a0 \u00a0void SetQuadSize(uint32 iWidth, uint32 iHeight);\r\n\r\n\u00a0\u00a0 \u00a0RenderTarget* GetRT();\r\n\r\n\u00a0\u00a0 \u00a0virtual bool SetShaderTechnique(ShaderTechnique* pShader) override;\r\n\r\nprotected:\r\n\u00a0\u00a0 \u00a0bool Update();\r\n\r\nprivate:\r\n\u00a0\u00a0 \u00a0int m_iScreenWidth;\r\n\u00a0\u00a0 \u00a0int m_iScreenHeight;\r\n\r\n\u00a0\u00a0 \u00a0ID3D10ShaderResourceView* m_pTextureRessourceView;\r\n\r\n\u00a0\u00a0 \u00a0float m_fAlpha;\r\n\r\n\u00a0\u00a0 \u00a0PTVertex m_aQuadVertices[6];\r\n\r\n\u00a0\u00a0 \u00a0RenderTarget* m_pRT;\r\n\u00a0\u00a0 \u00a0D3DXCOLOR m_Color;\r\n};\r\n\r\n#endif\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Voici le fichier TextureScreenQuad.cpp :<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &quot;TextureScreenQuad.h&quot;\r\n#include &quot;Defines.h&quot;\r\n#include &quot;D3D10Renderer.h&quot;\r\n#include &quot;ShaderTechnique.h&quot;\r\n#include &quot;ShaderTechnique_Declarations.h&quot;\r\n\r\nTextureScreenQuad::TextureScreenQuad() :\r\nMeshSceneNode(&quot;TextureScreenQuad&quot;),\r\nm_fAlpha(1.0f),\r\nm_pRT(nullptr)\r\n{\r\n\u00a0\u00a0 \u00a0ZeroMemory(&amp;m_aQuadVertices, sizeof(PTVertex) * 6);\r\n\u00a0\u00a0 \u00a0SetDrawMethod(DRAW_INDEXED);\r\n\u00a0\u00a0 \u00a0SetVertexType(VertexLayoutType::PT_VERTEX);\r\n}\r\n\r\nTextureScreenQuad::~TextureScreenQuad()\r\n{\r\n\u00a0\u00a0 \u00a0SAFE_RELEASE(m_pTextureRessourceView);\r\n}\r\n\r\nbool TextureScreenQuad::Initialize()\r\n{\r\n\u00a0\u00a0 \u00a0m_iScreenWidth = D3D10_RENDERER-&gt;GetViewportWidth();\r\n\u00a0\u00a0 \u00a0m_iScreenHeight = D3D10_RENDERER-&gt;GetViewportHeight();\r\n\r\n\u00a0\u00a0 \u00a0if (!Update())\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0MessageBoxA(nullptr, &quot;Erreur d'initialisation d'un TextureScreenQuad !&quot;, &quot;Erreur&quot;, MB_ICONHAND | MB_OK);\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0return true;\r\n}\r\n\r\nvoid TextureScreenQuad::SetTransparency(float fValue)\r\n{\r\n\u00a0\u00a0 \u00a0m_Color.a = fValue;\r\n\r\n\u00a0\u00a0 \u00a0if (GetShaderTechnique() != nullptr)\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0GetShaderTechnique()-&gt;SetColor(&quot;Color&quot;, m_Color);\r\n\u00a0\u00a0 \u00a0}\r\n}\r\n\r\nvoid TextureScreenQuad::SetColor(const D3DXCOLOR&amp; color)\r\n{\r\n\u00a0\u00a0 \u00a0m_Color = color;\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0if (GetShaderTechnique() != nullptr)\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0GetShaderTechnique()-&gt;SetColor(&quot;Color&quot;, m_Color);\r\n\u00a0\u00a0 \u00a0}\r\n}\r\n\r\nvoid TextureScreenQuad::OnRender(float fTimeSinceLastFrame)\r\n{\r\n\u00a0\u00a0 \u00a0if (IsVisible())\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0D3D10_RENDERER-&gt;EnableZBuffer(false);\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Renderable::OnRender(fTimeSinceLastFrame);\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0D3D10_RENDERER-&gt;EnableZBuffer(true);\r\n\u00a0\u00a0 \u00a0}\r\n}\r\n\r\nbool TextureScreenQuad::Update()\r\n{\r\n\u00a0\u00a0 \u00a0float left, right, top, bottom;\r\n\r\n\u00a0\u00a0 \u00a0left = (float) -m_iScreenWidth \/ 2;\r\n\u00a0\u00a0 \u00a0right = left + (float) m_iScreenWidth;\r\n\r\n\u00a0\u00a0 \u00a0top = (float) -m_iScreenHeight \/ 2;\r\n\u00a0\u00a0 \u00a0bottom = top + (float) m_iScreenHeight;\r\n\r\n\u00a0\u00a0 \u00a0\/\/ Premier triangle\r\n\u00a0\u00a0 \u00a0m_aQuadVertices[0].pos = D3DXVECTOR3(left, top, 0.0f);\u00a0 \/\/ Top left.\r\n\u00a0\u00a0 \u00a0m_aQuadVertices[0].texture = D3DXVECTOR2(0.0f, 0.0f);\r\n\r\n\u00a0\u00a0 \u00a0m_aQuadVertices[1].pos = D3DXVECTOR3(right, bottom, 0.0f);\u00a0 \/\/ Bottom right.\r\n\u00a0\u00a0 \u00a0m_aQuadVertices[1].texture = D3DXVECTOR2(1.0f, 1.0f);\r\n\r\n\u00a0\u00a0 \u00a0m_aQuadVertices[2].pos = D3DXVECTOR3(left, bottom, 0.0f);\u00a0 \/\/ Bottom left.\r\n\u00a0\u00a0 \u00a0m_aQuadVertices[2].texture = D3DXVECTOR2(0.0f, 1.0f);\r\n\r\n\u00a0\u00a0 \u00a0\/\/ Deuxi\u00e8me triangle\r\n\u00a0\u00a0 \u00a0m_aQuadVertices[3].pos = D3DXVECTOR3(left, top, 0.0f);\u00a0 \/\/ Top left.\r\n\u00a0\u00a0 \u00a0m_aQuadVertices[3].texture = D3DXVECTOR2(0.0f, 0.0f);\r\n\r\n\u00a0\u00a0 \u00a0m_aQuadVertices[4].pos = D3DXVECTOR3(right, top, 0.0f);\u00a0 \/\/ Top right.\r\n\u00a0\u00a0 \u00a0m_aQuadVertices[4].texture = D3DXVECTOR2(1.0f, 0.0f);\r\n\r\n\u00a0\u00a0 \u00a0m_aQuadVertices[5].pos = D3DXVECTOR3(right, bottom, 0.0f);\u00a0 \/\/ Bottom right.\r\n\u00a0\u00a0 \u00a0m_aQuadVertices[5].texture = D3DXVECTOR2(1.0f, 1.0f);\r\n\r\n\u00a0\u00a0 \u00a0\/\/ On d\u00e9clare les tableaux\r\n\u00a0\u00a0 \u00a0std::vector&lt;PTVertex&gt; vertices;\r\n\u00a0\u00a0 \u00a0\/\/ On reset les emplacements\r\n\u00a0\u00a0 \u00a0vertices.resize(6);\r\n\u00a0\u00a0 \u00a0\/\/ On affecte les valeurs\r\n\u00a0\u00a0 \u00a0std::copy(m_aQuadVertices, m_aQuadVertices + 6, vertices.begin());\r\n\r\n\u00a0\u00a0 \u00a0unsigned short i[6] = { 0, 1, 2, 3, 4, 5 };\r\n\r\n\u00a0\u00a0 \u00a0\/\/ On d\u00e9clare les tableaux\r\n\u00a0\u00a0 \u00a0std::vector&lt;unsigned short&gt; indices;\r\n\u00a0\u00a0 \u00a0\/\/ On reset les emplacements\r\n\u00a0\u00a0 \u00a0indices.resize(6);\r\n\u00a0\u00a0 \u00a0\/\/ On affecte les valeurs\r\n\u00a0\u00a0 \u00a0std::copy(i, i + 6, indices.begin());\r\n\r\n\u00a0\u00a0 \u00a0BuildMesh(vertices, indices);\r\n\r\n\u00a0\u00a0 \u00a0return true;\r\n}\r\n\r\nvoid TextureScreenQuad::SetQuadSize(uint32 iWidth, uint32 iHeight)\r\n{\r\n\u00a0\u00a0 \u00a0m_iScreenWidth = iWidth;\r\n\u00a0\u00a0 \u00a0m_iScreenHeight = iHeight;\r\n\r\n\u00a0\u00a0 \u00a0Update();\r\n}\r\n\r\nRenderTarget* TextureScreenQuad::GetRT()\r\n{\r\n\u00a0\u00a0 \u00a0return m_pRT;\r\n}\r\n\r\nbool TextureScreenQuad::SetShaderTechnique(ShaderTechnique* pShader)\r\n{\r\n\u00a0\u00a0 \u00a0bool bSuccess = Renderable::SetShaderTechnique(pShader);\r\n\r\n\u00a0\u00a0 \u00a0if (m_pRT == nullptr)\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0m_pRT = new RenderTarget();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0m_pRT-&gt;Initialize(m_iScreenWidth, m_iScreenHeight);\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0ShaderTechnique_FXAA* pFXAA = dynamic_cast&lt;ShaderTechnique_FXAA*&gt; (GetShaderTechnique());\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0pFXAA-&gt;SetRenderTarget(m_pRT);\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0SetColor(D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f));\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0return bSuccess;\r\n}\r\n\r\n<\/pre>\n<p><strong><br \/>\nR\u00e9sum\u00e9 : <\/strong><\/p>\n<p>Nous avons pr\u00e9sent\u00e9 une fa\u00e7on d&rsquo;utiliser un <strong>Quad<\/strong> de de <strong>post-traitement<\/strong> afin de modifier les couleurs de rendu du front buffer.<\/p>\n<p><strong>R\u00e9f\u00e9rences :<\/strong><\/p>\n<p>&#8211; Stackoverflow.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Intro : Dans un jeu, nous avons besoin d&rsquo;afficher une texture \/ image qui prend la place de tout l&rsquo;\u00e9cran. Cette image peut \u00eatre modifi\u00e9e pour appliquer un rendu de post-effet avec un fichier shader .fx. Explications : Nous nous servons d&rsquo;un tampon de vertices formant un carr\u00e9 ou d&rsquo;un quad. Voici le fichier TextureScreenQuad.h [&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\/5146"}],"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=5146"}],"version-history":[{"count":15,"href":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/index.php?rest_route=\/wp\/v2\/posts\/5146\/revisions"}],"predecessor-version":[{"id":5516,"href":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/index.php?rest_route=\/wp\/v2\/posts\/5146\/revisions\/5516"}],"wp:attachment":[{"href":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}