Difference between revisions 1326 and 1327 on hiwikiversity

{{कंप्यूटर ग्राफिक्स -- 2013-2014 -- info.uvt.ro/पेज हैडर}}

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

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

<source lang="csharp">
[...]
(contracted; show full)        [...]
    }
}
</source>

=== डायरेक्टएक्स में प्रोजेक्शन और मॉडलव्यू ===


=== वर्टिस (Vertices) का प्रयोग ===

=== लाइट्स, मैटेरियल्स और टेक्सचर्स ===

=== दृश्य गोला गोले(spheres) को जोड़ना ===

=== ब्लेंडिंग (Blending) ===<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()
        {
            // Set the projection to be Perspective.
            device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI/4,
                    this.Width/this.Height, 1f, 50f);
            // Orient the camera.
            device.Transform.View = Matrix.LookAtLH(new Vector3(0,0,-30), new Vector3(0,0,0), 
                    new Vector3(0,1,0));
            // Reset the world matrix to Identity.
            device.Transform.World = Matrix.Identity;
            [...]        
        }

        // 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)
        {
            [...]
        }
        [...]
    }
}
</source>


<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()
        {
            // Set the projection to be Perspective.
            device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI/4,
                    this.Width/this.Height, 1f, 50f);
            // Orient the camera.
            device.Transform.View = Matrix.LookAtLH(new Vector3(0,0,-30), new Vector3(0,0,0), 
                    new Vector3(0,1,0));
            // Reset the world matrix to Identity.
            device.Transform.World = Matrix.Identity;

            // Create a new matrix which will store our transformations.
            Matrix customMatrix = Matrix.Identity;
            // Apply a translation to our matrix.
            Matrix translation = Matrix.Translation(10, 10, 10);
            // Apply a rotation on the OX axis.             
            Matrix rotation = Matrix.RotationX(45);
            // Apply scaling.                
            Matrix scaling = Matrix.Scaling(0.5f, 0.5f, 0.5f);
            // Build the composite matrix.
            customMatrix = translation * rotation * scaling;

            // Set the world matrix to be our new custom matrix.
            device.Transform.World = customMatrix;
            [...]        
        }

        // 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)
        {
            [...]
        }
        [...]
    }
}
</source>


=== वर्टिस (Vertices) का प्रयोग ===

<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()
        {
            [...]
            // Create a list of PositionColored vertices and define their position and color.
            CustomVertex.PositionColored[] vertices = new CustomVertex.PositionColored[3];
            vertices[0].Position = new Vector3(0f, 0f, 0f);
            vertices[0].Color = Color.Red.ToArgb();
            vertices[1].Position = new Vector3(10f, 0f, 0f);
            vertices[1].Color = Color.Green.ToArgb();
            vertices[2].Position = new Vector3(5f, 10f, 0f);
            vertices[2].Color = Color.Yellow.ToArgb();

            // 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();
                // Let the device know your format has changed.
                device.VertexFormat = CustomVertex.PositionColored.Format;
                // Draw the triangle.
                device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, vertices);
            device.EndScene();
            [...]
        }

        // 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)
        {
            [...]
        }
        [...]
    }
}
</source>

<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)
        {
            [...]
        }

        private VertexBuffer vb = null;
        private CustomVertex.PositionColored[] vertices;

        private void CreateVertexBuffer(Direct3D.Device device, int noVertices)
        {
            vb = new VertexBuffer(typeof(CustomVertex.PositionColored),
                 noVertices, device, Usage.Dynamic | Usage.WriteOnly,
                 CustomVertex.PositionColored.Format, Pool.Default);
            vertices = new CustomVertex.PositionColored[noVertices];
            for (int i=0; i<noVertices; i++)
            {
                vertices[0].Position = new Vector3(i, i, i);
                vertices[0].Color = Color.Red.ToArgb();
            }
            vb.SetData(vertices, 0, LockFlags.None);
        }

        private void UseVertexBuffer(Direct3D.Device device, int noVertices)
        {
            device.SetStreamSource(0, vb, 0);
            device.VertexFormat = CustomVertex.PositionColored.Format;
            device.DrawUserPrimitives(PrimitiveType.LineList, noVertices/2 
                /*primitive count - 2 vertice per line implies a noVertices/2 primitives*/, 
                vertices);
        }
        [...]
    }
}
</source>




