Skip to content

Commit 027e73a

Browse files
MVP变换+简单插值(未经过透视插值矫正的)
0 parents  commit 027e73a

24 files changed

+1609
-0
lines changed

App.config

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
5+
</startup>
6+
</configuration>

Color01.cs

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Drawing;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace SortRenderWithCSharp {
9+
10+
/// <summary>
11+
/// RGB值被归一到01区间的Color类
12+
/// </summary>
13+
public struct Color01 {
14+
15+
float r, g, b, a;
16+
17+
public float R { get => MathF.Clamp01(r); set => r = value; }
18+
public float G { get => MathF.Clamp01(g); set => g = value; }
19+
public float B { get => MathF.Clamp01(b); set => b = value; }
20+
public float A { get => MathF.Clamp01(a); set => a = value; }
21+
22+
public static Color01 White = new Color01(1,1,1,1);
23+
public static Color01 Black = new Color01(0,0,0,1);
24+
25+
26+
public Color01(float r,float g,float b,float a) {
27+
this.r = r;
28+
this.g = g;
29+
this.b = b;
30+
this.a = a;
31+
}
32+
33+
public static Color01 operator +(Color01 left,Color01 right) {
34+
return new Color01(
35+
left.r + right.r,
36+
left.g + right.g,
37+
left.b + right.b,
38+
left.a + right.a
39+
);
40+
}
41+
42+
public static Color01 operator -(Color01 left,Color01 right) {
43+
return new Color01(
44+
left.r - right.r,
45+
left.g - right.g,
46+
left.b - right.b,
47+
left.a - right.a);
48+
}
49+
50+
/// <summary>
51+
/// 颜色的乘法大多用于光照,他跟向量的乘法定义不同,
52+
/// 这里直接是分量相乘
53+
/// </summary>
54+
/// <param name="left"></param>
55+
/// <param name="right"></param>
56+
/// <returns></returns>
57+
public static Color01 operator *(Color01 left,Color01 right) {
58+
return new Color01(
59+
left.r * right.r,
60+
left.g * right.g,
61+
left.b * right.b,
62+
left.a * right.a
63+
);
64+
}
65+
66+
/// <summary>
67+
/// 颜色数乘
68+
/// </summary>
69+
/// <param name="left"></param>
70+
/// <param name="value"></param>
71+
/// <returns></returns>
72+
public static Color01 operator *(Color01 left,float value) {
73+
return new Color01(
74+
left.r * value,
75+
left.g * value,
76+
left.b * value,
77+
left.a * value
78+
);
79+
}
80+
81+
public static Color01 operator /(Color01 left,float value) {
82+
return new Color01(
83+
left.r / value,
84+
left.g / value,
85+
left.b / value,
86+
left.a / value
87+
);
88+
}
89+
90+
public static Color01 LerpColor(Color01 left,Color01 right,float t) {
91+
return left + (right - left) * t;
92+
}
93+
94+
// 当前使用的Color变到Winform所用的Color类
95+
public Color ToColor() {
96+
return Color.FromArgb((int)(255*A),(int)(255*R),(int)(255*G),(int)(255*B));
97+
}
98+
99+
// 将Color类转化为Color01类
100+
public static Color01 FromColor(Color color) {
101+
return new Color01(
102+
r:color.R/255,
103+
g:color.G/255,
104+
b:color.B/255,
105+
a:color.A/255
106+
);
107+
}
108+
109+
public override string ToString() {
110+
return string.Format("(r:{0},g:{1},b:{2},a:{3}",r,g,b,a);
111+
}
112+
}
113+
}

MathF.cs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Drawing;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace SortRenderWithCSharp {
9+
class MathF {
10+
/// <summary>
11+
/// 快速平方根
12+
/// </summary>
13+
/// <param name="x"></param>
14+
/// <returns></returns>
15+
public static float Sqrt(float x) {
16+
unsafe {
17+
float a = x;
18+
uint i = *(uint*)&x;
19+
i = (i + 0x3f76cf62) >> 1;
20+
x = *(float*)&i;
21+
x = (x + a / x) * 0.5f;
22+
return x;
23+
}
24+
}
25+
26+
public static float LerpFloat(float v1,float v2,float t) {
27+
return v1 + (v2 - v1) * t;
28+
}
29+
30+
public static float Clamp01(float f) {
31+
if (f >= 1f)
32+
return 1f;
33+
else if (f <= 0)
34+
return 0;
35+
36+
return f;
37+
}
38+
}
39+
}

