[gelöst] Anfängerprobleme mit Depth-Buffer
-
Hallo allerseits!
Bisher verwendete ich keinen Depth-Buffer, da ich aber bei bestimmten Betrachtungswinkeln Darstellungsprobleme habe
(313° - alles klar
237° - auf einer Bildhälfte verschwinden Poygone (mit seltsamer Grenze)
248° - fast auf gesamter Bildbreite verschwinden Polygone (Grenze links noch sichtbar)),
würde ich gerne mal einen Depth Buffer ausprobieren. (Wie mir früher hier bereits empfohlen wurde, ich kam allerdings nicht mehr dazu bisher)Ich versuchte daher die Schritte in der DirectX SDK Documentation nachzuvollziehen, allerdings mit recht zweifelhaftem Erfolg:
Mein Depth Buffer Versuch:
http://img153.imageshack.us/img153/9459/depthbufferversuchcz7.png (27kB)
(dazu ergänzte ich im 3D INIT Bereich:d3dpp.SwapEffect = D3DSWAPEFFECT_COPY; d3dpp.EnableAutoDepthStencil = TRUE; d3dpp.AutoDepthStencilFormat = D3DFMT_D16; g_pd3dDevice->SetRenderState(D3DRS_ZENABLE , D3DZB_TRUE);)
Ohne Depth Buffer (bzw. mit d3dpp.EnableAutoDepthStencil = FALSE;):
http://img243.imageshack.us/img243/4663/sandrippelokvl4.png (27kB)Was könnte da falsch gelaufen sein?
Danke und Gruß,
dongDie relevanten Code Stellen:
(Sicherheitshalber nahezu vollständig, wirklich wichtig ist aber wohl nur der Init Teil ganz oben, denke ich...)3D-Initialisierung (Hauptsächlich Standard gemäß SDK Docu., Veränderungen für Depth Buffer gekenzeichnet):
HRESULT C3DFenster::InitD3D(HWND hWnd) { // Create the D3D object, which is needed to create the D3DDevice. if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) return E_FAIL; // Set up the structure used to create the D3DDevice. D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; //d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; //ANFANG FÜR DEPTH BUFFER VERÄNDERT(1/2): d3dpp.SwapEffect = D3DSWAPEFFECT_COPY; d3dpp.EnableAutoDepthStencil = TRUE; d3dpp.AutoDepthStencilFormat = D3DFMT_D16; //ENDE FÜR DEPTH BUFFER VERÄNDERT(1/2) // Create the Direct3D device. if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, // alternativ:D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) ) { return E_FAIL; } // Turn off culling, so we see the front and back of the triangle g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE ); // Turn off D3D lighting, since we are providing our own vertex colors g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE ); //ANFANG FÜR DEPTH BUFFER VERÄNDERT(2/2): g_pd3dDevice->SetRenderState(D3DRS_ZENABLE , D3DZB_TRUE); //ENDE FÜR DEPTH BUFFER VERÄNDERT(2/2) return S_OK; }Füllen des Puffers:
HRESULT C3DFenster::fuellePuffer() { CUSTOMVERTEX groessenmasstab[1]; // wird nur für den sizeof Operator weiter unten benötigt, der mit den Zeigern nicht funktioniert. VOID* pVertices; ... // Erzeuge den Vertex Puffer, falls noch nicht vorhanden if (b_bereit == false) this->bereiteVariablenVor(); for (int j = 0; j < this->yBloe3D; j++) { punkte[0].x = xOffset; punkte[0].z = yStep * j + yOffset + yStep; punkte[0].y = zskal(m_fwert[xpos(0)][ypos(j)]); punkte[0].color = farbeSand(m_fwert[xpos(0)][ypos(j)]); for (int i = 0; i < this->xBloe3D; i++) { punkte[2* i + 1].x = xStep * i + xOffset; punkte[2* i + 1].z = yStep * j + yOffset; punkte[2* i + 1].y = zskal(m_fwert[xpos(i)][ypos(j)]); punkte[2* i + 1].color = farbeSand(m_fwert[xpos(i)][ypos(j)]); punkte[2* i + 2].x = xStep * i + xOffset + xStep; punkte[2* i + 2].z = yStep * j + yOffset + yStep; punkte[2* i + 2].y = zskal(m_fwert[xpos(i+1)][ypos(j+1)]); punkte[2* i + 2].color = farbeSand(m_fwert[xpos(i+1)][ypos(j+1)]); } punkte[2* this->xBloe3D + 1].x = xStep * (this->xBloe3D - 1) + xOffset + xStep; punkte[2* this->xBloe3D + 1].z = yStep * j + yOffset; punkte[2* this->xBloe3D + 1].y = zskal(m_fwert[xpos(this->xBloe3D)][ypos(j)]); punkte[2* this->xBloe3D + 1].color = farbeSand(m_fwert[xpos(this->xBloe3D)][ypos(j)]); // Fill the vertex buffer # j if( FAILED( g_pVBar[j]->Lock( 0, (2* this->xBloe3D + 2) * sizeof(groessenmasstab), (void**)&pVertices, 0 ) ) ) return E_FAIL; memcpy( pVertices, punkte, (2* this->xBloe3D + 2) * sizeof(groessenmasstab) ); g_pVBar[j]->Unlock(); } .... return S_OK; }Rendern:
void C3DFenster::Render(void) { if (*g_b_zaehl_flash) { if (*g_flash > 255) *g_flash = 255; g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(*g_flash,*g_flash,100 + (*g_flash * 154 / 255)), 1.0f, 0 );} //Die Abfrage mit > 255 ist nur sicherheitshalber else g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,100), 1.0f, 0 ); if( SUCCEEDED( g_pd3dDevice->BeginScene() ) ) { // Setup the world, view, and projection Matrices SetupMatrices(); // Render the vertex buffer contents g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX ); // buffer loop for (int buff = 0; buff < dies->yBloe3D; buff++) { g_pd3dDevice->SetStreamSource( 0, g_pVBar[buff], 0, sizeof(CUSTOMVERTEX) ); g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, (dies->xBloe3D ) *2 ); } ..... g_pd3dDevice->EndScene(); } // Present the backbuffer contents to the display g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); }
-
schon mal in erwaegung gezogen, den zbuffer zu loeschen?
-
hellihjb schrieb:
schon mal in erwaegung gezogen, den zbuffer zu loeschen?
Volltreffer, Danke!

Nach Einfügen von
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,100), 1.0f, 0 );
in die Render Funktion ist alles in Ordnung.Ich sag ja: Anfängerproblem.

-
Hi,
oder auch:
g_pd3dDevice->Clear( ..., D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, ... );

Ciao,
Stefan
-
Stefan Zerbst schrieb:
oder auch:
g_pd3dDevice->Clear( ..., D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, ... );

...das ist natürlich noch besser!
