Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Practical 2:
- AIM: Code for diffuse lightening Using Direct3D 11.
- 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.
- Step 2:Click on View Code of Form 1.
- 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”.
- Step 4:Go to Properties Section of Form, select Paint in the Event List and enter as Form1_Paint.
- Step 5:Copy and Paste the below given code into Form’s C# code file. Namespace must be as
- Program:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using Microsoft.DirectX;
- using Microsoft.DirectX.Direct3D;
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- private Microsoft.DirectX.Direct3D.Device device;
- private CustomVertex.PositionNormalColored[] vertex = new CustomVertex.PositionNormalColored[3];
- public Form1()
- {
- InitializeComponent();
- InitDevice();
- }
- public void InitDevice()
- {
- PresentParameters pp = new PresentParameters(); pp.Windowed = true;
- pp.SwapEffect = SwapEffect.Discard;
- device = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, pp);
- device.Transform.Projection = Matrix.PerspectiveFovLH(3.14f / 4, device.Viewport.Width / device.Viewport.Height, 1f, 1000f);
- device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, 10), new Vector3(), new Vector3(0, 1, 0));
- device.RenderState.Lighting = false;
- vertex[0] = new CustomVertex.PositionNormalColored(new Vector3(0, 1, 1), new Vector3(1, 0, 1), Color.Red.ToArgb());
- vertex[1] = new CustomVertex.PositionNormalColored(new Vector3(-1, -1, 1), new Vector3(1, 0, 1), Color.Red.ToArgb());
- vertex[2] = new CustomVertex.PositionNormalColored(new Vector3(1, -1, 1), new Vector3(-1, 0, 1), Color.Red.ToArgb());
- 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;
- }
- public void Render()
- {
- device.Clear(ClearFlags.Target, Color.DarkOrange, 1, 0); device.BeginScene();
- device.VertexFormat = CustomVertex.PositionNormalColored.Format;
- device.DrawUserPrimitives(PrimitiveType.TriangleList, vertex.Length / 3, vertex);
- device.EndScene();
- device.Present();
- }
- private void Form1_Paint(object sender, PaintEventArgs e)
- {
- Render();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- }
- }
Add Comment
Please, Sign In to add comment