Техник Дата: Суббота, 02 Апреля 2011, 11:19 | Сообщение # 1
Глава X-ray Games
Сейчас нет на сайте
Здравствуйте, недавно сел за книгу и решил учить Directx 9. При работе с индексным буфером возникла проблема: вывожу на экран пирамиду, а у нее отображается основание и две передние грани, вроде все индексы пронумеровал правильно. Мне кажется, что проблема в отсечении невидимых поверхностей или что-то еще?Объясните пожалуйста. Ниже привожу исходники.
Code
#include <windows.h> #include <d3dx9.h> #include <d3d9.h> #define WINDOW_WIDTH 640 #define WINDOW_HEIGHT 480 HWND hWnd = NULL; IDirect3D9* g_pD3D = NULL; IDirect3DDevice9* g_pd3dDevice = NULL; IDirect3DVertexBuffer9* g_pVB = NULL; IDirect3DIndexBuffer9* g_pIndexBuffer = NULL; IDirect3DTexture9* g_pTexture = NULL; LRESULT WINAPI WndProc(HWND, UINT, WPARAM, LPARAM); HRESULT InitD3D9(); void Render(); void Cleanup(); HRESULT InitGeometry(); void SetMatrices(); void SetTexture(); struct CUSTOMVERTEX { FLOAT x,y,z; float tu,tv; }; #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_TEX1) int WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, int ) { WNDCLASSEX wc = {sizeof(WNDCLASSEX),CS_CLASSDC, WndProc, 0L,0L, GetModuleHandle(NULL),NULL,NULL,NULL,NULL, TEXT("WindowClass"),NULL}; RegisterClassEx(&wc); hWnd = CreateWindow(TEXT("WindowClass"),TEXT("Test"), WS_OVERLAPPEDWINDOW,100,100,640,480, GetDesktopWindow(),NULL,wc.hInstance,NULL); if( SUCCEEDED( InitD3D9( ) ) ) { if( SUCCEEDED( InitGeometry() ) ) { ShowWindow( hWnd, SW_SHOWDEFAULT ); UpdateWindow( hWnd ); MSG msg; ZeroMemory( &msg, sizeof(msg) ); while( msg.message!=WM_QUIT ) { if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } else Render(); } } } UnregisterClass( L"D3D Tutorial", wc.hInstance ); } HRESULT InitD3D9() { if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) return E_FAIL; D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp,sizeof(d3dpp)); d3dpp.Windowed = true; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; 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_AMBIENT, 0xffffffff ); g_pd3dDevice->SetRenderState( D3DRS_SHADEMODE, D3DSHADE_GOURAUD ); g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE ); return S_OK; } HRESULT InitGeometry() { CUSTOMVERTEX vertices[] = { { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}, { 0.0f, 0.0f, 0.5f, 1.0f, 1.0f}, { 0.5f, 0.0f, 0.5f, 0.0f, 1.0f}, { 0.5f, 0.0f, 0.0f, 1.0f, 0.0f}, { 0.2f, 0.5f, 0.2f, 1.0f, 0.0f}, }; g_pd3dDevice->CreateVertexBuffer(5*sizeof(CUSTOMVERTEX),0,D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT,&g_pVB,NULL); VOID* pVertices = NULL; if(FAILED(g_pVB->Lock(0,sizeof(vertices),(void**)&pVertices,0))) return E_FAIL; memcpy(pVertices,vertices,sizeof(vertices)); g_pVB->Unlock(); g_pd3dDevice->CreateIndexBuffer(sizeof(short)*18,0,D3DFMT_INDEX16,D3DPOOL_DEFAULT,&g_pIndexBuffer,NULL); short Indices[18] = { 0,1,2, 0,2,3, 3,4,2, 2,4,1, 1,4,0, 0,4,3, }; VOID* pIndexData = NULL; if(FAILED(g_pIndexBuffer->Lock(0,0,&pIndexData,0))) return E_FAIL; memcpy(pIndexData, &Indices, sizeof(Indices)); g_pIndexBuffer->Unlock(); return S_OK; } void SetMatrices() { D3DXMATRIX g_Transform; D3DXMatrixIdentity(&g_Transform); D3DXMatrixTranslation(&g_Transform,0.0f,0.0f,0.0f); g_pd3dDevice->SetTransform(D3DTS_WORLD,&g_Transform); D3DXMATRIX g_View; D3DXVECTOR3 Eye(-1.0f, 1.0f,-1.0f ); D3DXVECTOR3 At (0.0f, 0.0f, 0.0f); D3DXVECTOR3 Up (0.0f, 1.0f, 0.0f); D3DXMatrixIdentity(&g_View); D3DXMatrixLookAtLH(&g_View,&Eye,&At,&Up); g_pd3dDevice->SetTransform(D3DTS_VIEW,&g_View); D3DXMATRIX g_Projection; D3DXMatrixIdentity(&g_Projection); D3DXMatrixPerspectiveFovLH(&g_Projection,D3DX_PI/4,1.0f,1.0f,100.0f); g_pd3dDevice->SetTransform(D3DTS_PROJECTION,&g_Projection); } void SetTexture() { D3DXCreateTextureFromFile(g_pd3dDevice,L"1.jpg",&g_pTexture); g_pd3dDevice->SetTexture(0,g_pTexture); } LRESULT WINAPI WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) { switch( msg ) { case WM_DESTROY: Cleanup(); PostQuitMessage( 0 ); return 0; } return DefWindowProc( hWnd, msg, wParam, lParam ); } void Render() { g_pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,255), 1.0f,0); if(SUCCEEDED(g_pd3dDevice->BeginScene())) { SetMatrices(); SetTexture(); g_pd3dDevice->SetStreamSource(0,g_pVB,0,sizeof(CUSTOMVERTEX)); g_pd3dDevice->SetIndices(g_pIndexBuffer); g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX); g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,18,0,5); g_pd3dDevice->EndScene(); } g_pd3dDevice->Present(NULL,NULL,NULL,NULL); } void Cleanup() { if(g_pD3D) g_pD3D->Release(); if(g_pd3dDevice) g_pd3dDevice->Release(); if(g_pVB) g_pVB->Release(); if(g_pIndexBuffer) g_pIndexBuffer->Release(); }