在图形应用程序中,读者也希望为显示在屏幕上的内容保存一幅屏幕截图。这非常有用。幸运的是,在Direct3D中创建和保存屏幕截图非常简单明了。为了实现该功能,需要获取当前已渲染好的场景宽度和高度,创建Direct3D表面,将渲染后的场景复制到创建好的表面对象中,然后调用一个Direct3D函数将表面保存到Direct3D支持的文件格式中。该文件格式可以是前面讨论过的任何标准文件类型,如.jpg、.tga或.bmp。
这意味着为了保存程序屏幕截图,就要使用4个函数。
1.首先要获取当前的显示模式。这样就可以获取像渲染场景宽度和高度这样的信息。
2.接下来,要创建一个幕外表面。该表面用于存储已渲染好的场景副本,所以可以将其保存到文件中。
3.最后一个函数真正将场景数据复制到表面上。
4.除了这些步骤之外,调用D3DXSaveSurfaceToFile()即可,而Direct3D会处理其余的事情。
#include<d3d9.h>
#include<d3dx9.h>
#define WINDOW_CLASS "UGPDX"
#define WINDOW_NAME "Capturing Screen Shots"
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
// Function Prototypes...
bool InitializeD3D(HWND hWnd, bool fullscreen);
bool InitializeObjects();
void RenderScene();
void Shutdown();
// Direct3D object and device.
LPDIRECT3D9 g_D3D = NULL;
LPDIRECT3DDEVICE9 g_D3DDevice = NULL;
// Matrices.
D3DXMATRIX g_projection;
D3DXMATRIX g_ViewMatrix;
// Vertex buffer to hold the geometry.
LPDIRECT3DVERTEXBUFFER9 g_VertexBuffer = NULL;
// Holds a texture image.
LPDIRECT3DTEXTURE9 g_Texture = NULL;
// Records if we already saved the screen shot.
bool g_screenShotSaved = false;
// A structure for our custom vertex type
struct stD3DVertex
{
float x, y, z;
unsigned long color;
float tu, tv;
};
// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_VERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)
LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
case WM_KEYUP:
if(wParam == VK_ESCAPE) PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE prevhInst, LPSTR cmdLine, int show)
{
// Register the window class
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
WINDOW_CLASS, NULL };
RegisterClassEx(&wc);
// Create the application's window
HWND hWnd = CreateWindow(WINDOW_CLASS, WINDOW_NAME, WS_OVERLAPPEDWINDOW,
100, 100, WINDOW_WIDTH, WINDOW_HEIGHT,
GetDesktopWindow(), NULL, wc.hInstance, NULL);
// Initialize Direct3D
if(InitializeD3D(hWnd, false))
{
// Show the window
ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);
// Enter the message loop
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
RenderScene();
}
}
// Release any and all resources.
Shutdown();
// Unregister our window.
UnregisterClass(WINDOW_CLASS, wc.hInstance);
return 0;
}
bool InitializeD3D(HWND hWnd, bool fullscreen)
{
D3DDISPLAYMODE displayMode;
// Create the D3D object.
g_D3D = Direct3DCreate9(D3D_SDK_VERSION);
if(g_D3D == NULL) return false;
// Get the desktop display mode.
if(FAILED(g_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode)))
return false;
// Set up the structure used to create the D3DDevice
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
if(fullscreen)
{
d3dpp.Windowed = FALSE;
d3dpp.BackBufferWidth = WINDOW_WIDTH;
d3dpp.BackBufferHeight = WINDOW_HEIGHT;
}
else
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = displayMode.Format;
// Create the D3DDevice
if(FAILED(g_D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_D3DDevice)))
{
return false;
}
// Initialize any objects we will be displaying.
if(!InitializeObjects()) return false;
return true;
}
bool InitializeObjects()
{
// Fill in our structure to draw an object.
// x, y, z, color, texture coords.
stD3DVertex objData[] =
{
{-0.3f, -0.4f, 0.0f, D3DCOLOR_XRGB(255,255,255), 0.0f, 1.0f},
{0.3f, -0.4f, 0.0f, D3DCOLOR_XRGB(255,255,255), 1.0f, 1.0f},
{0.3f, 0.4f, 0.0f, D3DCOLOR_XRGB(255,255,255), 1.0f, 0.0f},
{0.3f, 0.4f, 0.0f, D3DCOLOR_XRGB(255,255,255), 1.0f, 0.0f},
{-0.3f, 0.4f, 0.0f, D3DCOLOR_XRGB(255,255,255), 0.0f, 0.0f},
{-0.3f, -0.4f, 0.0f, D3DCOLOR_XRGB(255,255,255), 0.0f, 1.0f}
};
// Create the vertex buffer.
if(FAILED(g_D3DDevice->CreateVertexBuffer(sizeof(objData), 0,
D3DFVF_VERTEX, D3DPOOL_DEFAULT, &g_VertexBuffer, NULL))) return false;
// Fill the vertex buffer.
void *ptr;
if(FAILED(g_VertexBuffer->Lock(0, sizeof(objData), (void**)&ptr, 0))) return false;
memcpy(ptr, objData, sizeof(objData));
g_VertexBuffer->Unlock();
// Load the texture image from file.
if(D3DXCreateTextureFromFile(g_D3DDevice, "ugp.tga", &g_Texture) != D3D_OK)
return false;
// Set the image states to get a good quality image.
g_D3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
g_D3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
// Set default rendering states.
g_D3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
g_D3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
// Set the projection matrix.
D3DXMatrixPerspectiveFovLH(&g_projection, 45.0f, WINDOW_WIDTH/WINDOW_HEIGHT,
0.1f, 1000.0f);
g_D3DDevice->SetTransform(D3DTS_PROJECTION, &g_projection);
// Define camera information.
D3DXVECTOR3 cameraPos(0.0f, 0.0f, -1.0f);
D3DXVECTOR3 lookAtPos(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 upDir(0.0f, 1.0f, 0.0f);
// Build view matrix.
D3DXMatrixLookAtLH(&g_ViewMatrix, &cameraPos, &lookAtPos, &upDir);
return true;
}
void RenderScene()
{
// Clear the backbuffer.
g_D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
// Begin the scene. Start rendering.
g_D3DDevice->BeginScene();
// Apply the view (camera).
g_D3DDevice->SetTransform(D3DTS_VIEW, &g_ViewMatrix);
// Draw square.
g_D3DDevice->SetTexture(0, g_Texture);
g_D3DDevice->SetStreamSource(0, g_VertexBuffer, 0, sizeof(stD3DVertex));
g_D3DDevice->SetFVF(D3DFVF_VERTEX);
g_D3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
// End the scene. Stop rendering.
g_D3DDevice->EndScene();
// Display the scene.
g_D3DDevice->Present(NULL, NULL, NULL, NULL);
// Save the screen shot.
if(!g_screenShotSaved && (GetKeyState('s') & 0x80 || GetKeyState('S') & 0x80))
{
// Surface that will store the screen as a image.
LPDIRECT3DSURFACE9 surface = NULL;
D3DDISPLAYMODE display;
g_D3DDevice->GetDisplayMode(0, &display);
// Create the off screen surface with the same info as the back buffer.
g_D3DDevice->CreateOffscreenPlainSurface(display.Width, display.Height,
D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &surface, NULL);
// Save the back buffer to the surface.
g_D3DDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &surface);
D3DXSaveSurfaceToFile("screenShot.tga", D3DXIFF_TGA, surface, NULL, NULL);
g_screenShotSaved = true;
// Release surface.
if(surface != NULL) surface->Release();
surface = NULL;
}
}
void Shutdown()
{
if(g_D3DDevice != NULL) g_D3DDevice->Release();
g_D3DDevice = NULL;
if(g_D3D != NULL) g_D3D->Release();
g_D3D = NULL;
if(g_VertexBuffer != NULL) g_VertexBuffer->Release();
g_VertexBuffer = NULL;
if(g_Texture != NULL) g_Texture->Release();
g_Texture = NULL;
}首先出现的是一个Direct3D表面对象,用于获取渲染场景,这样可以将其保存到图像文件中。接下来,定义了一个类型为D3DDISPLAYMODE的显示模式对象。这样就可以知道当前显示的宽度和高度,这些值在创建表面对象时要用到。GetDisplayMode()函数的参数包括交换链索引,以及要填充的对象的显示模式。由于并未使用交换链,因此该函数的第一个参数设为0。
接下来要做的事情是调用CreateOffscreenPlainSurface()函数创建表面。该函数的参数包括表面的宽度和高度、表面的pool的标识符、实际创建的表面对象,以及一个值为NULL的保留参数。
一旦创建完表面,就可以调用Direct3D设备对象的GetBackBuffer()函数,使用渲染好的场景填充该表面。GetBackBuffer()函数的参数包括交换链、后台缓存索引、后台缓存类型(DirectX9.0中只支持D3DBACKBUFFER_TYPE_MONO),以及保存已渲染的场景的表面对象。
既然有了表面数据,那么就可以保存这些数据,这只需调用D3DXSaveSurfaceToFile()函数即可。该函数的参数包括保存图像时想要使用的文件名、保存的文件类型、包含渲染数据的表面对象、保存文件时使用的调色板(该值可以设为NULL)以及一个定义要保存的表面区域的RECT结构。如果最后一个参数的值为NULL,那么就保存整幅图像。表4.3给出了可保存的文件类型。
保存时可用的文件类型
D3DXIFF_BMP | .bmp 图像 |
D3DXIFF_JPG | .jpg 图像 |
D3DXIFF_TGA | .tga 图像 |
D3DXIFF_PNG | .png 图像 |
D3DXIFF_DDS | .dds 图像 |
D3DXIFF_PPM | .ppm 图像 |
D3DXIFF_DIB | .dib 图像 |
D3DXIFF_HDR | .hdr 图像 |
D3DXIFF_PFM | .pfm 图像 |
该演示程序和纹理映射程序之间唯一的差别在于该演示程序末尾的RenderScene()函数。在此,按下键盘上的S键做个简单的测试。这样就可以得到显示模式,可以知道后台缓存的宽度和高度,创建表面,然后将渲染好的场景复制到该表面上。最后可以使用D3DXSaveSurfaceToFile()函数将场景保存到文件中。由于已经创建了表面,因为必须确保释放该对象以避免内存泄漏。