D3D加载2D纹理

时间:2023-02-07 23:28:46
D3D加载2D纹理D3D加载2D纹理View Code
  1 //利用D2D渲染纹理
  2 #include <Windows.h>
  3 #include <d3dx9.h>
  4 #include <d3d9.h>
  5 
  6 #pragma comment(lib,"d3d9.lib")
  7 #pragma comment(lib,"d3dx9.lib")
  8 #pragma comment(lib,"winmm.lib")
  9 
 10 #define        WNDCLASSNAME    ("ZMD3D")
 11 #define        WNDTITLE        ("D3D Texture Mapping")
 12 #define        WNDWIDTH        (640)
 13 #define        WNDHEIGHT        (480)
 14 
 15 //全局变量
 16 LPDIRECT3D9                g_pD3d        = NULL;
 17 LPDIRECT3DDEVICE9        g_pD3dDev    = NULL;    
 18 LPDIRECT3DTEXTURE9        g_Texture   = NULL; //纹理
 19 LPDIRECT3DVERTEXBUFFER9 g_pVB       = NULL; //顶点缓冲区对象  
 20 
 21 //定义顶点格式
 22 struct D3DVertex
 23 {
 24     float x,y,z,rhw;    //3d坐标
 25     DWORD    diffuse;    //顶点颜色
 26     float tu,tv;        //纹理uv坐标
 27 };
 28 
 29 D3DVertex g_Vertices[] = 
 30 {
 31     {0.0f,0.0f,0.5f,1.0f,0xffffffff,0.0f,0.0f},        //v0
 32     {256.0f,0.0f,0.5f,1.0f,0xffffffff,1.0f,0.0f},        //v1
 33     {256.0f,256.0f,0.5f,1.0f,0xffffffff,1.0f,1.0f},    //v2
 34 
 35     //{0.0f,0.0f,0.5f,1.0f,0xffffffff,0.0f,0.0f},    //v3
 36     //{256.0f,256.0f,0.5f,1.0f,0xffffffff,1.0f,1.0f},//v4
 37     {0.0f,256.0f,0.5f,1.0f,0xffffffff,0.0f,1.0f}    //v5
 38 };
 39 
 40 //自定义顶点格式描述
 41 #define     D3DFVF_CUSTOM    (D3DFVF_XYZRHW | D3DFVF_DIFFUSE|D3DFVF_TEX1)
 42 
 43 //全局函数
 44 //初始化d3d
 45 bool InitializeD3D(HWND hWnd,bool bFullScreen);
 46 //初始化渲染纹理
 47 bool InitializeObject();
 48 //渲染纹理
 49 void RenderScene();
 50 //释放资源
 51 void CleanUp();
 52 
 53 bool InitializeD3D(HWND hWnd,bool bFullScreen)
 54 {
 55     D3DDISPLAYMODE    displayMode;
 56     //创建D3D
 57     g_pD3d = Direct3DCreate9(D3D_SDK_VERSION);
 58     if (!g_pD3d)
 59     {
 60         return false;
 61     }
 62     //获取桌面显示模式
 63     if (FAILED(g_pD3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&displayMode)))
 64     {
 65         return false;
 66     }
 67     //填充设备参数
 68     D3DPRESENT_PARAMETERS    d3dpp;
 69     ZeroMemory(&d3dpp,sizeof(d3dpp));
 70     if (bFullScreen)
 71     {
 72         d3dpp.Windowed = FALSE;
 73         d3dpp.BackBufferWidth = WNDWIDTH;
 74         d3dpp.BackBufferHeight = WNDHEIGHT;
 75     }
 76     else
 77     {
 78         d3dpp.Windowed = TRUE;
 79     }
 80     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
 81     d3dpp.BackBufferFormat = displayMode.Format;
 82     
 83     //创建d3d设备
 84     if (FAILED(g_pD3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,
 85         D3DCREATE_HARDWARE_VERTEXPROCESSING,&d3dpp,&g_pD3dDev)))
 86     {
 87         return false;
 88     }
 89     
 90     //创建纹理
 91     if (FAILED(D3DXCreateTextureFromFile(g_pD3dDev,"aa.bmp"/*"jpg_雪洞山壁.dds"*/,&g_Texture)))
 92     {
 93         return false;
 94     }
 95     //初始化渲染图像
 96     if(!InitializeObject())
 97     {
 98         return false;
 99     }
