游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2201|回复: 5

D3D平面与模型共存问题?

[复制链接]

2

主题

7

帖子

13

积分

新手上路

Rank: 1

积分
13
发表于 2006-12-13 22:08:00 | 显示全部楼层 |阅读模式
我的一个用红色渲染的平面和一个.x文件导入的模型放在一起时,为何平面会变成黑色,然后我换了一个模型,平面又会变成另一种颜色,此问题已困扰我一个星期,请高手指教。
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-&gtresent( NULL, NULL, NULL, NULL );
                }
            }
        }
    }

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

2

主题

7

帖子

13

积分

新手上路

Rank: 1

积分
13
 楼主| 发表于 2006-12-13 22:12:00 | 显示全部楼层

Re:D3D平面与模型共存问题?

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 = d3dxMaterials.MatD3D;

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

        m_pMeshTextures = NULL;
        if( d3dxMaterials.pTextureFilename != NULL &&
            lstrlen(d3dxMaterials.pTextureFilename) > 0 )
        {
            // Create the texture
            if( FAILED( D3DXCreateTextureFromFile( m_pd3dDevice,
                d3dxMaterials.pTextureFilename,
                &m_pMeshTextures ) ) )
            {
                // 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.pTextureFilename, MAX_PATH - lenPrefix );
                // If texture is not in current folder, try parent folder
                if( FAILED( D3DXCreateTextureFromFile( m_pd3dDevice,
                    strTexture,
                    &m_pMeshTextures ) ) )
                {
                    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 );
        m_pd3dDevice->SetTexture( 0, m_pMeshTextures );
        OptimizeMesh->DrawSubset( i );
    }

}

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

    for (int i=0;i<ArraySize;i++)
    {
        g_Vertices.color=0xff990099;
        g_Vertices.x=0;
        g_Vertices.y=0;
        g_Vertices.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

主题

686

帖子

697

积分

高级会员

Rank: 4

积分
697
QQ
发表于 2006-12-14 21:07:00 | 显示全部楼层

Re:D3D平面与模型共存问题?

平面没有设贴图混合参数,默认的纹理混合参数是顶点色*纹理,这时的纹理还是Mesh的,所以平面颜色会跟着变

2

主题

7

帖子

13

积分

新手上路

Rank: 1

积分
13
 楼主| 发表于 2006-12-15 21:43:00 | 显示全部楼层

Re:D3D平面与模型共存问题?

但是我的平面都没有设置贴图啊,请高手用代码提示

0

主题

275

帖子

676

积分

高级会员

Rank: 4

积分
676
发表于 2006-12-16 15:01:00 | 显示全部楼层

Re:D3D平面与模型共存问题?

因??ender state是?我坏
mesh 使用?後render state 被污染了
?到平面render就??子玫缴弦淮蔚
所以每??object render 都要切?Qrender state

5

主题

686

帖子

697

积分

高级会员

Rank: 4

积分
697
QQ
发表于 2006-12-25 21:52:00 | 显示全部楼层

Re:D3D平面与模型共存问题?

即使没有设置纹理也是可以采样的,只是采样出来是一些其他值罢了
SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_DIFFUSE);
SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-26 03:13

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表