nikkkilll

khel2

Oct 4th, 2019
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. Practical 2:
  2. AIM: Code for diffuse lightening Using Direct3D 11.
  3. Step 1:Create new project, and select “Windows Forms Application”, select .NET Framework as 2.0 in Visuals C#.Right Click on properties Click on open click on build Select Platform Target and Select x86.
  4. Step 2:Click on View Code of Form 1.
  5. Step 3:Go to Solution Explorer, right click on project name, and select Add Reference. Click on Browse and select the given .dll files which are “Microsoft.DirectX”, “Microsoft.DirectX.Direct3D”, and “Microsoft.DirectX.DirectX3DX”.
  6. Step 4:Go to Properties Section of Form, select Paint in the Event List and enter as Form1_Paint.
  7. Step 5:Copy and Paste the below given code into Form’s C# code file. Namespace must be as
  8. Program:
  9. using System;
  10. using System.Collections.Generic;
  11. using System.ComponentModel;
  12. using System.Data;
  13. using System.Drawing;
  14. using System.Text;
  15. using System.Windows.Forms;
  16. using Microsoft.DirectX;
  17. using Microsoft.DirectX.Direct3D;
  18.  
  19. namespace WindowsFormsApplication1
  20. {
  21. public partial class Form1 : Form
  22. {
  23. private Microsoft.DirectX.Direct3D.Device device;
  24. private CustomVertex.PositionNormalColored[] vertex = new CustomVertex.PositionNormalColored[3];
  25.  
  26. public Form1()
  27. {
  28. InitializeComponent();
  29. InitDevice();
  30. }
  31. public void InitDevice()
  32. {
  33. PresentParameters pp = new PresentParameters(); pp.Windowed = true;
  34. pp.SwapEffect = SwapEffect.Discard;
  35. device = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, pp);
  36. device.Transform.Projection = Matrix.PerspectiveFovLH(3.14f / 4, device.Viewport.Width / device.Viewport.Height, 1f, 1000f);
  37. device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, 10), new Vector3(), new Vector3(0, 1, 0));
  38. device.RenderState.Lighting = false;
  39. vertex[0] = new CustomVertex.PositionNormalColored(new Vector3(0, 1, 1), new Vector3(1, 0, 1), Color.Red.ToArgb());
  40. vertex[1] = new CustomVertex.PositionNormalColored(new Vector3(-1, -1, 1), new Vector3(1, 0, 1), Color.Red.ToArgb());
  41. vertex[2] = new CustomVertex.PositionNormalColored(new Vector3(1, -1, 1), new Vector3(-1, 0, 1), Color.Red.ToArgb());
  42. device.RenderState.Lighting = true; device.Lights[0].Type = LightType.Directional; device.Lights[0].Diffuse = Color.Plum; device.Lights[0].Direction = new Vector3(0.8f, 0, -1); device.Lights[0].Enabled = true;
  43. }
  44.  
  45. public void Render()
  46. {
  47. device.Clear(ClearFlags.Target, Color.DarkOrange, 1, 0); device.BeginScene();
  48. device.VertexFormat = CustomVertex.PositionNormalColored.Format;
  49.  
  50.  
  51.  
  52.  
  53. device.DrawUserPrimitives(PrimitiveType.TriangleList, vertex.Length / 3, vertex);
  54.  
  55.  
  56. device.EndScene();
  57. device.Present();
  58. }
  59.  
  60. private void Form1_Paint(object sender, PaintEventArgs e)
  61. {
  62. Render();
  63. }
  64.  
  65. private void Form1_Load(object sender, EventArgs e)
  66. {
  67.  
  68. }
  69. }
  70. }
Add Comment
Please, Sign In to add comment