//根据光标位置进行画出连续的点,当按下鼠标左键为绿色,按下鼠标右键颜色为蓝色,不按则为红色。
#include "stdafx.h"
#define GLUT_DISABLE_ATEXIT_HACK
#include <GL/glut.h>
int width = 400, height = 300, x, y;
void display(){
glBegin(GL_POINTS);
glVertex2i(x, y); //Plot a point
glEnd();
glFlush();
}
void mouse(int button, int state, int x, int y){ //Upper left corner=(0,0)即左上角(0,0)
switch (button){
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
glColor3f(0, 1, 0);
else
glColor3f(1, 0, 0);
break;
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN)
glColor3f(0, 0, 1);
else
glColor3f(1, 0, 0);
break;
}
}
void motion(int mouseX, int mouseY){ //左上角(0,0)
x = mouseX;
y = height - 1 - mouseY; //Convert to image space coordinates即转换为图像空间坐标
glutPostRedisplay();
}
void passive(int mouseX, int mouseY){ //左上角(0,0)
x = mouseX;
y = height - 1 - mouseY; //转换为图形坐标空间
glutPostRedisplay();
}
void main(int argc, char * *argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowSize(width, height);
glutInitWindowPosition(200, 100);
glutCreateWindow("Trace");
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 0, 0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glutDisplayFunc(display);
glutMouseFunc(mouse); //设置鼠标事件函数
glutMotionFunc(motion); //设置鼠标移动函数
glutPassiveMotionFunc(passive); //设置鼠标被动函数
glutMainLoop();
}