=== लाइट्स, मैटेरियल्स और टेक्सचर्स ===
<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()
        {
            [...]
            this.SetUpMaterial(device);
            this.SetUpTexture(device);
            this.SetUpLights(device);
            [...]
        }

        private void Render()
        {
            [...]
            device.Transform.World = customMatrix;
            // Select material to be applied.
            device.Material = material;
            // Select texture to be applied.
            device.SetTexture(0, texture);

            // Draw object.
            [...]
        }

        // 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)
        {
            [...]
        }

        private void SetUpLights(Direct3D.Device device)
        {
            device.Lights[0].Type = LightType.Point; // LightType.Directional; LightType.Spot;
            // device.Lights[0].Ambient = Color.White;
            device.Lights[0].Specular = Color.White;
            device.Lights[0].Diffuse = Color.White;
            device.Lights[0].Position = new Vector3(x, y, z);
            // device.Lights[0].Direction = new Vector3(x, y, z);
            device.Lights[0].Attenuation1 = 0.001f;
            device.Lights[0].Range = 10000f;
            device.Lights[0].Enabled = true;
        }

                private Material material = null;

        private void SetUpMaterial(Direct3D.Device device, String fileName)
        {
            material = new Material();
            material.Ambient = Color.White;
            material.Diffuse = Color.White;
            material.Specular = Color.White;
        }

        private Texture texture = null;
        
        private void SetUpTexture(Direct3D.Device device)
        {
            texture = TextureLoader.FromFile(device, fileName);
        }
    }
}
</source>


=== दृश्य गोला गोले(spheres) को जोड़ना ===

<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);
        }

        private Mesh myMesh = null;

        public void Init()
        {
            [...]
            this.SetUpMaterial(device);
            this.SetUpTexture(device);
            this.SetUpLights(device);
            // Create a sphere mesh.
            myMesh = Mesh.Sphere(device, 3.0f, 60, 20);
            [...]
        }

        private void Render()
        {
            [...]
            device.Transform.World = customMatrix;
            // Select material to be applied.
            device.Material = material;

            // Apply texture on the sphere.

            // Draw the sphere mesh.
            myMesh.DrawSubset(0);

            [...]
        }

        // 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)
        {
            [...]
        }
    }
}
</source>


=== ब्लेंडिंग (Blending) ===

<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);
        }

        private Mesh myMesh = null;

        public void Init()
        {
            [...]
            this.SetUpMaterial(device);
            this.SetUpTexture(device);
            this.SetUpLights(device);
            // Create a sphere mesh.
            myMesh = Mesh.Sphere(device, 3.0f, 60, 20);
            [...]
        }

        private void Render()
        {
            [...]
            device.Transform.World = customMatrix;
            // Select material to be applied.
            device.Material = material;

            // Enable alpha blending test.
            device.RenderState.AlphaBlendEnable = true;
            device.RenderState.AlphaTestEnable = true;
            device.RenderState.SourceBlend = Blend.SourceAlpha;
            device.RenderState.DestinationBlend = Blend.DestinationAlpha;

            device.RenderState.ReferenceAlpha = 0x08;
            device.RenderState.AlphaFunction = Compare.GreaterEqual;

            // Select texture to be applied.
            device.SetTexture(0, texture);
            // Draw the sphere mesh.
            myMesh.DrawSubset(0);

            // Disable alpha blending test.
            device.RenderState.AlphaBlendEnable = false;
            device.RenderState.AlphaTestEnable = false;

            [...]
        }

        // 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)
        {
            [...]
        }
    }
}
</source>

=== लिंक ===
* [http://www.riemers.net/eng/Tutorials/DirectX/Csharp/series1.php प्रबंधित डायरेक्टएक्स9 ट्यूटोरियल]

{{कंप्यूटर ग्राफिक्स -- 2013-2014 -- info.uvt.ro/पेज फ़ुटर}}

[[Category:HI]]