Home > Software engineering >  Directx10 is used to transform a BMP image onto the screen, the picture is drawn
Directx10 is used to transform a BMP image onto the screen, the picture is drawn

Time:11-17

Purpose: to a picture, using the DX10 point elves render to create window
Question: can't painted the original image size, is always be zoom, zoom the default size to create the viewport object (D3D10_VIEWPORT) a quarter of the
For example, I created window is 800 x600, to draw the picture is 224 x224, if create a viewport is 800 x600 (equals the window), then draw the picture is 400 x300.
Why, I want to draw the size of the original, need what to do,


The key code below
HRESULT InitD3D (HWND HWND)
{
//create Direct3D object, the object is used to create Direct3D device object

ID3D10RenderTargetView * ppRenderTargetViews [1];
ID3D10DepthStencilView * ppDepthStencilView=NULL;
If (g_pShaderResource!=NULL)
{
G_pShaderResource - & gt; Release ();
}
DXGI_SWAP_CHAIN_DESC sd;
Sd. BufferDesc. Width=800;
Sd. BufferDesc. Height=600;
Sd. BufferDesc. RefreshRate. The Numerator=1;
Sd. BufferDesc. RefreshRate. The Denominator=1;
Sd. BufferDesc. Format=DXGI_FORMAT_R8G8B8A8_UNORM;

Sd. SampleDesc. Count=1;
Sd. SampleDesc. Quality=0;

Sd. BufferUsage=DXGI_USAGE_RENDER_TARGET_OUTPUT;
Sd. BufferCount=1;
Sd. OutputWindow=hWnd;
Sd. Windowed=TRUE;
Sd. SwapEffect=DXGI_SWAP_EFFECT_DISCARD;
Sd. The Flags=0;


OutputDebugString (L "Init Start \ n");

D3D10_DRIVER_TYPE_REFERENCE D3D10CreateDeviceAndSwapChain (0, 0, 0, D3D10_SDK_VERSION, & amp; Sd, & amp; MSwapChain, & amp; G_pd3dDevice);

//create some elves
HRESULT SpriteResult=D3DX10CreateSprite (g_pd3dDevice, 1, & amp; G_pSprite);
If (FAILED (SpriteResult))
{
OutputDebugString (L "CreateSprite Failed \ n");
}

ID3D10Texture2D * _texture;
D3DX10CreateTextureFromFile (g_pd3dDevice, L "1. JPG", NULL, NULL,
(ID3D10Resource * *) & amp; _texture, NULL);
G_pd3dDevice - & gt; CreateShaderResourceView (_texture, NULL, & amp; G_pShaderResource);

G_pd3dDevice - & gt; OMGetRenderTargets (1, ppRenderTargetViews, & amp; PpDepthStencilView);

If (ppRenderTargetViews [0]==NULL) {
//create a render target view
ID3D10Texture2D * pBackBuffer=NULL;
//after a buffer address
HRESULT HRESULT=mSwapChain - & gt; GetBuffer (0, __uuidof (ID3D10Texture2D), (LPVOID *) & amp; PBackBuffer);
If (FAILED (hResult))
Return hResult;

//create the target view
HResult=g_pd3dDevice - & gt; CreateRenderTargetView (pBackBuffer, NULL, & amp; G_pRenderTargetView);

//release buffer
PBackBuffer - & gt; Release ();
If (FAILED (hResult))
Return hResult;
G_pd3dDevice - & gt; OMSetRenderTargets (1, & amp; G_pRenderTargetView ppDepthStencilView);
}

//set the viewport
D3D10_VIEWPORT vp;
The vp. Width=800;
The vp. Height=600;
The vp. MinDepth=0.0 f;
The vp. MaxDepth=1.0 f;
The vp. TopLeftX=0;
The vp. TopLeftY=0;
G_pd3dDevice - & gt; RSSetViewports (1, & amp; The vp);

Return S_OK.
}



VOID Render ()
{
OutputDebugString (L "Render Start \ n");
Float ClearColor [4]={0.5 0.5 f, f, f 0.5, 0.5} f;
G_pd3dDevice - & gt; ClearRenderTargetView (g_pRenderTargetView ClearColor);

D3DX10_SPRITE SpriteToDraw;

D3DXMatrixTranslation (& amp; SpriteToDraw. MatWorld, 0.0 f, f, 0.0 0.0 f);

D3D10_RASTERIZER_DESC rasterizerDesc=
{
D3D10_FILL_SOLID D3D10_CULL_NONE, FALSE, 0, 0.0 f, 0.0 f,
TRUE, FALSE, FALSE, FALSE
};
ID3D10RasterizerState * _rasterizerState;
G_pd3dDevice - & gt; CreateRasterizerState (& amp; RasterizerDesc, & amp; _rasterizerState);
G_pd3dDevice - & gt; RSSetState (_rasterizerState);

SpriteToDraw. TexCoord. X=0.0 f;
SpriteToDraw. TexCoord. Y=0.0 f;
SpriteToDraw. TexSize. X=1.0 f;
SpriteToDraw. TexSize. Y=1.0 f;
SpriteToDraw. ColorModulate=D3DXCOLOR (1.0 f, f 1.0, 1.0 f, 1.0 f);
SpriteToDraw. PTexture=g_pShaderResource;
SpriteToDraw. TextureIndex=0;


If (SUCCEEDED (g_pSprite - & gt; The Begin (D3DX10_SPRITE_SORT_TEXTURE)))
{
OutputDebugString (L "g_pSprite - & gt; The Begin \ n ");
HRESULT DrawResult=g_pSprite - & gt; DrawSpritesBuffered (& amp; SpriteToDraw, 1);
If (FAILED (DrawResult)) {
OutputDebugString (L "the Draw Failed \ n");
WCHAR sOut2 [256]={0};
Wsprintf (sOut2, L "the Draw Failed % d", DrawResult);
OutputDebugString (sOut2);
}
G_pSprite - & gt; Flush ();
G_pSprite - & gt; The End ();
}

//will buffer draw graphics in the background to submit to the front desk buffer according to
MSwapChain - & gt; The Present (0, 0);
}

CodePudding user response:

You can calculate the transformation matrix to zoom in

CodePudding user response:

reference 1st floor WJN92 response:
you can calculate the transformation matrix to zoom
transformation matrix, I know, but I was wondering is why not set up other painting out of the original size picture

CodePudding user response:

Because the image aspect ratio and length-width ratio of different viewport

CodePudding user response:

Look at this function XMMatrixPerspectiveFovLH
  • Related