[D3D9] D3DFVF_XYZ、D3DFVF_XYZRHW 的区别 (以纹理渲染的代码举例说明)

来源:互联网 发布:网络重载连接器hdc cm 编辑:程序博客网 时间:2024/05/01 23:56

POSITION (D3DFVF_XYZ)
尚未进行世界、视图、投影坐标转换。使用本地坐标系,其方向与左手坐标系一致:X轴向右递增,Y轴向上递增,Z轴向屏幕内侧递增。xyz各个轴的范围都是[-1,1],中心点是(0,0)
左上(-1.f,1.f)  右上(1.f,1.f)
左下(-1.f,-1.f) 右下(1.f,-1.f)

SV_POSION (D3DFVF_XYZRHW)

已经进行过坐标转换了。坐标和屏幕坐标一样,X轴向右递增,Y轴向下递增
左上(0,0)           右上((int)width,0)
左下(0,(int)height) 右下((int)width,(int)height)

红龙书中使用D3DFVF_XYZ显示一张图片的代码如下

IDirect3DDevice9* pDevice9 = NULL;IDirect3DVertexBuffer9* pVertexBuffer9 = NULL;IDirect3DTexture9* pTexture9  = NULL; struct Vertex{ Vertex(float x, float y, float z,   float u, float v){_x  = x;  _y  = y;  _z  = z;_u  = u;  _v  = v;}    float _x, _y, _z;    float _u, _v; static const DWORD FVF;}; const DWORD Vertex::FVF = D3DFVF_XYZ | D3DFVF_TEX1; bool InitDevice(HWND hwnd, D3DDEVTYPE deviceType = D3DDEVTYPE_HAL){ RECT rc;::GetClientRect(hwnd, &rc);// Step 1: Create the IDirect3D9 object. IDirect3D9* d3d9 = Direct3DCreate9(D3D_SDK_VERSION); if (!d3d9) return false; // Step 2: Check for hardware vp.int vp = 0;D3DCAPS9 caps;d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, deviceType, &caps); if (caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;elsevp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;// Step 3: Fill out the D3DPRESENT_PARAMETERS structure.D3DPRESENT_PARAMETERS d3dpp = {};d3dpp.BackBufferWidth = rc.right - rc.left;d3dpp.BackBufferHeight = rc.bottom - rc.top;d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;d3dpp.BackBufferCount = 1;d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;d3dpp.MultiSampleQuality = 0;d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;d3dpp.hDeviceWindow = hwnd;d3dpp.Windowed = TRUE;d3dpp.EnableAutoDepthStencil = true;d3dpp.AutoDepthStencilFormat = D3DFMT_D16;d3dpp.Flags = 0;d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;// Step 4: Create the device. HRESULT hr = d3d9->CreateDevice(D3DADAPTER_DEFAULT, // primary adapterdeviceType,         // device typehwnd,               // window associated with devicevp,                 // vertex processing&d3dpp,             // present parameters&pDevice9);            // return created deviced3d9->Release(); if (FAILED(hr))return false;// call functions below if we want to display texturepDevice9->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);pDevice9->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);pDevice9->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);// Don't use lighting for this sample. pDevice9->SetRenderState(D3DRS_LIGHTING, FALSE);// get textureD3DXCreateTextureFromFile(pDevice9, "dx5_logo.bmp", &pTexture9);hr = pDevice9->CreateVertexBuffer(6 * sizeof(Vertex),D3DUSAGE_WRITEONLY,Vertex::FVF,D3DPOOL_MANAGED,&pVertexBuffer9,0);if (FAILED(hr))return false;InitVertexBuffer();return true;}void InitVertexBuffer(){Vertex* vertexs = NULL;if (FAILED(pVertexBuffer9->Lock(0, 0, (void**)&vertexs, 0)))return;vertexs[0] = Vertex(-1.0f, -1.0f, 0.f, 0.0f, 1.0f);vertexs[1] = Vertex(-1.0f, 1.0f, 0.f, 0.0f, 0.0f);vertexs[2] = Vertex(1.0f, 1.0f, 0.f, 1.0f, 0.0f);vertexs[3] = Vertex(-1.0f, -1.0f, 0.f, 0.0f, 1.0f);vertexs[4] = Vertex(1.0f, 1.0f, 0.f, 1.0f, 0.0f);vertexs[5] = Vertex(1.0f, -1.0f, 0.f, 1.0f, 1.0f);pVertexBuffer9->Unlock(); }bool Display(){if (!pDevice9)return false;pDevice9->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);pDevice9->BeginScene();pDevice9->SetFVF(Vertex::FVF);pDevice9->SetStreamSource(0, pVertexBuffer9, 0, sizeof(Vertex));pDevice9->SetTexture(0, pTexture9);pDevice9->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);pDevice9->EndScene();pDevice9->Present(0, 0, 0, 0);return true;}

使用D3DFVF_XYZRHW,则有两处不同:
1、顶点格式及顶点结构体的定义不同;
2、顶点赋值逻辑不同。

顶点格式定义如下:

struct Vertex{Vertex(float x, float y, float z,   float rhw,   float u, float v){_x  = x;  _y  = y;  _z  = z;_rhw = rhw;_u  = u;  _v  = v;}float _x, _y, _z;float _rhw;        // eye distance          float _u, _v; // texture coordinatesstatic const DWORD FVF;};const DWORD Vertex::FVF = D3DFVF_XYZRHW | D3DFVF_TEX1;
顶点赋值如下(备注:width和height代表纹理的宽高):
void InitVertexBuffer(){Vertex* vertexs = NULL;if (FAILED(pVertexBuffer9->Lock(0, 0, (void**)&vertexs, 0)))return;vertexs[0] = Vertex(0.0f, Height, 0.f, 1.f, 0.0f, 1.0f);vertexs[1] = Vertex(0.0f, 0, 0.f, 1.f, 0.0f, 0.0f);vertexs[2] = Vertex(Width, 0, 0.f, 1.f, 1.0f, 0.0f);vertexs[3] = Vertex(0.0f, Height, 0.f, 1.f, 0.0f, 1.0f);vertexs[4] = Vertex(Width, 0, 0.f, 1.f, 1.0f, 0.0f);vertexs[5] = Vertex(Width, Height, 0.f, 1.f, 1.0f, 1.0f);pVertexBuffer9->Unlock();}


0 0
原创粉丝点击