コーンのメッシュを描画する

コーンに色を設定した3Dモデル(Cone.x)を描画します。

前田稔の超初心者のプログラム入門

プロジェクトの説明

  1. 3Dモデルのファイルを入力して、WORLD 座標を回転しながら描画します。
    DirectX のプロジェクトを構築する に習って、空のプロジェクトから作成します。
    次のファイルを格納して、プロジェクトに取り込んで下さい。
    ファイル名 説明
    DXCone.cpp コーンのメッシュ(cone.x)を描画する
    cone.x を "C:\data\xfile\" のフォルダーに格納して下さい。
    cone.x は「初心者のプログラム(DirectX10)/X-FILE(Cone.x) の頂点データを定義する」からダウンロード出来ます。
  2. _mesh がメッシュ(cone.x)の領域です。
    _materials は3Dモデルのマテリアルを格納する領域です。
    _textures は3Dモデルのテクスチャを格納する領域です。
    ref class D3DModel : public System::Windows::Forms::Form
    {
        Device      ^_device;       // rendering device
        Mesh        ^_mesh;         // mesh object
        array<ExtendedMaterial> ^_materials;    // mesh Materials
        array<Texture^>         ^_textures;     // mesh Textures
    
  3. OnResetDevice() で Cone.x を入力します。
    SetCurrentDirectory() で X-FILE が格納されているフォルダーを設定します。
    Mesh::FromFile() が3Dモデル(Cone.x)を入力するメソッドです。
    gcnew array(); で _texture を生成します。
    モデルの中でテクスチャーが設定されていれば _texture に読み込みます。
        void OnResetDevice(Object^ sender, EventArgs^ e)
        {
            _device->RenderState->ZBufferEnable = true;  // Turn on the zbuffer
            _device->RenderState->Lighting = true;       // make sure lighting is enabled
            // X-FILE をロード
            System::IO::Directory::SetCurrentDirectory("C:\\data\\xfile\\");
            _mesh = Mesh::FromFile("cone.x",MeshFlags::Managed,_device,_materials);
    
            // テクスチャーがあれば読み込み
            _textures = gcnew array<Texture^>(_materials->Length);
            for(int i=0; i<_materials->Length; i++)
            {   _textures[i] = nullptr;
                if (_materials[i].TextureFilename!=nullptr &&
                    _materials[i].TextureFilename->Length >= 1)
                {   _textures[i]= TextureLoader::FromFile(_device,_materials[i].TextureFilename); }
            }
        
  4. 次に3Dモデルに法線ベクトルを設定する処理です。
    モデルに「法線ベクトルが設定されていない」なんてことは、通常はありません。 (^_^;)
    ところが DirectX に添付されている tiger.x には、法線ベクトルが設定されていないのです。
    おかげで DirectX の初期の頃から散々悩まされて続けてきました。 (;_;)
    これは Microsoft の陰謀で「そんなことぐらい解らないものは DirectX を使うな」と言う事でしょうか?
    この処理を追加すると tiger.x が描画出来るので試してみて下さい。
  5. メッシュを描画する関数です。
    Clear() で画面をクリアします。
    SetupLights() でライトを設定します。
    SetupMatrices() は描画環境を設定する関数です。
    メッシュの Material と Texture をデバイスに設定します。
    _mesh.DrawSubset(i) でメッシュを描画します。
        void Render()
        {
            if (_device==nullptr)   return;
            if (pause)              return;
    
            _device->Clear(ClearFlags::Target|ClearFlags::ZBuffer,System::Drawing::Color::Blue,1.0f,0);
            _device->BeginScene();
            SetupLights();
            SetupMatrices();
            // メッシュの描画
            for(int i=0; i<_materials->Length; i++)
            {
                _device->SetTexture(0,_textures[i]);
                _device->Material = _materials[i].Material3D;
                _mesh->DrawSubset(i);
            }
            _device->EndScene();
            _device->Present();
        }
        
  6. C# でも同様のプログラムを作成しています。
    超初心者のプログラム入門(C#)から コーンのメッシュを描画する を参照して下さい。
    リンクがエラーになるときは「前田稔の超初心者のプログラム入門」から辿って下さい。
    tiger.x も描画出来るので試してみて下さい。 ヽ(^^ )
    このプログラムは DX.BAT でコンパイルすることも出来ます。

超初心者のプログラム入門(C/C++)