Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

MadProgrammer

Zbuffer not Zbuffering in D3D 9

This topic is 5392 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

my zbuffer isn''t working.... anyone know why? code: #include <d3dx9.h> #define WIN32_LEAN_AND_MEAN #define WIN32_EXTRA_LEAN #define D3DFVF_D3DVertex (D3DFVF_XYZ | D3DFVF_DIFFUSE) #define SCREEN_WIDTH 1280 #define SCREEN_HEIGHT 1024 bool initD3D(HWND hWnd); void setRotation(); void setCamera(); void setPerspective(); void think(); void render(); void shutdownD3D(); bool loadObjects(); void unloadObjects(); LPDIRECT3D9 d3d_object = 0; LPDIRECT3DDEVICE9 d3d_device = 0; LPDIRECT3DVERTEXBUFFER9 d3d_vertexbuff = 0; struct VERTEX { float x, y, z; DWORD color; }; LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { if(msg == WM_KEYDOWN) { if(wParam == VK_ESCAPE) PostQuitMessage(0); } else if(msg == WM_CLOSE) { PostQuitMessage(0); } return DefWindowProc(hWnd, msg, wParam, lParam); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { MSG msg; HWND hWnd; WNDCLASSEX winClass; winClass.cbSize = sizeof(WNDCLASSEX); winClass.style = CS_HREDRAW | CS_VREDRAW; winClass.lpfnWndProc = WndProc; winClass.cbClsExtra = 0; winClass.cbWndExtra = 0; winClass.hInstance = hInstance; winClass.hIcon = LoadIcon(0, IDI_APPLICATION); winClass.hIconSm = LoadIcon(0, IDI_APPLICATION); winClass.hCursor = LoadCursor(0, IDC_ARROW); winClass.hbrBackground = 0; winClass.lpszMenuName = 0; winClass.lpszClassName = "RubiksCube"; if(!RegisterClassEx(&winClass)) return 0; // that''s not good hWnd = CreateWindowEx(0, "RubiksCube", "Rubiks Cube", WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_SYSMENU | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, hInstance, 0); if(!hWnd) return 0; // that''s not good either ShowWindow(hWnd, SW_SHOW); UpdateWindow(hWnd); if(!initD3D(hWnd)) return 0; // that''s bad, too if(!loadObjects()) return 0; // could not load objects while(1) { if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) { if(msg.message == WM_QUIT) break; else { TranslateMessage(&msg); DispatchMessage(&msg); } } else { think(); render(); } } unloadObjects(); shutdownD3D(); UnregisterClass("RubiksCube", hInstance); return msg.wParam; } bool initD3D(HWND hWnd) { D3DDISPLAYMODE d3d_displaymode; D3DPRESENT_PARAMETERS d3d_params; D3DCAPS9 d3d_caps; ZeroMemory(&d3d_displaymode, sizeof(d3d_displaymode)); ZeroMemory(&d3d_params, sizeof(d3d_params)); ZeroMemory(&d3d_caps, sizeof(d3d_caps)); d3d_object = Direct3DCreate9(D3D_SDK_VERSION); if(!d3d_object) { MessageBox(hWnd, "Could not initialize Direct3D 9!", "Error", MB_OK); return false; } if(FAILED(d3d_object->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3d_displaymode))) { MessageBox(hWnd, "Error setting the display mode!", "Error", MB_OK); return false; } if(FAILED(d3d_object->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &d3d_caps))) { MessageBox(hWnd, "Error retrieving device capabilities!", "Error", MB_OK); return false; } DWORD vertexProcessing = 0; if(d3d_caps.VertexProcessingCaps) vertexProcessing |= D3DCREATE_HARDWARE_VERTEXPROCESSING; else vertexProcessing |= D3DCREATE_SOFTWARE_VERTEXPROCESSING; d3d_params.Windowed = false; d3d_params.BackBufferWidth = SCREEN_WIDTH; d3d_params.BackBufferHeight = SCREEN_HEIGHT; d3d_params.SwapEffect = D3DSWAPEFFECT_DISCARD; d3d_params.BackBufferFormat = d3d_displaymode.Format; d3d_params.BackBufferCount = 1; d3d_params.EnableAutoDepthStencil = TRUE; d3d_params.AutoDepthStencilFormat = D3DFMT_D16; if(FAILED(d3d_object->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, vertexProcessing, &d3d_params, &d3d_device))) { MessageBox(hWnd, "CreateDevice() Failed!", "Error", MB_OK); return false; } if(!d3d_device) { MessageBox(hWnd, "No Direct 3D Device!", "Error", MB_OK); return false; } d3d_device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE); d3d_device->SetRenderState(D3DRS_LIGHTING, false); d3d_device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); setPerspective(); return true; } void setPerspective() { D3DXMATRIXA16 projection; D3DXMatrixPerspectiveFovLH(&projection, 1.0f, 1.0f, 0.0f, 500.0f); d3d_device->SetTransform(D3DTS_PROJECTION, &projection); } void setRotation() { static float ang = 0.0f; ang += 0.01f; ang = (float)fmod(ang, 2 * 3.14159f); D3DXMATRIXA16 worldMatrix; D3DXMatrixRotationZ(&worldMatrix, 0.0f); d3d_device->SetTransform(D3DTS_WORLD, &worldMatrix); } void setCamera() { static float ang = 0.0f; ang += 0.01f; ang = (float)fmod(ang, 2 * 3.14159f); D3DXMATRIXA16 viewMatrix; D3DXVECTOR3 eye(5.0f * (float)cos(ang), 0.0f, 5.0f * (float)sin(ang)); D3DXVECTOR3 lookAt(0.0f, 0.0f, 0.0f); D3DXVECTOR3 up(0.0f, 1.0f, 0.0f); D3DXMatrixLookAtLH(&viewMatrix, &eye, &lookAt, &up); d3d_device->SetTransform(D3DTS_VIEW, &viewMatrix); } void think() { } void render() { d3d_device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); d3d_device->BeginScene(); setRotation(); setCamera(); d3d_device->SetStreamSource(0, d3d_vertexbuff, 0, sizeof(VERTEX)); d3d_device->SetFVF(D3DFVF_D3DVertex); d3d_device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2); d3d_device->DrawPrimitive(D3DPT_TRIANGLELIST, 6, 2); d3d_device->EndScene(); d3d_device->Present(0, 0, 0, 0); } void shutdownD3D() { if(d3d_device) { d3d_device->Release(); d3d_device = 0; } if(d3d_object) { d3d_object->Release(); d3d_object = 0; } } bool loadObjects() { VERTEX cube[12] = {{-1.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255)}, // face 1 { 1.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255)}, { 1.0f, -1.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255)}, {-1.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255)}, { 1.0f, -1.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255)}, {-1.0f, -1.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255)}, {-1.0f, 1.0f, -1.0f, D3DCOLOR_XRGB(0, 255, 0)}, // face 2 { 1.0f, 1.0f, -1.0f, D3DCOLOR_XRGB(255, 255, 255)}, { 1.0f, -1.0f, -1.0f, D3DCOLOR_XRGB(0, 255, 0)}, {-1.0f, 1.0f, -1.0f, D3DCOLOR_XRGB(0, 255, 0)}, { 1.0f, -1.0f, -1.0f, D3DCOLOR_XRGB(0, 255, 0)}, {-1.0f, -1.0f, -1.0f, D3DCOLOR_XRGB(255, 255, 255)}}; if(FAILED(d3d_device->CreateVertexBuffer(sizeof(cube), 0, D3DFVF_D3DVertex, D3DPOOL_DEFAULT, &d3d_vertexbuff, 0))) return false; VERTEX *vertices; if(FAILED(d3d_vertexbuff->Lock(0, sizeof(cube), (void**)&vertices, 0))) return false; memcpy(vertices, cube, sizeof(cube)); d3d_vertexbuff->Unlock(); return true; } void unloadObjects() { if(d3d_vertexbuff) { d3d_vertexbuff->Release(); d3d_vertexbuff = 0; } }

Share this post


Link to post
Share on other sites
Advertisement
It could be that D3DFMT_D16 isn''t a valid depth buffer format for the display mode you''re using. Check the debug output to see if its warning you about anything. You can use CheckDeviceFormat and CheckDepthStencilMatch to find a correct match for your device format.

You could also try setting the D3DRS_ZWRITEENABLE renderstate. It should be enabled by default, but you never know.

Share this post


Link to post
Share on other sites
Thanks, everyone!

ardev and treething''s ideas made it work... i still have much to learn about Direct3D / 3D programming in general.

Thanks again!

the MadProgrammer

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!