D3D球体算法的两种索引实现

#include "d3d9.h" #include "d3dx9.h" #define USE_INDEX_STRIP LPDIRECT3D9 g_pD3D=NULL; LPDIRECT3DDEVICE9 g_pd3dDevice=NULL; LPDIRECT3DVERTEXBUFFER9 g_pVB=NULL; LPDIRECT3DINDEXBUFFER9 g_pIB=NULL; int g_rings=10;//y方向得环数 int g_segments=10;//xz平面的环数 struct CUSTOMVERTEX { float x,y,z; D3DCOLOR color; float nx,ny,nz; }; #ifndef USE_INDEX_STRIP struct MYINDEX { WORD _0,_1,_2; }; #endif #define D3DFVF_CUSTOM (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_NORMAL) HRESULT InitD3D(HWND hwnd) { if (NULL==(g_pD3D=Direct3DCreate9(D3D_SDK_VERSION))) { return E_FAIL; } D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp,sizeof(d3dpp)); d3dpp.BackBufferFormat=D3DFMT_UNKNOWN; d3dpp.Windowed=TRUE; d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD; d3dpp.EnableAutoDepthStencil=TRUE; d3dpp.AutoDepthStencilFormat=D3DFMT_D16; if (FAILED(g_pD3D->CreateDevice( D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL ,hwnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&g_pd3dDevice))) { return E_FAIL; } g_pd3dDevice->SetRenderState(D3DRS_ZENABLE,TRUE); g_pd3dDevice->SetRenderState(D3DRS_LIGHTING,FALSE); g_pd3dDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE); // g_pd3dDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME ); return S_OK; } HRESULT InitGeometry() { //使用D3DPT_TRIANGLESTRIP来索引描绘球体,比较简单,索引建立的空间也较小 #ifdef USE_INDEX_STRIP if (FAILED(g_pd3dDevice->CreateVertexBuffer((g_rings+1)*(g_segments+1)*sizeof(CUSTOMVERTEX),0,D3DFVF_CUSTOM,D3DPOOL_MANAGED,&g_pVB,NULL))) return E_FAIL; if (FAILED(g_pd3dDevice->CreateIndexBuffer(g_rings*(g_segments+1)*2*sizeof(WORD),0,D3DFMT_INDEX16 ,D3DPOOL_MANAGED,&g_pIB,NULL))) { return E_FAIL; } CUSTOMVERTEX* vertics; if (FAILED(g_pVB->Lock(0,0,(VOID**)&vertics,0))) return E_FAIL; WORD* indices=0; WORD vindex=0; if (FAILED(g_pIB->Lock(0,0,(VOID**)&indices,0))) { return E_FAIL; } float deltaRing=D3DX_PI/(g_rings); float deltaSegment=2.0f*D3DX_PI/g_segments; //经典的球体建立算法 for(int j=0;j<g_rings+1;j++) { float radius=sinf(j*deltaRing); float y0=cosf(j*deltaRing); for (int i=0;i<g_segments+1;i++) { float x0=radius*sinf(i*deltaSegment); float z0=radius*cosf(i*deltaSegment); vertics->x=x0; vertics->y=y0; vertics->z=z0; vertics->nx=x0; vertics->ny=y0; vertics->nz=z0; vertics->color=0xff4040; vertics++; if (j!=g_rings)//除了第一点和最后一点只有一次,其他都由点都有两次索引 { *indices=vindex; indices++; *indices=vindex+(WORD)(g_segments+1); indices++; vindex++; } } } g_pVB->Unlock(); g_pIB->Unlock(); //使用D3DPT_TRIANGLELIST来索引描绘球体 #else MYINDEX* indices; if (FAILED(g_pd3dDevice->CreateVertexBuffer((g_rings+1)*(g_segments+1)*sizeof(CUSTOMVERTEX),0,D3DFVF_CUSTOM,D3DPOOL_MANAGED,&g_pVB,NULL))) return E_FAIL; if (FAILED(g_pd3dDevice->CreateIndexBuffer((g_rings+1)*(g_segments+1)*2*sizeof(MYINDEX),0,D3DFMT_INDEX16 ,D3DPOOL_MANAGED,&g_pIB,NULL))) { return E_FAIL; } CUSTOMVERTEX* vertics; if (FAILED(g_pVB->Lock(0,0,(VOID**)&vertics,0))) return E_FAIL; float deltaRing=D3DX_PI/(g_rings); float deltaSegment=2.0*D3DX_PI/g_segments; for(int j=0;j<g_rings+1;j++) { float radius=sinf(j*deltaRing); float y0=cosf(j*deltaRing); for (int i=0;i<g_segments+1;i++) { float x0=radius*sinf(i*deltaSegment); float z0=radius*cosf(i*deltaSegment); vertics->x=x0; vertics->y=y0; vertics->z=z0; vertics->nx=x0; vertics->ny=y0; vertics->nz=z0; vertics->color=0xff4040; vertics++; } } if (FAILED(g_pIB->Lock(0,0,(VOID**)&indices,0))) { return E_FAIL; } //建立的索引把他想象成一个由两个三角形组成得四边形 for (int j=0;j<g_rings;j++) { for (int i=0;i<(g_segments+1);i++) { indices->_0=i+j*(g_segments+1);//左上 indices->_1=(i+1)%(g_segments+1)+j*(g_segments+1);//右上 indices->_2=(i+1)%(g_segments+1)+(j+1)*(g_segments+1);//右下 indices++; indices->_0=i+j*(g_segments+1);//左上 indices->_1=(i+1)%(g_segments+1)+(j+1)*(g_segments+1);//右下 indices->_2=i+(j+1)*(g_segments+1);//左下 indices++; } } g_pVB->Unlock(); g_pIB->Unlock(); #endif return S_OK; } void SetupMatrixs() { D3DXMATRIXA16 matWorld; D3DXMatrixIdentity(&matWorld); D3DXMatrixRotationY(&matWorld,timeGetTime()/1000.0f); g_pd3dDevice->SetTransform(D3DTS_WORLD,&matWorld); D3DXVECTOR3 eye(0.0f,3.0f,-5.0f); D3DXVECTOR3 lookAt(0.0f,0.0f,0.0f); D3DXVECTOR3 up(0,1,0); D3DXMATRIXA16 matView; D3DXMatrixLookAtLH(&matView,&eye,&lookAt,&up); g_pd3dDevice->SetTransform(D3DTS_VIEW,&matView); D3DXMATRIXA16 matProj; D3DXMatrixPerspectiveFovLH(&matProj,D3DX_PI/4,1.0f,1.0f,100.0f); g_pd3dDevice->SetTransform(D3DTS_PROJECTION,&matProj); } void Render() { g_pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,255),1.0f,0); if (FAILED(g_pd3dDevice->BeginScene())) { SetupMatrixs(); g_pd3dDevice->SetStreamSource(0,g_pVB,0,sizeof(CUSTOMVERTEX)); g_pd3dDevice->SetFVF(D3DFVF_CUSTOM); g_pd3dDevice->SetIndices(g_pIB); #ifdef USE_INDEX_STRIP g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLESTRIP,0,0,(g_segments+1)*(1+g_rings),0,2*(g_segments+1)*g_rings-2*g_rings); #else g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,(g_segments+1)*(g_rings+1),0,(g_segments+1)*(g_rings+1)*2); #endif g_pd3dDevice->EndScene(); } g_pd3dDevice->Present(NULL,NULL,NULL,NULL); } void CleanUp() { if(g_pIB) g_pIB->Release(); if(g_pVB) g_pVB->Release(); if(g_pd3dDevice) g_pd3dDevice->Release(); if(g_pD3D) g_pD3D->Release(); } LRESULT CALLBACK MsgProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam) { switch (uMsg) { case WM_DESTROY: CleanUp(); PostQuitMessage(0); return 0; } return DefWindowProc(hwnd,uMsg,wParam,lParam); } int WinMain(__in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in_opt LPSTR lpCmdLine, __in int nShowCmd ) { WNDCLASSEX wc={sizeof(WNDCLASSEX),CS_CLASSDC,MsgProc,0,0,GetModuleHandle(NULL),NULL,NULL,NULL,NULL,"D3D",NULL}; RegisterClassEx(&wc); HWND hwnd=CreateWindow("D3D","sphere turtorial",WS_OVERLAPPEDWINDOW,100,100,400,400,GetDesktopWindow(),NULL,wc.hInstance,NULL); if (SUCCEEDED(InitD3D(hwnd))) { if (SUCCEEDED(InitGeometry())) { ShowWindow(hwnd,SW_SHOWDEFAULT); UpdateWindow(hwnd); MSG msg; ZeroMemory(&msg,sizeof(msg)); while (msg.message!=WM_QUIT) { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } else Render(); } } } UnregisterClass("D3D",wc.hInstance); return 0; }

 

 

这里使用了两种索引的方法去索引描绘球体,代码不算高深,放在这里只是为了备忘而已,也方便各位学习,大家共同进步。呵呵!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(算法,null,Parameters,callback,include,float)