計算機圖形學E10——Bezier曲線

其他計算機圖形學實驗見 鏈接

#include<gl/glut.h>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const float window_width = 800, window_height = 600;
const int d = 8;

struct point
{
	float x, y;
	point() {}
	point(float xx, float yy)
		:x(xx), y(yy) {}
};
vector<point> input_vertice;
vector<point> controlpoint;

char OP;
float controlpoint_color[3] = { 1,0,0 };
float straightline_color[3] = { 0,1,0 };
float bezierline_color[3] = { 0,0,1 };

void draw_a_point(float x, float y, float color[]);
void deCasteljau();
void mymouse(int button, int state, int x, int y);
void dragmouse(int x, int y);
void keyboard(unsigned char key, int x, int y);
int getdis(int x, int y);//獲取離得最近的點

int main(int argc, char* argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowPosition(50, 50);
	glutInitWindowSize(window_width, window_height);
	glutCreateWindow("de Casteljau 繪製bezier曲線");
	cout << "鍵盤點擊p後,點擊左鍵繪製控制點" << endl;
	cout << "鍵盤點擊i後,插入控制點" << endl;
	cout << "鍵盤點擊d後,移動控制點" << endl;
	cout << "鍵盤點擊e後,點擊鼠標左鍵刪除控制點" << endl;
	cout << "按ESC退出" << endl << endl;

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0, window_width, 0, window_height);

	glClearColor(1, 1, 1, 1);
	glClear(GL_COLOR_BUFFER_BIT);

	glutMouseFunc(&mymouse);
	glutMotionFunc(&dragmouse);
	glutKeyboardFunc(&keyboard);

	glutMainLoop();
	return 0;
}

void draw_a_point(float x, float y, float color[])
{
	glPointSize(3.0f);
	glBegin(GL_POINTS);
	glColor3fv(color);
	glVertex2f(x, y);
	glEnd();
	glFlush();
}

int getdis(int x, int y)
{
	int ans = -1;
	float shortdis = 99999999;
	for (int i = 0; i < input_vertice.size(); i++)
	{
		float dis = sqrt(pow(x - input_vertice[i].x, 2) + pow(y - input_vertice[i].y, 2));
		if (dis < shortdis && dis <= d)
		{
			shortdis = dis;
			ans = i;
		}
	}
	return ans;
}

int index1 = -1;
void mymouse(int button, int state, int x, int y)
{
	if (OP == 'p')//繪製控制點
	{
		int drag_point_index = -1;
		if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
		{
			int index = getdis(x, window_height - y);
			if (index == -1)//鼠標範圍內沒有控制點,新加操作點
			{
				draw_a_point(x, window_height - y, controlpoint_color);
				point p(x, window_height - y);
				input_vertice.push_back(p);
				cout << "新輸入的控制點" << input_vertice.size() << ":(" << x << ", " << window_height - y << ")" << endl;
			}

			//把新的輸入的控制點賦值給控制點數組
			controlpoint = input_vertice;

			if (input_vertice.size() >= 2)//大於等於兩個點的時候,畫bezier曲線,控制點留給bezier函數畫
				deCasteljau();
			else				//小於兩個點的時候,只畫出控制點
			{
				glBegin(GL_LINE_STRIP);
				glColor3fv(straightline_color);
				for (int i = 0; i < input_vertice.size(); i++)
					glVertex2f(input_vertice[i].x, input_vertice[i].y);
				glEnd();
				glFlush();
			}
			//glutPostRedisplay();//
		}
	}
	if (OP == 'i')//插入控制點
	{
		//int index1 = -1;//不可以放在這裏,否則會因爲不斷刷新導致判斷up時候index恆等於-1
		if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
		{
			index1 = getdis(x, window_height - y);
			cout << "將插入新的控制點到 選中的控制點" << index1 << "前" << endl;
		}
		if (button == GLUT_LEFT_BUTTON && state == GLUT_UP && index1 != -1)
		{
			input_vertice.insert(input_vertice.begin() + index1, point(x, window_height - y));//在下標pos前插入一個元素
			//cout << input_vertice.size() << endl;
		}

		//把新的輸入的控制點賦值給控制點數組
		controlpoint = input_vertice;
		
		if (input_vertice.size() >= 2)//大於等於兩個點的時候,畫bezier曲線,控制點留給bezier函數畫
			deCasteljau();
		else				//小於兩個點的時候,只畫出控制點
		{
			glBegin(GL_LINE_STRIP);
			glColor3fv(straightline_color);
			for (int i = 0; i < input_vertice.size(); i++)
				glVertex2f(input_vertice[i].x, input_vertice[i].y);
			glEnd();
			glFlush();
		}
	}

	if (OP == 'e')
	{
		if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)//刪除控制點
		{
			int index = getdis(x, window_height - y);
			cout << "刪除點" << index << endl;
			if (index != -1)//鼠標範圍內有控制點了,可以刪除,否則不做操作
			{
				vector<point>::iterator it = input_vertice.begin();
				cout << (*(it + index)).x << " " << (*(it + index)).y << endl;
				input_vertice.erase(it + index);
			}
			else
				cout << "沒有可以刪除的點" << endl;

			glClear(GL_COLOR_BUFFER_BIT);

			//把新的輸入的控制點賦值給控制點數組
			controlpoint = input_vertice;
			//bezier曲線
			deCasteljau();
		}
	}
}

