0% found this document useful (0 votes)
28 views2 pages

Code

The document defines a Form2 class that inherits from the Form class and contains code to render a 3D textured triangle using DirectX. It initializes a Direct3D device, creates vertex and texture objects, sets up the projection and view matrices. In the Paint event, it clears the render target, begins and ends the scene, sets the texture, draws the triangle primitive using the vertex buffer and presents the back buffer to the screen.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views2 pages

Code

The document defines a Form2 class that inherits from the Form class and contains code to render a 3D textured triangle using DirectX. It initializes a Direct3D device, creates vertex and texture objects, sets up the projection and view matrices. In the Paint event, it clears the render target, begins and ends the scene, sets the texture, draws the triangle primitive using the vertex buffer and presents the back buffer to the screen.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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 WindowsFormsApplication4
{

public partial class Form2 : Form


{
private Microsoft.DirectX.Direct3D.Device device;
private CustomVertex.PositionTextured[] v = new CustomVertex.PositionTextured[6];
private Texture texture;
public Form2()
{
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)


{

PresentParameters obj = new PresentParameters();


obj.Windowed = true;
obj.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this,
CreateFlags.HardwareVertexProcessing, obj);
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, 20), new Vector3(0,
0, 10), new Vector3(0, 1, 2));
device.RenderState.Lighting = false;
v[0] = new CustomVertex.PositionTextured(new Vector3(0, 2, 1), 0, 0);
v[1] = new CustomVertex.PositionTextured(new Vector3(0, -2, 1), 0, 1);
v[2] = new CustomVertex.PositionTextured(new Vector3(2, -2, 1),-1,1);
v[3] = new CustomVertex.PositionTextured(new Vector3(2, -2, 1), -0, 0);
v[4] = new CustomVertex.PositionTextured(new Vector3(2, 2, 1), 0, 1);
v[5] = new CustomVertex.PositionTextured(new Vector3(0, 2, 1), -1, 1);
texture = new Texture(device, new Bitmap("D:\\design\\p2.jpg"), 0,
Pool.Managed);
}

private void Form2_Paint(object sender, PaintEventArgs e)


{
device.Clear(ClearFlags.Target, Color.CornflowerBlue, 1, 0);
device.BeginScene();
device.SetTexture(0, texture);
device.VertexFormat = CustomVertex.PositionTextured.Format;
device.DrawUserPrimitives(PrimitiveType.TriangleList, v.Length / 3, v);
device.EndScene();
device.Present();

}
}
}

You might also like