标签:nts data- mem diff geometry size target dap triangle
CD3DDlg m_D3DDlg;
m_D3DDlg.CreateEx( 0, AfxRegisterWndClass(NULL,NULL,NULL,NULL), L"D3D Window", WS_POPUP|WS_CAPTION|WS_VISIBLE, CRect(100,100,500,500), this, 0);6. D3DMFCAppDlg 结束button生成函数加入
m_D3DDlg.DestroyWindow();7. CD3DDlg 头文件引入文件和定义顶点结构
#pragma comment(lib,"d3d9.lib") #pragma comment(lib,"d3dx9.lib") #include <d3d9.h> #include <d3dx9math.h> typedef struct _CUSTOMVERTEX1 { D3DXVECTOR3 position; D3DXVECTOR3 normal; float fu; float fv; }CUSTOMVERTEX1; #define D3DFVF_CUSTOMVERTEX1 (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1) typedef struct _CUSTOMVERTEX2 { D3DXVECTOR3 position; D3DXVECTOR3 normal; }CUSTOMVERTEX2; #define D3DFVF_CUSTOMVERTEX2 (D3DFVF_XYZ|D3DFVF_NORMAL)这里也许涉及到安装DirectX 只是网上非常多样例了,我仅仅关注实例练习,仅仅是说假设安装了D3D SDK。那么路径配置正确这样写不会有错误。
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); afx_msg void OnDestroy(); afx_msg void OnTimer(UINT_PTR nIDEvent);9. 类向导加入私有函数
void InitD3D9(void); void Render(void); void CleanUp(void); void InitGeometry(void); void SetupMatrices(void); void SetLight(void); void SetMaterial1(void); void SetMaterial2(void);10 .类向导加入私有变量
LPDIRECT3D9 m_pD3D9; LPDIRECT3DDEVICE9 m_pD3DDevice9; LPDIRECT3DVERTEXBUFFER9 m_pVB1; LPDIRECT3DVERTEXBUFFER9 m_pVB2; LPDIRECT3DTEXTURE9 m_pD3DTexture9; int m_nRotateY;
11. CD3DDlg实现文件 初始化 D3D
<pre name="code" class="cpp">void CD3DDlg::InitD3D9(void) { m_pD3D9=Direct3DCreate9(D3D_SDK_VERSION); D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp,sizeof(D3DPRESENT_PARAMETERS)); d3dpp.BackBufferFormat=D3DFMT_UNKNOWN; d3dpp.BackBufferCount=1; d3dpp.EnableAutoDepthStencil=TRUE; d3dpp.AutoDepthStencilFormat=D3DFMT_D16; d3dpp.Windowed=TRUE; d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD; m_pD3D9->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &m_pD3DDevice9); m_pD3DDevice9->SetRenderState(D3DRS_ZENABLE,D3DZB_TRUE); m_pD3DDevice9->SetRenderState(D3DRS_LIGHTING,TRUE); m_pD3DDevice9->SetRenderState(D3DRS_NORMALIZENORMALS,TRUE); }12. 初始化图元
void CD3DDlg::InitGeometry(void) { D3DXCreateTextureFromFile(m_pD3DDevice9,_T("texture.jpg"),&m_pD3DTexture9); CUSTOMVERTEX1 vertices1[32]; vertices1[0].position=D3DXVECTOR3(-1.0f,0.0f,0.0f); vertices1[0].normal=D3DXVECTOR3(-1.0f,0.0f,0.0f); vertices1[0].fu=0.0f; vertices1[0].fv=0.5f; for (int i=1;i<32;i++) { float theat=(i-1)*2*D3DX_PI/30.0f; vertices1[i].position=D3DXVECTOR3(1.0f,sinf(theat),cosf(theat)); vertices1[i].normal=D3DXVECTOR3(0.0f,sinf(theat),cosf(theat)); float m=sqrtf(5.0f); float beta=(0.5f-1.0f/m+(i-1.0f)/15.0f/m)*D3DX_PI; vertices1[i].fu=0.5*sinf(beta); vertices1[i].fv=0.5-0.5*cosf(beta); } m_pD3DDevice9->CreateVertexBuffer( sizeof(vertices1), 0, D3DFVF_CUSTOMVERTEX1, D3DPOOL_DEFAULT, &m_pVB1, NULL); VOID* pVertices=NULL; m_pVB1->Lock( 0, sizeof(vertices1), &pVertices, 0); memcpy(pVertices,vertices1,sizeof(vertices1)); m_pVB1->Unlock(); CUSTOMVERTEX2 vertices2[32]; vertices2[0].position=D3DXVECTOR3(1.0f,0.0f,0.0f); vertices2[0].normal=D3DXVECTOR3(1.0f,0.0f,0.0f); for (int i=1;i<32;i++) { float theat=(i-1)*2*D3DX_PI/30.0f; vertices2[i].position=vertices1[32-i].position; vertices2[i].normal=D3DXVECTOR3(1.0f,0.0f,0.0f); } m_pD3DDevice9->CreateVertexBuffer( sizeof(vertices2), 0, D3DFVF_CUSTOMVERTEX2, D3DPOOL_DEFAULT, &m_pVB2, NULL); m_pVB2->Lock( 0, sizeof(vertices2), &pVertices, 0); memcpy(pVertices,vertices2,sizeof(vertices2)); m_pVB2->Unlock(); }13. 设置左边变换
void CD3DDlg::SetupMatrices(void) { D3DXMATRIX matWorld; float angle=m_nRotateY*D3DX_PI/180; D3DXMatrixRotationY(&matWorld,angle ); m_pD3DDevice9->SetTransform(D3DTS_WORLD,&matWorld); D3DXVECTOR3 eye(0.0f,3.0f,-5.0f); D3DXVECTOR3 lookAt(0.0f,0.0f,0.0f); D3DXVECTOR3 up(0.0f,1.0f,0.0f); D3DXMATRIX matView; D3DXMatrixLookAtLH(&matView,&eye,&lookAt,&up); m_pD3DDevice9->SetTransform(D3DTS_VIEW,&matView); D3DXMATRIXA16 matProj; D3DXMatrixPerspectiveFovLH(&matProj,D3DX_PI/4,1.0f,1.0f,100.0f); m_pD3DDevice9->SetTransform(D3DTS_PROJECTION,&matProj); }14. 设置材料1
void CD3DDlg::SetMaterial1(void) { D3DMATERIAL9 mtrl; ZeroMemory(&mtrl,sizeof(D3DMATERIAL9)); mtrl.Diffuse.r=mtrl.Ambient.r=1.0f; mtrl.Diffuse.g=mtrl.Ambient.g=1.0f; mtrl.Diffuse.b=mtrl.Ambient.b=1.0f; mtrl.Diffuse.a=mtrl.Ambient.a=1.0f; m_pD3DDevice9->SetMaterial(&mtrl); }15. 设置材料2
void CD3DDlg::SetMaterial2(void) { D3DMATERIAL9 mtrl; ZeroMemory(&mtrl,sizeof(D3DMATERIAL9)); mtrl.Diffuse.r=mtrl.Ambient.r=1.0f; mtrl.Diffuse.g=mtrl.Ambient.g=1.0f; mtrl.Diffuse.b=mtrl.Ambient.b=0.0f; mtrl.Diffuse.a=mtrl.Ambient.a=1.0f; m_pD3DDevice9->SetMaterial(&mtrl); }16.设置关照
void CD3DDlg::SetLight(void) { D3DLIGHT9 light; ZeroMemory(&light,sizeof(light)); light.Type=D3DLIGHT_DIRECTIONAL; light.Diffuse.r=1.0f; light.Diffuse.g=1.0f; light.Diffuse.b=1.0f; light.Direction=D3DXVECTOR3(0.0f,0.0,1.0f); light.Range=1000.0f; m_pD3DDevice9->SetLight(0,&light); m_pD3DDevice9->LightEnable(0,TRUE); m_pD3DDevice9->SetRenderState(D3DRS_AMBIENT,D3DCOLOR_RGBA(32,32,32,0)); }17. 渲染 运行
void CD3DDlg::Render(void) { m_pD3DDevice9->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,255), 1.0f, 0); m_pD3DDevice9->BeginScene(); SetupMatrices(); SetLight(); SetMaterial1(); m_pD3DDevice9->SetTexture(0,m_pD3DTexture9); m_pD3DDevice9->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_MODULATE); m_pD3DDevice9->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE); m_pD3DDevice9->SetTextureStageState(0,D3DTSS_COLORARG2,D3DTA_DIFFUSE); m_pD3DDevice9->SetFVF(D3DFVF_CUSTOMVERTEX1); m_pD3DDevice9->SetStreamSource(0,m_pVB1,0,sizeof(CUSTOMVERTEX1)); m_pD3DDevice9->DrawPrimitive(D3DPT_TRIANGLEFAN,0,30); m_pD3DDevice9->SetTexture(0,NULL); m_pD3DDevice9->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_DISABLE); SetMaterial2(); m_pD3DDevice9->SetFVF(D3DFVF_CUSTOMVERTEX2); m_pD3DDevice9->SetStreamSource(0,m_pVB2,0,sizeof(CUSTOMVERTEX2)); m_pD3DDevice9->DrawPrimitive(D3DPT_TRIANGLEFAN,0,30); m_pD3DDevice9->EndScene(); m_pD3DDevice9->Present(NULL,NULL,NULL,NULL); }18. 释放资源
void CD3DDlg::CleanUp(void) { m_pVB1->Release(); m_pVB2->Release(); m_pD3DTexture9->Release(); m_pD3DDevice9->Release(); m_pD3D9->Release(); }19. 最终该公有函数了 初始化
int CD3DDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CWnd::OnCreate(lpCreateStruct) == -1) return -1; InitD3D9(); InitGeometry(); SetTimer(1,40,NULL); return 0; }20. 定时运行渲染
void CD3DDlg::OnTimer(UINT_PTR nIDEvent) { Render(); m_nRotateY+=2; CWnd::OnTimer(nIDEvent); }21. 结束
void CD3DDlg::OnDestroy() { CWnd::OnDestroy(); CleanUp(); }
谁解释也是白搭,仅仅有多练。只是推荐的就是抄程序也要抄对。
m_pD3DDevice9->CreateVertexBuffer( sizeof(vertices1), 0, D3DFVF_CUSTOMVERTEX1, D3DPOOL_DEFAULT, &m_pVB1, NULL);每个參数一行,至于什么函数分行写还是须要自己把握的。微软内部调用时第一个參数和函数同行。最后一个括号会再分一行,我认为不好就调成这种格式了。
标签:nts data- mem diff geometry size target dap triangle
原文地址:http://www.cnblogs.com/tlnshuju/p/7127039.html