GP Prac 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 22

Practical 1: - To create a Device using DirectX.

Code: -
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.Direct3D;
using Microsoft.DirectX;

namespace Practical_1
{
public partial class Form1 : Form
{
Microsoft.DirectX.Direct3D.Device device;
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);
}
public void Render()
{
device.Clear(ClearFlags.Target, Color.Orange, 0, 1);
device.Present();
}

1 SIGNATURE:____________________
private void Form1_Paint(object sender, PaintEventArgs e)
{
Render();
}
}
}

O/P: -

2 SIGNATURE:____________________
Date: -21/07/22
Practical 2: - Draw triangle using Direct3D 11
Code: -
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX;
using System.Windows.Forms;

namespace Practical2
{
public partial class Form1 : Form
{
Microsoft.DirectX.Direct3D.Device device;
public Form1()
{
InitializeComponent();
InitDevice();
}

private void InitDevice()


{
PresentParameters pp = new PresentParameters();
pp.Windowed = true;
pp.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware,
this,CreateFlags.HardwareVertexProcessing, pp);
}
public void Render()
{
CustomVertex.TransformedColored[] vertex = new
CustomVertex.TransformedColored[3];
vertex[0].Position = new Vector4(240, 110, 0, 1.0f);
3 SIGNATURE:____________________
vertex[0].Color = System.Drawing.Color.FromArgb(0, 255, 0).ToArgb();
vertex[1].Position = new Vector4(380, 420, 0, 1.0f);
vertex[1].Color = System.Drawing.Color.FromArgb(0, 0, 255).ToArgb();
vertex[2].Position = new Vector4(110, 420, 0, 1.0f);
device.Clear(ClearFlags.Target, Color.Purple, 0, 1);
device.BeginScene();
device.VertexFormat = CustomVertex.TransformedColored.Format;
device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, vertex);
device.EndScene();
device.Present();
}

private void Form1_Paint(object sender, PaintEventArgs e)


{
Render();
}
}
}

O/P: -

4 SIGNATURE:____________________
Date: -28/07/22

Practical 3: - Texturing (Texture the triangle using Direct 3D 11)


Code: -
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX;
using System.Windows.Forms;

namespace Practical_3
{
public partial class Form1 : Form
{
Microsoft.DirectX.Direct3D.Device device;
private CustomVertex.PositionTextured[] vertex = new
CustomVertex.PositionTextured[3];
private Texture texture;
public Form1()
{
InitializeComponent();
InitDevice();
}

private 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, 1000);

5 SIGNATURE:____________________
device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, 20), new
Vector3(), new Vector3(0, 1, 0));
device.RenderState.Lighting = false;
vertex[0] = new CustomVertex.PositionTextured(new Vector3(0, 0, 0), 0,
0);
vertex[1] = new CustomVertex.PositionTextured(new Vector3(5, 0, 0), 0,
1);
vertex[2] = new CustomVertex.PositionTextured(new Vector3(0, 5, 0), -
1, 1);
texture = new Texture(device, new
Bitmap("C:\\Users\\Admin\\Downloads\\Wallpapers\\4.jpg"), 0,
Pool.Managed);
}

private void Form1_Paint(object sender, PaintEventArgs e)


{
device.Clear(ClearFlags.Target, Color.Brown,
0, 1); device.BeginScene();
device.SetTexture(0, texture);
device.VertexFormat = CustomVertex.PositionTextured.Format;
device.DrawUserPrimitives(PrimitiveType.TriangleList, vertex.Length / 3,
vertex);
device.EndScene();
device.Present();
}
}
}
O/P: -

6 SIGNATURE:____________________
7 SIGNATURE:____________________
Date: -25/08/22

Practical 4: - Lightning (Programmable Diffuse Lightning using


Direct3D 11)
Code: -
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 Practical_4
{
public partial class Form1 : Form
{
private Microsoft.DirectX.Direct3D.Device device;
private CustomVertex.PositionNormalColored[] vertex = new
CustomVertex.PositionNormalColored[3];
public Form1()
{
InitializeComponent();
InitDevice();
}

private 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);
8 SIGNATURE:____________________
device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, 10), new
Vector3(), new Vector3(0, 1, 0));

device.RenderState.Lighting = true;

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.CornflowerBlue, 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();
}
}
}

