首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络游戏 >

关于顶点帖图,该怎么处理

2012-02-20 
关于顶点帖图我这里构建了4个顶点,用一张图贴上去,但是出问题code如下!----#included3dx9.h////////////

关于顶点帖图
我这里构建了4个顶点,用一张图贴上去,但是出问题

code如下!

----

#include   <d3dx9.h>


//////////////////////////////////////////////////////////////////////////
LRESULT   CALLBACK   WndProc(HWND   hwnd,   UINT   msg,   WPARAM   wParam,   LPARAM   lParam);
int   InitDevice(HINSTANCE   hinstance,   int   width,   int   height,   bool   screen,   D3DDEVTYPE   deviceType,   IDirect3DDevice9**   device);
int   InitGame();
int   Render();
int   Release();
//////////////////////////////////////////////////////////////////////////
IDirect3DDevice9*   device   =   0;
struct   VERTEX
{
//D3DXVECTOR3   position;
//D3DXVECTOR3   normal;
float   x,   y,   z,   rhw;
float   tu,   tv;
};
const   DWORD   FVF   =   (D3DFVF_XYZRHW   |   D3DFVF_TEX1);
LPDIRECT3DVERTEXBUFFER9   VB;
LPDIRECT3DINDEXBUFFER9     IB;
LPDIRECT3DTEXTURE9             backgroundTexture;
//////////////////////////////////////////////////////////////////////////


//==============================
//   winmain
//==============================

int   WINAPI   WinMain(HINSTANCE   hInstance,   HINSTANCE   hPrevInstance,   LPSTR   lpCmdLine,   int   nShowCmd)
{
MSG   msg;
ZeroMemory(&msg,   sizeof(MSG));

InitDevice(hInstance,     800,   600,   true,   D3DDEVTYPE_HAL,   &device);
InitGame();
device-> Clear(NULL,   NULL,   NULL,   NULL,   NULL,   NULL);

while(msg.message   !=   WM_QUIT)
{
if(::PeekMessage(&msg,   0,   0,   0,   PM_REMOVE))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}

//
//Sleep(1000);
device-> SetCursorPosition(400,   400,   0);
Render();
device-> Present(NULL,   NULL,   NULL,   NULL);
}

Release();
device-> Release();
return   1;
}

//===================================
//   render   the   game
//===================================

int   Render()
{
//Begin   the   scene
if   (   SUCCEEDED(device-> BeginScene())   )
{
//Setup   texture   stage   states   for   the   background
device-> SetTexture(   0,   backgroundTexture   );
device-> SetTextureStageState(   0,   D3DTSS_COLORARG1,   D3DTA_TEXTURE   );
device-> SetTextureStageState(   0,   D3DTSS_COLOROP,       D3DTOP_SELECTARG1   );

//sets   the   flexible   vertex   format
device-> SetFVF(FVF);
device-> SetStreamSource(   0,   VB,   0,   sizeof(VERTEX)   );
device-> SetIndices(   IB   );
//device-> SetRenderState(D3DRS_FILLMODE,   D3DFILL_WIREFRAME);

device-> DrawIndexedPrimitive(   D3DPT_TRIANGLELIST,   0,   0,   4,   0,   2   );
device-> EndScene();
}

return   1;
}


//==================================
//   initialization   game
//==================================

int   InitGame()
{

//
//   load   texture


//

D3DXCreateTextureFromFileEx(   device,   "E:\\dx   sample\\terrain\\Terrain\\Terrain\\lake.bmp ",  
D3DX_DEFAULT,   D3DX_DEFAULT,   D3DX_DEFAULT,   0,   D3DFMT_UNKNOWN,   D3DPOOL_MANAGED,   D3DX_FILTER_TRIANGLE|D3DX_FILTER_MIRROR,
D3DX_FILTER_TRIANGLE|D3DX_FILTER_MIRROR,   0,   NULL,   NULL,   &backgroundTexture);


//
//initialization   vertex
//
VERTEX   Vertices[]   =
{
{     0.0f,     0.0f,   0.5f,   1.0f,   0.0f,   0.0f,   },   //   x,   y,   z,   rhw,   color
{     800.0f,     0.0f,   0.5f,   1.0f,   1.0f,   0.0f,   },
{     800.0f,   600.0f,   0.5f,   1.0f,   1.0f,   1.0f,   },
{     0.0f,   600.0f,   0.5f,   1.0f,   0.0f,   1.0f,   },
};

DWORD   sizeofVertices   =   sizeof(Vertices);

if   (   FAILED(   device-> CreateVertexBuffer(   sizeofVertices,   D3DUSAGE_WRITEONLY,   FVF,   D3DPOOL_MANAGED,   &VB,   NULL   )   )   )
{
MessageBox(0,   "CreateVertexBuffer()   failed! ",   0,   0);
return   0;
}

VERTEX*   pVertices;
//   the   third   parameter   changed   from   BYTE**   to   VOID**   in   DX9
if(   FAILED(   VB-> Lock(   0,   sizeofVertices,   (VOID**)&pVertices,   0   )   )   )
return   E_FAIL;
memcpy(   pVertices,   Vertices,   sizeofVertices);
VB-> Unlock();


//
//   Initialize   the   Index   buffer
//

WORD   wIndices[]   =   {0,   1,   2,   0,   2,   3};

DWORD   sizeofIndices   =   sizeof(wIndices);

//   Create   the   index   buffer
//   six   parameter   new   in   DX9
if(   FAILED(   device-> CreateIndexBuffer(   sizeofIndices,   0,   D3DFMT_INDEX16,   D3DPOOL_MANAGED,   &IB,   NULL   )   )   )
return   E_FAIL;

VOID*   pIndices;
//   DX9   third   parameter   changed   from   BYTE**   to   VOID   **
if(   FAILED(   IB-> Lock(   0,   sizeofIndices,   (VOID**)&pIndices,   0   )   )   )
return   E_FAIL;
memcpy(   pIndices,   wIndices,   sizeof(wIndices)   );
IB-> Unlock();

return   1;
}


