-
创建窗口
LPDIRECT3D9 g_D3d = nullptr;//D3d对象 LPDIRECT3DDEVICE9 g_D3DDevice = nullptr;//D3d设备
代码:
1. 创建D3d对象 g_D3d = Direct3DCreate9(D3D_SDK_VERSION); if (g_D3d == nullptr) { return false;//D3d对象创建失败 } 2. 创建设备对象 D3DDISPLAYMODE displayMode;//显示模式 if (FAILED(g_D3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode))) {//获取主显卡的显示模式 return false; } D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp, sizeof(d3dpp)); d3dpp.Windowed = true;//是否窗口化 d3dpp.BackBufferFormat = displayMode.Format;//显示模式(不同机器不一样) d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;//最快的交换方法 if (FAILED(g_D3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,//使用硬件渲染 hwnd, D3DCREATE_HARDWARE_VERTEXPROCESSING,//使用硬件做图形处理顶点 &d3dpp, &g_D3DDevice))) { return false; } 3. 准备渲染 g_D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET,//对目标窗口 D3DCOLOR_XRGB(0, 0, 0),//r,g,b 1.0f,//深度缓存 0);//模板缓存 g_D3DDevice->BeginScene();//开始场景(开始渲染) //输出显示3D图形 g_D3DDevice->EndScene();//结束渲染 g_D3DDevice->Present(NULL, NULL, NULL, NULL);//把后台缓存显示 4. 释放对象 if (g_D3DDevice != nullptr) { g_D3DDevice->Release(); } if (g_D3d != nullptr) { g_D3d->Release(); }
-
绘制基本图元
1. 定义顶点缓存 LPDIRECT3DVERTEXBUFFER9 g_vertexBuffer = nullptr; 2. 定义顶点结构体 struct stVertex { FLOAT x, y, z, rhw; DWORD color; }; #define d3dfvf_user (D3DFVF_XYZRHW|D3DFVF_DIFFUSE) 3. 初始化顶点结构体 unsigned long col = D3DCOLOR_XRGB(255, 255, 255); stVertex obj[] = { {420,150,0.5,1,col}, {420,350,0.5,1,col}, {220,150,0.5,1,col}, {220,350,0.5,1,col}, }; if (FAILED(g_D3DDevice->CreateVertexBuffer(4 * sizeof(stVertex), 0, d3dfvf_user, D3DPOOL_DEFAULT, &g_vertexBuffer, nullptr))) { return FALSE; } void* ptr; if (FAILED(g_vertexBuffer->Lock(0, sizeof(obj), (void**)&ptr, 0))) { return FALSE; } memcpy(ptr, obj, sizeof(obj)); if (FAILED(g_vertexBuffer->Unlock())) { return FALSE; } 4. 绘制图形 g_D3DDevice->BeginScene();//开始场景(开始渲染) //输出显示3D图形 g_D3DDevice->SetStreamSource(0, g_vertexBuffer, 0, sizeof(stVertex)); g_D3DDevice->SetFVF(d3dfvf_user); g_D3DDevice->DrawPrimitive(D3DPT_LINELIST, 0, 2); g_D3DDevice->EndScene();//结束渲染
Direct3D开发笔记
最新推荐文章于 2024-09-12 15:34:48 发布