{"id":5128,"date":"2016-07-17T06:20:34","date_gmt":"2016-07-17T06:20:34","guid":{"rendered":"http:\/\/anthroponaute.fr\/blog-informatique\/?p=5128"},"modified":"2016-07-17T14:06:28","modified_gmt":"2016-07-17T14:06:28","slug":"une-classe-rendertarget-pour-afficher-un-tampon-dimage-de-rendu","status":"publish","type":"post","link":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/?p=5128","title":{"rendered":"Une classe RenderTarget pour afficher un tampon d&rsquo;image de rendu"},"content":{"rendered":"<p><a href=\"https:\/\/anthropoya.cluster014.ovh.net\/blog-informatique\/wp-content\/uploads\/2016\/07\/rtt2.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-5151\" src=\"https:\/\/anthropoya.cluster014.ovh.net\/blog-informatique\/wp-content\/uploads\/2016\/07\/rtt2.png\" alt=\"rtt\" width=\"486\" height=\"331\" srcset=\"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/wp-content\/uploads\/2016\/07\/rtt2.png 967w, https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/wp-content\/uploads\/2016\/07\/rtt2-300x204.png 300w, https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/wp-content\/uploads\/2016\/07\/rtt2-624x425.png 624w\" sizes=\"(max-width: 486px) 100vw, 486px\" \/><\/a><\/p>\n<p><strong>Intro :<\/strong><\/p>\n<p>Nous avons parfois besoin de <strong>stocker le rendu 3D dans une texture<\/strong> que l&rsquo;on pourra utiliser n&rsquo;importe tout dans le programme !<\/p>\n<p><strong>Explications :<\/strong><\/p>\n<p>Voici le fichier RenderToTexture.h :<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#ifndef RENDER_TO_TEXTURE_H\r\n#define RENDER_TO_TEXTURE_H\r\n\r\n#include &lt;d3d10.h&gt;\r\n#include &quot;Types.h&quot;\r\n\r\nclass RenderTarget\r\n{\r\npublic:\r\n\u00a0\u00a0 \u00a0RenderTarget();\r\n\u00a0\u00a0 \u00a0virtual ~RenderTarget();\r\n\r\n\u00a0\u00a0 \u00a0bool Initialize(uint32 iTextureWidth, uint32 iTextureHeight);\r\n\r\n\u00a0\u00a0 \u00a0void SetRenderTarget(ID3D10DepthStencilView* pDepthStencilView);\r\n\u00a0\u00a0 \u00a0void ClearRenderTarget(ID3D10DepthStencilView* pDepthStencilView);\r\n\r\n\u00a0\u00a0 \u00a0ID3D10ShaderResourceView* GetShaderResourceView();\r\n\r\nprivate:\r\n\u00a0\u00a0 \u00a0ID3D10Texture2D* m_pRenderTargetTexture;\r\n\u00a0\u00a0 \u00a0ID3D10RenderTargetView* m_pRenderTargetView;\r\n\u00a0\u00a0 \u00a0ID3D10ShaderResourceView* m_pShaderResourceView;\r\n};\r\n\r\n#endif\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Voici le fichier RenderToTexture.cpp :<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &quot;Defines.h&quot;\r\n#include &quot;RenderTarget.h&quot;\r\n#include &quot;D3D10Renderer.h&quot;\r\n\r\nRenderTarget::RenderTarget() :\r\nm_pRenderTargetTexture(nullptr),\r\nm_pRenderTargetView(nullptr),\r\nm_pShaderResourceView(nullptr)\r\n{\r\n}\r\n\r\nRenderTarget::~RenderTarget()\r\n{\r\n\u00a0\u00a0 \u00a0SAFE_RELEASE(m_pRenderTargetTexture);\r\n\u00a0\u00a0 \u00a0SAFE_RELEASE(m_pRenderTargetView);\r\n\u00a0\u00a0 \u00a0SAFE_RELEASE(m_pShaderResourceView);\r\n}\r\n\r\nbool RenderTarget::Initialize(uint32 iTextureWidth, uint32 iTextureHeight)\r\n{\r\n\u00a0\u00a0 \u00a0\/* On cr\u00e9\u00e9 les descriptions de texture 2D, de la render target et de la ressource du shader *\/\r\n\u00a0\u00a0 \u00a0D3D10_TEXTURE2D_DESC textureDesc;\r\n\u00a0\u00a0 \u00a0D3D10_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;\r\n\u00a0\u00a0 \u00a0D3D10_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;\r\n\r\n\u00a0\u00a0 \u00a0HRESULT hr;\r\n\u00a0\u00a0 \u00a0\/\/ Initialize the render target texture description.\r\n\u00a0\u00a0 \u00a0ZeroMemory(&amp;textureDesc, sizeof(textureDesc));\r\n\r\n\u00a0\u00a0 \u00a0\/\/ Setup the render target texture description.\r\n\u00a0\u00a0 \u00a0textureDesc.Width = iTextureWidth;\r\n\u00a0\u00a0 \u00a0textureDesc.Height = iTextureHeight;\r\n\u00a0\u00a0 \u00a0textureDesc.MipLevels = 1;\r\n\u00a0\u00a0 \u00a0textureDesc.ArraySize = 1;\r\n\u00a0\u00a0 \u00a0textureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;\r\n\u00a0\u00a0 \u00a0textureDesc.SampleDesc.Count = 1;\r\n\u00a0\u00a0 \u00a0textureDesc.Usage = D3D10_USAGE_DEFAULT;\r\n\u00a0\u00a0 \u00a0textureDesc.BindFlags = D3D10_BIND_RENDER_TARGET | D3D10_BIND_SHADER_RESOURCE;\r\n\u00a0\u00a0 \u00a0textureDesc.CPUAccessFlags = 0;\r\n\u00a0\u00a0 \u00a0textureDesc.MiscFlags = 0;\r\n\r\n\u00a0\u00a0 \u00a0\/\/ Create the render target texture.\r\n\u00a0\u00a0 \u00a0hr = D3D10_RENDERER-&gt;GetDevice()-&gt;CreateTexture2D(&amp;textureDesc, NULL, &amp;m_pRenderTargetTexture);\r\n\u00a0\u00a0 \u00a0if (FAILED(hr))\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return false;\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0\/\/ Setup the description of the render target view.\r\n\u00a0\u00a0 \u00a0renderTargetViewDesc.Format = textureDesc.Format;\r\n\u00a0\u00a0 \u00a0renderTargetViewDesc.ViewDimension = D3D10_RTV_DIMENSION_TEXTURE2D;\r\n\u00a0\u00a0 \u00a0renderTargetViewDesc.Texture2D.MipSlice = 0;\r\n\r\n\u00a0\u00a0 \u00a0\/\/ Create the render target view.\r\n\u00a0\u00a0 \u00a0hr = D3D10_RENDERER-&gt;GetDevice()-&gt;CreateRenderTargetView(m_pRenderTargetTexture, &amp;renderTargetViewDesc, &amp;m_pRenderTargetView);\r\n\u00a0\u00a0 \u00a0if (FAILED(hr))\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return false;\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0\/\/ Setup the description of the shader resource view.\r\n\u00a0\u00a0 \u00a0shaderResourceViewDesc.Format = textureDesc.Format;\r\n\u00a0\u00a0 \u00a0shaderResourceViewDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;\r\n\u00a0\u00a0 \u00a0shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;\r\n\u00a0\u00a0 \u00a0shaderResourceViewDesc.Texture2D.MipLevels = 1;\r\n\r\n\u00a0\u00a0 \u00a0\/\/ Create the shader resource view.\r\n\u00a0\u00a0 \u00a0hr = D3D10_RENDERER-&gt;GetDevice()-&gt;CreateShaderResourceView(m_pRenderTargetTexture, &amp;shaderResourceViewDesc, &amp;m_pShaderResourceView);\r\n\u00a0\u00a0 \u00a0if (FAILED(hr))\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return false;\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0return true;\r\n}\r\n\r\nvoid RenderTarget::SetRenderTarget(ID3D10DepthStencilView* pDepthStencilView)\r\n{\r\n\u00a0\u00a0 \u00a0D3D10_RENDERER-&gt;GetDevice()-&gt;OMSetRenderTargets(1, &amp;m_pRenderTargetView, pDepthStencilView);\r\n}\r\n\r\nvoid RenderTarget::ClearRenderTarget(ID3D10DepthStencilView* pDepthStencilView)\r\n{\r\n\u00a0\u00a0 \u00a0float color[4] = { 1.0f, 1.0f, 1.0f, 1.0f };\r\n\r\n\u00a0\u00a0 \u00a0\/\/ On efface le back buffer\r\n\u00a0\u00a0 \u00a0D3D10_RENDERER-&gt;GetDevice()-&gt;ClearRenderTargetView(m_pRenderTargetView, color);\r\n\r\n\u00a0\u00a0 \u00a0\/\/ On efface le depth buffer\r\n\u00a0\u00a0 \u00a0D3D10_RENDERER-&gt;GetDevice()-&gt;ClearDepthStencilView(pDepthStencilView, D3D10_CLEAR_DEPTH, 1.0f, 0);\r\n}\r\n\r\nID3D10ShaderResourceView* RenderTarget::GetShaderResourceView()\r\n{\r\n\u00a0\u00a0 \u00a0return m_pShaderResourceView;\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Comment l&rsquo;utiliser ?<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nvoid D3D10Renderer::Render()\r\n{\r\n\u00a0\u00a0 \u00a0[...]\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0\u00a0 \/\/ Efface la surface de rendu\r\n\u00a0\u00a0 \u00a0m_pd3dDevice-&gt;ClearDepthStencilView(m_pDepthStencilView, D3D10_CLEAR_DEPTH, 1.0f, 0 );\r\n\u00a0\u00a0\u00a0 m_pd3dDevice-&gt;ClearRenderTargetView(m_pRenderTargetView, afClearColor);\r\n\r\n\u00a0\u00a0 \u00a0SCENE_MANAGER-&gt;DrawAll(fTimeSinceLastFrame);\r\n\r\n\u00a0\u00a0 \u00a0RenderToTexture(fTimeSinceLastFrame);\r\n\r\n\u00a0\u00a0 \u00a0m_pQuad-&gt;OnRender(fTimeSinceLastFrame);\r\n\r\n\u00a0\u00a0 \u00a0m_pSwapChain-&gt;Present(0, 0);\r\n}\r\n\r\nvoid D3D10Renderer::RenderToTexture(float fTimeSinceLastFrame)\r\n{\r\n\u00a0\u00a0 \u00a0\/\/ Set the render target to be the render to texture.\r\n\u00a0\u00a0 \u00a0m_pQuad-&gt;GetRT()-&gt;SetRenderTarget(m_pDepthStencilView);\r\n\r\n\u00a0\u00a0 \u00a0\/\/ Clear the render to texture.\r\n\u00a0\u00a0 \u00a0m_pQuad-&gt;GetRT()-&gt;ClearRenderTarget(m_pDepthStencilView);\r\n\r\n\u00a0\u00a0 \u00a0\/\/ Render the scene now and it will draw to the render to texture instead of the back buffer.\r\n\u00a0\u00a0 \u00a0SCENE_MANAGER-&gt;DrawAll(fTimeSinceLastFrame);\r\n\r\n\u00a0\u00a0 \u00a0\/\/ Reset the render target back to the original back buffer and not the render to texture anymore.\r\n\u00a0\u00a0 \u00a0m_pd3dDevice-&gt;OMSetRenderTargets(1, &amp;m_pRenderTargetView, m_pDepthStencilView);\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>R\u00e9sum\u00e9 :<\/strong><\/p>\n<p>Nous avons pr\u00e9senter la fa\u00e7on la plus optimale possible d&rsquo;utiliser une RenderTarget !<\/p>\n<p><strong>R\u00e9f\u00e9rences :<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Intro : Nous avons parfois besoin de stocker le rendu 3D dans une texture que l&rsquo;on pourra utiliser n&rsquo;importe tout dans le programme ! Explications : Voici le fichier RenderToTexture.h : &nbsp; Voici le fichier RenderToTexture.cpp : &nbsp; Comment l&rsquo;utiliser ? &nbsp; R\u00e9sum\u00e9 : Nous avons pr\u00e9senter la fa\u00e7on la plus optimale possible d&rsquo;utiliser une [&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\/5128"}],"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=5128"}],"version-history":[{"count":15,"href":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/index.php?rest_route=\/wp\/v2\/posts\/5128\/revisions"}],"predecessor-version":[{"id":5153,"href":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/index.php?rest_route=\/wp\/v2\/posts\/5128\/revisions\/5153"}],"wp:attachment":[{"href":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}