O/P: -
9 SIGNATURE:____________________
10 SIGNATURE:____________________
Date: -25/08/22
Practical 5: - Specular Lightning (Programmable Spot Lightning
using Direct3D 11)
Code: -
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 Practical_5
{
public partial class Form1 : Form
{
Microsoft.DirectX.Direct3D.Device device;
Microsoft.DirectX.Direct3D.Texture
texture; Microsoft.DirectX.Direct3D.Font
font; public Form1()
{
InitializeComponent(
); InitDevice();
InitFont();
LoadTexture();
}

private void LoadTexture()


{
texture = TextureLoader.FromFile(device, "C:\\Users\\Admin\\
Downloads\\Wallpapers\\1.jpg", 400, 400, 1, 0, Format.A8B8G8R8,
Pool.Managed, Filter.Point, Filter.Point, Color.Transparent.ToArgb());
}
private void InitFont()
{

11 SIGNATURE:____________________
System.Drawing.Font f = new System.Drawing.Font("Arial", 16f,
FontStyle.Regular);
font = new Microsoft.DirectX.Direct3D.Font(device, f);
}

private void InitDevice()


{
PresentParameters pp = new
PresentParameters(); pp.Windowed = true;
pp.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware,
this, CreateFlags.HardwareVertexProcessing, pp);
}

private void Render()


{
device.Clear(ClearFlags.Target, Color.CornflowerBlue, 0, 1);
device.BeginScene();
using (Sprite s = new Sprite(device))
{
s.Begin(SpriteFlags.AlphaBlend);
s.Draw2D(texture, new Rectangle(0, 0, 0, 0), new Rectangle(0,
0, device.Viewport.Width, device.Viewport.Height), new Point(0, 0), 0f,
new Point(0, 0), Color.White);
font.DrawText(s, "Dnyanasadhana college", new Point(0, 0),
Color.Black);
s.End();
}
device.EndScene();
device.Present();
}

private void Form1_Paint(object sender, PaintEventArgs e)


{
Render();
}
}
}
O/P: -
12 SIGNATURE:____________________
13 SIGNATURE:____________________
Date: -08/09/22

Practical 6: - Roll a Ball game in Unity


Code: -
PlayerController.cs:
using UnityEngine;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour
{ Rigidbody2D rigidbody;
int coins = 0;
public Text coinText;
void Start() {
rigidbody = GetComponent<Rigidbody2D>();
}
void Update() {
if (Input.GetKey(KeyCode.UpArrow))
{ rigidbody.AddForce(Vector2.up);
}
if (Input.GetKey(KeyCode.DownArrow))
{ rigidbody.AddForce(Vector2.down);
}
if (Input.GetKey(KeyCode.LeftArrow)) {
rigidbody.AddForce(Vector2.left);
}
if (Input.GetKey(KeyCode.RightArrow)) { rigidbody.AddForce(Vector2.right);
}
}

14 SIGNATURE:____________________
private void OnTriggerEnter2D(Collider2D collision) {
collision.gameObject.SetActive(false);
coins++;
coinText.text = "Coins:" + coins.ToString();
}
}

O/P: -

15 SIGNATURE:____________________
Date: -15/09/22
Practical 7: - Coin collator game in Unity
Code: -
CoinController.cs:
using UnityEngine;
public class CoinController : MonoBehaviour
{ private void Update() {
transform.Rotate(new Vector3(0, 20, 0));
}
private void OnTriggerEnter(Collider other)
{ gameObject.SetActive(false);
}
}

SphereController.cs:
using UnityEngine;
public class SphereController : MonoBehaviour
{ Rigidbody rigidbody;
public float speed = 150;
void Start() {
rigidbody = GetComponent<Rigidbody>();
}
void FixedUpdate() {
if (Input.GetKey(KeyCode.UpArrow)) {
rigidbody.AddForce(Vector3.forward *

16 SIGNATURE:____________________
Time.fixedDeltaTime * speed);

}
if (Input.GetKey(KeyCode.DownArrow))
{ rigidbody.AddForce(Vector3.back *
Time.fixedDeltaTime * speed);
}
if (Input.GetKey(KeyCode.LeftArrow)) {
rigidbody.AddForce(Vector3.left *
Time.fixedDeltaTime * speed);
}
if (Input.GetKey(KeyCode.RightArrow)) {
rigidbody.AddForce(Vector3.right *
Time.fixedDeltaTime * speed);
}
}
}

O/P: -

17 SIGNATURE:____________________
18 SIGNATURE:____________________
Date: -22/09/22
Practical 8: - Break the wall game in Unity
Code: -
RotateCube.cs:
using UnityEngine;
public class CoinController : MonoBehaviour
{ private void Update() {
transform.Rotate(new Vector3(0, 20, 0));
}
}

SphereController.cs:
using UnityEngine;
public class SphereController : MonoBehaviour
{ Rigidbody rigidbody;
public float speed = 150;
void Start() {
rigidbody = GetComponent<Rigidbody>();
}
void FixedUpdate() {
if (Input.GetKey(KeyCode.UpArrow)) {
rigidbody.AddForce(Vector3.forward *
Time.fixedDeltaTime * speed);
}
if (Input.GetKey(KeyCode.DownArrow)) {
rigidbody.AddForce(Vector3.back *

19 SIGNATURE:____________________
Time.fixedDeltaTime * speed);
}
if (Input.GetKey(KeyCode.LeftArrow)) {
rigidbody.AddForce(Vector3.left *
Time.fixedDeltaTime * speed);
}
if (Input.GetKey(KeyCode.RightArrow)) {
rigidbody.AddForce(Vector3.right *
Time.fixedDeltaTime * speed);
}
}
}

O/P: -

20 SIGNATURE:____________________
21 SIGNATURE:____________________
22 SIGNATURE:____________________

You might also like