您的位置:首页 > 编程语言

使用鼠标左键点击绘制一个矩形 右键清屏 源代码

2013-10-21 18:21 357 查看
#include<windows.h>

#include <gl/glut.h>

#include <iostream>

using namespace std;

static GLint corner[2]={0,0};

static GLint numCorners = 0;

const GLint screenWidth = 640;

const GLint screenHeight = 480;

void myDisplay(void);

void myInit(void);

void myMouse(GLint button,GLint state, GLint x , GLint y);

void moveTo(GLint x, GLint y);

void LineTo(GLint x, GLint y);

int main(int argc, char ** argv){

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowSize(screenWidth,screenHeight);

glutInitWindowPosition(100,150);

glutCreateWindow("Drawing with mouse");

glutDisplayFunc(myDisplay);

myInit();

glutMouseFunc(myMouse);

glutMainLoop();

return 0;

}

void myMouse(GLint button ,GLint state, GLint x, GLint y){

if (state == GLUT_DOWN)

{

if (button == GLUT_LEFT_BUTTON)

{

if (numCorners==0)

{

moveTo(x, screenHeight-y);

glBegin(GL_POINTS);

glVertex2d(x, screenHeight-y);

glEnd();

++numCorners;

cout<<"numCorners = 0"<<" "<<numCorners<<endl;

}

else if (numCorners == 1)

{

glRecti(corner[0],corner[1],x,screenHeight-y);

numCorners = 0;

cout<<"numCorners = 1"<<" "<<numCorners<<endl;

}

glFlush();

}

if (button == GLUT_RIGHT_BUTTON)

{

glClearColor(1.0,1.0,1.0,0.0);

glClear(GL_COLOR_BUFFER_BIT);

glFlush();

}

}

}

void moveTo(GLint x, GLint y){

corner[0] = x;

corner[1] = y;

}

void myDisplay(void){

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(0.0f,0.0f,0.0f);

}

void myInit(void){

glClearColor(1.0,1.0,1.0,0.0);

glColor3f(0.0f,0.0f,0.0f);

glPointSize(2.0);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0.0,(GLdouble)screenWidth,0.0,(GLdouble)screenHeight);

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