Rendering to texture

Source: Internet
Author: User
Rendering to textures is an advanced technology in d3d. On the one hand, it is very simple, on the other hand it is very powerful and can produce many special effects. For example, the luminous effect, Environment ing, and shadow ing can all be achieved through it. Rendering to texture is only an extension of rendering to the surface. We only need to add more. First, we need to create a texture and take preventive measures. In the second step, we can render the appropriate scenario to the texture we created. Then, we use this texture in the final rendering.
· Main. cpp
First, we need to declare the required object. Of course, we need a texture for rendering. In addition, we need two surface objects. One is used to store the background buffer, and the other is used as the rendering object of the texture. I will introduce them in detail later. In addition, we need two matrices, one for Texture projection and the other for storing the original matrix.
Lpdirect3dtexture9 prendertexture = NULL;
Lpdirect3dsurface9 prendersurface = NULL, pbackbuffer = NULL;
D3dxmatrix matprojection, matoldprojection;
Now let's create a texture. The first two parameters are the width and height of the texture. The third parameter is the hierarchical progressive texture sequence parameter of the texture. Here it is set to 1. The fourth parameter is very important and must be set to d3dusage_rendertarget, it indicates that the texture we created is used for rendering. The remaining parameters are the fingerprint format, the memory location of the vertex buffer, and a pointer to the texture. When a texture is used to render an object, the memory position of the vertex buffer must be set to d3d_defailt.
G_app.getdevice ()-> createtexture (256,256, 1, d3dusage_rendertarget, d3dfmt_r5g6b5, d3dpool_default, & prendertexture, null );
To access texture memory objects, we need a surface object, because the texture in d3d uses this surface to store texture data. To obtain the surface of the texture surface, call the getsurfacelevel () method (). The first parameter is set to 0, and the second parameter is a pointer to the surface object.
Prendertexture-> getsurfacelevel (0, & prendersurface );
The next step is to create a projection matrix suitable for the texture dimension, because the texture's horizontal/vertical ratio is different from the background buffer.
D3dxmatrixperspectivefovlh (& matprojection, d3dx_pi/4.0f, 1,1, 100 );
Before circular rendering, we must save the background buffer and its projection matrix.
G_app.getdevice ()-> gettransform (d3dts_projection, & matoldprojection );
G_app.getdevice ()-> getrendertarget (0, & pbackbuffer );
The rendering loop function can be divided into two parts. The first part is the process of rendering to the texture. Therefore, the rendering object must be set as a texture surface. Then we can render the object. Rendering to another surface is similar to normal rendering to the background buffer. The difference is that the prensent () function is not called first, because the content on the texture does not need to be displayed on the screen. As usual, we need to reset the surface color buffer and call the beginsence () and endsence () methods. To make proper rendering, we must set a projection matrix that matches the texture surface. Otherwise, the final image may be distorted.
// Render-to-texture
G_app.getdevice ()-> setrendertarget (0, prendersurface); // set new render target
G_app.getdevice ()-> clear (0, null, d3dclear_target | d3dclear_zbuffer, d3dcolor_xrgb (100,100,100), 1.0f, 0); // clear texture
G_app.getdevice ()-> beginscene ();
G_app.getdevice ()-> settexture (0, ppyramidetexture );
D3dxmatrixrotationy (& matrotationy, frotation );
D3dxmatrixtranslation (& mattranslation, 0.0f, 0.0f, 5.0f );
G_app.getdevice ()-> settransform (d3dts_world, & (matrotationy * mattranslation ));
G_app.getdevice ()-> settransform (d3dts_projection, & matprojection); // set Projection Matrix
G_app.getdevice ()-> setstreamsource (0, ptrianglevb, 0, sizeof (d3dvertex ));
G_app.getdevice ()-> drawprimitive (d3dpt_trianglelist, 0, 4 );
G_app.getdevice ()-> endscene ();
The second part of the rendering loop is the process of rendering the final scene (that is, the process of displaying the screen ). The rendering object is reset to the background buffer, and the projection matrix is reset to the original projection matrix. As the texture is ready, it is associated with texture layer 0.
// Render scene with texture
G_app.getdevice ()-> setrendertarget (0, pbackbuffer); // set back buffer
G_app.getdevice ()-> clear (0, null, d3dclear_target | d3dclear_zbuffer, d3dcolor_xrgb (0, 0), 1.0f, 0 );
G_app.getdevice ()-> beginscene ();
G_app.getdevice ()-> settexture (0, prendertexture); // set rendered texture
G_app.getdevice ()-> settransform (d3dts_world, & mattranslation );
G_app.getdevice ()-> settransform (d3dts_projection, & matoldprojection); // restore Projection Matrix
G_app.getdevice ()-> setstreamsource (0, pquadvb, 0, sizeof (d3dvertex ));
G_app.getdevice ()-> drawprimitive (d3dpt_trianglestrip, 0, 2 );
G_app.getdevice ()-> endscene ();
G_app.getdevice ()-> present (null, null );
Finally, we call the release () method to release the surface object.
Prendersurface-> release ();
Prendersurface = NULL;
Pbackbuffer-> release ();
Pbackbuffer = NULL;
Rendering texture allows you to do many things, but you must pay attention to some limitations. First, the depth buffer must always be greater than or equal to the rendering object size. In addition, the format of the rendering object and the depth buffer must be consistent.

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.