【计算机图形学】01—分形树(OpenGL)(C++语言)(程序+运行结果)

计算机图形学01—分形树(OpenGL)(C++语言)

本篇用来展示使用OpenGL做的分形树。编程语言使用的是C++语言。

程序运行结果如下所示:

在这里插入图片描述

在程序中还设置了场景漫游,可以使用键盘来控制前进、后退、向左转以及向右转。

程序代码:

由于程序较多,这里展示画分形树的重要函数代码,完整代码资源可见计算机图形学—分形树(OpenGL)

void LSystemRule1()
{
	//初始化
	//std::cout << "进入LSystemRule()" << endl;
	way = "FA[+X][-&X]-%XB";
	//std::cout << "进入LSystemRule()  定义了三条规则" << endl;
	len = 30.0f;//长度
	Alphaz = 40;
	Alphay = 60;
	n = 8;
	k = 0;
	stackpointer = 0;//栈顶指针
	for (int i = 0; i < 150; i++)
	{
		stack[i].point.x = 0.0;
		stack[i].point.y = 0.0;
		stack[i].point.z = 0.0;
		stack[i].directionz = NULL;
		stack[i].directiony = NULL;
	}
	temprule = way;
	for (int i = 1; i <= n; i++)
	{
		int curlen = temprule.length();
		int pos = 0, j = 0;
		while (j < curlen)
		{
			if (temprule[j] == 'X')//迭代,将其中的F替换成文法模型
			{
				rule += way;
				j++;
				pos = rule.length() - 1;
			}
			else//保留转角
			{
				rule += temprule[j];
				pos++;
				j++;
			}
		}
		temprule = rule;
		rule.clear();
	}
	rule = temprule;//迭代好之后的文法规则

}
void LSystemRule2()
{
	//初始化
	std::cout << "进入LSystemRule()" << endl;
	/*grassway[0] = "FFF[+&FF]F[-%%%%%&F]F";
	grassway[1] = "FFF[+%FF]F[-F&[+%F]]";
	grassway[2] = "FF-[&&--F+F+F]+[%+F+F-F]";
	grassway[3] = "FF[+F+F-[++F-F++F]]F[--F-F+[F-F++F]]";
	grassway[4] = "FF[&F&F%[F%F&&F]]F[%F%F&[F%F&&F]]";*/
	gr = "FF[+%%F+F-[%F-F++F]]F[%-F-F+[F-F++F]]";
	std::cout << "进入LSystemRule()  定义了三条规则" << endl;
	lenline = 0.1f;//长度
	Alpha = 35;
	num = 2;

	stackpointer1 = 0;
	for (int i = 0; i < 150; i++)
	{
		stack1[i].point.x = 0.0;
		stack1[i].point.y = 0.0;
		stack1[i].point.z = 0.0;
		stack1[i].directiony = NULL;
		stack1[i].directionz = NULL;
	}
	//rule = way[0];
	//rule1 = grassway[rand() % 5];
	rule1 = gr;
	for (int i = 1; i <= num; i++)
	{
		int curlen = temprule1.length();
		//cout << curlen << endl;
		int pos = 0, j = 0;
		while (j < curlen)
		{
			if (temprule1[j] == 'F')
			{
				//rule1 += grassway[rand() % 5];
				rule1 += gr;
				//rule = way[0];
				j++;
				pos = rule1.length() - 1;
			}
			else
			{
				rule1 += temprule1[j];
				pos++;
				j++;
			}
		}
		temprule1 = rule1;
		//rule1.empty();
		rule1.clear();
	}
	rule1 = temprule1;

	std::cout << "出LSystemRule()" << endl;
}
void drawGrass(CVector3D p,float R,float G,float B)
{
	Node  Nextnode, Curnode;
	Curnode.point.x = p.x;
	Curnode.point.y = p.y;
	Curnode.point.z = p.z;
	Curnode.directionz = -85;
	Curnode.directiony = 0;
	int length = rule1.length();
	//std::cout << "长度 = " << length << endl;
	int  i = 0;
	//cout << rule << endl;
	glRotatef(180, 1.0, 0.0, 0.0);
	glTranslatef(0.0, 30.0, 0.0);
	glPushMatrix();
	while (i < length)
	{

		//std::cout << "rule[i] = " << rule[i] << endl;
		switch (rule1[i])
		{
		case 'F':
			Nextnode.point.x = Curnode.point.x + lenline * cos(Curnode.directionz * PI / 180) * cos(Curnode.directiony * PI / 180);
			Nextnode.point.y = Curnode.point.y + lenline * sin(Curnode.directionz * PI / 180);
			Nextnode.point.z = Curnode.point.z - lenline * cos(Curnode.directionz * PI / 180) * sin(Curnode.directiony * PI / 180);
			Nextnode.directiony = Curnode.directiony;
			Nextnode.directionz = Curnode.directionz;
			/*std::cout << "Nextnode.point.x= " << Nextnode.point.x << endl;
				std::cout << "Nextnode.point.y= " << Nextnode.point.y << endl;
				std::cout << "Nextnode.point.z= " << Nextnode.point.z << endl;*/
			glBegin(GL_LINES);
			glColor3f(R,G,B);
			glVertex3f(Curnode.point.x, Curnode.point.y, Curnode.point.z);
			glVertex3f(Nextnode.point.x, Nextnode.point.y, Nextnode.point.z);
			glEnd();
			Curnode = Nextnode;
			break;
		case '[':
			stack1[stackpointer1] = Curnode;
			stackpointer1++;
			break;
		case ']':
			Curnode = stack1[stackpointer1 - 1];
			stackpointer1--;
			break;
		case '+':
			Curnode.directionz = Curnode.directionz + Alpha;
			break;
		case '-':
			Curnode.directionz = Curnode.directionz - Alpha;
			break;
		case '&':
			Curnode.directiony = Curnode.directiony + Alpha;
			break;
		case '%':
			Curnode.directiony = Curnode.directiony - Alpha;
			break;
		default:
			;
		}
		i++;
	}
	glPopMatrix();
	//std::cout << "完成!" << endl;
}

