Directx11 tutorial (65) rendering to texture

Source: Internet
Author: User

Generally, our render target is post-buffered, but you can also set the render target as a 2D texture, and then display the 2D texture through the texture method, you can display multiple scenes on one screen.

In this tutorial, we use mytutoriald3d11_59 to render the texture.

First, create a rendertextureclass. In this class, we create a 2D texture, set this 2D texture to render target, and have a m_shaderresourceview object through which, we can access this 2D texture. The subsequent 2D Rendering uses this object to implement texture operations.

The rendertexture class is very simple. The main variables and functions are:

Class rendertextureclass
{
Public:
Rendertextureclass ();
Rendertextureclass (const rendertextureclass &);
~ Rendertextureclass ();

Bool initialize (id3d11device *, Int, INT );
Void Shutdown ();

Void setrendertarget (id3d11devicecontext *, id3d11depthstencilview *);
Void clearrendertarget (id3d11devicecontext *, id3d11depthstencilview *, float );
Id3d11shaderresourceview * getshaderresourceview ();

PRIVATE:
Id3d11texture2d * m_rendertargettexture; // render target view
Id3d11rendertargetview * m_rendertargetview;
Id3d11shaderresourceview * m_shaderresourceview; // shader Resource
};

Next, we will create a debugwindowclass class, which is used to display a small window on the screen and put some common information in this window class. This class uses 2D rendering, that is, the input projection matrix. It is a parallel projection matrix instead of a perspective projection matrix. In d3cclass, we use m_d3d-> getorthomatrix (orthomatrix); to obtain the parallel projection matrix. This class creates a rectangle by drawing two triangles, and then displays the input texture in the rectangle frame by pasting the texture.

This class uses textureshaderclass for rendering. The shader files used are texture. Vs and texture. PS.

In graphicsclass, the code of the rendering scenario has changed. We need to render two passes in the scenario, the first pass, change the render target, and render the scenario to the texture. In the second pass normal Rendering scenario, debugwindowclass is used to display the texture generated in the first pass through textures. The main code is as follows:

Bool graphicsclass: frame (float DT)
{
D3dxmatrix worldmatrix, viewmatrix, orthomatrix;
Bool result;

// Render the scene to the texture
Result = rendertotexture (DT );
If (! Result)
{
Return false;
}

// Clear the rendering background.
M_d3d-> beginscene (0.0f, 0.0f, 0.0f, 1.0f );
// Call the render function to render 3D scenes
// Render is the private function of graphicsclass.
Result = render (DT );
If (! Result)
{
Return false;
}
// Turn off zbuffer and start 2D rendering.
M_d3d-> turnzbufferoff ();

// Obtain the world, view, and orthogonal projection matrix.
D3dxmatrixidentity (& worldmatrix );
M_camera-> getviewmatrix (& viewmatrix );
M_d3d-> getorthomatrix (orthomatrix );

// Prepare the vertex buffer of the debug small window.
Result = m_debugwindow-> render (m_d3d-> getdevicecontext (), 20, 10 );
If (! Result)
{
Return false;
}

// Use the texture shader to render the debug small window, use a Paster to render the previous scene to the texture, and paste it in the small window.
Result = m_textureshader-> render (m_d3d-> getdevicecontext (), m_debugwindow-> getindexcount (), worldmatrix, viewmatrix,
Orthomatrix, m_rendertexture-> getshaderresourceview ());
If (! Result)
{
Return false;
}

// Enable Z buffer
M_d3d-> turnzbuffon ();

// Present the rendered scene to the screen
M_d3d-> endscene ();
Return true;
}

After the program is executed, the interface is as follows:

Complete code can be found:

Project File mytutoriald3d11_62

Download Code:

Provided later

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.