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

DX3D如何改变一个三角形在在窗口上的位置

2012-02-07 
DX3D怎么改变一个三角形在在窗口上的位置?这是我写得代码,但是就是移动不了位置,各位同学们帮帮忙啦,我刚

DX3D怎么改变一个三角形在在窗口上的位置?

这是我写得代码,但是就是移动不了位置,各位同学们帮帮忙啦,我刚学dx,搞不懂那个矩阵转过来转过去的有什么用。

C/C++ code
#include <Windows.h>#include<d3d9.h>#include<d3dx9.h>#pragma comment(lib, "d3d9.lib")#pragma comment(lib, "d3dx9.lib")LRESULT WINAPI MsgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam);const int width=800;const int height=600;//----------------------------------------------------IDirect3D9*       g_d3d=NULL;IDirect3DDevice9* g_dev=NULL;IDirect3DVertexBuffer9 * g_vb=NULL;D3DXMATRIX             g_orgho;D3DXMATRIX x,y,p;//----------------------------------------------------bool InitializeD3D(HWND,bool);bool InitializeObject();void Render();void shutdown();#define D3DFVF_VERTEX (D3DFVF_XYZRHW)    struct stD3DVertex    {        float x,y,z,row;        DWORD col;    };//----------------------------------------------------int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE PrevInstance,PSTR cmdLine,int ShowCmd){    HWND hWnd;//窗口句柄    //=================定义窗口类======================    WNDCLASS wc;    wc.style       = CS_CLASSDC;    wc.lpfnWndProc = &MsgProc;    wc.cbClsExtra  = 0;    wc.cbWndExtra  = 0;    wc.hInstance   =NULL ;    wc.hIcon       =NULL ;    wc.hCursor     =LoadCursor(NULL,IDC_ARROW) ;    wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);    wc.lpszMenuName  =NULL;    wc.lpszClassName ="Class1";    //----------------------------------------------------        //=================注册窗口类=========================    RegisterClass(&wc);    //----------------------------------------------------    //创建窗口    hWnd=CreateWindow("Class1","Game",WS_OVERLAPPEDWINDOW,100,100,width,height,NULL,NULL,hInstance,NULL);    ShowWindow(hWnd,SW_SHOWDEFAULT);    UpdateWindow(hWnd);    MSG msg;                               if (InitializeD3D(hWnd,false))         {                             ZeroMemory(&msg, sizeof(msg));                 while(msg.message != WM_QUIT)                 {                     if(PeekMessage(&msg,hWnd,0,0,PM_REMOVE))                     {                         TranslateMessage(&msg);                         DispatchMessage(&msg);                     }                     else                     {                         Render();                     }                 }                     }                shutdown();    UnregisterClass("Class1",hInstance);    return 0;}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);        return 0;        break;    }    return DefWindowProc(hWnd,msg,wParam,lParam);}bool InitializeD3D(HWND hWnd,bool FullScreen){    D3DPRESENT_PARAMETERS d3dpp;    D3DDISPLAYMODE Displaymode;    ZeroMemory(&d3dpp, sizeof(d3dpp));    g_d3d =Direct3DCreate9(D3D_SDK_VERSION);    if(g_d3d==NULL){MessageBox(0,"获得D3D接口失败!",0,0);return false;}    if(FAILED(g_d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &Displaymode)))        {MessageBox(0,"获得设备显示模式失败!",0,0);return false;}     if(FullScreen)     {           d3dpp.Windowed=false;           d3dpp.BackBufferHeight=height;           d3dpp.BackBufferWidth=width;     }     else     d3dpp.Windowed = TRUE;     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;     d3dpp.BackBufferFormat = Displaymode.Format;    if(FAILED(g_d3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING        ,&d3dpp,&g_dev))){MessageBox(0,"获得D3D设备失败!",0,0);return false;}    if (FAILED(InitializeObject())){MessageBox(0,"初始化图形对象失败!",0,0);return false;}        return true;    }bool InitializeObject(){                DWORD col=D3DCOLOR_XRGB(255,255,0);    stD3DVertex st[3]=    {        {320,150,0, 1.0f ,col},        {420,350,0,1.0f,D3DCOLOR_XRGB(255,0,0)},        {220,350,0,1.0f,D3DCOLOR_XRGB(0,255,0)},            };            g_dev->CreateVertexBuffer(sizeof(st),0,D3DFVF_VERTEX,D3DPOOL_DEFAULT,&g_vb,NULL);    if (g_vb==NULL){MessageBox(0,"创建顶点缓存失败!",0,0);return false;}    void*pt=NULL;    g_vb->Lock(0,sizeof(st),(void**)&pt,0);    memcpy(pt,st,sizeof(st));    g_vb->Unlock();            D3DXVECTOR3 pos(0.0f,0.0f,-5.0f);    D3DXVECTOR3 target(0.0f,0.0f,0.0f);    D3DXVECTOR3 up(0.0f,1.0f,0.0f);    D3DXMATRIX V;    D3DXMatrixLookAtLH(&V,&pos,&target,&up);    g_dev->SetTransform(D3DTS_VIEW,&V);    D3DXMATRIX proj;    D3DXMatrixPerspectiveFovLH(&proj,D3DX_PI*0.5f,(float)width/(float)height,1.0f,1000.0f);    g_dev->SetTransform(D3DTS_PROJECTION,&proj);    D3DXMatrixTranslation(&x, 5000.0f, 100.0f, 0.0f);    g_dev->SetTransform(D3DTS_PROJECTION,&x);    return true;}void Render(){    g_dev->SetTransform(D3DTS_WORLD,&p);g_dev->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);g_dev->BeginScene();g_dev->SetStreamSource(0,g_vb,NULL,sizeof(stD3DVertex));g_dev->SetFVF(D3DFVF_VERTEX);g_dev->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);g_dev->EndScene();g_dev->Present(0,0,0,0);}void shutdown(){    if (g_d3d!=NULL)g_d3d->Release();    if(g_dev!=NULL)g_dev->Release();    g_d3d=NULL;    g_dev=NULL;    g_vb->Release();      } 



[解决办法]
D3DXMATRIX pos(100,10,2); //三角形在场景中的位置
Device->SetTransform(D3DTS_WORLD, &pos); //使用宏D3DTS_WORLD,将三角形放到世界中

热点排行