1: #include <GL/glut.h>
2: int ww=600,wh=400;
3: int first=0;
4: int xi,yi,xf,yf;
5: void drawLine(int x1,int y1,int x2,int y2)
6: {
7: glClear(GL_COLOR_BUFFER_BIT);
8: glLineWidth(5.0);
9: glBegin(GL_LINES);
10: glVertex2i(x1,y1);
11: glVertex2i(x2,y2);
12: glEnd();
13: glFlush();
14: }
15: void display()
16: {
17: glClearColor(0.2, 0.4, 0.0, 1.0);
18: glColor3f(0.7, 0.4, 0.0);
19: glClear(GL_COLOR_BUFFER_BIT);
20: glFlush();
21: }
22: void mouse(int btn,int state,int x,int y)
23: {
24: if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
25: {
26: switch(first)
27: {
28: case 0:
29: xi=x;
30: yi=wh-y;
31: first=1;
32: break;
33: case 1:
34: xf=x;
35: yf=wh-y;
36: drawLine(xi,yi,xf,yf);
37: first=0;
38: break;
39: }
40: }
41: }
42: void myinit()
43: {
44: glViewport(0,0,ww,wh);
45: glMatrixMode(GL_PROJECTION);
46: glLoadIdentity();
47: gluOrtho2D(0.0,(GLdouble)ww,0.0,(GLdouble)wh);
48: glMatrixMode(GL_MODELVIEW);
49: }
50: int main(int argc,char** argv)
51: {
52: glutInit(&argc,argv);
53: glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
54: glutInitWindowSize(ww,wh);
55: glutCreateWindow("Draw Line With Mouse Click");
56: glutDisplayFunc(display);
57: myinit();
58: glutMouseFunc(mouse);
59: glutMainLoop();
60: return 0;
61: }
Thursday, May 15, 2014
Opengl,C++ : Draw Line With Mouse Click
Subscribe to:
Post Comments (Atom)
hi
ReplyDelete