最简单的电脑与用户之间的交互就是使用鼠标
//#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;
}
分成两个程序,第一个就是简单那的在屏幕上画点,第二个程序将画的点进行描绘成直线段,程序非常的简单。