• 加载网格X文件代码(Unicode版本)


    1.网格.cpp

    使用小红龙的文件,使用的是多字节字符集

    #include "d3dUtility.h"
    #include <fstream>
    #include <vector>
    
    IDirect3DDevice9* Device = 0; 
    
    const int Width  = 640;
    const int Height = 480;
    
    ID3DXMesh* Mesh = 0;
    std::vector<D3DMATERIAL9> Mtrls(0);
    std::vector<IDirect3DTexture9*> Textures(0);
    
    bool Setup()
    {
        HRESULT hr = 0;
        ID3DXBuffer* adjBuffer = 0;
        ID3DXBuffer* mtrlBuffer = 0;
        DWORD numMtrls = 0;
        hr=D3DXLoadMeshFromX(
            "1.x",
            D3DXMESH_MANAGED,
            Device,
            &adjBuffer,
            &mtrlBuffer,
            0,
            &numMtrls,
            &Mesh);
        if(FAILED(hr))
        {
            ::MessageBox(0,"D3DXLoadMeshFromX() - Failed",0,0);
            return false;
        }
    
            //检查x文件中有没有材质部分,
        if(mtrlBuffer != 0 && numMtrls != 0)
        {
            D3DXMATERIAL* mtrls = (D3DXMATERIAL*)mtrlBuffer->GetBufferPointer();//有材质部分,就把它们提取出来
            for(int i=0; i<numMtrls; i++)
            {
                mtrls[i].MatD3D.Ambient = mtrls[i].MatD3D.Diffuse;
                Mtrls.push_back(mtrls[i].MatD3D);
                if(mtrls[i].pTextureFilename != 0)
                {
                    IDirect3DTexture9* tex = 0;
                    D3DXCreateTextureFromFile(Device, mtrls[i].pTextureFilename, &tex);
                    Textures.push_back(tex);
                }else{
                    Textures.push_back(0);
                }
            }
        }
        d3d::Release<ID3DXBuffer*>(mtrlBuffer);
    
        hr = Mesh->OptimizeInplace( //使用OptimizeInplace()进行网格优化后,Mesh 的几何信息将按照属性进行排序,这样各个子集的顶点/索引将组成连续的块
            D3DXMESHOPT_ATTRSORT |
            D3DXMESHOPT_COMPACT |
            D3DXMESHOPT_VERTEXCACHE,
            (DWORD*)adjBuffer->GetBufferPointer(),
            0,0,0);
        d3d::Release<ID3DXBuffer*>(adjBuffer);
    
        if(FAILED(hr))
        {
            ::MessageBox(0,"OptimizeInplace() - FAILED",0,0);
            return false;
        }
    
        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);
        
        D3DXVECTOR3 pos(4.0f, 4.f, -13.0f);
        D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
        D3DXVECTOR3 up(0.0f, 1.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,
                1.0f,
                1000.0f);
        Device->SetTransform(D3DTS_PROJECTION, &proj);
        return true;
    }
    
    void Cleanup()
    {
    
    }
    
    bool Display(float timeDelta)
    {if( Device )
    {
    
        
    
        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[i]);
                Device->SetTexture(0, Textures[i]);
                Mesh->DrawSubset(i);
            }
    
    
        Device->EndScene();
        Device->Present(0, 0, 0, 0);
    }
    return true;
    }
    
    //
    // WndProc
    //
    LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch( msg )
        {
        case WM_DESTROY:
            ::PostQuitMessage(0);
            break;
    
        case WM_KEYDOWN:
            if( wParam == VK_ESCAPE )
                ::DestroyWindow(hwnd);
            break;
        }
        return ::DefWindowProc(hwnd, msg, wParam, lParam);
    }
    
    //
    // WinMain
    //
    int WINAPI WinMain(HINSTANCE hinstance,
        HINSTANCE prevInstance, 
        PSTR cmdLine,
        int showCmd)
    {
        if(!d3d::InitD3D(hinstance,
            Width, Height, true, D3DDEVTYPE_HAL, &Device))
        {
            ::MessageBox(0, "InitD3D() - FAILED", 0, 0);
            return 0;
        }
    
        if(!Setup())
        {
            ::MessageBox(0, "Setup() - FAILED", 0, 0);
            return 0;
        }
    
        d3d::EnterMsgLoop( Display );
    
        Cleanup();
    
        Device->Release();
    
        return 0;
    }
    View Code

    运行结果

    2.网格.CPP(使用Unicode字符集)

    #include "d3dUtility.h"
    
    
    IDirect3DDevice9* Device = 0; 
    
    const int Width  = 640;
    const int Height = 480;
    
    #include <fstream>
    #include <vector>
    
    
    
    LPD3DXMESH          Mesh     = NULL; // 网格对象
    D3DMATERIAL9*       Materials    = NULL; // 网格的材质信息
    LPDIRECT3DTEXTURE9* Textures     = NULL; // 网格的纹理信息
    DWORD               NumMtrls    = 0;    // 材质的数目
    bool Setup()
    {
        HRESULT hr = 0;
        ID3DXBuffer* adjBuffer = 0;
        ID3DXBuffer* mtrlBuffer = 0;
        
    
        hr = D3DXLoadMeshFromX(
            L"1.x",
            D3DXMESH_MANAGED,
            Device,
            &adjBuffer,
            &mtrlBuffer,
            0,
            &NumMtrls,
            &Mesh);
        if(FAILED(hr))
        {
            ::MessageBox(0,L"D3DXLoadMeshFromX() - Failed",0,0);
            return false;
        }
    
        D3DXMATERIAL *pMtrls = (D3DXMATERIAL*)mtrlBuffer->GetBufferPointer();
        //创建一个D3DXMATERIAL结构体用于读取材质和纹理信息
        Materials = new D3DMATERIAL9[NumMtrls];
        Textures  = new LPDIRECT3DTEXTURE9[NumMtrls];
     
        for (DWORD i=0; i<NumMtrls; i++) 
        {
            //获取材质,并设置一下环境光的颜色值
            Materials [i] = pMtrls[i].MatD3D;
            Materials [i].Ambient = Materials[i].Diffuse;
     
            //创建一下纹理对象
            Textures[i]  = NULL;
            D3DXCreateTextureFromFileA(Device, pMtrls[i].pTextureFilename, &Textures[i]);
        }
     
        d3d::Release<ID3DXBuffer*>(mtrlBuffer);
    
        hr = Mesh->OptimizeInplace( //使用OptimizeInplace()进行网格优化后,Mesh 的几何信息将按照属性进行排序,这样各个子集的顶点/索引将组成连续的块
            D3DXMESHOPT_ATTRSORT |
            D3DXMESHOPT_COMPACT |
            D3DXMESHOPT_VERTEXCACHE,
            (DWORD*)adjBuffer->GetBufferPointer(),
            0,0,0);
        d3d::Release<ID3DXBuffer*>(adjBuffer);
    
        if(FAILED(hr))
        {
            ::MessageBox(0,L"OptimizeInplace() - FAILED",0,0);
            return false;
        }
    
        Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
        Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
        Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
    
            //灯光
        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(0.0f, 10.0f, -50.0f);
        D3DXVECTOR3 target(0.0f, 10.0f, 0.0f);
        D3DXVECTOR3 up(0.0f, 1.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,
                1.0f,
                1000.0f);
        Device->SetTransform(D3DTS_PROJECTION, &proj);
        return true;
    }
    
    void Cleanup()
    {
    
    }
    
    bool Display(float timeDelta)
    {if( Device )
    {
    
    
    
        Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
        Device->BeginScene();
                for(int i=0; i<NumMtrls; i++)
            {
                Device->SetMaterial(&Materials[i]);
                Device->SetTexture(0, Textures[i]);
                Mesh->DrawSubset(i);
            }
    
        Device->EndScene();
        Device->Present(0, 0, 0, 0);
    }
    return true;
    }
    
    //
    // WndProc
    //
    LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch( msg )
        {
        case WM_DESTROY:
            ::PostQuitMessage(0);
            break;
    
        case WM_KEYDOWN:
            if( wParam == VK_ESCAPE )
                ::DestroyWindow(hwnd);
            break;
        }
        return ::DefWindowProc(hwnd, msg, wParam, lParam);
    }
    
    //
    // WinMain
    //
    int WINAPI WinMain(HINSTANCE hinstance,
        HINSTANCE prevInstance, 
        PSTR cmdLine,
        int showCmd)
    {
        if(!d3d::InitD3D(hinstance,
            Width, Height, true, D3DDEVTYPE_HAL, &Device))
        {
            ::MessageBox(0, L"InitD3D() - FAILED", 0, 0);
            return 0;
        }
    
        if(!Setup())
        {
            ::MessageBox(0, L"Setup() - FAILED", 0, 0);
            return 0;
        }
    
        d3d::EnterMsgLoop( Display );
    
        Cleanup();
    
        Device->Release();
    
        return 0;
    }
    View Code

    加载资源:

    运行结果:

     3.添加其他X文件

    添加马厩模型文件

     修改加载的X文件

     修改相应X文件加载的纹理文件位置

    代码:

    #include "d3dUtility.h"
    #include <fstream>
    #include <vector>
    #pragma  comment(lib, "d3d9.lib")
    #pragma  comment(lib, "d3dx9.lib")
    #pragma  comment(lib, "winmm.lib")
    IDirect3DDevice9* Device = 0; 
    
    const int Width  = 640;
    const int Height = 480;
    
    
    LPD3DXMESH          Mesh     = NULL; // 网格对象
    D3DMATERIAL9*       Materials    = NULL; // 网格的材质信息
    LPDIRECT3DTEXTURE9* Textures     = NULL; // 网格的纹理信息
    DWORD               NumMtrls    = 0;    // 材质的数目
    
    bool Setup()
    {
        HRESULT hr = 0;
        ID3DXBuffer* adjBuffer = 0;
        ID3DXBuffer* mtrlBuffer = 0;
        
    
        hr=D3DXLoadMeshFromX(
            L"马厩//JZ_majiu.X",
            D3DXMESH_MANAGED,
            Device,
            &adjBuffer,
            &mtrlBuffer,
            0,
            &NumMtrls,
            &Mesh);
        if(FAILED(hr))
        {
            ::MessageBox(0,L"D3DXLoadMeshFromX() - Failed",0,0);
            return false;
        }
    
            // 读取材质和纹理数据
        D3DXMATERIAL *pMtrls = (D3DXMATERIAL*)mtrlBuffer->GetBufferPointer(); 
        //创建一个D3DXMATERIAL结构体用于读取材质和纹理信息
    
        Materials = new D3DMATERIAL9[NumMtrls];
        Textures  = new LPDIRECT3DTEXTURE9[NumMtrls];
    
        for (DWORD i=0; i<NumMtrls; i++) 
        {
            //获取材质,并设置一下环境光的颜色值
            Materials [i] = pMtrls[i].MatD3D;
            Materials [i].Ambient = Materials[i].Diffuse;
     
            //创建一下纹理对象
            Textures[i]  = NULL;
            D3DXCreateTextureFromFileA(Device, pMtrls[i].pTextureFilename, &Textures[i]);
        }
     
        d3d::Release<ID3DXBuffer*>(mtrlBuffer);
    
        hr = Mesh->OptimizeInplace( //使用OptimizeInplace()进行网格优化后,Mesh 的几何信息将按照属性进行排序,这样各个子集的顶点/索引将组成连续的块
            D3DXMESHOPT_ATTRSORT |
            D3DXMESHOPT_COMPACT |
            D3DXMESHOPT_VERTEXCACHE,
            (DWORD*)adjBuffer->GetBufferPointer(),
            0,0,0);
        d3d::Release<ID3DXBuffer*>(adjBuffer);
    
        if(FAILED(hr))
        {
            ::MessageBox(0,L"OptimizeInplace() - FAILED",0,0);
            return false;
        }
    
        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);
        
        D3DXVECTOR3 pos(4.0f, 4.f, -13.0f);
        D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
        D3DXVECTOR3 up(0.0f, 1.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,
                1.0f,
                1000.0f);
        Device->SetTransform(D3DTS_PROJECTION, &proj);
        return true;
    }
    
    void Cleanup()
    {
    
    }
    
    bool Display(float timeDelta)
    {if( Device )
    {
    
        
    
        Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
        Device->BeginScene();
            
        for(int i=0; i<NumMtrls; i++)
            {
                Device->SetMaterial(&Materials[i]);
                Device->SetTexture(0, Textures[i]);
                Mesh->DrawSubset(i);
            }
    
    
        Device->EndScene();
        Device->Present(0, 0, 0, 0);
    }
    return true;
    }
    
    //
    // WndProc
    //
    LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch( msg )
        {
        case WM_DESTROY:
            ::PostQuitMessage(0);
            break;
    
        case WM_KEYDOWN:
            if( wParam == VK_ESCAPE )
                ::DestroyWindow(hwnd);
            break;
        }
        return ::DefWindowProc(hwnd, msg, wParam, lParam);
    }
    
    //
    // WinMain
    //
    int WINAPI WinMain(HINSTANCE hinstance,
        HINSTANCE prevInstance, 
        PSTR cmdLine,
        int showCmd)
    {
        if(!d3d::InitD3D(hinstance,
            Width, Height, true, D3DDEVTYPE_HAL, &Device))
        {
            ::MessageBox(0, L"InitD3D() - FAILED", 0, 0);
            return 0;
        }
    
        if(!Setup())
        {
            ::MessageBox(0, L"Setup() - FAILED", 0, 0);
            return 0;
        }
    
        d3d::EnterMsgLoop( Display );
    
        Cleanup();
    
        Device->Release();
    
        return 0;
    }
    View Code

    运行结果:

  • 相关阅读:
    网站备份list
    vnc checklist
    appnode iptables 规则后面覆盖前面的
    Appnode + Discuz checklist
    解决WORD文档无法显示链接的图像问题
    应用容器Application container
    要研究的内容
    转 Flex MXML编译成AS类
    Flex文件结构
    int a
  • 原文地址:https://www.cnblogs.com/szmtjs10/p/16201528.html
Copyright © 2020-2023  润新知