100     return true;
101 }
102 //D3D渲染2d精灵
103 void Render2D(LPDIRECT3DTEXTURE9 pTex,float fDestX,float fDestY,int nWidth,int nHeight)
104 {
105     if (pTex)
106     {
107         DWORD dwDiffuse    = 0xffffffff;
108         D3DVertex vertex[4];
109         ZeroMemory(vertex,sizeof(vertex));
110         vertex[0].rhw = vertex[1].rhw = vertex[2].rhw = vertex[3].rhw = 1.0f;
111         vertex[0].z = vertex[1].z = vertex[2].z = vertex[3].z = 0.5f;
112         vertex[0].diffuse = vertex[1].diffuse = vertex[2].diffuse = vertex[3].diffuse = dwDiffuse;
113 
114         vertex[0].y = vertex[1].y = fDestY; vertex[1].x = vertex[2].x = fDestX +nWidth;
115         vertex[0].x = vertex[3].x = fDestX; vertex[3].y = vertex[2].y = fDestY +nHeight;
116         
117         vertex[0].tv = vertex[1].tv = 0.0f;
118         vertex[1].tu = vertex[2].tu = 1.0f;
119         vertex[0].tu = vertex[3].tu = 0.0f;
120         vertex[3].tv = vertex[2].tv = 1.0f;
121         
122         //设置顶点格式
123         g_pD3dDev->SetFVF(D3DFVF_CUSTOM);
124         //设置纹理
125         g_pD3dDev->SetTexture(0,pTex);
126         g_pD3dDev->DrawPrimitiveUP(D3DPT_TRIANGLEFAN,2,&vertex,sizeof(D3DVertex));
127     }
128 }
129 
130 bool InitializeObject()
131 {
132     //定义6个顶点绘制一个矩形
133     /*
134         v0            v1
135       v3|-----------|
136         |            |
137         |            |
138         |            |
139         |            |
140      v5    |-----------|v2
141                   v4
142     */              
143     D3DVertex Vertices[] = 
144     {
145         {0.0f,0.0f,0.5f,1.0f,0xffffffff,0.0f,0.0f},        //v0
146         {256.0f,0.0f,0.5f,1.0f,0xffffffff,1.0f,0.0f},        //v1
147         {256.0f,256.0f,0.5f,1.0f,0xffffffff,1.0f,1.0f},    //v2
148 
149         //{0.0f,0.0f,0.5f,1.0f,0xffffffff,0.0f,0.0f},    //v3
150         //{256.0f,256.0f,0.5f,1.0f,0xffffffff,1.0f,1.0f},//v4
151         {0.0f,256.0f,0.5f,1.0f,0xffffffff,0.0f,1.0f}    //v5
152     };
153     //创建顶点缓冲区
154     /*if(FAILED(g_pD3dDev->CreateVertexBuffer(4*sizeof(D3DVertex),0,D3DFVF_CUSTOM,D3DPOOL_MANAGED,&g_pVB,NULL)))
155     {
156         return false;
157     }*/
158     //填充顶点缓冲
159     /*void *pVertices = NULL;
160     if (FAILED(g_pVB->Lock(0,sizeof(Vertices),(void **)&pVertices,0)))
161     {
162         return false;
163     }
164     memcpy(pVertices,Vertices,sizeof(Vertices));
165     g_pVB->Unlock();*/
166 
167     return true;
168 }
169 
170 //释放资源
171 void CleanUp()
172 {
173     if (g_Texture)
174     {
175         g_Texture->Release();
176     }
177     if(g_pVB)
178     {
179         g_pVB->Release();
180     }
181     if(g_pD3dDev)
182     {
183         g_pD3dDev->Release();
184     }
185     if(g_pD3d)
186     {
187         g_pD3d->Release();
188     }
189 }
190 
191 void RenderScene()
192 {
193     //请客后台缓冲区
194     g_pD3dDev->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);
195     if (SUCCEEDED(g_pD3dDev->BeginScene()))
196     {
197         //开始后台缓冲区绘制贴图
198         //g_pD3dDev->SetStreamSource(0,g_pVB,0,sizeof(D3DVertex));
199         //g_pD3dDev->SetFVF(D3DFVF_CUSTOM);
200         //g_pD3dDev->SetTexture(0,g_Texture);
201         //g_pD3dDev->DrawPrimitive(D3DPT_TRIANGLEFAN,0,2); //只要四个顶点        //使用自己创建顶点缓存
202         //g_pD3dDev->DrawPrimitiveUP(D3DPT_TRIANGLEFAN,2,&g_Vertices,sizeof(D3DVertex));    //使用内部动态顶点缓存
203         //结束后台缓冲区绘图
204 
205         Render2D(g_Texture,150.0f,150.0f,256,256);
206         g_pD3dDev->EndScene();
207     }
208     //提交到前台缓冲区
209     g_pD3dDev->Present(0,0,0,0);
210 }
211 
212 //消息处理
213 LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )  
214 {  
215     switch( msg )  
216     {  
217     case WM_DESTROY:
218         //程序结束时
219         CleanUp();
220         PostQuitMessage( 0 );  
221         return 0;  
222     }  
223 
224     return DefWindowProc( hWnd, msg, wParam, lParam );  
225 }  
226 
227 
228 //入口函数
229 int WINAPI WinMain(  HINSTANCE hInstance,  HINSTANCE hPrevInstance,  LPSTR lpCmdLine,  int nShowCmd )
230 {
231     WNDCLASS        winclass;
232     ZeroMemory(&winclass,sizeof(WNDCLASS));
233     winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
234     winclass.lpfnWndProc    = MsgProc;
235     winclass.cbClsExtra        = 0;
236     winclass.cbWndExtra        = 0;
237     winclass.hInstance        = hInstance;
238     winclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
239     winclass.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
240     winclass.lpszMenuName    = NULL; 
241     winclass.lpszClassName    = WNDCLASSNAME;
242     winclass.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
243 
244     if (!RegisterClass(&winclass)) {
245         return -1;
246     }
247 
248     int nX = ( GetSystemMetrics(SM_CXSCREEN) - WNDWIDTH)/2;
249     int nY = ( GetSystemMetrics(SM_CYSCREEN) - WNDHEIGHT)/2;
250 
251     HWND hWnd = CreateWindow(WNDCLASSNAME,WNDTITLE,WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX,
252         nX,nY,WNDWIDTH,WNDHEIGHT,NULL,NULL,hInstance,NULL);
253 
254     if (!hWnd)
255     {
256         return -1;
257     }
258     //初始化d3d
259     if(InitializeD3D(hWnd,false))
260     {
261         //填充顶点,创建定点缓冲区
262         if(InitializeObject())
263         {
264             ShowWindow( hWnd, SW_SHOWDEFAULT );  
265             UpdateWindow( hWnd );
266             //进入消息循环
267             MSG msg;
268             ZeroMemory(&msg,sizeof(msg));
269             while (msg.message != WM_QUIT)
270             {
271                 if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
272                 {
273                     TranslateMessage(&msg);  
274                     DispatchMessage(&msg);
275                 }
276                 else
277                 {
278                     //进行渲染
279                     RenderScene();
280                 }
281             }
282         }
283     }
284 
285     UnregisterClass(WNDCLASSNAME, winclass.hInstance); 
286     return 0;
287 }