构建基础的C#3D工程
2017-05-25 21:29
274 查看
1.创建Vector4 来表示顶点数据类
2.创建一个基础的矩阵类
3。创建一个基础的三角形类
4.创建一个3D的三角形类
5。基础的表现形式
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _3DTemple { class Vector4 { public double x, y, z, w; public Vector4() { } public Vector4(double x,double y,double z,double w) { this.x = x; this.y = y; this.z = z; this.w = w; } public Vector4(Vector4 vets) { this.x = vets.x; this.y = vets.y; this.z = vets.z; this.w = vets.w; } } }
2.创建一个基础的矩阵类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _3DTemple { class Matrix4x4 { private double[,] pts; public Matrix4x4() { pts = new double[4,4]; } public double this[int i,int j] { get { return pts[i - 1,j - 1]; } set { pts[i - 1, j - 1] = value; } } //矩阵 X 矩阵 //|a1,a2,a3| |f1,f2,f3| | a1 * f1 + a2 * g1 + a3 * h1 , a1 * f2 + a2 * g2 + a3 * h2,a1 * f3 + a2 * g3 + a3 * h3| //|b1,b2,b3| |g1,g2,g3| | b1 * f1 + b2 * g1 + b3 * h1, b1 * f2 + b2 * g2 + b3 * h2,b1 * f3 + b2 * g3 + b3 * h3| //|c1,c2,c3| x |h1,h2,h3| = | c1 * f1 + c2 * g1 + c3 * h1, c1 * f2 + c2 * g2 + c3 * h2,c1 * f3 + c2 * g3 + c3 * h3| //矩阵 X N //|a1,a2,a3| |Na1,Na2,Na3| //|b1,b2,b3| |Nb1,Nb2,Nb3| //|c1,c2,c3| x N = |Nb1,Nb2,Nb3| //矩阵 和矩阵 相乘的函数 public Matrix4x4 Mul(Matrix4x4 m) { Matrix4x4 newM = new Matrix4x4(); for (int W = 1; W<=4;W++) { for (int h = 1;h<=4;h++) { for (int n = 1; n <=4;n++) { newM[W, h] += this[W, n] * m[n, h]; } } } return newM; } //矩阵和向量的乘法 //|a1,a2,a3| |f1| | a1 * f1 + b1 * g1 + c1 * h1 | //|b1,b2,b3| |g1| | a2 * f1 + b2 * g1 + c2 * g1 | //|c1,c2,c3| x |h1| = | a3 * f1 + b3 * g1 + c3 * h1 | public Vector4 Mul(Vector4 vets) { Vector4 newVets = new Vector4(); newVets.x = vets.x * this[1, 1] + vets.y * this[2, 1] + vets.z * this[3, 1] + vets.w * this[4, 1]; newVets.y = vets.x * this[1, 2] + vets.y * this[2, 2] + vets.z * this[3, 2] + vets.w * this[4, 2]; newVets.z = vets.x * this[1, 3] + vets.y * this[2, 3] + vets.z * this[3, 3] + vets.w * this[4, 3]; newVets.w = vets.x * this[1, 4] + vets.y * this[2, 4] + vets.z * this[3, 4] + vets.w * this[4, 4]; return newVets; } } }
3。创建一个基础的三角形类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; namespace MaterixTreanform { class Tringle { PointF A, B, C; public Tringle(PointF A,PointF B,PointF C) { this.A = A; this.B = B; this.C = C; } public void Draw(Graphics g) { Pen pen = new Pen(Color.Red,2); g.DrawLine(pen,A,B); g.DrawLine(pen, B, C); g.DrawLine(pen, C, A); } public void Rotate(int degress) { float angle = (float)(degress/360.0f * Math.PI); //旋转矩阵后的各个坐标系 float newX = (float)(A.X * Math.Cos(angle) - A.Y * Math.Sin(angle)); float newY = (float)(A.X * Math.Sin(angle) + A.Y * Math.Cos(angle)); A.X = newX; A.Y = newY; newX = (float)(B.X * Math.Cos(angle) - B.Y * Math.Sin(angle)); newY = (float)(B.X * Math.Sin(angle) + B.Y * Math.Cos(angle)); B.X = newX; B.Y = newY; newX = (float)(C.X * Math.Cos(angle) - C.Y * Math.Sin(angle)); newY = (float)(C.X * Math.Sin(angle) + C.Y * Math.Cos(angle)); C.X = newX; C.Y = newY; } } }
4.创建一个3D的三角形类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; namespace _3DTemple { class Tringle3D { private Vector4 a, b, c; public Vector4 A, B, C; public Tringle3D() { } public Tringle3D(Vector4 a,Vector4 b,Vector4 c) { this.A = this.a = new Vector4(a); this.B = this.b = new Vector4(b); this.C = this.c = new Vector4(c); } //三角形利用矩阵的乘法进行变换 public void Transform(Matrix4x4 m) { this.a = m.Mul(this.A); this.b = m.Mul(this.B); this.c = m.Mul(this.C); } //绘制三角形 public void Draw(Graphics g) { g.TranslateTransform(300.0f,300.0f); g.DrawLines(new Pen(Color.Red,2),get2DPointArray()); } public PointF[] get2DPointArray() { PointF[] arr = new PointF[4]; arr[0] = Get2DPointF(this.a); arr[1] = Get2DPointF(this.b); arr[2] = Get2DPointF(this.c); arr[3] = arr[0]; return arr; } //3d 的坐标转换成2D的 做透视除法 private PointF Get2DPointF(Vector4 v) { PointF p = new PointF(); p.X = (float)(v.x / v.w); p.Y = (float)(v.y / v.w); return p; } } }
5。基础的表现形式
相关文章推荐
- 【视频+工程+文档】Unity学习必备C#语法基础-20节全
- Webpack、Vue、Element UI基础工程构建
- 从零开始,教你用Webpack构建React基础工程
- 【学习笔记】3D图形核心基础精炼版-8:stage3D实战-范例工程2
- 从零开始,教你用Webpack构建React基础工程
- C# 1之外:构建于坚实基础上的新特性
- 3D数学基础之C#实现矩阵变换
- 【学习笔记】3D图形核心基础精炼版-11:stage3D实战-光照效果和范例工程3
- Aooms_基于SpringCloud的微服务基础开发平台实战_002_工程构建
- C#基础回顾:使用csc.exe构建C#应用程序
- 从零开始,教你用Webpack构建React基础工程
- webpack2.0构建Reactjs基础工程
- Direct 3D和XNA游戏开发基础(C#语言版)(奋斗的小鸟)_PDF 电子书
- Webpack构建React基础工程
- 从零开始,教你用Webpack构建React基础工程
- 从零基础认识maven工程构建
- Unity 3D开发-C#脚本语言的一些基础用法
- 【学习笔记】3D图形核心基础精炼版-7:stage3D实战-范例工程1
- C#基础学习日志===&amp;amp;amp;amp;gt;命名空间的使用&amp;amp;amp;amp;lt;===
- 《C#妹妹和Objective-C阿姨对话录》(04)垃圾回收基础--拆迁队那点事