码迷,mamicode.com
首页 > Windows程序 > 详细

Windows D3D渲染到纹理

时间:2019-09-25 12:27:39      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:move   int   form   nbsp   register   rtt   数据   remove   cleanup   

D3D渲染到纹理

  1 #include <d3dx9.h>
  2 
  3 //-----------------------------------------------------------------------------
  4 // Desc: 全局变量
  5 //-----------------------------------------------------------------------------
  6 LPDIRECT3D9             g_pD3D       = NULL;    //Direct3D对象
  7 LPDIRECT3DDEVICE9       g_pd3dDevice = NULL;    //Direct3D设备对象
  8 LPDIRECT3DVERTEXBUFFER9 g_pVB        = NULL;    //顶点缓冲区对象
  9 LPDIRECT3DTEXTURE9      g_pTexture   = NULL;    //纹理对象
 10 
 11 LPDIRECT3DTEXTURE9      g_pRender2Tex = NULL;
 12 LPDIRECT3DSURFACE9      g_pRenderSur = NULL;
 13 LPDIRECT3DSURFACE9      g_pBackupSur = NULL;
 14 
 15 //-----------------------------------------------------------------------------
 16 // Desc: 顶点结构
 17 //-----------------------------------------------------------------------------
 18 struct CUSTOMVERTEX
 19 {
 20     FLOAT x, y, z;    //顶点位置  
 21     FLOAT u,v ;          //顶点纹理坐标
 22 };
 23 #define D3DFVF_CUSTOMVERTEX   (D3DFVF_XYZ|D3DFVF_TEX1)
 24 
 25 
 26 //-----------------------------------------------------------------------------
 27 // Desc: 设置变换矩阵
 28 //-----------------------------------------------------------------------------
 29 VOID SetupMatrices()
 30 {
 31     //创建并设置世界矩阵
 32     D3DXMATRIXA16 matWorld;
 33     D3DXMatrixIdentity( &matWorld );
 34     g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
 35 
 36     //创建并设置观察矩阵
 37     D3DXVECTOR3 vEyePt( 0.0f, 0.0f, -10 );
 38     D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );
 39     D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
 40     D3DXMATRIXA16 matView;
 41     D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
 42     g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
 43 
 44     //创建并设置投影矩阵
 45     D3DXMATRIXA16 matProj;
 46     D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
 47     g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
 48 }
 49 
 50 
 51 //-----------------------------------------------------------------------------
 52 // Desc: 初始化Direct3D
 53 //-----------------------------------------------------------------------------
 54 HRESULT InitD3D( HWND hWnd )
 55 {
 56     //创建Direct3D对象, 该对象用于创建Direct3D设备对象
 57     if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
 58         return E_FAIL;
 59 
 60     //设置D3DPRESENT_PARAMETERS结构, 准备创建Direct3D设备对象
 61     D3DPRESENT_PARAMETERS d3dpp;
 62     ZeroMemory( &d3dpp, sizeof(d3dpp) );
 63     d3dpp.Windowed = TRUE;
 64     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
 65     d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
 66 
 67     //创建Direct3D设备对象
 68     if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
 69                                       D3DCREATE_SOFTWARE_VERTEXPROCESSING,
 70                                       &d3dpp, &g_pd3dDevice ) ) )
 71     {
 72         return E_FAIL;
 73     }
 74 
 75     //禁用照明效果
 76     g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE ); 
 77 
 78     //设置变换矩阵
 79     SetupMatrices();
 80 
 81     HRESULT hr = g_pd3dDevice->CreateTexture(256,256,1,D3DUSAGE_RENDERTARGET,
 82         D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &g_pRender2Tex, NULL);
 83     if (FAILED(hr))
 84     {
 85         return E_FAIL;
 86     }
 87     g_pRender2Tex->GetSurfaceLevel(0, &g_pRenderSur);
 88     
 89     return S_OK;
 90 }
 91 
 92 
 93 //-----------------------------------------------------------------------------
 94 // Desc: 创建场景图形
 95 //-----------------------------------------------------------------------------
 96 HRESULT InitGriphics()
 97 {
 98     //创建纹理对象
 99     if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, L"texture.jpg", &g_pTexture ) ) )
