论坛风格切换
  • 687阅读
  • 5回复

RotateTriangle [复制链接]

上一主题 下一主题
离线galekkomari
 
发帖
40
重量
2000
香味
212
好感度
200
200
只看楼主 倒序阅读 使用道具 0F 发表于: 2010-09-25
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private Device device = null;

private float angle = 5;
public void InitializeGraphics()
{
//Set the presentation parameters
PresentParameters presentParameters = new PresentParameters();

presentParameters.Windowed = true;//true=windowed, false=full screened
presentParameters.SwapEffect = SwapEffect.Discard;//Flip and Copy are other possibilities

//Create the device
device = new Device(0,//the adapter
DeviceType.Hardware, //"Reference" is another possibility
this, //which control to bind this device to
CreateFlags.SoftwareVertexProcessing, //processing done by the CPU
presentParameters); //how data is present to the screen
device.DeviceResizing += new CancelEventHandler(this.CancelResize);
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);

}

//protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
//{
////clears the screen with a blue color
//device.Clear(ClearFlags.Target, System.Drawing.Color.CornflowerBlue, 1.0f, 0);

///*
// create an array that will store the vertices of a triangle
// Transformed means the position are actual screen coordinates
// Colored means that this point has a color associated with it
//*/
//CustomVertex.TransformedColored[] verts = new CustomVertex.TransformedColored[3];
//verts[0].Position = new Vector4(this.Width / 2.0f, 50.0f, 0.5f, 1.0f);
//verts[0].Color = System.Drawing.Color.Aqua.ToArgb();

//verts[1].Position = new Vector4(this.Width - (this.Width / 5.0f), this.Height - (this.Height / 5.0f), 0.5f, 1.0f);
//verts[1].Color = System.Drawing.Color.Black.ToArgb();

//verts[2].Position = new Vector4(this.Width / 5.0f, this.Height - (this.Height / 5.0f), 0.5f, 1.0f);
//verts[2].Color = System.Drawing.Color.Purple.ToArgb();

////start the rendering
//device.BeginScene();
//device.VertexFormat = CustomVertex.TransformedColored.Format;
//device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, verts);
//device.EndScene();

////rendering is complete so display it
//device.Present();
////this.Invalidate();
//}

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
//clears the screen with a blue color
device.Clear(ClearFlags.Target, System.Drawing.Color.CornflowerBlue, 1.0f, 0);
SetupCamera();


/*
create an array that will store the vertices of a triangle
Position means the coordinate are world space and has to be transformed
Colored means that this point has a color associated with it
*/
CustomVertex.PositionColored[] verts = new CustomVertex.PositionColored[3];
verts[0].Position = new Vector3(0.0f, 1.0f, 1.0f);
verts[0].Color = System.Drawing.Color.Aqua.ToArgb();

verts[1].Position = new Vector3(-1.0f, -1.0f, 1.0f);
verts[1].Color = System.Drawing.Color.Black.ToArgb();

verts[2].Position = new Vector3(1.0f, -1.0f, 1.0f);
verts[2].Color = System.Drawing.Color.Purple.ToArgb();

//start the rendering
device.BeginScene();
device.VertexFormat = CustomVertex.PositionColored.Format;
device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, verts);
device.EndScene();

//rendering is complete so display it
device.Present();
this.Invalidate();
}

private void SetupCamera()
{

//disables culling
device.RenderState.CullMode = Cull.None;

////Rotation about the z axis
//device.Transform.World = Matrix.RotationZ(angle / (float)Math.PI);
//angle += 0.1f;

//Rotation about an arbitrary axis
device.Transform.World = Matrix.RotationAxis(
new Vector3(
angle / ((float)Math.PI * 2.0f),
angle / ((float)Math.PI * 4.0f),
angle / ((float)Math.PI * 6.0f)),
angle / (float)Math.PI);
angle += 0.1f;




device.Transform.Projection = Matrix.PerspectiveFovLH(
(float)Math.PI / 4,//angle of the Field Of View
this.Width / this.Height, //similar to the aspect of a tv
1.0f, //the near plane
100.0f); //the far plane

device.Transform.View = Matrix.LookAtLH(
new Vector3(0, 0, 5.0f),//camera position
new Vector3(0,1,1), //what direction to look
new Vector3(0, 1, 0)); //what direction is up
device.RenderState.Lighting = false;//there are no lights in this scene, let each object emit its own light
}


private void CancelResize(object sender, CancelEventArgs e)
{
e.Cancel = true;
}





public Form1()
{
InitializeComponent();
}
}
}
附件: WindowsFormsApplication1.zip (66 K) 下载次数:1
20-10-07-97-13-D8-61-1D-C0-EF-07-2F-82-05-04-F6
离线夏影
发帖
1945
重量
2065
香味
4608
好感度
200
195
只看该作者 1F 发表于: 2010-09-25
这个它又是干什么的……
离线fhja
发帖
1830
重量
2120
香味
2506
好感度
200
200
只看该作者 2F 发表于: 2010-09-25
编译失败。。。。
离线nemoma

发帖
182
重量
21
香味
705
好感度
200
200
只看该作者 3F 发表于: 2010-09-26
这个它又是干什么的……
夏影 发表于 2010年9月25日 07:35


一个立体三角形转啊转

平台是C# + DirectX SDK
离线Louter
发帖
303
重量
2074
香味
368
好感度
200
200
只看该作者 4F 发表于: 2010-09-26
用WC3的一维数组也做过这种东西...
离线fhja
发帖
1830
重量
2120
香味
2506
好感度
200
200
只看该作者 5F 发表于: 2010-09-26
立体三角形
快速回复
限100 字节
克里喵子:注意回帖时随时存档,随时使用草稿功能才是上策!
 
上一个 下一个