计算机图形学 opengl选择直线并移动,改变颜色

  • Post author:
  • Post category:其他


#include <iostream>
#include <gl/glut.h>
#include <math.h>

#include <GL/glut.h>
#include <vector>
#include <iostream>

float red = 0.0, green = 0.0, blue = 0.0;//颜色
float lines[50][4];//存储线段[x1,y1,x2,y2]。最大50
int LinesColor[50];//记录线段的颜色 -1为黑色,0为红,1为绿,2为蓝,默认为黑色
int linesNnm = 0;//存储线段的数量
bool rightdown = false;
float dx1=0, dy1=0, dx2=0, dy2=0;//临时线
bool isMove = false;
int selectLine_N = -1;
int nowColor = -1;//当前的颜色


void init(void) {
	glClearColor(1.0, 1.0, 1.0, 1.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glMatrixMode(GL_PROJECTION);
	gluOrtho2D(0.0, 200.0, 0.0, 150.0);
}


void mouseMove(GLint xMouse, GLint yMouse) {
	if (rightdown) {
		isMove = true;
		dx2 = (xMouse - 400) / 400.0;
		dy2 = (250 - yMouse) / 250.0;
		glutPostRedisplay();

	}
}


void mousePlot(GLint button, GLint action, GLint xMouse, GLint yMouse) { //鼠标事件
	
	if (button == GLUT_LEFT_BUTTON && action == GLUT_DOWN)
	{
		std::cout << "LD   ";
		float x0 = (xMouse - 400) / 400.0;
		float y0 = (250 - yMouse) / 250.0;
		float d_min = 99999999.0;
		selectLine_N = -1;
		for (int i = 0; i < linesNnm; i++) {
			float x1 = lines[i][0];
			float y1 = lines[i][1];
			float x2 = lines[i][2];
			float y2 = lines[i][3];
			float k = (y1 - y2) / (x1 - x2);
			float b = y1 - k * x1;
			float d = fabs(k * x0 - y0 + b) / sqrt(k * k + 1);
			if (d < d_min && d < 0.02/*最小距离*/ ) {
				d_min = d;
				selectLine_N = i;
			 }
		}
		if (selectLine_N >= 0) { std::cout << "选择了"; std::cout << selectLine_N; std::cout << "号线   ";	}

	}
	else if (button == GLUT_RIGHT_BUTTON && action == GLUT_DOWN) {
		std::cout << "RD:";
		std::cout << xMouse-400;
		std::cout << ":";
		std::cout << 250-yMouse;
		std::cout << "  ";
		isMove = false;
		rightdown = true;
		dx1 = (xMouse - 400)/400.0;
		dy1 = (250 - yMouse) / 250.0;
	}
	else if (button == GLUT_RIGHT_BUTTON && action == GLUT_UP) {
		std::cout<<"RU   ";
		rightdown = false;
		if (isMove) {
			lines[linesNnm][0] = dx1;
			lines[linesNnm][1] = dy1;
			lines[linesNnm][2] = dx2;
			lines[linesNnm][3] = dy2;
			LinesColor[linesNnm] = nowColor;
			linesNnm++;
		}
		dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0;

	}
	
}

//移动直线
void moveLine(GLint reductionKey, GLint xMouse, GLint yMouse)
{
	if (selectLine_N >= 0) 
	{

		switch (reductionKey)
		{
		case GLUT_KEY_UP:
			lines[selectLine_N][1] += 0.05;
			lines[selectLine_N][3] += 0.05;
			glutPostRedisplay();
			break;
		case GLUT_KEY_DOWN:
			lines[selectLine_N][1] -= 0.05;
			lines[selectLine_N][3] -= 0.05;
			glutPostRedisplay();
			break;
		case GLUT_KEY_LEFT:
			lines[selectLine_N][0] -= 0.05;
			lines[selectLine_N][2] -= 0.05;
			glutPostRedisplay();
			break;
		case GLUT_KEY_RIGHT:
			lines[selectLine_N][0] += 0.05;
			lines[selectLine_N][2] += 0.05;
			glutPostRedisplay();
			break;
		default:
			break;
		}
	}
}


void DrawLine(float x1, float y1, float x2, float y2,int i) {

	glPointSize(10.0);
	glBegin(GL_LINES);
	if(i>=0)
	switch (LinesColor[i])
	{
	case -1:
		glColor3f(0.0, 0.0, 0.0); break;
	case 0:
		glColor3f(1.0, 0.0, 0.0); break;
	case 1:
		glColor3f(0.0, 1.0, 0.0); break;
	case 2:
		glColor3f(0.0, 0.0, 1.0); break;
		default:
			break;
	}
	
	glVertex2f(x1, y1);
	glVertex2f(x2, y2);
	glEnd();
	
}

void mydisplay() {
	glColor3f(red, green, blue);
	glClear(GL_COLOR_BUFFER_BIT);
	DrawLine(dx1, dy1, dx2, dy2,-1);
	for (int i = 0; i < linesNnm; i++) {
		DrawLine(lines[i][0], lines[i][1], lines[i][2], lines[i][3],i);
	}
	glFlush();
}


//菜单
void colorOption(GLint selectedOption)
{
	switch (selectedOption) {
	case 1:
		red = 1.0;
		blue = 0;
		green = 0;
		nowColor = 0;
		if (selectLine_N >= 0) LinesColor[selectLine_N] = nowColor;
		glutPostRedisplay();
		break;
	case 2:
		red = 0;
		blue = 0;
		green = 1.0;
		nowColor = 1;
		if (selectLine_N >= 0) LinesColor[selectLine_N] = nowColor;
		glutPostRedisplay();
		break;
	case 3:
		red = 0;
		blue = 1.0;
		green = 0;
		nowColor = 2;
		if (selectLine_N >= 0) LinesColor[selectLine_N] = nowColor;
		glutPostRedisplay();
		break;
	case 4:
		red = 0;
		blue = 0;
		green = 0;
		nowColor = -1;
		if (selectLine_N >= 0) LinesColor[selectLine_N] = nowColor;
		glutPostRedisplay();
		break;
	default:
		break;
	}
	
}

void main(int argc, char** argv) {
	
	glutInit(&argc,argv);
	glutInitWindowSize(800, 500);//设置窗口大小
	glutCreateWindow("");
	glutDisplayFunc(mydisplay);
	glClearColor(1.0, 1.0, 1.0,0.0);

	glutDisplayFunc(mydisplay);
	glutMouseFunc(mousePlot);    //设置鼠标监听
	glutMotionFunc(mouseMove);
	glutCreateMenu(colorOption);  //创建菜单           
	glutAddMenuEntry("RED", 1);
	glutAddMenuEntry("GREEN", 2);
	glutAddMenuEntry("BLUE", 3);
	glutAddMenuEntry("BLACK", 4);
	glutAttachMenu(GLUT_MIDDLE_BUTTON);

	glutSpecialFunc(moveLine);
	glutMainLoop();


}





版权声明:本文为a2323247730原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。