Revision 1325 of "कंप्यूटर ग्राफिक्स/2013-14/प्रयोगशाला 14" on hiwikiversity

== डायरेक्टएक्स9 में संक्षिप्त परिचय ==
=== अवलोकन ===
इस प्रयोगशाला में हम डायरेक्टएक्स में सरल दृश्य के बनाने और इसे के अंदर कुछ तत्वों के साथ ड्रा के विषय में कुछ सामान्य मुद्दों पर चर्चा करेंगे। कोड सी# में लिखे जाएँगे। यह बारीकी से जावा जैसा दिखता है।

=== साधारण हैलो वर्ल्ड एप्लीकेशन बनाना ===

<source lang="csharp">
[...]
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Direct3D = Microsoft.DirectX.Direct3D;

namespace FirstDirectX
{

    public class HelloDirectX : System.Windows.Forms.Form
    {
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(true);
            
            HelloDirectX frm = new     HelloDirectX();
            frm.Show();

            Application.run(frm);
        }

        [...]
    }
}
</source>

हर प्रबंधित डायरेक्टएक्स एप्लीकेशन में मूल रूप से आवश्यक:
* '''आरंभीकरण (initialization)''' विधि जहां Direct3D.Device वस्तु को जोड़ने पूरा किया है। (ओजीएल ''Init'' फ़ंक्शन के समान)
* '''रेंडरिंग (rendering)''' विधि जो दृश्य हैंडलिंग करता है। (ओजीएल डिस्प्ले फ़ंक्शन समान)
* एक '''डिवाइस रीसेट''' हैंडलिंग विधि। इसका उदाहरण है जब खिड़की विंडो के साइज में परिवर्तन (resize) किया जाता है। तभ ऐसी घटना पर यह हैंडलिंग विधि शुरू हो जाती है।

इसके अलावा हम माउस, कीबोर्ड, जॉयस्टिक आदि का उपयोग के लिये कुछ '''यूजर इंटरेक्शन (user interaction)''' की आवश्यकता होती है।

=== डायरेक्टएक्स डिवाइस के आरंभीकरण (Initialization)===
डायरेक्टएक्स डिवाइस के आरंभीकरण में आम तौर पर एक कस्टम विधि के अंदर किया जाता है। जिसे दृश्य रेंडरिंग से पहले कॉल(Call) करने की जरूरत होती है। कोड के अगले खंड से पता चलता है कि हम यह कैसे प्राप्त कर सकते हैं:

<source lang="csharp">
[...]
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Direct3D = Microsoft.DirectX.Direct3D;

namespace FirstDirectX
{

    public class HelloDirectX : System.Windows.Forms.Form
    {
        private Direct3D.Device device = null;

        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(true);
            
            HelloDirectX frm = new HelloDirectX();
            // Initialize the device.
            frm.Init();
            // Show the form.
            frm.Show();

            Application.run(frm);
        }

        public void Init()
        {
            // Do we support hardware vertex processing? If so, use it. 
            // If not, downgrade to software.
            Caps caps = Direct3D.Manager.GetDeviceCaps(Direct3D.Manager.Adapters.Default.Adapter,
                    Direct3D.DeviceType.Hardware);
            CreateFlags flags;

            if (caps.DeviceCaps.SupportsHardwareTransformAndLight)
                flags = CreateFlags.HardwareVertexProcessing;
            else
                flags = CreateFlags.SoftwareVertexProcessing;

            // Everything checks out - create a simple, windowed device.
            PresentParameters d3dpp = new PresentParameters();

            d3dpp.BackBufferFormat = Format.Unknown;
            d3dpp.SwapEffect = SwapEffect.Discard;
            d3dpp.Windowed = true;
            d3dpp.EnableAutoDepthStencil = true;
            d3dpp.AutoDepthStencilFormat = DepthFormat.D16;
            d3dpp.PresentationInterval = PresentInterval.Immediate;

            device = new Direct3D.Device(0, Direct3D.DeviceType.Hardware, this, flags, 
                    d3dpp);            
        }

        [...]
    }
}
</source>

=== रेंडरिंग (Rendering) ===
डायरेक्टएक्स दृश्य रेंडरिंग के लिए '' 'OnPaint' '' विधि अधिभावी(overriding) आवश्यक है:

<source lang="csharp">
[...]
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Direct3D = Microsoft.DirectX.Direct3D;

namespace FirstDirectX
{

    public class HelloDirectX : System.Windows.Forms.Form
    {
        private Direct3D.Device device = null;

        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(true);
            
            HelloDirectX frm = new HelloDirectX();
            // Initialize the device.
            frm.Init();
            // Show the form.
            frm.Show();

            Application.run(frm);
        }

        public void Init()
        {
            [...]        
        }

        private void Render()
        {
            // Now we can clear just view-port's portion of the buffer to black
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer,
                    Color.FromArgb(255, 0, 0, 0), 1.0f, 0);

            device.BeginScene();
                // Place your code here.
            device.EndScene();

            try
            {
                device.Present();
            }
            catch (DeviceLostException)
            {
                device.Reset(device.PresentationParameters);
            }
        }

        // We need to override this method so that we can control what is being drawn on the screen.
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            this.Render();
        }

        [...]
    }
}
</source>

=== डिवाइस रीसेट (Device resetting) ===
'''OnResetDevice''' ईवेंट हैंडलर किसी भी Direct3D संबंधित वस्तुओं को बनाने और इनिशियलाइज़ करने के लिए एक अच्छी जगह है। जो डिवाइस रीसेट के दौरान अमान्य(invalid) हो सकता है:
<source lang="csharp">
[...]
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Direct3D = Microsoft.DirectX.Direct3D;

namespace FirstDirectX
{

    public class HelloDirectX : System.Windows.Forms.Form
    {
        private Direct3D.Device device = null;

        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(true);
            
            HelloDirectX frm = new HelloDirectX();
            // Initialize the device.
            frm.Init();
            // Show the form.
            frm.Show();

            Application.run(frm);
        }

        public void Init()
        {
            [...]        
        }

        private void Render()
        {
            [...]        
        }

        // We need to override this method so that we can control what is being drawn on the screen.
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            this.Render();
        }

        // Event handler for managing the device reset instance.
        public void OnResetDevice(object sender, EventArgs e)
        {
            // Reinitialize the DX device.
            Direct3D.Device device = (Direct3D.Device)sender;

            // Reinitialize some parameters:
            device.RenderState.CullMode = Cull.None;
            device.RenderState.FillMode = FillMode.Solid;
            device.RenderState.Lighting = true;
        }
        [...]
    }
}
</source>

[[Category:HI]]