Directx11 Tutorial Seven 2D rendering

Source: Internet
Author: User
Tags win32

This tutorial follows the architecture of the D3D11 texture tutorial, which is posted again to see



One This time: Modelclass

   int mscrrenwidth, mscrrenheight;
   int Mbitmapwidth, mbitmapheight;

These four private properties are added

First talk about the WIN32 2D coordinate system, WIN32 2D coordinate system origin in the upper left corner of the window.

The picture above probably draws a little bit


While D3d11 's 3D left-hand coordinate system origin is in the center of the window,

As shown in figure:


At this point Modelclass provides a function to call the update vertex cache per frame

BOOL Modelclass::updatebuffers (id3d11devicecontext* d3ddevicecontext, int positionx, int positiony) {// The most primitive data in the vertex cache has been changed, belonging to the dynamic vertex cache (formerly tutorial Those original vertex data, although the latter is in good faith to transform the matrix, but has not changed the original data)//How to render the position of the picture has not changed, the Exit function, this can save a lot of processing if ((Positionx = =
	MPREVIOUSPOSX) && (positiony = = Mpreviousposy)) {return true;
	}//If changing the position of the rendered picture changes, update the position mpreviousposx = Positionx;

	Mpreviousposy = Positiony;

	Find the left of the picture under the Win32 coordinates, right, top, bottom coordinates, from WIN32 coordinates posx and posy to D3d11 coordinate system float, right, top, bottom;
	left = (float) ((MSCRRENWIDTH/2) *-1) + (float) Positionx;
	right = left + (float) mbitmapwidth;
	top = (float) (MSCRRENHEIGHT/2)-(float) positiony;

	Bottom = top-(float) mbitmapheight;
	Creates a temporary vertex array Vertex *vertexs;
	Vertexs = new Vertex[mvertexcount];
	if (!vertexs) {return false;
	}//Load temporary vertex data, these are DX11 coordinates, i.e. the center of the screen is the origin vertexs[0].pos = XMFLOAT3 (left, top, 0.0f);

	Vertexs[0].color = XMFLOAT2 (0.0f, 0.0f);
	Vertexs[1].pos = XMFLOAT3 (right, bottom, 0.0f);

	Vertexs[1].color = XMFLOAT2 (1.0f, 1.0f); VERTEXS[2]. pos = XMFLOAT3 (left, bottom, 0.0f);

	Vertexs[2].color = XMFLOAT2 (0.0f, 1.0f);
	Vertexs[3].pos = XMFLOAT3 (left, top, 0.0f);

	Vertexs[3].color = XMFLOAT2 (0.0f, 0.0f);
	Vertexs[4].pos = XMFLOAT3 (right, top, 0.0f);

	Vertexs[4].color = XMFLOAT2 (1.0f, 0.0f);
	Vertexs[5].pos = XMFLOAT3 (right, bottom, 0.0f);

	Vertexs[5].color = XMFLOAT2 (1.0f, 1.0f);
	Lock the vertex cache in order to be able to write (dynamic cache cannot be written with updatesubresources) D3d11_mapped_subresource Mappedresource;

	HR (D3ddevicecontext->map (md3dvertexbuffer, 0, D3d11_map_write_discard, 0, &mappedresource));
	Gets the pointer to the vertex cache vertex* verticesptr;

	Verticesptr = (vertex*) mappedresource.pdata;

	Copy the data into the vertex cache memcpy (verticesptr, (void*) Vertexs, (sizeof (VERTEX) * mvertexcount));

	Unlock vertex cache D3ddevicecontext->unmap (md3dvertexbuffer, 0);
	Releases the vertex array delete vertexs;
	Vertexs = NULL;
return true; }

The function's formal parameter is a 2D picture in the x and Y coordinates of the WIN32 coordinates, after all, this provides the parameters for 2D mapping is very intuitive, this is used by GDI's classmates are very clear

In this function, the coordinates of the upper left point of the picture are converted from the WIN32 2D coordinate space to the coordinates of the d3d11 3D coordinate system space (Z is set to 0.0f), because the coordinates of the upper left corner of the picture are calculated, and then by the width of the picture, height, screen width, screen height, the other three points in the figure ( Lower left, right, bottom right) in the D3d11 's left hand 3D coordinate system value, four points just constitute a 3D coordinate system of a square, composed of two triangles, this translates to 6 vertex data into the d3d11 vertex cache, to draw, then 2D rendering success.


Of course there are two problems here: First, the Viewmatrix of the D3D11 is Mcamera position (0.0,0.0,x), where x<0.0f, so that the image rendered in accordance with the provided formal parameters and WIN32 coordinate system to render position effect as seen

The second is that the perspective projection matrix is replaced by the orthogonal projection matrix Orthomatrix


The running program results are as follows:,

Mmodel->render (Md3d->getdevicecontext (), 400,300); Window width 800, height 600, by WIN32 coordinates see is in the middle of the window





The source generation links for the program are as follows:

Click to open link




    &NBSP;

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.