游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1851|回复: 2

导入X文件后,显示不正常!!!!!

[复制链接]

2

主题

2

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2010-8-22 14:54:00 | 显示全部楼层 |阅读模式
bool Setup()
{
        HRESULT hr = 0;

        //
        // Load the XFile data.
        //

        ID3DXBuffer* adjBuffer  = 0;
        ID3DXBuffer* mtrlBuffer = 0;
        DWORD        numMtrls   = 0;

        hr = D3DXLoadMeshFromX(  
                "bigship1.x",
                D3DXMESH_MANAGED,
                Device,
                &adjBuffer,
                &mtrlBuffer,
                0,
                &numMtrls,
                &Mesh);

        if(FAILED(hr))
        {
                ::MessageBox(0, "D3DXLoadMeshFromX() - FAILED", 0, 0);
                return false;
        }

        //
        // Extract the materials, and load textures.
        //

        if( mtrlBuffer != 0 && numMtrls != 0 )
        {
                D3DXMATERIAL* mtrls = (D3DXMATERIAL*)mtrlBuffer->GetBufferPointer();

                for(int i = 0; i < numMtrls; i++)
                {
                        // the MatD3D property doesn't have an ambient value set
                        // when its loaded, so set it now:
                        mtrls.MatD3D.Ambient = mtrls.MatD3D.Diffuse;

                        // save the ith material
                        Mtrls.push_back( mtrls.MatD3D );

                        // check if the ith material has an associative texture
                        if( mtrls.pTextureFilename != 0 )
                        {
                                // yes, load the texture for the ith subset
                                IDirect3DTexture9* tex = 0;
                                D3DXCreateTextureFromFile(
                                        Device,
                                        mtrls.pTextureFilename,
                                        &tex);

                                // save the loaded texture
                                Textures.push_back( tex );
                        }
                        else
                        {
                                // no texture for the ith subset
                                Textures.push_back( 0 );
                        }
                }
        }
        d3d::Release<ID3DXBuffer*>(mtrlBuffer); // done w/ buffer

        //
        // Optimize the mesh.
        //

        hr = Mesh->OptimizeInplace(               
                D3DXMESHOPT_ATTRSORT |
                D3DXMESHOPT_COMPACT  |
                D3DXMESHOPT_VERTEXCACHE,
                (DWORD*)adjBuffer->GetBufferPointer(),
                0, 0, 0);

        d3d::Release<ID3DXBuffer*>(adjBuffer); // done w/ buffer

        if(FAILED(hr))
        {
                ::MessageBox(0, "OptimizeInplace() - FAILED", 0, 0);
                return false;
        }

       
         //Set texture filters.
       
        Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
        Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
        Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);

        //
        // Set Lights.
        //

        D3DXVECTOR3 dir(1.0f, -0.0f, 0.25f);
        D3DXCOLOR col(1.0f, 1.0f, 1.0f, 1.0f);
        D3DLIGHT9 light = d3d::InitDirectionalLight(&dir, &col);

        Device->SetLight(0, &light);
        Device->LightEnable(0, true);
        Device->SetRenderState(D3DRS_NORMALIZENORMALS, true);
        Device->SetRenderState(D3DRS_SPECULARENABLE, true);

        //
        // Set camera.
        ////

        D3DXVECTOR3 pos(5.0f, 3.0f, -10.0f);
        D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
        D3DXVECTOR3 up(0.0f, 5.0f, 0.0f);

        D3DXMATRIX V;
        D3DXMatrixLookAtLH(
                &V,
                &pos,
                &target,
                &up);

        Device->SetTransform(D3DTS_VIEW, &V);

        //
        // Set projection matrix.
        //

        D3DXMATRIX proj;
        D3DXMatrixPerspectiveFovLH(
                        &proj,
                        D3DX_PI * 0.45f, // 45 - degree
                        (float)Width / (float)Height,
                        1.0f,
                        1000.0f);
        Device->SetTransform(D3DTS_PROJECTION, &proj);

        return true;
}

