基于x-scan扫描线的3D模型渲染算法

基于x-scan算法实现的z-buffer染色。c#语言,.net core framework 3.1运行。

模型是读取3D Max的obj模型。

x-scan算法实现:

cs 复制代码
public List<Vertex3> xscan() {
	List<Vertex3> results = new List<Vertex3>();

	SurfaceFormula formula = getFormula();
	Box rect = getBound();

	for (int y = rect.IntMinY; y <= rect.IntMaxY; y++) {

		List<double> set = new List<double>();

		for (int i = 0; i < this.vectex.Length; i++) {
			Vertex3 v1 = this.vectex[i];
			Vertex3 v2 = this.vectex[(i + 1) % vectex.Length];

			if (v1.IntY == v2.IntY || y < Math.Min(v1.Y, v2.Y) || y > Math.Max(v1.Y,v2.Y ))
				continue;

			LineFormula formula1 = new LineFormula(v1, v2);
			Double? x = formula1.getX(y);

			if (x != null) {
				bool e = false;
				foreach (double d in set) 
					if (Math.Abs(d - x.Value) < 0.005)
						e = true;
				
				if (e == false)
					set.Add(x.Value);
			}

		}

		if (set.Count > 0 && set.Count % 2 == 0) {
			set.Sort();

			for (int i=0; i<set.Count-1; i++)
				for (int x = (int)set[i]; x <= (int) set[i + 1]; x++)
					results.Add(new Vertex3(x, y, formula.getZ(x, y)));
			
		}
	}

	return results;
}

z-buffer算法:

cs 复制代码
private List<Vertex3RGB> zbuffer(Model.Model model, Bitmap canvas) {
	int?[,] deep = new int?[canvas.Width, canvas.Height];
	Vertex3RGB[,] pixels = new Vertex3RGB[canvas.Width, canvas.Height];

	for (int i = 0; i < canvas.Width; i++)
		for (int j = 0; j < canvas.Height; j++)
			deep[i, j] = null;


	foreach (Surface surface in model.Surface)
	{
		Vertex3 nv = surface.NormalVector();
		if (this.normalvector && nv.Z <= 0)
			continue;

		List<Vertex3> results = surface.xscan();

		foreach (Vertex3 v in results)
		{

			if (v.IntX < 0 || v.IntX >= canvas.Width)
				continue;
			if (v.IntY < 0 || v.IntY >= canvas.Height)
				continue;

			if (deep[v.IntX, v.IntY] == null || v.IntZ > deep[v.IntX, v.IntY])
			{
				deep[v.IntX, v.IntY] = v.IntZ;
				pixels[v.IntX, v.IntY] = new Vertex3RGB(v, brushColor.ToArgb(), nv);
			}
		}
	}


	List<Vertex3RGB> r = new List<Vertex3RGB>();
	for (int i = 0; i < pixels.GetLength(0); i++)
		for (int j = 0; j < pixels.GetLength(1); j++)
			if (pixels[i, j] != null)
				r.Add(pixels[i, j]);

	return r;
}

原始模型如下(使用法相量):

基于x-scan的zbuffer算法(使用法向量):

基于x-scan光照渲染(使用法向量):

相关推荐
kaikaile199518 分钟前
基于 MATLAB 的室内三维定位
算法
AGI前沿32 分钟前
AdamW的继任者?AdamHD让LLM训练提速15%,性能提升4.7%,显存再省30%
人工智能·算法·语言模型·aigc
Tan_Ying_Y34 分钟前
什么是垃圾回收算法 他的底层原理是什么?
算法
Xの哲學1 小时前
Linux 分区表深度技术剖析
linux·网络·算法·架构·边缘计算
写写闲篇儿1 小时前
经典算法题剖析之传递信息(三)
算法
上不如老下不如小1 小时前
2025年第七届全国高校计算机能力挑战赛初赛 Python组 编程题汇总
开发语言·python·算法
小年糕是糕手1 小时前
【C++】类和对象(二) -- 构造函数、析构函数
java·c语言·开发语言·数据结构·c++·算法·leetcode
kupeThinkPoem2 小时前
跳表有哪些算法?
数据结构·算法
前端小L2 小时前
图论专题(二十一):并查集的“工程应用”——拔线重连,修复「连通网络」
数据结构·算法·深度优先·图论·宽度优先
88号技师2 小时前
2025年9月一区SCI-孤行尺蠖觅食优化算法Solitary Inchworm Foraging-附Matlab免费代码
开发语言·算法·数学建模·matlab·优化算法