回 帖 发 新 帖 刷新版面

主题:[讨论]D3D平面与模型共存问题?

我的一个用红色渲染的平面和一个.x文件导入的模型放在一起时,为何平面会变成黑色,然后我换了一个模型,平面又会变成另一种颜色,此问题已困扰我一个星期,请高手指教。

回复列表 (共9个回复)

沙发

你SetRenderStater()的状态设置问题,你最好渲染完一个就把状态材质 纹理等等完全恢复道原始状态,在开始设置下一个状态的在渲染,你在渲染某一个时候把状态改变了!没有你代码不知道具体是那里,你自己检查一下

板凳

winmain函数的内容如下:
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
    // Register the window class
    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
                      GetModuleHandle(NULL), NULL, NULL, NULL, (LPCTSTR)IDR_MENU1,
                      "D3D Tutorial", NULL };
    RegisterClassEx( &wc );
    // Create the application's window
    HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 03: Matrices",
                              WS_OVERLAPPEDWINDOW, 100, 100, 800, 800,
                              NULL, NULL, wc.hInstance, NULL );
    InitDxinput(hInst,hWnd);
    LoadString(hInst, IDR_MENU1, szWindowClass, MAX_LOADSTRING);
    // Initialize Direct3D
    if( SUCCEEDED( InitD3D( hWnd ) ) )
    {
        MyObject MyActor(g_pd3dDevice);
        MyMap mymap(g_pd3dDevice);
        MyCamera camera(g_pd3dDevice);

        // Create the scene geometry
        if( SUCCEEDED( mymap.InitGeometry() ) )
        {

            // Show the window
            ShowWindow( hWnd, SW_SHOWDEFAULT );
            UpdateWindow( hWnd );
            MyActor.InitGeometry("move.X");


            // Enter the message loop
            MSG msg;
            ZeroMemory( &msg, sizeof(msg) );
            while( msg.message!=WM_QUIT )
            {
                if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
                {
                    TranslateMessage( &msg );
                    DispatchMessage( &msg );
                }
                else
                {
                    updataDxinput();
                                        
                    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255,255,255), 1.0f, 0 );
                    if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
                    {
                        camera.SetupViewMatrices(MyvEyePt,MyvLookatPt,MyvUpVec);
                        mymap.SetupWorldMatrices(0,0,0,1,10);
                        
                        mymap.Render();
                        MyActor.SetupWorldMatrices(0,30,0,0.5,10);
                        MyActor.Render();

                        g_pd3dDevice->EndScene();
                    }
                    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
                }
            }
        }
    }

    UnregisterClass( "D3D Tutorial", wc.hInstance );
    return 0;
}

由于我接触D3D不是很久,对楼上的“你最好渲染完一个就把状态材质 纹理等等完全恢复道原始状态”这句话我不是很明白,能否说清楚点呢?

3 楼

MyObject类:
HRESULT MyObject::InitGeometry(char FileName[])
{
    LPD3DXBUFFER pD3DXMtrlBuffer;
    if( FAILED( D3DXLoadMeshFromX( FileName, D3DXMESH_SYSTEMMEM, //加载模型的信息
        m_pd3dDevice, NULL, 
        &pD3DXMtrlBuffer, NULL, &m_dwNumMaterials, 
        &m_pMesh ) ) )
    { 
        MessageBox(NULL, ".X文件加载失败", "Meshes.exe", MB_OK);
        return E_FAIL;

    }
    D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
    m_pMeshMaterials = new D3DMATERIAL9[m_dwNumMaterials];
    m_pMeshTextures  = new LPDIRECT3DTEXTURE9[m_dwNumMaterials];

    for( DWORD i=0; i<m_dwNumMaterials; i++ )
    {
        // Copy the material
        m_pMeshMaterials[i] = d3dxMaterials[i].MatD3D;

        // Set the ambient color for the material (D3DX does not do this)环境光
        m_pMeshMaterials[i].Ambient = m_pMeshMaterials[i].Diffuse;

        m_pMeshTextures[i] = NULL;
        if( d3dxMaterials[i].pTextureFilename != NULL && 
            lstrlen(d3dxMaterials[i].pTextureFilename) > 0 )
        {
            // Create the texture
            if( FAILED( D3DXCreateTextureFromFile( m_pd3dDevice, 
                d3dxMaterials[i].pTextureFilename, 
                &m_pMeshTextures[i] ) ) )
            {
                // If texture is not in current folder, try parent folder
                const TCHAR* strPrefix = TEXT("..\\");
                const int lenPrefix = lstrlen( strPrefix );
                TCHAR strTexture[MAX_PATH];
                lstrcpyn( strTexture, strPrefix, MAX_PATH );
                lstrcpyn( strTexture + lenPrefix, d3dxMaterials[i].pTextureFilename, MAX_PATH - lenPrefix );
                // If texture is not in current folder, try parent folder
                if( FAILED( D3DXCreateTextureFromFile( m_pd3dDevice, 
                    strTexture, 
                    &m_pMeshTextures[i] ) ) )
                {
                    MessageBox(NULL, "Could not find texture map", "Meshes.exe", MB_OK);
                }
            }
        }
    }

    m_pMesh->Optimize(D3DXMESHOPT_COMPACT |D3DXMESHOPT_ATTRSORT,NULL,NULL,NULL,NULL,&OptimizeMesh);
    // Done with the material buffer
    pD3DXMtrlBuffer->Release();
    return S_OK;
}


