OpenGL 学习之简单的图形用户界面和交互输入

最简单的电脑与用户之间的交互就是使用鼠标

//#include <gl/glut.h>
//#include <stdlib.h>
//#include <stdio.h>
//
//
//GLsizei winWidth = 500,winHeight = 500;
//
//void init()
//{
//	glClearColor(0.0,0.0,0.0,0.0);
//	glMatrixMode(GL_PROJECTION);
//	gluOrtho2D(0.0,200.0,0.0,200.0);
//}
//
//void display()
//{
//	glClear(GL_COLOR_BUFFER_BIT);
//	glColor3f(1.0,0.0,0.0);
//	glPointSize(5);
//}
//
//void reshape(int w,int h)
//{
//	glViewport(0,0,(GLsizei)w,(GLsizei)h);
//	glMatrixMode(GL_PROJECTION);
//	glLoadIdentity();
//	gluOrtho2D(0.0,(GLdouble)w,0.0,(GLdouble)h);
//	winHeight = w;
//	winHeight = h;
//}
//
//void plotPoint(GLint x,GLint y)
//{
//	
//	glBegin(GL_POINTS);
//		glVertex2i(x,y);
//	glEnd();
//}
//
//
//
//void mousefun(GLint button,GLint action,GLint xMouse,GLint yMouse)
//{
//	//GLint temp[2];
//	//int ch;
//	FILE *fp;
//	
//	if (button == GLUT_LEFT_BUTTON && action == GLUT_DOWN)
//	{
//		plotPoint(xMouse,winHeight-yMouse);
//		printf("the x is %d\nthe y is %d\n",xMouse,winHeight-yMouse);
//		//temp[0] = xMouse;
//		//temp[1] = yMouse;
//		
//	}
//	fp = fopen("D:\\1.txt","w+");
//	if (fp==NULL)
//	{
//		printf("cannot open this file!\n");
//	}
//	else
//	{
//		
//		fprintf(fp,"%d\n",xMouse);
//		fprintf(fp,"%d\n",winHeight-yMouse);
//		//printf("OK!");
//		
//	}
//	fclose(fp);
//	glFlush();
//	//return temp;
//}
//
void 
void show_number(GLint xMouse,GLint yMouse)
{
	if (button == GLUT_LEFT_BUTTON && action == GLUT_DOWN)
	{
		
	}
}
//int main(int argc,char **argv)
//{
//	glutInit(&argc,argv);
//	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
//	glutInitWindowPosition(200,200);
//	glutInitWindowSize(winWidth,winHeight);
//	glutCreateWindow("hello");
//	init();
//	//show_number()
//	glutDisplayFunc(display);
//	glutReshapeFunc(reshape);
//
//	glutMouseFunc(mousefun);
//
//	glutMainLoop();
//	return 0;
//}


#include <gl/glut.h>
#include <stdio.h>
#include <stdlib.h>

GLsizei winwidth = 500;
GLsizei winHeight = 500;
GLint endPtctr = 0;

class scrPt
{
public:
	GLint x,y;
};

void init()
{
	glClearColor(0.0,0.0,0.0,1.0);
	glMatrixMode(GL_PROJECTION);
	gluOrtho2D(0.0,300.0,0.0,300.0);
}

void display()
{
	glClear(GL_COLOR_BUFFER_BIT);
}

void reshape(int w,int h)
{
	glViewport(0,0,(GLsizei)w,(GLsizei)h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0.0,(GLdouble)w,0.0,(GLdouble)h);

	winwidth = w;
	winHeight = h;
}


void plotpoint(GLint x,GLint y)
{
	glPointSize(5);
	glBegin(GL_POINTS);
		glVertex2i(x,y);
	//glVertex2i(endpt2.x,endpt2.y);
	glEnd();
}
void draw_line(scrPt endpt1,scrPt endpt2)
{
	
	//glFlush();
	glLineWidth(5);
	glColor3f(1.0,0.0,0.0);
	glBegin(GL_LINES);
		
		glVertex2i(endpt1.x,endpt1.y);
		glVertex2i(endpt2.x,endpt2.y);
	glEnd();
}

void polyline(GLint button,GLint action,GLint xMouse, GLint yMouse)
{
	static scrPt endpt1,endPt2;

	if(endPtctr == 0)
	{
		if (button == GLUT_LEFT_BUTTON && action ==GLUT_DOWN)
		{
			endpt1.x = xMouse;
			endpt1.y = winHeight - yMouse;
			plotpoint(xMouse,winHeight-yMouse);
			endPtctr = 1;
		}
		else
		{
			if (button == GLUT_RIGHT_BUTTON)//quit the program
			{
				exit(0);
			}
		}
	}
	else
	{
		if (button == GLUT_LEFT_BUTTON && action == GLUT_DOWN)
		{
			endPt2.x = xMouse;
			endPt2.y = winHeight - yMouse;

			draw_line(endpt1,endPt2);
			
				

			endpt1 = endPt2;
		} 
		else
		{
			if (button == GLUT_RIGHT_BUTTON)
			{
				exit(0);
			}
		}
	}

	glFlush();
}

int main(int argc,char **argv)
{
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize(winwidth,winHeight);
	glutInitWindowPosition(200,200);
	glutCreateWindow("hello");

	init();
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);

	glutMouseFunc(polyline);

	glutMainLoop();
	return 0;
}

分成两个程序,第一个就是简单那的在屏幕上画点,第二个程序将画的点进行描绘成直线段,程序非常的简单。

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值