void Cleanup()
{
        d3d::Release<ID3DXMesh*>(Mesh);

        for(int i = 0; i < Textures.size(); i++)
                d3d::Release<IDirect3DTexture9*>( Textures );
}

bool Display(float timeDelta)
{
        if( Device )
        {
                //
                // Update: Update the camera.
                //

                if( ::GetAsyncKeyState('W') & 0x8000f )
                        TheCamera.walk(4.0f * timeDelta);

                if( ::GetAsyncKeyState('S') & 0x8000f )
                        TheCamera.walk(-4.0f * timeDelta);

                if( ::GetAsyncKeyState('A') & 0x8000f )
                        TheCamera.strafe(-4.0f * timeDelta);

                if( ::GetAsyncKeyState('D') & 0x8000f )
                        TheCamera.strafe(4.0f * timeDelta);

                if( ::GetAsyncKeyState('R') & 0x8000f )
                        TheCamera.fly(4.0f * timeDelta);

                if( ::GetAsyncKeyState('F') & 0x8000f )
                        TheCamera.fly(-4.0f * timeDelta);

                if( ::GetAsyncKeyState(VK_UP) & 0x8000f )
                        TheCamera.pitch(1.0f * timeDelta);

                if( ::GetAsyncKeyState(VK_DOWN) & 0x8000f )
                        TheCamera.pitch(-1.0f * timeDelta);

                if( ::GetAsyncKeyState(VK_LEFT) & 0x8000f )
                        TheCamera.yaw(-1.0f * timeDelta);
                       
                if( ::GetAsyncKeyState(VK_RIGHT) & 0x8000f )
                        TheCamera.yaw(1.0f * timeDelta);

                if( ::GetAsyncKeyState('N') & 0x8000f )
                        TheCamera.roll(1.0f * timeDelta);

                if( ::GetAsyncKeyState('M') & 0x8000f )
                        TheCamera.roll(-1.0f * timeDelta);

                // Update the view matrix representing the cameras
        // new position/orientation.
                D3DXMATRIX V;
                TheCamera.getViewMatrix(&V);
                Device->SetTransform(D3DTS_VIEW, &V);

                Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
                Device->BeginScene();

                for(int i = 0; i < Mtrls.size(); i++)
                {
                        Device->SetMaterial( &Mtrls );
                        Device->SetTexture(0, Textures);
                        Mesh->DrawSubset(i);
                }       

                Device->EndScene();
                Device-&gtresent(0, 0, 0, 0);
        }
        return true;
}

显示太近,不能全部看清模型。调了D3DXMatrixPerspectiveFovLH(
                        &proj,
                        D3DX_PI * 0.45f, // 45 - degree
                        (float)Width / (float)Height,
                        1.0f,
                        1000.0f);也不能看清。
恳求高手指点!!!!由于初级用户无法上传图片,没有贴图

11

主题

27

帖子

27

积分

注册会员

Rank: 2

积分
27
发表于 2010-8-22 21:33:00 | 显示全部楼层

Re: 导入X文件后,显示不正常!!!!!

估计是你的模型太大了,缩小一下比例,我第一次加载也是这样 很大,看不完,后来缩小就得了
如缩小到1/5
//缩放
D3DXMATRIX scal;
D3DXMatrixScaling(&scal, 0.2f, 0.2f, 0.2f);

5

主题

14

帖子

14

积分

新手上路

Rank: 1

积分
14
发表于 2010-8-23 16:29:00 | 显示全部楼层

Re:导入X文件后,显示不正常!!!!!

D3DXVECTOR3 pos(5.0f, 3.0f, -10.0f);
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 5.0f, 0.0f);

UP里改为0.0f,1.0f,0.0f
然后POS根据你的模型位置调整,你可以先试着调整为
pos(50.0f,0.0f,0.0f)

具体情况要根据模型来定,一般来说调整摄像机位置后就能看到
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-8-18 20:26

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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