|
楼主 |
发表于 2011-5-3 20:51
|
显示全部楼层
====简单绘图器,头文件 SimplePaint.h===============- #include "stdafx.h"
- #define PT_FIRST_CLICK 0
- #define PT_SENCOND_CLICK 1
- #define PT_NEVER_CLICK 2
- #define ST_LINE 10
- #define ST_BLOCK 11
- #define ST_CIRCLE 12
- struct PicItem
- {
- GLint x_1,x_2,y_1,y_2;
- int PaintTag;
- int StyleTag;
- };
- typedef vector<PicItem> PicItem_vec;
- class struct_SPAINT_Global
- {
- public:
- struct_SPAINT_Global()
- {
- PaintTag = PT_NEVER_CLICK;
- StyleTag = ST_CIRCLE;
- DEG_TO_RAD = 0.017453;
- }
- public:
- GLfloat DEG_TO_RAD;
- GLsizei ww,hh;
- GLint x_1,x_2,y_1,y_2;
- int PaintTag;
- int StyleTag;
- PicItem_vec PicItemHist;
- };
- void PlotItem(int x_1,int y_1,int x_2,int y_2,int PaintTag,int StyleTag);
- void SPAINT_userReshape(GLsizei w,GLsizei h);
- void SPAINT_display();
- void SPAINT_init();
- void SPAINT_mykey(unsigned char key,int x,int y);
- void SPAINT_mymouse(int button, int state, int x,int y);
- int Simple_Paint(int argc, _TCHAR* argv[]);
- void SPAINT_myPasstiveMotion(int x,int y);
复制代码
====简单绘图器,代码文件 SimplePaint.cpp==============
- #include "stdafx.h"
- #include "SimplePaint.h"
- struct_SPAINT_Global SPAINT_Global;
- void SPAINT_userReshape(GLsizei w,GLsizei h)
- {
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D(-(GLdouble)w/2,(GLdouble)w/2,-(GLdouble)h/2,(GLdouble)h/2);
- glMatrixMode(GL_MODELVIEW);
- glViewport(0,0,w,h);
- SPAINT_Global.ww=w;
- SPAINT_Global.hh=h;
- }
- void SPAINT_display()
- {
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(0.0,0.0,0.0);
- //绘制已有的图形
- for(int i=0;i<SPAINT_Global.PicItemHist.size();++i)
- {
- PlotItem(
- SPAINT_Global.PicItemHist[i].x_1,
- SPAINT_Global.PicItemHist[i].y_1,
- SPAINT_Global.PicItemHist[i].x_2,
- SPAINT_Global.PicItemHist[i].y_2,
- SPAINT_Global.PicItemHist[i].PaintTag,
- SPAINT_Global.PicItemHist[i].StyleTag
- );
- }
- glColor3f(1.0,0.0,0.0);
- //绘制试探的图形
- PlotItem(
- SPAINT_Global.x_1,
- SPAINT_Global.y_1,
- SPAINT_Global.x_2,
- SPAINT_Global.y_2,
- PT_SENCOND_CLICK,
- SPAINT_Global.StyleTag
- );
- //抗锯齿
- glLineWidth(1.5);
- glPolygonMode(GL_FRONT,GL_LINE);
- glPolygonMode(GL_BACK,GL_LINE);
- glEnable(GL_LINE_SMOOTH|GL_POINT_SMOOTH);
- glutSwapBuffers();
- }
- void PlotItem(int x_1,int y_1,int x_2,int y_2,int PaintTag,int StyleTag)
- {
- GLfloat da;
- GLfloat R =(x_1 - x_2)*(x_1 - x_2) + (y_1 - y_2)*(y_1 - y_2);
- R = pow((float)R,(float)0.5)/2;
- switch(PaintTag)
- {
- case PT_FIRST_CLICK:
- case PT_NEVER_CLICK:
- return;
- case PT_SENCOND_CLICK:
- switch(StyleTag)
- {
- case ST_BLOCK:
- glBegin(GL_POLYGON);
- {
- glVertex2i(x_1,y_1);
- glVertex2i(x_1,y_2);
- glVertex2i(x_2,y_2);
- glVertex2i(x_2,y_1);
- }
- glEnd();
- break;
- case ST_LINE:
- glBegin(GL_LINES);
- {
- glVertex2i(x_1,y_1);
- glVertex2i(x_2,y_2);
- }
- glEnd();
- break;
- case ST_CIRCLE:
- glBegin(GL_LINE_LOOP);
- {
- for(int i=0;i<40;++i)
- {
- da= 360/40;
- glVertex2f((x_1+x_2)/2 + R*cos(i*SPAINT_Global.DEG_TO_RAD*da),
- (y_1+y_2)/2 + R*sin(i*SPAINT_Global.DEG_TO_RAD*da));
- }
- }
- glEnd();
- break;
- }
- }
- }
- void SPAINT_init()
- {
- glClearColor(1.0,1.0,1.0,1.0);
- glColor3f(1.0,1.0,1.0);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D(-1.0,1.0,-1.0,1.0);
- }
- void SPAINT_mykey(unsigned char key,int x,int y)
- {
- if (key=='L'||key=='l')
- SPAINT_Global.StyleTag = ST_LINE;
- else if(key=='B'||key=='b')
- SPAINT_Global.StyleTag = ST_BLOCK;
- else if(key=='C'||key=='c')
- SPAINT_Global.StyleTag = ST_CIRCLE;
- }
- void SPAINT_mymouse(int button, int state, int x,int y)
- {
- if(state == GLUT_DOWN && button == GLUT_LEFT_BUTTON)
- {
- if(SPAINT_Global.PaintTag == PT_FIRST_CLICK || SPAINT_Global.PaintTag == PT_NEVER_CLICK)
- {
- SPAINT_Global.x_2 = x - SPAINT_Global.ww/2;
- SPAINT_Global.y_2 = SPAINT_Global.hh/2 - y;
- SPAINT_Global.PaintTag = PT_SENCOND_CLICK;
- }
- else if(SPAINT_Global.PaintTag == PT_SENCOND_CLICK)
- {
- SPAINT_Global.x_1 = x - SPAINT_Global.ww/2;
- SPAINT_Global.y_1 = SPAINT_Global.hh/2 - y;
- PicItem tPI;
- tPI.x_1 = SPAINT_Global.x_1;
- tPI.x_2 = SPAINT_Global.x_2;
- tPI.y_1 = SPAINT_Global.y_1;
- tPI.y_2 = SPAINT_Global.y_2;
- tPI.PaintTag = SPAINT_Global.PaintTag;
- tPI.StyleTag = SPAINT_Global.StyleTag;
- SPAINT_Global.PicItemHist.push_back(tPI);
- SPAINT_Global.PaintTag = PT_FIRST_CLICK;
- }
- }
- glutPostRedisplay();
- }
- void SPAINT_myPasstiveMotion(int x,int y)
- {
- if(SPAINT_Global.PaintTag == PT_FIRST_CLICK || SPAINT_Global.PaintTag == PT_NEVER_CLICK)
- return;
- SPAINT_Global.x_1 = x - SPAINT_Global.ww/2;
- SPAINT_Global.y_1 = SPAINT_Global.hh/2 - y;
- glutPostRedisplay();
- }
- int Simple_Paint(int argc, _TCHAR* argv[])
- {
- glutInit(& argc,argv);
- glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
- glutInitWindowSize(500,500);
- glutInitWindowPosition(0,0);
- glutCreateWindow("Simple");
- //设置回调函数
- glutDisplayFunc(SPAINT_display);
- glutReshapeFunc(SPAINT_userReshape);
- glutKeyboardFunc(SPAINT_mykey);
- glutMouseFunc(SPAINT_mymouse);
- glutPassiveMotionFunc(SPAINT_myPasstiveMotion);
- //
- cout<<"C : 画圆环"<<endl;
- cout<<"L : 画直线"<<endl;
- cout<<"B : 画矩形"<<endl;
- //
- //进入主消息循环
- SPAINT_init();
- glutMainLoop();
- return 0;
- }
复制代码
|
|