トーラスをライトで照らして描画する

C# DirectX でトーラスをライトで照らして、回転しながら描画します。
ライトの座標も回転してみましょう。

前田稔(Maeda Minoru)の超初心者のプログラム入門

プロジェクトの設定

  1. DirectX 3D で Form を表示 に習って、空のプロジェクトから作成します。
    プログラムファイル(DXLight.cs)をダウンロードして、プロジェクトに取り込んで下さい。
    ファイル名 説明
    DXLight.cs トーラスをライトで照らす
  2. DirectX3D の初期化を行う InitializeGraphics() です。
    DepthStencil を true にして、Format を設定します。
        bool InitializeGraphics()
        {
            try
            {
                presentParams.Windowed = true;
                presentParams.SwapEffect = SwapEffect.Discard;
                presentParams.EnableAutoDepthStencil = true;
                presentParams.AutoDepthStencilFormat = DepthFormat.D16;
    
                // Create the D3DDevice
                device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
                device.DeviceReset += new System.EventHandler(this.OnResetDevice);
                this.OnResetDevice(device, null);
                pause = false;
            }
            catch (DirectXException)
            {
                return false;
            }
            return true;
        }
        
  3. DirectX3D のリセットを行う OnResetDevice() です。
    ZBufferEnable に true を設定します。
    true に設定するとポリゴンがZ座標でソートされて、背後のポリゴンが隠れます。
    メッシュをライトで照らすので Lighting を true に設定します。
    Mesh.Torus() でトーラスのメッシュを生成します。
        public void OnResetDevice(object sender, EventArgs e)
        {
            Device dev = (Device)sender;
    
            dev.RenderState.ZBufferEnable = true;  // Turn on the zbuffer
            dev.RenderState.Lighting = true;       // make sure lighting is enabled
            //☆ トーラスメッシュの生成
            mesh = Mesh.Torus(device, 16.0f, 40.0f, 32, 32);
        }
        
  4. ライトを設定する SetupLights() です。
    Direction がライトの座標です。
    Enabled を true に設定して下さい。
        private void SetupLights()
        {
            System.Drawing.Color col = System.Drawing.Color.White;
            Direct3D.Material mtrl = new Direct3D.Material();
            mtrl.Diffuse = col;
            mtrl.Ambient = col;
            device.Material = mtrl;
    
            device.Lights[0].Type = LightType.Directional;
            device.Lights[0].Diffuse = System.Drawing.Color.DarkTurquoise;
            //☆ ライトの座標を設定する
            device.Lights[0].Direction = new Vector3(50.0f, -40.0f, 100.0f);
            device.Lights[0].Enabled = true;    //turn it on
    
            //Ambient light is light that scatters and lights all objects evenly
            device.RenderState.Ambient = System.Drawing.Color.FromArgb(0x202020);
        }
        
  5. トーラスを自動的に回転する SetupMatrices() 関数です。
    Matrix.RotationY() でワールド座標を回転します。
        void SetupMatrices()
        {   device.Transform.World = Matrix.RotationY(Environment.TickCount / 1000.0f);
            device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f, 3.0f, -200.0f),
                new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f));
            device.Transform.Projection = Matrix.PerspectiveFovLH((float)(Math.PI/4), 1.0f, 1.0f, 500.0f);
        }
        
  6. トーラスを描画する Render() です。
    SetupLights() でライトを設定して下さい。
        private void Render()
        {
            if (device == null) return;
            if (pause)          return;
    
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.Blue, 1.0f, 0);
            //Begin the scene
            device.BeginScene();
            // Setup the lights and materials
            SetupLights();
            // Setup the world, view, and projection matrices
            SetupMatrices();
            // Draw the mesh subset
            mesh.DrawSubset(0);
            //End the scene
            device.EndScene();
            device.Present();
        }
        
  7. C/C++ でも同様のプログラムを作成しています。
    「超初心者のプログラム入門(C/C/C++)/Direct 3D で Torus を描画する」を参照して下さい。
    Direct 3D で Torus を描画する

【演習】

  1. 回転速度を遅く(速く)して描画して下さい。
  2. トーラスの色を変えて描画して下さい。
    次のコードが色の設定です。
    device.Lights[0].Diffuse = System.Drawing.Color.DarkTurquoise;
    16進数で設定するときは、次のように書きます。
    device.Lights[0].Diffuse = System.Drawing.Color.FromArgb(0xF0F020);
  3. 他のメッシュも表示してみて下さい。
  4. ティーポットを表示して下さい。
    サイズが以外と小さいので、カメラの座標を調整して下さい。
  5. WORLD 座標に加えて、カメラの座標も回転して下さい。
    device.Lights[0].Direction = new Vector3((float)Math.Cos(Environment.TickCount / 250.0f), 1.0f, (float)Math.Sin(Environment.TickCount / 250.0f));

【NOTE】

  1. 多角形のメッシュを生成する Mesh.Polygon() のパラメータの説明です。
    カリングを設定しないと裏面は描画されません。
    引数 説明
    device デバイスの指定
    50.0f 正多角形の一辺の長さ
    6 多角形の角数
  2. 直方体のメッシュを生成する Mesh.Box() のパラメータの説明です。
    引数 説明
    device デバイスの指定
    20.0f X軸方向の長さ(Width)
    40.0f Y軸方向の長さ(Height)
    60.0f Z軸方向の長さ(Depth)
  3. 円柱,円錐のメッシュを生成する Mesh.Cylinder() のパラメータの説明です。
    引数 説明
    device デバイスの指定
    30.0f 底面の半径
    0.0f 上面の半径(ゼロのとき円錐)
    80.0f 円柱の高さ
    32 回転方向の分割数
    32 Z軸方向の分割数
  4. 球メッシュを生成する Mesh.Sphere() のパラメータの説明です。
    引数 説明
    device デバイスの指定
    50.0f 球の半径
    32 Y軸回転方向の分割数
    32 Y軸方向の分割数
  5. トーラスのメッシュを生成する Mesh.Torus() のパラメータの説明です。
    引数 説明
    device デバイスの指定
    16.0f トーラスの太さ
    40.0f トーラスの外側の半径
    32 上下方向の分割数
    32 回転方向の分割数
  6. ティーポットを生成する Mesh.Teapot() のパラメータです。
    サイズは決まっていて device だけです。
    引数 説明
    device デバイスの指定
  7. マテリアルの説明は Windows Guid を参照して下さい。

超初心者のプログラム入門(C# Frame Work)