Problem mit dem ZBuffer



  • Wenn ich in DX9 ein Modell ohne Z-Buffer lade, ist alles, was vorn ist, von den
    hinteren Polygonen überschrieben und das sieht komisch aus.
    Um richtig,also von hinten nach vorn, zu zeichenen, hab ich mit

    g_pd3dDevice1->SetRenderState(D3DRS_ZWRITEENABLE,TRUE);
    g_pd3dDevice1->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
    

    den Z-Buffer aktiviert.
    Nun wird aber nichtsmehr angezeigt. Alles schwarz.
    Muss man noch was zusätzlich aktivieren?



  • der zbuffer muss beim erstellen des 3ddevice angegeben werden

    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;
    

    und deim löschen mit "clear()" muss der zbuffer gelöscht werden

    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
                             D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
    

    steht alles in den tutorials des 9er sdk ausführlich erklärt. auch die beispielprogramme sind sehr gut gemacht. bei solchen fragen immer erst mal dort nachschauen.



  • Ich hab mir ja schon die Tutorials angeguckt und per Google nach weiteren gesucht,
    aber es funktioniert trotzdem nicht. Sobald die Z-Komponente der Vertices
    größer oder kleiner 0 ist, wird das Modell nicht mehr gezeichnet.



  • Ich hab hier mal einen Beispielcode. Es wird wird ein Rechteck mit Textur gezeichnet.
    Sobald man an der Z-Komponente rumspielt wirds nicht mehr angezeigt,
    obwohl ich glaube alles richtig eingestellt zu haben.

    #include <d3d9.h>
    #include <d3dx9tex.h>
    #include <d3dx9core.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include "Globals.h"
    #include "Modell.h"
    #include "Shape.h"
    
    #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE|D3DFVF_TEX1)
    
    LPDIRECT3D9             g_pD3D       = NULL; 
    LPDIRECT3DDEVICE9       g_pd3dDevice = NULL; 
    LPDIRECT3DVERTEXBUFFER9 g_pVB        = NULL; 
    LPDIRECT3DTEXTURE9		g_pTexture		= NULL;
    LPD3DXSPRITE			g_pSprite		= NULL;
    LPDIRECT3DINDEXBUFFER9	g_pIndex		= NULL;
    Modell m1;
    
    HWND hWnd=NULL;
    LOGFONT lf;
    
    HRESULT InitD3D( HWND hWnd )
    {
    
        if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
            return E_FAIL;
    
    	D3DDISPLAYMODE d3ddm; 
    	if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) ) 
        return E_FAIL;
        D3DPRESENT_PARAMETERS d3dpp;
        ZeroMemory( &d3dpp, sizeof(d3dpp) );
        d3dpp.Windowed = TRUE;
        d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
        d3dpp.BackBufferFormat = d3ddm.Format;
    	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);
        return S_OK;
    }
    
    HRESULT InitVB()
    {
    
        if( FAILED( g_pd3dDevice->CreateVertexBuffer( 4*sizeof(CUSTOMVERTEX),
                                                      0, D3DFVF_CUSTOMVERTEX,
                                                      D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
        {
            return E_FAIL;
        }
    
        CUSTOMVERTEX* pVertices;
        if( FAILED( g_pVB->Lock( 0, sizeof(pVertices), (void**)&pVertices, 0 ) ) )
            return E_FAIL;
    
    	pVertices[0].x=100;pVertices[0].y=100;pVertices[1].z=0;pVertices[0].rhw=0;pVertices[0].color=0xffffffff;
    	pVertices[0].tv=0.0f;
    	pVertices[0].tu=0.0f;
    	pVertices[1].x=500;pVertices[1].y=100;pVertices[1].z=0;pVertices[0].rhw=0;pVertices[1].color=0xffffffff;
    	pVertices[2].x=100;pVertices[2].y=500;pVertices[2].z=0;pVertices[0].rhw=0;pVertices[2].color=0xffffffff;
    	pVertices[3].x=500;pVertices[3].y=500;pVertices[3].z=0;pVertices[0].rhw=0;pVertices[3].color=0xffffffff;
    	pVertices[1].tv=0.0f;
    	pVertices[1].tu=1.0f;
    	pVertices[2].tv=1.0f;
    	pVertices[2].tu=0.0f;
    	pVertices[3].tv=1.0f;
    	pVertices[3].tu=1.0f;
    
        g_pVB->Unlock();
    
    		WORD wIndices[6] = { 
    		0,1,2, 
    		1,2,3 
    		};
    		WORD *pInd;
    
    	if( FAILED( g_pd3dDevice->CreateIndexBuffer(60*sizeof(WORD),D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,D3DPOOL_DEFAULT,&g_pIndex,NULL)))
    		return E_FAIL;
    	if( FAILED(g_pIndex->Lock(0,sizeof(pInd),(void**)&pInd,0)))
    		return E_FAIL;
    	pInd[0]=0;
    	pInd[1]=1;
    	pInd[2]=2;
    	pInd[3]=1;
    	pInd[4]=3;
    	pInd[5]=2;
    	g_pIndex->Unlock();
    
    	D3DXCreateTextureFromFile(g_pd3dDevice,
    								"9.jpg",
    								&g_pTexture);
    	//m1.LoadFromFile("character.txt",g_pd3dDevice);
    
        return S_OK;
    }
    
    VOID Cleanup()
    {
        if( g_pVB != NULL )        
            g_pVB->Release();
    
        if( g_pd3dDevice != NULL ) 
            g_pd3dDevice->Release();
    
        if( g_pD3D != NULL )       
            g_pD3D->Release();
    }
    
    VOID Render()
    {	
        g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );
    
        if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
        {
    
    		D3DXMATRIX matView; 
    		D3DXMatrixLookAtLH( &matView, &D3DXVECTOR3( 0.0f, 3.0f,-5.0f ), 
    							&D3DXVECTOR3( 0.0f, 0.0f, 0.0f ), 
    							&D3DXVECTOR3( 0.0f, 1.0f, 0.0f )); 
    		g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView ); 
    		D3DXMATRIX matProj;
    		D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, -10.0f, 100.0f );
    		g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
    
    		D3DXMATRIX matWorld;
    		D3DXMatrixRotationY( &matWorld, timeGetTime()/200.0f );
    		g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
    
    		g_pd3dDevice->SetTexture( 0, g_pTexture );
    		g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_ADDSIGNED );
            g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
            g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
            g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
            g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
    
    		g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
    		//g_pd3dDevice->SetRenderState(D3DRS_ZWRITEENABLE,TRUE);
    		g_pd3dDevice->SetRenderState(D3DRS_DITHERENABLE, FALSE);
    		g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
    		g_pd3dDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
    
            g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
    		g_pd3dDevice->SetIndices(g_pIndex);
    		g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
            g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
    		g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,4,0,6);
    
            g_pd3dDevice->EndScene();
        }
    	//m1.Render(g_pd3dDevice);
        if(g_pd3dDevice->Present( NULL, NULL, NULL, NULL )==!D3D_OK)
    	{
    		MessageBox(NULL,"Present Failed","",MB_OK);
    	}
    
    }
    
    LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
    {
        switch( msg )
        {
            case WM_DESTROY:
                Cleanup();
                PostQuitMessage( 0 );
                return 0;
        }
    
        return DefWindowProc( hWnd, msg, wParam, lParam );
    }
    
    INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
    {
    
        WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
                          GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
                          "D3D Tutorial", NULL };
        RegisterClassEx( &wc );
    
        hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 02: Vertices",
                                  WS_OVERLAPPEDWINDOW, 0, 0, 1024, 750,
                                  GetDesktopWindow(), NULL, wc.hInstance, NULL );
    
        if( SUCCEEDED( InitD3D( hWnd ) ) )
        {
            if( SUCCEEDED( InitVB() ) )
            {
                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( "D3D Tutorial", wc.hInstance );
        return 0;
    }
    


  • Hat sich geklärt.


Log in to reply