博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
D3D 点列练习
阅读量:5955 次
发布时间:2019-06-19

本文共 3724 字,大约阅读时间需要 12 分钟。

画四个点在窗体。

#pragma once#pragma comment(lib,"d3d9.lib")#pragma comment(lib,"d3dx9.lib")#include
#include
//TODO: -1 custom vertexstruct CUSTOMVERTEX{ float x; float y; float z; float rhw;};#define D3DFVF_CUSTOMVERTEX D3DFVF_XYZRHWHRESULT InitD3D(HWND hWnd);void CleanUp();void Render();LRESULT CALLBACK MsgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam);HRESULT InitD3DVertexBuffer();//TODO: 0 declare d3d variables.LPDIRECT3D9 g_pd3d = NULL;LPDIRECT3DDEVICE9 g_pd3dDevice9 = NULL;LPDIRECT3DVERTEXBUFFER9 g_pd3dVB = NULL;//application entry point.INT WINAPI wWinMain( HINSTANCE, HINSTANCE, LPWSTR, INT){ //initialize wnd class. WNDCLASSEX wcex; ZeroMemory(&wcex,sizeof(wcex)); wcex.cbSize=sizeof(wcex); wcex.hInstance=GetModuleHandle(NULL); wcex.lpfnWndProc=MsgProc; wcex.lpszClassName=L"Self001"; wcex.style=CS_CLASSDC; //register wnd class. RegisterClassEx(&wcex); //create window. HWND hWnd=CreateWindowEx( WS_EX_OVERLAPPEDWINDOW, L"Self001", L"Self001 Window", WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, NULL, NULL, wcex.hInstance, NULL); //init d3d if(SUCCEEDED(InitD3D(hWnd))) { //show window. ShowWindow(hWnd,SW_SHOWDEFAULT); UpdateWindow(hWnd); //message loop. MSG msg; ZeroMemory(&msg,sizeof(msg)); while(msg.message != WM_QUIT) { if(PeekMessage(&msg,hWnd,0,0,PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } else { Render(); } } } //unregister wnd class. UnregisterClass(L"Self001",wcex.hInstance); return 0;}//init d3d.HRESULT InitD3D(HWND hWnd){ //create d3d. g_pd3d=Direct3DCreate9(D3D_SDK_VERSION); if(g_pd3d == NULL) { return E_FAIL; } //initialize d3d present parameters. D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp,sizeof(d3dpp)); d3dpp.Windowed=TRUE; d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat=D3DFMT_UNKNOWN; //create d3d device. if(FAILED(g_pd3d->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice9))) { return E_FAIL; } //init vertex buffer. if(FAILED(InitD3DVertexBuffer())) { return E_FAIL; } return S_OK;}//clean up d3d.void CleanUp(){ //TODO: 3 clean up d3d. if(g_pd3dVB != NULL) { g_pd3dVB->Release(); } //release device. if(g_pd3dDevice9 != NULL) { g_pd3dDevice9->Release(); } //release d3d. if(g_pd3d != NULL) { g_pd3d->Release(); }}//render the scene.void Render(){ //clear target. g_pd3dDevice9->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0); //draw primitive. if(SUCCEEDED(g_pd3dDevice9->BeginScene())) { //TODO: 2 render d3d. g_pd3dDevice9->SetStreamSource(0,g_pd3dVB,0,sizeof(CUSTOMVERTEX)); g_pd3dDevice9->SetFVF(D3DFVF_CUSTOMVERTEX); g_pd3dDevice9->DrawPrimitive(D3DPT_POINTLIST,0,4); g_pd3dDevice9->EndScene(); } //present sence. g_pd3dDevice9->Present(NULL,NULL,NULL,NULL);}//window message handler.LRESULT CALLBACK MsgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam){ switch(msg) { case WM_DESTROY: CleanUp(); return 0L; } return DefWindowProc(hWnd,msg,wParam,lParam);}//init d3d vertex buffer.HRESULT InitD3DVertexBuffer(){ //TODO: 1 init vertex buffer data. //create vertex data. CUSTOMVERTEX vertices[4]= { {50.0f,50.0f,1.0f,1.0f}, {250.0f,50.0f,1.0f,1.0f}, {250.0f,250.0f,1.0f,1.0f}, {50.0f,250.0f,1.0f,1.0f} }; //create vertex buffer. if(FAILED(g_pd3dDevice9->CreateVertexBuffer( sizeof(vertices), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pd3dVB, NULL))) { return E_FAIL; } //fill vertex buffer. void* pVertices=NULL; if(FAILED(g_pd3dVB->Lock(0,sizeof(vertices),&pVertices,0))) { return E_FAIL; } memcpy(pVertices,vertices,sizeof(vertices)); g_pd3dVB->Unlock(); return S_OK;}

1. 编程的路上练习还是偏多的,这里练习D3D 点列,六中图元中的第一个。

2.我知道不是非常明显,只是四点像素点又能怎么明显呢。

你可能感兴趣的文章
hdu6086(AC 自动机)
查看>>
使用xshell远程连接Linux
查看>>
转:理解TCP序列号(Sequence Number)和确认号(Acknowledgment Number)
查看>>
杭电ACM1007
查看>>
faster-RCNN台标检测
查看>>
Unix环境高级编程 centos中配置apue编译环境
查看>>
运算符
查看>>
数据结构之各排序算法
查看>>
网页分帧操作<frameset>,<iframe>标签
查看>>
Vue生产环境部署
查看>>
php访问数据库
查看>>
一段用于地址清洗的代码
查看>>
FreeSWITCH快速录音
查看>>
前端测试框架(学习之路)前言
查看>>
POJ 2689 Prime Distance【大区间素数筛选】【埃氏筛法】【经典题】
查看>>
App专项测试之弱网测试
查看>>
Ext.onReady(function(){} )函数的作用域分析(1)
查看>>
[xsy1144]选物品
查看>>
酒店之王
查看>>
html5判断用户摇晃了手机(转)
查看>>