100     {
101        MessageBox(NULL, L"创建纹理失败", L"Texture.exe", MB_OK);
102        return E_FAIL;
103     }
104 
105     //顶点数据
106     CUSTOMVERTEX g_Vertices[] =
107     {
108         { -3,   -3,  0.0f,  0.0f, 1.0f},   
109         { -3,    3,  0.0f,  0.0f, 0.0f},    
110         {  3,   -3,  0.0f,  1.0f, 1.0f},    
111         {  3,    3,  0.0f,  1.0f, 0.0f }
112 
113     };
114     
115     //创建顶点缓冲区
116     if( FAILED( g_pd3dDevice->CreateVertexBuffer( 4*sizeof(CUSTOMVERTEX),
117                                                  0, D3DFVF_CUSTOMVERTEX,
118                                                  D3DPOOL_MANAGED, &g_pVB,NULL ) ) )
119     {
120         return E_FAIL;
121     }
122 
123     //填充顶点缓冲区
124     VOID* pVertices;
125     if( FAILED( g_pVB->Lock( 0, sizeof(g_Vertices), (void**)&pVertices, 0 ) ) )
126         return E_FAIL;
127     memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
128     g_pVB->Unlock();
129 
130     return S_OK;
131 }
132 
133 
134 //-----------------------------------------------------------------------------
135 // Desc: 释放创建的对象
136 //-----------------------------------------------------------------------------
137 VOID Cleanup()
138 {
139     //释放纹理对象
140     if( g_pTexture != NULL )
141         g_pTexture->Release();
142 
143     //释放顶点缓冲区对象
144     if( g_pVB != NULL )        
145         g_pVB->Release();
146 
147     //释放Direct3D设备对象
148     if( g_pd3dDevice != NULL ) 
149         g_pd3dDevice->Release();
150 
151     //释放Direct3D对象
152     if( g_pD3D != NULL )       
153         g_pD3D->Release();
154 }
155 
156 
157 //-----------------------------------------------------------------------------
158 // Desc: 渲染图形 
159 //-----------------------------------------------------------------------------
160 VOID Render()
161 {
162     //清空后台缓冲区
163     g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(45, 50, 170), 1.0f, 0 );
164     
165     //开始在后台缓冲区绘制图形
166     if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
167     {
168         g_pd3dDevice->GetRenderTarget(0, &g_pBackupSur);
169         g_pd3dDevice->SetRenderTarget(0, g_pRenderSur);
170 
171         /*RTT*/
172         g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0 );
173         g_pd3dDevice->SetTexture( 0, g_pTexture ); //设置纹理
174         g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
175         g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
176         g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2);
177 
178         D3DXSaveTextureToFileA("tt.tga", D3DXIFF_TGA, g_pRender2Tex, NULL);
179 
180         /*真是场景*/
181         g_pd3dDevice->SetRenderTarget(0, g_pBackupSur);
182 
183         g_pd3dDevice->SetTexture( 0, g_pTexture ); //设置纹理
184         g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
185         g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
186         g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2);
187 
188 
189         //结束在后台缓冲区绘制图形
190         g_pd3dDevice->EndScene();
191     }
192 
193     //将在后台缓冲区绘制的图形提交到前台缓冲区显示
194     g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
195 }
196 
197 
198 //-----------------------------------------------------------------------------
199 // Desc: 消息处理
200 //-----------------------------------------------------------------------------
201 LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
202 {
203     switch( msg )
204     {
205     case WM_DESTROY:
206         Cleanup();
207         PostQuitMessage( 0 );
208         return 0;
209     }
210 
211     return DefWindowProc( hWnd, msg, wParam, lParam );
212 }
213 
214 
215 
216 //-----------------------------------------------------------------------------
217 // Desc: 入口函数
218 //-----------------------------------------------------------------------------
219 INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
220 {
221 
222     //注册窗口类
223     WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
224                       GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
225                        L"ClassName", NULL };
226     RegisterClassEx( &wc );
227 
228     //创建窗口
229     HWND hWnd = CreateWindow(  L"ClassName", L"纹理影射基础",
230                               WS_OVERLAPPEDWINDOW, 200, 100, 800, 600,
231                               GetDesktopWindow(), NULL, wc.hInstance, NULL );
232 
233     //初始化Direct3D
234     if( SUCCEEDED( InitD3D( hWnd ) ) )
235     {
236         //创建场景图形
237         if( SUCCEEDED( InitGriphics() ) )
238         {
239             //显示窗口
240             ShowWindow( hWnd, SW_SHOWDEFAULT );
241             UpdateWindow( hWnd );
242 
243             //进入消息循环
244             MSG msg;
245             ZeroMemory( &msg, sizeof(msg) );
246             while( msg.message!=WM_QUIT )
247             {
248                 if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
249                 {
250                     TranslateMessage( &msg );
251                     DispatchMessage( &msg );
252                 }
253                 else
254                 {
255                     Render();  //渲染图形
256                 }
257             }
258         }
259     }
260 
261     UnregisterClass(  L"ClassName", wc.hInstance );
262     return 0;
263 }

 

Windows D3D渲染到纹理

标签:move   int   form   nbsp   register   rtt   数据   remove   cleanup   

原文地址:https://www.cnblogs.com/diaoss/p/11583572.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!