﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-Daywei0123-随笔分类-DirectX</title><link>http://www.cppblog.com/Daywei0123/category/18411.html</link><description>技术追求卓越 梦想创造未来</description><language>zh-cn</language><lastBuildDate>Sat, 28 Apr 2012 20:17:46 GMT</lastBuildDate><pubDate>Sat, 28 Apr 2012 20:17:46 GMT</pubDate><ttl>60</ttl><item><title>Recording DirectX and OpenGL Rendered Animations（翻译）</title><link>http://www.cppblog.com/Daywei0123/archive/2012/04/28/173044.html</link><dc:creator>Daywei</dc:creator><author>Daywei</author><pubDate>Sat, 28 Apr 2012 07:08:00 GMT</pubDate><guid>http://www.cppblog.com/Daywei0123/archive/2012/04/28/173044.html</guid><wfw:comment>http://www.cppblog.com/Daywei0123/comments/173044.html</wfw:comment><comments>http://www.cppblog.com/Daywei0123/archive/2012/04/28/173044.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Daywei0123/comments/commentRss/173044.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Daywei0123/services/trackbacks/173044.html</trackback:ping><description><![CDATA[<span style="font-size: 18pt">记录DirectX和OpenGL渲染的动画</span><br /><strong>简介</strong><br />&nbsp;&nbsp;&nbsp; 当我们创建游戏和仿真模拟时，有时我们有必要记录渲染的内容。在某些情况下渲染过于复杂和耗时，这是不可避免的。<br />&nbsp;&nbsp;&nbsp;&nbsp; 在DirectX中，库函数D3DXSaveSurfaceToFile()保存表面为一张图片文件。对OpenGL，我们用glReadPixels()来读渲染的图像像素然后手动的保存它们为一张图片文件。然而这些表面只是针对单帧记录的，对记录一段连续帧没有简单的方法存在。换句话说,没有库函数来记录我们的完整存在渲染动画效果。<br />&nbsp;&nbsp;&nbsp; 在这方面,本文提出了几类,这有助于创造电影DirectX的方法和动画。用类CDxToMovie和 CGLToMovie电影可以选择性地或连续的从DirectX和OpenGL渲染帧来创建。一般来说,一个典型的电影创作过程涉及复杂的任务,例如读图的内容,选择帧速率设置,编解码器的设置,初始化媒体流,写媒体流等（详细讨论关于如何创建位图图像序列的电影,请参考这篇文章<a href="http://gopalakrishna.palem.in/createmovie.html">Create Movie from HBitmap</a>）。类CDxToMovie和CGLToMovie这里介绍的抽象出所有不必要的复杂性和易于使用的界面,提供简单方法解释如下<br /><strong>从DirectX渲染序列记录一个电影<br /></strong>&nbsp;&nbsp;&nbsp; 类CDxToMovie可以记录DirectX渲染序列成电影文件。该类用到DirectX 9.0接口例如LPDIRECT3DSURFACE9，因此你应该用DirectX 9.0 SDK 或者其他的兼容的地方使用这个类 。<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;开始从本文中的DirectX代码拷贝文件DxToMovie.h，RenderTarget.h，AviFile.h和AviFile.cpp到你的工程目录下然后添加他们到你的工程中，然后添加vfw.lib，一旦添加到你的工程中，你可以通过#include "DxToMovie.h"访问。CDxToMovie构造函数接受不同的参数如输出电影文件名,电影帧的宽度和高度的要求,每像素比特数等&#8230;如下所示, 
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">CDxToMovie(LPCTSTR&nbsp;lpszOutputMovieFileName&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;_T(</span><span style="color: #000000">"</span><span style="color: #000000">Output.avi</span><span style="color: #000000">"</span><span style="color: #000000">),<br /><img id="Codehighlighter1_129_149_Open_Image" onclick="this.style.display='none'; Codehighlighter1_129_149_Open_Text.style.display='none'; Codehighlighter1_129_149_Closed_Image.style.display='inline'; Codehighlighter1_129_149_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_129_149_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_129_149_Closed_Text.style.display='none'; Codehighlighter1_129_149_Open_Image.style.display='inline'; Codehighlighter1_129_149_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;nFrameWidth&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;GetSystemMetrics(SM_CXSCREEN),&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_129_149_Closed_Text">/**/</span><span id="Codehighlighter1_129_149_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">Movie&nbsp;Frame&nbsp;Width</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /><img id="Codehighlighter1_217_238_Open_Image" onclick="this.style.display='none'; Codehighlighter1_217_238_Open_Text.style.display='none'; Codehighlighter1_217_238_Closed_Image.style.display='inline'; Codehighlighter1_217_238_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_217_238_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_217_238_Closed_Text.style.display='none'; Codehighlighter1_217_238_Open_Image.style.display='inline'; Codehighlighter1_217_238_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;nFrameHeight&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;GetSystemMetrics(SM_CYSCREEN),&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_217_238_Closed_Text">/**/</span><span id="Codehighlighter1_217_238_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">Movie&nbsp;Frame&nbsp;Height</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /><img id="Codehighlighter1_284_301_Open_Image" onclick="this.style.display='none'; Codehighlighter1_284_301_Open_Text.style.display='none'; Codehighlighter1_284_301_Closed_Image.style.display='inline'; Codehighlighter1_284_301_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_284_301_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_284_301_Closed_Text.style.display='none'; Codehighlighter1_284_301_Open_Image.style.display='inline'; Codehighlighter1_284_301_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;nBitsPerPixel&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">32</span><span style="color: #000000">,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_284_301_Closed_Text">/**/</span><span id="Codehighlighter1_284_301_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">Bits&nbsp;per&nbsp;Pixel</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /><img id="Codehighlighter1_365_395_Open_Image" onclick="this.style.display='none'; Codehighlighter1_365_395_Open_Text.style.display='none'; Codehighlighter1_365_395_Closed_Image.style.display='inline'; Codehighlighter1_365_395_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_365_395_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_365_395_Closed_Text.style.display='none'; Codehighlighter1_365_395_Open_Image.style.display='inline'; Codehighlighter1_365_395_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;dwCodec&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;mmioFOURCC(</span><span style="color: #000000">'</span><span style="color: #000000">M</span><span style="color: #000000">'</span><span style="color: #000000">,</span><span style="color: #000000">'</span><span style="color: #000000">P</span><span style="color: #000000">'</span><span style="color: #000000">,</span><span style="color: #000000">'</span><span style="color: #000000">G</span><span style="color: #000000">'</span><span style="color: #000000">,</span><span style="color: #000000">'</span><span style="color: #000000">4</span><span style="color: #000000">'</span><span style="color: #000000">),&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_365_395_Closed_Text">/**/</span><span id="Codehighlighter1_365_395_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">Video&nbsp;Codec&nbsp;for&nbsp;Compression</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /><img id="Codehighlighter1_441_482_Open_Image" onclick="this.style.display='none'; Codehighlighter1_441_482_Open_Text.style.display='none'; Codehighlighter1_441_482_Closed_Image.style.display='inline'; Codehighlighter1_441_482_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_441_482_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_441_482_Closed_Text.style.display='none'; Codehighlighter1_441_482_Open_Image.style.display='inline'; Codehighlighter1_441_482_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;dwFrameRate&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_441_482_Closed_Text">/**/</span><span id="Codehighlighter1_441_482_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">Frame&nbsp;Rate&nbsp;(FPS)&nbsp;setting&nbsp;for&nbsp;the&nbsp;Movie</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span></div>然而,应该注意到的是,这是一个时间设置，后来在电影记录时候不能改变&nbsp;。每个CDxToMovie对应一个不同的电影文件和再造一个CDxToMovie对象具有相同的输出文件的名字不会追加以前的电影内容,将覆盖它。 
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">CDxToMovie g_MovieRecorder(</span><span style="color: #000000">"</span><span style="color: #000000">Output.Avi</span><span style="color: #000000">"</span><span style="color: #000000">, </span><span style="color: #000000">320</span><span style="color: #000000">, </span><span style="color: #000000">240</span><span style="color: #000000">);</span></div>&nbsp;&nbsp;&nbsp; 一旦创建CDxToMovie对象，方法CDxToMovie::OnCreateDevice()在你的程序Direct3D设备创建的时候会被调用。类似的，CDxToMovie::OnLostDevice(),CDxToMovie::OnResetDevice()和CDxToMovie::OnDestroyDevice()也会在设备丢失销毁的时候各自被调用。这些函数的原型显示如下 
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #0000ff">class</span><span style="color: #000000"> CDxToMovie<br /><img id="Codehighlighter1_21_317_Open_Image" onclick="this.style.display='none'; Codehighlighter1_21_317_Open_Text.style.display='none'; Codehighlighter1_21_317_Closed_Image.style.display='inline'; Codehighlighter1_21_317_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_21_317_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_21_317_Closed_Text.style.display='none'; Codehighlighter1_21_317_Open_Image.style.display='inline'; Codehighlighter1_21_317_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"> </span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_21_317_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_21_317_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /> HRESULT OnCreateDevice(LPDIRECT3DDEVICE9 pd3dDevice);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /> HRESULT OnDestroyDevice(LPDIRECT3DDEVICE9 pd3dDevice);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /> HRESULT OnLostDevice();<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /> HRESULT OnResetDevice(LPDIRECT3DDEVICE9 pd3dDevice, <br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /> </span><span style="color: #0000ff">const</span><span style="color: #000000"> D3DSURFACE_DESC</span><span style="color: #000000">*</span><span style="color: #000000"> pBackBufferSurfaceDesc);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" /> }</span></span><span style="color: #000000">;</span></div>函数OnCreateDevice()和OnDestroyDevice()接受一个单单参数，指向你程序的Direct3D设备对象。OnLostDevice()没有参数，但是OnResetDevice()还要有一个指针指向你设备的后缓冲区表面 D3DSURFACE_DESC*。CDxToMovie对象提供一些信息在D3DSURFACE_DESC里 创造一个合适的offscreen渲染目标,可以用来记录你的应用程序的渲染。<br />真正记录的功能是通过函数CDxToMovie::StartRecordingMovie()和CDxToMovie::PauseRecordingMovie().这两个函数必须每一帧都要在<font face="Courier New">IDirect3DDevice9::BeginScene()</font> 和 <code>IDirect3DDevice9::EndScene()之间。如下所示 
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">g_pd3dDevice</span><span style="color: #000000">-&gt;</span><span style="color: #000000">BeginScene();<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;Capture&nbsp;the&nbsp;Rendering&nbsp;onto&nbsp;CDxToMovie's&nbsp;Render&nbsp;Target</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;g_MovieRecorder.StartRecordingMovie(g_pd3dDevice);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;Render&nbsp;as&nbsp;usual<img src="http://www.cppblog.com/Images/dot.gif"  alt="" />..&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g_pd3dDevice</span><span style="color: #000000">-&gt;</span><span style="color: #000000">Clear(</span><span style="color: #000000">0</span><span style="color: #000000">,NULL,D3DCLEAR_TARGET</span><span style="color: #000000">|</span><span style="color: #000000">D3DCLEAR_ZBUFFER,&nbsp;D3DCOLOR_XRGB(</span><span style="color: #000000">0</span><span style="color: #000000">,</span><span style="color: #000000">0</span><span style="color: #000000">,</span><span style="color: #000000">200</span><span style="color: #000000">),</span><span style="color: #000000">1</span><span style="color: #000000">,</span><span style="color: #000000">0</span><span style="color: #000000">);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g_pd3dDevice</span><span style="color: #000000">-&gt;</span><span style="color: #000000">SetStreamSource(</span><span style="color: #000000">0</span><span style="color: #000000">,g_pVB,</span><span style="color: #000000">0</span><span style="color: #000000">,</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(CUSTOMVERTEX));<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g_pd3dDevice</span><span style="color: #000000">-&gt;</span><span style="color: #000000">SetFVF(D3DFVF_CUSTOMVERTEX);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g_pd3dDevice</span><span style="color: #000000">-&gt;</span><span style="color: #000000">DrawPrimitive(D3DPT_TRIANGLELIST,</span><span style="color: #000000">0</span><span style="color: #000000">,</span><span style="color: #000000">1</span><span style="color: #000000">);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;g_MovieRecorder.PauseRecordingMovie(g_pd3dDevice);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;Copy&nbsp;the&nbsp;CDxToMovie's&nbsp;Render&nbsp;Target&nbsp;content&nbsp;back&nbsp;onto&nbsp;BackBuffer's&nbsp;Surface</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;g_pd3dDevice</span><span style="color: #000000">-&gt;</span><span style="color: #000000">StretchRect(g_MovieRecorder.RecordingSurface(),<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NULL,pBackSurface,<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">,D3DTEXF_NONE);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;g_pd3dDevice</span><span style="color: #000000">-&gt;</span><span style="color: #000000">EndScene();</span></div></code>在上面的代码段中，&nbsp;<code>g_MovieRecorder.StartRecordingMovie(g_pd3dDevice)</code>将所有随后的渲染在CDxToMovie的内部渲染目标直到<font face="Courier New">g_MovieRecorder.PauseRecordingMovie(g_pd3dDevice)</font>被调用。所有的渲染工作在CDxToMovie的内部渲染目标上做，你的程序back surface将没有任何有效的内容显示在你的应用程序窗口。这不要紧,如果你的申请不只是创作这部电影没有任何动画直接呈现在屏幕上。然而,如果你记录电影从一个互动游戏会话上,它会变坏屏幕不更新到最新的渲染的内容(因为通过重新渲染目标，CDxToMovie内容被偷了)。为了避免它,你可以选择性地复制回CDxToMovie内部的渲染目标的内容到你的应用程序的back surface 使用方法IDirect3DDevice9:StretchRect(),其次是常见的g_pd3dDevice - &gt; EndScene()和g_pd3dDevice - &gt;Present()的要求会更新内容的呈现在屏幕上背缓冲区,使屏幕上更新。<br />如果你想避免一些帧被选择性记录在这部电影,只是不要叫g_MovieRecorder.StartRecordingMovie和g_MovieRecorder.PauseRecordingMovie()(相应的 g_pd3dDevice - &gt; StretchRect())对那些帧,并会直接渲染动画在屏幕上(没有被重定向到CDxToMovie内部的渲染目标)。<br />演示代码提供这个项目提供了一个简单的应用程序,使得DirectX屏幕上的一个三角形的动作,鼠标移动窗户上,这将simulatenously被渲染成电影文件和记录(名叫output.avi)。跑演示的可执行程序,确保你有MPG4编解码器的计算机上安装,目录有写权限去创建输出电影文件。详情请设置解码器和平衡,请参考这篇文章<a href="http://gopalakrishna.palem.in/createmovie.html">Create Movie from HBitmap</a><br /><br /><a name="Recording a Movie from OpenGL Rendered Sequence"><strong>Recording a Movie from OpenGL Rendered Sequence</strong></a><br />先暂时不翻译了，以后再翻。<br /><br />注：第一次翻译，水平比较差，还望各位看客见谅。<br />Reference：&nbsp;<a href="http://gopalakrishna.palem.in/SimulationRecording.html">Recording DirectX and OpenGL Rendered Animations</a><img src ="http://www.cppblog.com/Daywei0123/aggbug/173044.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Daywei0123/" target="_blank">Daywei</a> 2012-04-28 15:08 <a href="http://www.cppblog.com/Daywei0123/archive/2012/04/28/173044.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于DirectX使用的一些注意事项</title><link>http://www.cppblog.com/Daywei0123/archive/2012/02/16/165746.html</link><dc:creator>Daywei</dc:creator><author>Daywei</author><pubDate>Thu, 16 Feb 2012 07:24:00 GMT</pubDate><guid>http://www.cppblog.com/Daywei0123/archive/2012/02/16/165746.html</guid><wfw:comment>http://www.cppblog.com/Daywei0123/comments/165746.html</wfw:comment><comments>http://www.cppblog.com/Daywei0123/archive/2012/02/16/165746.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Daywei0123/comments/commentRss/165746.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Daywei0123/services/trackbacks/165746.html</trackback:ping><description><![CDATA[<div>1.关于创建Direct3D设备对象<br />&nbsp;&nbsp;&nbsp;创建Direct3D设备对象时，需要先创建Direct3D对象，然后再调用Direct3D对象的接口函数IDirect3D9：：CreateDevice创建Direct3D设备对象。通过同一个Direct3D对象创建的所有Direct3D设备对象共享相同的物理资源（显卡）。因为共享同一硬件，所以如果通过一个Direct3D对象创建多个Direct3D渲染设备对象会明显降低系统性能。<br />&nbsp;&nbsp;&nbsp;在创建Direct3D设备对象之前，还需要先初始化D3DPRESENT_PARAMENTERS结构，该结构用于创建Direct3D设备对象。此结构将会影响Direct3D设备的显示方法。<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<h1>D3DPRESENT_PARAMETERS</h1>
<p>Describes the presentation parameters.</p><pre class="syntax">typedef struct D3DPRESENT_PARAMETERS {
    UINT BackBufferWidth, BackBufferHeight;
    D3DFORMAT BackBufferFormat;
    UINT BackBufferCount;
    D3DMULTISAMPLE_TYPE MultiSampleType;
    DWORD MultiSampleQuality;
    D3DSWAPEFFECT SwapEffect;
    HWND hDeviceWindow;
    BOOL Windowed;
    BOOL EnableAutoDepthStencil;
    D3DFORMAT AutoDepthStencilFormat;
    DWORD Flags;
    UINT FullScreen_RefreshRateInHz;
    UINT PresentationInterval;
} D3DPRESENT_PARAMETERS, *LPD3DPRESENT_PARAMETERS;</pre>
<h4><a name="members"></a>Members</h4>
<dl>
<dt><strong>BackBufferWidth, BackBufferHeight</strong> 
<dd>Width and height of the new swap chain's back buffers, in pixels. If Windowed is FALSE (the presentation is full-screen), these values must equal the width and height of one of the enumerated display modes found through <a href="mk:@MSITStore:C:\Program%20Files\Microsoft%20DirectX%20SDK%20(February%202010)\Documentation\DirectX9\windows_graphics.chm::/IDirect3D9__EnumAdapterModes.htm">IDirect3D9::EnumAdapterModes</a>. If Windowed is TRUE and either of these values is zero, the corresponding dimension of the client area of the hDeviceWindow (or the focus window, if hDeviceWindow is NULL) is taken. 
<dt><strong>BackBufferFormat</strong> 
<dd>The back buffer format. For more information about formats, see <a href="mk:@MSITStore:C:\Program%20Files\Microsoft%20DirectX%20SDK%20(February%202010)\Documentation\DirectX9\windows_graphics.chm::/D3DFORMAT.htm">D3DFORMAT</a>. This value must be one of the render-target formats as validated by <a href="mk:@MSITStore:C:\Program%20Files\Microsoft%20DirectX%20SDK%20(February%202010)\Documentation\DirectX9\windows_graphics.chm::/IDirect3D9__CheckDeviceType.htm">IDirect3D9::CheckDeviceType</a>. You can use <a href="mk:@MSITStore:C:\Program%20Files\Microsoft%20DirectX%20SDK%20(February%202010)\Documentation\DirectX9\windows_graphics.chm::/IDirect3DDevice9__Getdisplaymode.htm">IDirect3DDevice9::GetDisplayMode</a> to obtain the current format. 
<p>In fact, D3DFMT_UNKNOWN can be specified for the BackBufferFormat while in windowed mode. This tells the runtime to use the current display-mode format and eliminates the need to call <strong>IDirect3DDevice9::GetDisplayMode</strong>.</p>
<p>For windowed applications, the back buffer format no longer needs to match the display-mode format because color conversion can now be done by the hardware (if the hardware supports color conversion). The set of possible back buffer formats is constrained, but the runtime will allow any valid back buffer format to be presented to any desktop format. (There is the additional requirement that the device be operable in the desktop mode; devices typically do not operate in 8 bits per pixel modes.)</p>
<p>Full-screen applications cannot do color conversion.</p>
<dt><strong>BackBufferCount</strong> 
<dd>This value can be between 0 and <a href="mk:@MSITStore:C:\Program%20Files\Microsoft%20DirectX%20SDK%20(February%202010)\Documentation\DirectX9\windows_graphics.chm::/D3DPRESENT_BACK_BUFFERS.htm">D3DPRESENT_BACK_BUFFERS_MAX</a> (or <a href="mk:@MSITStore:C:\Program%20Files\Microsoft%20DirectX%20SDK%20(February%202010)\Documentation\DirectX9\windows_graphics.chm::/D3DPRESENT_BACK_BUFFERS.htm">D3DPRESENT_BACK_BUFFERS_MAX_EX</a> when using Direct3D 9Ex). Values of 0 are treated as 1. If the number of back buffers cannot be created, the runtime will fail the method call and fill this value with the number of back buffers that could be created. As a result, an application can call the method twice with the same D3DPRESENT_PARAMETERS structure and expect it to work the second time. 
<p>The method fails if one back buffer cannot be created. The value of BackBufferCount influences what set of swap effects are allowed. Specifically, any D3DSWAPEFFECT_COPY swap effect requires that there be exactly one back buffer.</p>
<dt><strong>MultiSampleType</strong> 
<dd>Member of the <a href="mk:@MSITStore:C:\Program%20Files\Microsoft%20DirectX%20SDK%20(February%202010)\Documentation\DirectX9\windows_graphics.chm::/d3dmultisample_type.htm">D3DMULTISAMPLE_TYPE</a> enumerated type. The value must be D3DMULTISAMPLE_NONE unless SwapEffect has been set to D3DSWAPEFFECT_DISCARD. Multisampling is supported only if the swap effect is D3DSWAPEFFECT_DISCARD. 
<dt><strong>MultiSampleQuality</strong> 
<dd>Quality level. The valid range is between zero and one less than the level returned by pQualityLevels used by <a href="mk:@MSITStore:C:\Program%20Files\Microsoft%20DirectX%20SDK%20(February%202010)\Documentation\DirectX9\windows_graphics.chm::/IDirect3D9__CheckDeviceMultiSampleType.htm">IDirect3D9::CheckDeviceMultiSampleType</a>. Passing a larger value returns the error D3DERR_INVALIDCALL. Paired values of render targets or of depth stencil surfaces and <strong>D3DMULTISAMPLE_TYPE</strong> must match. 
<dt><strong>SwapEffect</strong> 
<dd>Member of the <a href="mk:@MSITStore:C:\Program%20Files\Microsoft%20DirectX%20SDK%20(February%202010)\Documentation\DirectX9\windows_graphics.chm::/D3DSWAPEFFECT.htm">D3DSWAPEFFECT</a> enumerated type. The runtime will guarantee the implied semantics concerning buffer swap behavior; therefore, if Windowed is TRUE and SwapEffect is set to D3DSWAPEFFECT_FLIP, the runtime will create one extra back buffer and copy whichever becomes the front buffer at presentation time. 
<p>D3DSWAPEFFECT_COPY requires that BackBufferCount be set to 1.</p>
<p>D3DSWAPEFFECT_DISCARD will be enforced in the debug runtime by filling any buffer with noise after it is presented.</p>
<table>
<tbody>
<tr>
<td>
<p>Differences between Direct3D9 and Direct3D9Ex</p>
<p>In Direct3D9Ex, D3DSWAPEFFECT_FLIPEX is added to designate when an application is adopting flip mode. That is, whan an application's frame is passed in window's mode (instead of copied) to the Desktop Window Manager(DWM) for composition. Flip mode provides more efficient memory bandwidth and enables an application to take advantage of full-screen-present statistics. It does not change full screen behavior. Flip mode behavior is available beginning with Windows 7.</p></td></tr></tbody></table>
<dt><strong>hDeviceWindow</strong> 
<dd>The device window determines the location and size of the back buffer on screen. This is used by Direct3D when the back buffer contents are copied to the front buffer during <a href="mk:@MSITStore:C:\Program%20Files\Microsoft%20DirectX%20SDK%20(February%202010)\Documentation\DirectX9\windows_graphics.chm::/IDirect3DDevice9__Present.htm">IDirect3DDevice9::Present</a>. 
<ul><li>
<p>For a full-screen application, this is a handle to the top window (which is the focus window). </p>
<p>For applications that use multiple full-screen devices (such as a multimonitor system), exactly one device can use the focus window as the device window. All other devices must have unique device windows.</p></li><li>For a windowed-mode application, this handle will be the default target window for <strong>IDirect3DDevice9::Present</strong>. If this handle is NULL, the focus window will be taken. </li></ul>
<p>Note that no attempt is made by the runtime to reflect user changes in window size. The back buffer is not implicitly reset when this window is reset. However, the <strong>IDirect3DDevice9::Present</strong> method does automatically track window position changes.</p>
<dt><strong>Windowed</strong> 
<dd>TRUE if the application runs windowed; FALSE if the application runs full-screen. 
<dt><strong>EnableAutoDepthStencil</strong> 
<dd>If this value is TRUE, Direct3D will manage depth buffers for the application. The device will create a depth-stencil buffer when it is created. The depth-stencil buffer will be automatically set as the render target of the device. When the device is reset, the depth-stencil buffer will be automatically destroyed and recreated in the new size. 
<p>If EnableAutoDepthStencil is TRUE, then AutoDepthStencilFormat must be a valid depth-stencil format.</p>
<dt><strong>AutoDepthStencilFormat</strong> 
<dd>Member of the <strong>D3DFORMAT</strong> enumerated type. The format of the automatic depth-stencil surface that the device will create. This member is ignored unless EnableAutoDepthStencil is TRUE. 
<dt><strong>Flags</strong> 
<dd>One of the <a href="mk:@MSITStore:C:\Program%20Files\Microsoft%20DirectX%20SDK%20(February%202010)\Documentation\DirectX9\windows_graphics.chm::/D3DPRESENTFLAG.htm">D3DPRESENTFLAG</a> constants. 
<dt><strong>FullScreen_RefreshRateInHz</strong> 
<dd>The rate at which the display adapter refreshes the screen. The value depends on the mode in which the application is running: 
<ul><li>For windowed mode, the refresh rate must be 0.</li><li>For full-screen mode, the refresh rate is one of the refresh rates returned by <strong>IDirect3D9::EnumAdapterModes</strong>. </li></ul>
<dt><strong>PresentationInterval</strong> 
<dd>The maximum rate at which the swap chain's back buffers can be presented to the front buffer. For a detailed explanation of the modes and the intervals that are supported, see <a href="mk:@MSITStore:C:\Program%20Files\Microsoft%20DirectX%20SDK%20(February%202010)\Documentation\DirectX9\windows_graphics.chm::/D3DPRESENT.htm">D3DPRESENT</a>. </dd></dl>
<h4><a name="requirements"></a>Requirements</h4>
<p class="clsInfo"><strong>Header: </strong>Declared in D3D9Types.h.&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;几个重要的参数加以解释说明一下，其中BackBufferWidth和BackBufferHeight指定后台缓存区的宽高（以像素为单位）。如果图形以窗口方式显示并且该成员变量被设置为0，则系统自动使用显示窗口客户区的宽高作为后台缓冲区的宽高。BackBufferCount指定后台缓冲区的数量。该值可以为0、1、2、3，其中0和1时都表示创建一个后台缓冲区。通常使用一个后台缓冲区和一个主缓存，主缓存将其显示在屏幕上。<br /></p></div><img src ="http://www.cppblog.com/Daywei0123/aggbug/165746.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Daywei0123/" target="_blank">Daywei</a> 2012-02-16 15:24 <a href="http://www.cppblog.com/Daywei0123/archive/2012/02/16/165746.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于Direct2D使用的问题</title><link>http://www.cppblog.com/Daywei0123/archive/2012/01/30/164689.html</link><dc:creator>Daywei</dc:creator><author>Daywei</author><pubDate>Mon, 30 Jan 2012 09:13:00 GMT</pubDate><guid>http://www.cppblog.com/Daywei0123/archive/2012/01/30/164689.html</guid><wfw:comment>http://www.cppblog.com/Daywei0123/comments/164689.html</wfw:comment><comments>http://www.cppblog.com/Daywei0123/archive/2012/01/30/164689.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Daywei0123/comments/commentRss/164689.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Daywei0123/services/trackbacks/164689.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;这两天考虑把DirectX 版本升级，同时把相关的功能也升级，比如画图，之前用的是DirectDraw比较老，现在想改用Direct2D。<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Direct2D是DirectX11中的新特性，可以说是DirectDraw的升级版。DirectX最新版本<a href="http://www.microsoft.com/download/en/details.aspx?displaylang=en&amp;id=6812">http://www.microsoft.com/download/en/details.aspx?displaylang=en&amp;id=6812</a>，当然要想使用DirectX11需要系统支持，win7中就已带DirectX11，而vista中使用就需要Update，以下来自MSDN文档<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>Direct3D 11 Runtime<br /><br /></strong>To obtain the Direct3D 11 runtime, please install the release version of Windows 7 or Windows Server 2008 R2. Windows Vista users can follow the procedure described in <a href="http://go.microsoft.com/fwlink/?LinkId=160189">KB 971644</a> (or <a href="http://support.microsoft.com/kb/971512/">KB 971512</a> for corporate network users). <br /><br /><a href="http://support.microsoft.com/kb/971644">http://support.microsoft.com/kb/971644</a><br /><br />具体如何去实现一个DirectX2D Application&nbsp; 参见DirectX 2010.6 版本中SDK文档&#8212;&#8212;Windows DirectX Graphics Documention&#8212;&#8212;Direct2D<br /><br />Direct2D入门：<a href="http://blog.pfan.cn/lym51/52200.html">http://blog.pfan.cn/lym51/52200.html</a><br />使用Direct2D绘图<a href="http://msdn.microsoft.com/zh-cn/magazine/ee413543.aspx">http://msdn.microsoft.com/zh-cn/magazine/ee413543.aspx</a><img src ="http://www.cppblog.com/Daywei0123/aggbug/164689.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Daywei0123/" target="_blank">Daywei</a> 2012-01-30 17:13 <a href="http://www.cppblog.com/Daywei0123/archive/2012/01/30/164689.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>如何从DirectX中获取图像</title><link>http://www.cppblog.com/Daywei0123/archive/2011/12/23/162678.html</link><dc:creator>Daywei</dc:creator><author>Daywei</author><pubDate>Fri, 23 Dec 2011 09:45:00 GMT</pubDate><guid>http://www.cppblog.com/Daywei0123/archive/2011/12/23/162678.html</guid><wfw:comment>http://www.cppblog.com/Daywei0123/comments/162678.html</wfw:comment><comments>http://www.cppblog.com/Daywei0123/archive/2011/12/23/162678.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Daywei0123/comments/commentRss/162678.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Daywei0123/services/trackbacks/162678.html</trackback:ping><description><![CDATA[<div style="padding-bottom: 0px; overflow-y: auto; background-color: rgb(255,255,255); margin: 5px; padding-left: 0px; padding-right: 0px; font-family: Arial, Verdana, sans-serif; word-wrap: break-word; font-size: 12px; padding-top: 0px">
<p style="padding-bottom: 2px; margin: 4px 0px; padding-left: 0px; padding-right: 0px; padding-top: 2px"><pre style="background-color: rgb(240,240,240); margin: 4px 0px" class="cpp" name="code"><br class="Apple-interchange-newline" />BOOL CD3DCameraViewWnd::GetImageInfo(DWORD* pPixelBuffer, int&amp; width,int&amp; height)
{
	BOOL result = FALSE;

	IDirect3DSurface9 *_surfaceback = NULL; 
	if (FAILED(m_pDevice-&gt;GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &amp;_surfaceback)))
	{
		result = FALSE;
	}
	  

	D3DSURFACE_DESC surfaceDesc; 
	if (!FAILED(_surfaceback-&gt;GetDesc(&amp;surfaceDesc)))
	{
		width = surfaceDesc.Width;
		height = surfaceDesc.Height;
	}

	D3DLOCKED_RECT lockedRect; 
	HRESULT hr = _surfaceback-&gt;LockRect( &amp;lockedRect,0, 0); /* pointer to receive locked data指向申请到的内存区域*/ /* lock entire surface*/// no lock flags specified 

	if (!FAILED(hr))
	{
		pPixelBuffer = (DWORD*)lockedRect.pBits;
		result = TRUE;
	}

	_surfaceback-&gt;UnlockRect();
	_surfaceback-&gt;Release();

	return result;
}</pre>
<p style="padding-bottom: 2px; margin: 4px 0px; padding-left: 0px; padding-right: 0px; padding-top: 2px"><br /></p>但是这样存在着性能的问题，经测试获取一帧需要200ms左右，为什么会存在这样的问题？因为这边有资源锁定操作。 
<p>&nbsp;</p>
<p style="padding-bottom: 2px; margin: 4px 0px; padding-left: 0px; padding-right: 0px; padding-top: 2px">
<p style="padding-bottom: 2px; margin: 4px 0px; padding-left: 0px; padding-right: 0px; padding-top: 2px">现在关键的问题就在于,资源锁定的操作速度总是非常之慢.当然,你会跟我提, OpenGL似乎能够快捷地完成这项任务.但是,在Direct3D中,资源锁定操作确实是很慢的.这里面一个主要的原因是,API,驱动,以及硬件要处理一些不可回避的后台操作.那就是GPU与CPU是并行运行的,若不加任何措施,将引起类似多线程程序同步时的竞态条件的问题.</p>
<p style="padding-bottom: 2px; margin: 4px 0px; padding-left: 0px; padding-right: 0px; padding-top: 2px">如果你试图去修改的资源正同时被一个位于GPU处理序列中的指令使用,那么整个渲染流程就会因为你的资源锁定而停顿或强制刷新(stalls and flushes).停顿(stall)会一直持续到你完成了对资源的修改并调用Unlock().而强制刷新(flush)则会要求GPU在你得到这个资源的访问权之前完成目前所有的任务.</p>
<p style="padding-bottom: 2px; margin: 4px 0px; padding-left: 0px; padding-right: 0px; padding-top: 2px">如何去解决这个问题？下面的参考资料中有一些解决方案，我没有经过认真测试，我试了其中一个GetRenderTargetData 这样的一种方法 ，感觉不太好用，why请看Reference3</p>
<p style="padding-bottom: 2px; margin: 4px 0px; padding-left: 0px; padding-right: 0px; padding-top: 2px"><br /></p>
<p style="padding-bottom: 2px; margin: 4px 0px; padding-left: 0px; padding-right: 0px; padding-top: 2px">下面给出我的解决方案</p>
<p style="padding-bottom: 2px; margin: 4px 0px; padding-left: 0px; padding-right: 0px; padding-top: 2px"><pre style="background-color: rgb(240,240,240); margin: 4px 0px" class="cpp" name="code">BOOL CD3DCameraViewWnd::GetImageInfo(DWORD* pPixelBuffer, int&amp; width,int&amp; height)
{
	BOOL result = FALSE;

	IDirect3DSurface9 *_surfaceback = NULL; 
	if (FAILED(m_pDevice-&gt;GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &amp;_surfaceback)))
	{
		result = FALSE;
	}
	  

	D3DSURFACE_DESC surfaceDesc; 
	if (!FAILED(_surfaceback-&gt;GetDesc(&amp;surfaceDesc)))
	{
		width = surfaceDesc.Width;
		height = surfaceDesc.Height;
	}</pre><pre style="background-color: rgb(240,240,240); margin: 4px 0px" class="cpp" name="code"><br /></pre><pre style="background-color: rgb(240,240,240); margin: 4px 0px" class="cpp" name="code"><span style="white-space: pre" class="Apple-tab-span">	</span>LPDIREC3DSURFACE9 surf;</pre><pre style="background-color: rgb(240,240,240); margin: 4px 0px" class="cpp" name="code"><span style="white-space: pre" class="Apple-tab-span">	</span>if(FAILED(m_pDevice-&gt;CreateOffscreenPlainSurface(width, height,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM, &amp;surf,NULL)))</pre><pre style="background-color: rgb(240,240,240); margin: 4px 0px" class="cpp" name="code"><span style="white-space: pre" class="Apple-tab-span">	</span>{</pre><pre style="background-color: rgb(240,240,240); margin: 4px 0px" class="cpp" name="code"><span style="white-space: pre" class="Apple-tab-span">		</span>result = FALSE;</pre><pre style="background-color: rgb(240,240,240); margin: 4px 0px" class="cpp" name="code"><span style="white-space: pre" class="Apple-tab-span">	</span>}</pre><pre style="background-color: rgb(240,240,240); margin: 4px 0px" class="cpp" name="code"><span style="white-space: pre" class="Apple-tab-span">	</span>D3DXLoadSurfaceFromSurface(surf, NULL,NULL,_surfaceback,NULL,NULL,D3DX_FILTER_NONE,0);

	D3DLOCKED_RECT lockedRect; 
	HRESULT hr = surf-&gt;LockRect( &amp;lockedRect,0, 0); /* pointer to receive locked data指向申请到的内存区域*/ /* lock entire surface*/// no lock flags specified 

	if (!FAILED(hr))
	{
		pPixelBuffer = (DWORD*)lockedRect.pBits;
		result = TRUE;
	}

	surf-&gt;UnlockRect();</pre><pre style="background-color: rgb(240,240,240); margin: 4px 0px" class="cpp" name="code"><pre style="background-color: rgb(240,240,240); margin: 4px 0px" class="cpp" name="code"><span style="white-space: pre" class="Apple-tab-span">	</span>surf-&gt;Release();</pre>
	_surfaceback-&gt;Release();

	return result;
}</pre><br /><br />
<p>&nbsp;</p><br />
<p>&nbsp;</p>
<p style="padding-bottom: 2px; margin: 4px 0px; padding-left: 0px; padding-right: 0px; padding-top: 2px">参考资料：Reference1.http://www.cnblogs.com/mixiyou/archive/2010/02/25/1673060.html</p>
<p style="padding-bottom: 2px; margin: 4px 0px; padding-left: 0px; padding-right: 0px; padding-top: 2px"><span style="white-space: pre" class="Apple-tab-span"></span>&nbsp; &nbsp;Reference<span class="Apple-converted-space">&nbsp;</span>2.http://www.cnblogs.com/mixiyou/archive/2010/02/25/1673425.html</p>
<p style="padding-bottom: 2px; margin: 4px 0px; padding-left: 0px; padding-right: 0px; padding-top: 2px"><span style="white-space: pre" class="Apple-tab-span"></span>&nbsp; &nbsp;Reference&nbsp;3.<a href="http://blog.csdn.net/Nightmare/article/details/1707362" _xhe_href="http://blog.csdn.net/Nightmare/article/details/1707362">http://blog.csdn.net/Nightmare/article/details/1707362</a></p>
<p style="padding-bottom: 2px; margin: 4px 0px; padding-left: 0px; padding-right: 0px; padding-top: 2px"><span style="white-space: pre" class="Apple-tab-span"></span>&nbsp;&nbsp; Reference 4.<a href="http://www.cnblogs.com/lancidie/archive/2011/3/14.html" _xhe_href="http://www.cnblogs.com/lancidie/archive/2011/3/14.html">http://www.cnblogs.com/lancidie/archive/2011/3/14.html</a><br /></p></div> <img src ="http://www.cppblog.com/Daywei0123/aggbug/162678.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Daywei0123/" target="_blank">Daywei</a> 2011-12-23 17:45 <a href="http://www.cppblog.com/Daywei0123/archive/2011/12/23/162678.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>