有其他问题欢迎私信,一起讨论学习!

以下是一个简单的 C++ 分形代码,使用了 OpenGL 库来渲染图形: ```c++ #include <iostream> #include <math.h> #include <GL/glut.h> // 定义窗口大小和的参数 const int window_width = 800; const int window_height = 600; const float angle = 25.0f; const float scale = 0.7f; const int depth = 12; // 递归绘制枝的函数 void drawTree(float x1, float y1, float x2, float y2, float angle, int depth) { if (depth == 0) { return; } // 计算下一个节点的坐标 float x3 = x2 * scale + x1 * (1 - scale); float y3 = y2 * scale + y1 * (1 - scale); // 计算旋转角度 float radian = angle * M_PI / 180.0f; float cosA = cos(radian); float sinA = sin(radian); // 计算旋转矩阵 float x4 = (x3 - x2) * cosA - (y3 - y2) * sinA + x2; float y4 = (x3 - x2) * sinA + (y3 - y2) * cosA + y2; // 绘制当前节点和下一个节点之间的线段 glBegin(GL_LINES); glVertex2f(x1, y1); glVertex2f(x2, y2); glEnd(); // 递归绘制下一个节点的子 drawTree(x2, y2, x4, y4, angle, depth - 1); drawTree(x4, y4, x3, y3, angle, depth - 1); } // OpenGL 初始化函数 void init() { glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-1.0, 1.0, -1.0, 1.0); } // OpenGL 渲染函数 void display() { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f, 1.0f, 1.0f); glLineWidth(1.0f); drawTree(0, -0.8, 0, -0.6, angle, depth); glFlush(); } // 主函数 int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(window_width, window_height); glutCreateWindow("Fractal Tree"); glutDisplayFunc(display); init(); glutMainLoop(); return 0; } ``` 该代码使用递归绘制分形,从根节点开始,每个节点生成两个子节点,直到达到指定的深度。每个节点的位置和旋转角度根据上一个节点计算得出。通过 OpenGL 库绘制线段来构成的形状。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值