//=====================================
//   initialization   the   device
//=====================================

int   InitDevice(HINSTANCE   hinstance,   int   width,   int   height,   bool   windowed,   D3DDEVTYPE   deviceType,   IDirect3DDevice9**   device)
{
WNDCLASS   wc;
wc.style   =   CS_HREDRAW   |   CS_VREDRAW;
wc.lpfnWndProc   =   (WNDPROC)WndProc;
wc.cbClsExtra   =   0;
wc.cbWndExtra   =   0;
wc.hInstance   =   hinstance;
wc.hIcon   =   NULL;
wc.hCursor   =   LoadCursor(0,   IDC_ARROW);
wc.hbrBackground   =   (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName   =   0;
wc.lpszClassName   =   "Scene ";


if   (   !RegisterClass(&wc)   )
{
MessageBox(0,   "RegisterClass()   Failed! ",   0,   0);
return   0;
}

HWND   hwnd   =   0;
hwnd   =   ::CreateWindow( "Scene ",   "Scene ",   WS_EX_TOPMOST,   0,   0,   width,   height,   0,   0,   hinstance,   0);
if   (   !hwnd   )
{
MessageBox(0,   "CreateWindow()   Failed ",   0,   0);
return   0;
}

ShowWindow(hwnd,   SW_SHOW);
UpdateWindow(hwnd);

//
//   initialization   d3d
//

//   create   the   IDirect3D9   object
HRESULT   hr   =   0;
IDirect3D9*   d3d9   =   0;
d3d9   =   Direct3DCreate9(D3D_SDK_VERSION);
if   (   !d3d9   )
{
MessageBox(0,   "Direct3DCreate9()   failed! ",   0,   0);
return   0;
}

//   check   for   hardware   vp
D3DCAPS9   caps;
d3d9-> GetDeviceCaps(D3DADAPTER_DEFAULT,   D3DDEVTYPE_HAL,   &caps);
int   vp   =   0;
if   (   caps.DevCaps   &   D3DDEVCAPS_HWTRANSFORMANDLIGHT   )
vp   =   D3DCREATE_HARDWARE_VERTEXPROCESSING;
else
vp   =   D3DCREATE_SOFTWARE_VERTEXPROCESSING;

//   fill   in   the   D3DPRESENT_PARAMETERS   structure
D3DPRESENT_PARAMETERS   d3dpp;
d3dpp.BackBufferWidth   =   width;
d3dpp.BackBufferHeight   =   height;
d3dpp.BackBufferFormat   =   D3DFMT_A8R8G8B8;
d3dpp.BackBufferCount   =   1;
d3dpp.MultiSampleType   =   D3DMULTISAMPLE_NONE;
d3dpp.MultiSampleQuality   =   0;
d3dpp.SwapEffect   =   D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow   =   hwnd;
d3dpp.Windowed   =   windowed;
d3dpp.EnableAutoDepthStencil   =   true;
d3dpp.AutoDepthStencilFormat   =   D3DFMT_A8R8G8B8;
d3dpp.Flags   =   0;
d3dpp.FullScreen_RefreshRateInHz   =   D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval   =   D3DPRESENT_INTERVAL_IMMEDIATE;

//   create   the   device
hr   =   d3d9-> CreateDevice(  
D3DADAPTER_DEFAULT,   //primary   adapter
D3DDEVTYPE_HAL,           //device   type
hwnd,                               //window   associated   with   device
vp,                                   //vertex   processing   method
&d3dpp,                           //present   parameters
device                           //return   create   device
);

if   (   FAILED(hr)   )
{
//try   again   using   a   16-bit   depth   buffer
d3dpp.AutoDepthStencilFormat   =   D3DFMT_D16;
hr   =   d3d9-> CreateDevice(  
D3DADAPTER_DEFAULT,   //primary   adapter
D3DDEVTYPE_HAL,           //device   type
hwnd,                               //window   associated   with   device


vp,                                   //vertex   processing   method
&d3dpp,                           //present   parameters
device                           //return   create   device
);

if   (   FAILED(hr)   )
{
d3d9-> Release();
MessageBox(0,   "CreateDevice()   failed! ",   0,   0);
return   0;
}
}

//device-> Release();


d3d9-> Release();
}


//================================
//   release   resource
//================================

int   Release()
{
backgroundTexture-> Release();
return   1;
}


//================================
//   message   disposal
//================================

LRESULT   CALLBACK   WndProc(HWND   hwnd,   UINT   msg,   WPARAM   wParam,   LPARAM   lParam)
{
switch(   msg   )
{
case   WM_DESTROY:
{
::PostQuitMessage(0);
break;
}

case   WM_KEYDOWN:
{
if(   wParam   ==   VK_ESCAPE   )
{
::DestroyWindow(hwnd);
}
break;
}
}

return   ::DefWindowProc(hwnd,   msg,   wParam,   lParam);
}

[解决办法]
1, Clear函数使用错误
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
// ...
device-> Clear(NULL, NULL, NULL, NULL, NULL, NULL);
}

首先Clear参数不对;另外应该每次渲染前都调用一次,清除FrameBuffer、ZBuffer。

可以放到Render()函数里:
int Render()
{
device-> Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(99,99,99), 1.0f, 0 );
}

2,好多D3D资源没释放, 有内存泄漏。
D3D Device指针等等 。

热点排行