void dragmouse(int x, int y)
{
	int index = getdis(x, window_height - y);
	if (OP == 'd' && index != -1)//鼠標範圍內有控制點了,可以拖動
	{
		cout << "修改控制點" << input_vertice.size() << ":(" << input_vertice[index].x << ", " << input_vertice[index].y << ")" << endl;
		input_vertice[index].x = x;
		input_vertice[index].y = window_height - y;

		//把新的輸入的控制點賦值給控制點數組
		controlpoint = input_vertice;

		if (input_vertice.size() >= 2)//大於等於兩個點的時候,畫bezier曲線,控制點留給bezier函數畫
			deCasteljau();
		else				//小於兩個點的時候,只畫出控制點
		{
			glBegin(GL_LINE_STRIP);
			glColor3fv(straightline_color);
			for (int i = 0; i < input_vertice.size(); i++)
				glVertex2f(input_vertice[i].x, input_vertice[i].y);
			glEnd();
			glFlush();
		}
	}
}

void keyboard(unsigned char key, int x, int y)
{
	if (key == 27)
		exit(0);
	if (key == 'p')//畫點
	{
		OP = 'p';
		cout << "請點擊鼠標左鍵,開始繪製控制點以及bezier曲線" << endl;
	}
	if (key == 'd')//拖動端點
	{
		OP = 'd';
		cout << "請按住端點拖動" << endl;
	}
	if (key == 'i')//插入控制點
	{
		OP = 'i';
		cout << "請點擊一個已有的控制點按住不放,鬆開位置爲新插入頂點的位置。新插入的頂點位於點擊的頂點之前。 "<< endl;
	}
	if (key == 'e')
	{
		OP = 'e';
		cout << "鼠標左鍵點擊要刪除的點" << endl;
	}
}

void deCasteljau()
{
	vector<point> bezierpoint;
	int n = controlpoint.size();

	for (double t = 0.0; t <= 1.0; t += 0.005 / n)//保證點的數量足夠多,使曲線更加圓滑
	{
		for (int i = 1; i < n; i++)//控制每走一輪,控制點都會少 1
		{
			for (int j = 0; j < n - i; j++)//每走一輪,控制點都會少 1
			{
				if (i == 1)//迭代第一輪必須是輸入的那幾個控制點,so,,不可以只用一個controlpoint,input_vertice也有用。。。。
				{
					controlpoint[j].x = (1 - t) * input_vertice[j].x + t * input_vertice[j + 1].x;
					controlpoint[j].y = (1 - t) * input_vertice[j].y + t * input_vertice[j + 1].y;
					continue;
				}
				else
				{
					controlpoint[j].x = (1 - t) * controlpoint[j].x + t * controlpoint[j + 1].x;
					controlpoint[j].y = (1 - t) * controlpoint[j].y + t * controlpoint[j + 1].y;
				}
			}
		}
		//cout << "控制點數量=" << controlpoint.size() << endl;一直是點的數目,因爲之前直接存在這個裏面了,但是有用的只有下標爲1的纔有用
		bezierpoint.push_back(controlpoint[0]);//把最後一個在bezier曲線上的點保存起來,最後鏈接在一起即爲bezier曲線
	}

	glClear(GL_COLOR_BUFFER_BIT);//清空屏幕上上一次的bezier曲線
	//輸出新的所有的控制點
	glBegin(GL_LINE_STRIP);
	glColor3fv(straightline_color);
	for (int i = 0; i < input_vertice.size(); i++)
		glVertex2f(input_vertice[i].x, input_vertice[i].y);
	glEnd();
	glFlush();

	//畫出bezier曲線
	glLineWidth(3.0f);
	glBegin(GL_LINE_STRIP);
	glColor3fv(bezierline_color);
	for (int i = 0; i < bezierpoint.size(); i++)
		glVertex2f(bezierpoint[i].x, bezierpoint[i].y);
	glEnd();
	glFlush();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章