#include <gl/glut.h>
int iPointNum = 0; //已确定点的数目
int x1=0,x2=0,y1=0,y2=0; //确定的点坐标
int tempPoint[2];
int winWidth = 500, winHeight =500; //窗口的宽度和高度
void Initial(void)
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
}
void ChangeSize(int w, int h)
{
winWidth = w; winHeight = h;
glViewport(0, 0, w, h); //指定窗口显示区域
glMatrixMode(GL_PROJECTION); //设置投影参数
glLoadIdentity();
gluOrtho2D(0.0,winWidth,0.0,winHeight);
}
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
if(iPointNum ==1) {
glEnable(GL_COLOR_LOGIC_OP);
glLogicOp(GL_XOR);
glBegin(GL_LINES); //绘制直线段
glVertex2i(x1,y1);
glVertex2i(x2,y2);
glEnd();
tempPoint[0]=x2;
tempPoint[1]=y2;
iPointNum =2;
glFlush();
}
else
{
glEnable(GL_COLOR_LOGIC_OP);
glLogicOp(GL_XOR);
glBegin(GL_LINES); //绘制直线段
glVertex2i(x1,y1);
glVertex2i(tempPoint[0],tempPoint[1]);
glEnd();
glFlush();
glBegin(GL_LINES); //绘制直线段
glVertex2i(x1,y1);
glVertex2i(x2,y2);
glEnd();
tempPoint[0]=x2;
tempPoint[1]=y2;
glFlush();
}
}
void MousePlot(GLint button, GLint action, GLint xMouse, GLint yMouse)
{
if(button == GLUT_LEFT_BUTTON && action == GLUT_DOWN) {
if(iPointNum == 0 || iPointNum == 2){
iPointNum = 1;
x1 = xMouse; y1 = winHeight - yMouse;
}
else {
iPointNum = 2;
x2 = xMouse; y2 = winHeight - yMouse;
glutPostRedisplay(); //指定窗口重新绘制
}
}
if(button == GLUT_RIGHT_BUTTON && action == GLUT_DOWN){
iPointNum = 0;
glutPostRedisplay();
}
}
void PassiveMouseMove (GLint xMouse, GLint yMouse)
{
if(iPointNum == 1)
{
x2 = xMouse;
y2 = winHeight - yMouse;
glutPostRedisplay();
}
else if(iPointNum == 2)
{
x2 = xMouse;
y2 = winHeight - yMouse;
glutPostRedisplay();
}
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //使用单缓存及RGB模型
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("异或模式橡皮线");
glutDisplayFunc(Display);
glutReshapeFunc(ChangeSize); //指定窗口在整形回调函数
glutMouseFunc(MousePlot); //指定鼠标响应函数
glutPassiveMotionFunc(PassiveMouseMove); //指定鼠标移动响应函数
Initial();
glutMainLoop();
}
int iPointNum = 0; //已确定点的数目
int x1=0,x2=0,y1=0,y2=0; //确定的点坐标
int tempPoint[2];
int winWidth = 500, winHeight =500; //窗口的宽度和高度
void Initial(void)
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
}
void ChangeSize(int w, int h)
{
winWidth = w; winHeight = h;
glViewport(0, 0, w, h); //指定窗口显示区域
glMatrixMode(GL_PROJECTION); //设置投影参数
glLoadIdentity();
gluOrtho2D(0.0,winWidth,0.0,winHeight);
}
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
if(iPointNum ==1) {
glEnable(GL_COLOR_LOGIC_OP);
glLogicOp(GL_XOR);
glBegin(GL_LINES); //绘制直线段
glVertex2i(x1,y1);
glVertex2i(x2,y2);
glEnd();
tempPoint[0]=x2;
tempPoint[1]=y2;
iPointNum =2;
glFlush();
}
else
{
glEnable(GL_COLOR_LOGIC_OP);
glLogicOp(GL_XOR);
glBegin(GL_LINES); //绘制直线段
glVertex2i(x1,y1);
glVertex2i(tempPoint[0],tempPoint[1]);
glEnd();
glFlush();
glBegin(GL_LINES); //绘制直线段
glVertex2i(x1,y1);
glVertex2i(x2,y2);
glEnd();
tempPoint[0]=x2;
tempPoint[1]=y2;
glFlush();
}
}
void MousePlot(GLint button, GLint action, GLint xMouse, GLint yMouse)
{
if(button == GLUT_LEFT_BUTTON && action == GLUT_DOWN) {
if(iPointNum == 0 || iPointNum == 2){
iPointNum = 1;
x1 = xMouse; y1 = winHeight - yMouse;
}
else {
iPointNum = 2;
x2 = xMouse; y2 = winHeight - yMouse;
glutPostRedisplay(); //指定窗口重新绘制
}
}
if(button == GLUT_RIGHT_BUTTON && action == GLUT_DOWN){
iPointNum = 0;
glutPostRedisplay();
}
}
void PassiveMouseMove (GLint xMouse, GLint yMouse)
{
if(iPointNum == 1)
{
x2 = xMouse;
y2 = winHeight - yMouse;
glutPostRedisplay();
}
else if(iPointNum == 2)
{
x2 = xMouse;
y2 = winHeight - yMouse;
glutPostRedisplay();
}
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //使用单缓存及RGB模型
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("异或模式橡皮线");
glutDisplayFunc(Display);
glutReshapeFunc(ChangeSize); //指定窗口在整形回调函数
glutMouseFunc(MousePlot); //指定鼠标响应函数
glutPassiveMotionFunc(PassiveMouseMove); //指定鼠标移动响应函数
Initial();
glutMainLoop();
}