Matrix4x4.cs

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace SortRenderWithCSharp {
8+
/// <summary>
9+
/// 4x4的矩阵对象
10+
/// </summary>
11+
public class Matrix4x4 {
12+
// 矩阵内部值
13+
public float[,] value = new float[4,4];
14+
15+
/// <summary>
16+
/// 将当前矩阵设为单位矩阵
17+
/// </summary>
18+
public void Identity() {
19+
for (int i=0;i<4;i++) {
20+
value[i, i] = 1;
21+
}
22+
}
23+
24+
/// <summary>
25+
/// 矩阵乘法
26+
/// </summary>
27+
/// <param name="right"></param>
28+
/// <returns></returns>
29+
public static Matrix4x4 operator *(Matrix4x4 left,Matrix4x4 right) {
30+
Matrix4x4 matrix = new Matrix4x4();
31+
32+
for (int i=0;i<4;i++) {
33+
for (int j=0;j<4;j++) {
34+
35+
// 初始化
36+
matrix.value[i, j] = 0;
37+
38+
// (i,j)表示矩阵第i行乘于另一个矩阵的第j列
39+
for (int k=0;k<4;k++) {
40+
matrix.value[i, j] += left.value[i, k] * right.value[k,j];
41+
}
42+
43+
}
44+
}
45+
46+
return matrix;
47+
}
48+
49+
/// <summary>
50+
/// 矩阵数乘
51+
/// </summary>
52+
/// <param name="left"></param>
53+
/// <param name="value"></param>
54+
/// <returns></returns>
55+
public static Matrix4x4 operator *(Matrix4x4 left,float value) {
56+
57+
Matrix4x4 matrix = new Matrix4x4() ;
58+
59+
for (int i=0;i<4;i++) {
60+
for (int j=0;j<4;j++) {
61+
matrix.value[i,j] = left.value[i,j] *value;
62+
}
63+
}
64+
65+
return matrix;
66+
}
67+
68+
/// <summary>
69+
/// 矩阵与向量相乘,这里将向量作为列矩阵看待,所以这里相当于进行了一次矩阵乘法
70+
/// </summary>
71+
/// <param name="left"></param>
72+
/// <param name="right"></param>
73+
/// <returns></returns>
74+
public static Vector3 operator *(Matrix4x4 left,Vector3 right) {
75+
return new Vector3 {
76+
X = left.value[0, 0] * right.X + left.value[0, 1] * right.Y + left.value[0, 2] * right.Z + left.value[0, 3] * right.W,
77+
Y = left.value[1, 0] * right.X + left.value[1, 1] * right.Y + left.value[1, 2] * right.Z + left.value[1, 3] * right.W,
78+
Z = left.value[2, 0] * right.X + left.value[2, 1] * right.Y + left.value[2, 2] * right.Z + left.value[2, 3] * right.W,
79+
W = left.value[3, 0] * right.X + left.value[3, 1] * right.Y + left.value[3, 2] * right.Z + left.value[3, 3] * right.W,
80+
};
81+
}
82+
}
83+
}

Program.cs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Windows.Forms;
7+
namespace SortRenderWithCSharp {
8+
class Program {
9+
/// <summary>
10+
/// 应用程序的主入口点。
11+
/// </summary>
12+
[STAThread]
13+
static void Main() {
14+
Application.EnableVisualStyles();
15+
Application.SetCompatibleTextRenderingDefault(false);
16+
Application.Run(new SoftRenderForm());
17+
}
18+
}
19+
}

Properties/AssemblyInfo.cs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// 有关程序集的一般信息由以下
6+
// 控制。更改这些特性值可修改
7+
// 与程序集关联的信息。
8+
[assembly: AssemblyTitle("SortRenderWithCSharp")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("Microsoft")]
12+
[assembly: AssemblyProduct("SortRenderWithCSharp")]
13+
[assembly: AssemblyCopyright("Copyright © Microsoft 2019")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// 将 ComVisible 设置为 false 会使此程序集中的类型
18+
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
19+
//请将此类型的 ComVisible 特性设置为 true。
20+
[assembly: ComVisible(false)]
21+
22+
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
23+
[assembly: Guid("334284c7-c848-4000-925c-7e63acd6427b")]
24+
25+
// 程序集的版本信息由下列四个值组成:
26+
//
27+
// 主版本
28+
// 次版本
29+
// 生成号
30+
// 修订号
31+
//
32+
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
33+
// 方法是按如下所示使用“*”: :
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

0 commit comments

Comments
 (0)