游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1179|回复: 0

导入一个X文件,模型无法显示!!!!急

[复制链接]

2

主题

2

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2010-8-22 13:23:00 | 显示全部楼层 |阅读模式

  注:  没有用龙书上带的X文件,自己找的X文件.


#include "d3dUtility.h"
#include "camera.h"
#include <vector>

//
// Globals
//

IDirect3DDevice9* Device = 0;
const int Width  = 640;
const int Height = 480;
ID3DXMesh*                      Mesh = 0;
std::vector<D3DMATERIAL9>       Mtrls(0);
std::vector<IDirect3DTexture9*> Textures(0);
Camera TheCamera(Camera:ANDOBJECT);

//
// Framework functions
//
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, -1.0f, 1.0f);
        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.5f, // 90 - degree
                        (float)Width / (float)Height,
                        300.0f,
                        7000.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);

                //static float y = 0.0f;
                //D3DXMATRIX yRot;
                //D3DXMatrixRotationY(&yRot, y);
                //y += timeDelta;

                //if( y >= 6.28f )
                //        y = 0.0f;

                //D3DXMATRIX World = yRot;

                //Device->SetTransform(D3DTS_WORLD, &World);

                //
                // Render
                //

                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;
}
[em7]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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