VOID MyObject::Render()
{

    for( DWORD i=0; i<m_dwNumMaterials; i++ )
    {
        // Set the material and texture for this subset
        m_pd3dDevice->SetMaterial( &m_pMeshMaterials[i] );
        m_pd3dDevice->SetTexture( 0, m_pMeshTextures[i] );
        OptimizeMesh->DrawSubset( i );
    }

}

4 楼

MyMap类:
HRESULT MyMap::InitGeometry()
{
    firstDot=4+(height/10-1)*2;
    CUSTOMVERTEX g_Vertices[ArraySize];

    for (int i=0;i<ArraySize;i++)
    {
        g_Vertices[i].color=0xff990099;
        g_Vertices[i].x=0;
        g_Vertices[i].y=0;
        g_Vertices[i].z=0;

    }
    g_Vertices[0].x =-width/2;
    g_Vertices[0].z=height/2;
    g_Vertices[1].x=width/2;
    g_Vertices[1].z=height/2;
    g_Vertices[2].x=-width/2;
    g_Vertices[2].z=-height/2;
    g_Vertices[3].x=width/2;
    g_Vertices[3].z=-height/2;
    for (int i=0;i<height/10-1;i++)
    {
        g_Vertices[i*2+4].color=0xff000000;
        g_Vertices[i*2+4].x=-width/2;
        g_Vertices[i*2+4].y=0;
        g_Vertices[i*2+4].z=height/2-10*(i+1);
    }
    for (int i=0;i<height/10-1;i++)
    {
        g_Vertices[i*2+5].color=0xff000000;
        g_Vertices[i*2+5].x=width/2;
        g_Vertices[i*2+5].y=0;
        g_Vertices[i*2+5].z=height/2-10*(i+1);
    }
    for (int i=0;i<width/10-1;i++)
    {
        g_Vertices[firstDot+i*2].color=0xff000000;
        g_Vertices[firstDot+i*2].x=-width/2+10*(i+1);
        g_Vertices[firstDot+i*2].y=0;
        g_Vertices[firstDot+i*2].z=height/2;
    }
    for (int i=0;i<width/10-1;i++)
    {
        g_Vertices[firstDot+1+i*2].color=0xff000000;
        g_Vertices[firstDot+1+i*2].x=-width/2+10*(i+1);
        g_Vertices[firstDot+1+i*2].y=0;
        g_Vertices[firstDot+1+i*2].z=-height/2;
    }
    // Initialize three vertices for rendering a triangle

    // Create the vertex buffer.
    if( FAILED(m_pd3dDevice->CreateVertexBuffer( ArraySize*sizeof(CUSTOMVERTEX),0,D3DFVF_CUSTOMVERTEX,
        D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
    {
        return E_FAIL;
    }

    // Fill the vertex buffer.
    VOID* pVertices;
    if( FAILED( g_pVB->Lock( 0, sizeof(g_Vertices), (void**)&pVertices, 0 ) ) )
        return E_FAIL;
    memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
    g_pVB->Unlock();

    return S_OK;
}

VOID MyMap::Render()
{

    m_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
    m_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
    m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
    for (int i=4;i<firstDot;i+=2)
    {
    m_pd3dDevice->DrawPrimitive(D3DPT_LINELIST,i,1);
    }
    for (int i=14;i<ArraySize;i+=2)
    {
    m_pd3dDevice->DrawPrimitive(D3DPT_LINELIST,i,1);
    }
}

5 楼

材质问题

6 楼

请高手指明如何解决

7 楼

唉,D3D我还没学,X文件的原理都还没搞清楚呢,呵呵

8 楼

唉,哪位高手能指教啊

9 楼

VOID MyObject::Render()
{

    for( DWORD i=0; i<m_dwNumMaterials; i++ )
    {
        // Set the material and texture for this subset
        [color=FF0000]m_pd3dDevice->SetMaterial( &m_pMeshMaterial[i] );//think[/color]
        m_pd3dDevice->SetTexture( 0, m_pMeshTextures[i] );
        OptimizeMesh->DrawSubset( i );
    }

}

VOID MyMap::Render()
{

    m_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
    m_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
    [color=0000FF]m_pd3dDevice->SetMaterial(&m_pYourPlaneMaterial);//add this code[/color]
    m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
    for (int i=4;i<firstDot;i+=2)
    {
    m_pd3dDevice->DrawPrimitive(D3DPT_LINELIST,i,1);
    }
    for (int i=14;i<ArraySize;i+=2)
    {
    m_pd3dDevice->DrawPrimitive(D3DPT_LINELIST,i,1);
    }
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册