1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <windows.h>
#include <iostream.h>
#include <math.h>
#include <gl/gl.h>
#include <gl/glut.h> // (or others, depending on the system in use)
 
#define     PI              3.1415926
#define     Window_Width    400
#define     Window_Height   400
#define     DOT             0
#define     RUBBER          1
#define     SOLID           2
 
int         position_x = 0;
int         position_y = 0;
int         width,  height;
int         point[1000][2];
int         num = 0;
 
struct Point
{
    float   x;
    float   y;
};
Point   left_down, right_up;
Point   start_point, end_point;
 
int     draw_type;
int     redraw;
 
void init (void) // 초기화설정, 상수값 설정
{
    glClearColor (1.0, 1.0, 1.0, 0.0) ; // Set display-window color to white.
    glMatrixMode (GL_PROJECTION); // Set projection parameters.
    gluOrtho2D (-200, 200, -200, 200); // 관측공간 설정
    width = Window_Width;
    height = Window_Height;
    draw_type = DOT; // 점선이냐 실선이냐
    redraw = 0;
}
 
void MyReshape(int w, int h)
{
    glViewport(position_x, position_y, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D (-200, 200, -200, 200);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
 
 
void Draw_Rectangle(void) //사각형 그리기
{
     
    if (draw_type == RUBBER)
    {
        glEnable(GL_LINE_STIPPLE);
        glLineStipple(1, 0x0101); // 점선의 모양 결정
        // 0x0101 은 16진수 숫자로써 점선의 패턴을 만들어내는 코드 이다
        glBegin(GL_LINE_LOOP);
        glVertex2f(left_down.x, left_down.y);
        glVertex2f(right_up.x, left_down.y);
        glVertex2f(right_up.x, right_up.y);
        glVertex2f(left_down.x, right_up.y);
        glEnd();
    }          
    else if ( draw_type == SOLID)
    {
        glDisable(GL_LINE_STIPPLE);
        glRectf(left_down.x, left_down.y, right_up.x, right_up.y);
    };
}
 
 
void RenderScene (void) // 전체적인 그림 그리기
 {
    glClear (GL_COLOR_BUFFER_BIT);
     
    glDisable(GL_LINE_STIPPLE);
    glBegin (GL_LINES);
    glColor3f(1.0, 0.0, 0.0);
    glVertex2f (-200, 0);
    glVertex2f ( 200, 0);
     
    glColor3f(0.0, 0.0, 1.0);
    glVertex2f (0, -200);
    glVertex2f (0, 200);
    glEnd();
     
    Draw_Rectangle();
     
     
    glFlush ();
    glutSwapBuffers();
}
 
void Check_Position(void) // 마우스좌표
{
    if ( start_point.x <= end_point.x )
    {
        if ( start_point.y <= end_point.y )
        {
            left_down.x =   start_point.x;
            left_down.y =   start_point.y;
            right_up.x =    end_point.x;
            right_up.y =    end_point.y;
        }
        else {
            left_down.x =   start_point.x;
            left_down.y =   end_point.y;
            right_up.x =    end_point.x;
            right_up.y =    start_point.y;
        }
    }
    else {
        if ( start_point.y <= end_point.y )
        {
            left_down.x =   end_point.x;
            left_down.y =   start_point.y;
            right_up.x =    start_point.x;
            right_up.y =    end_point.y;
        }
        else
        {
            left_down.x =   end_point.x;
            left_down.y =   end_point.y;
            right_up.x =    start_point.x;
            right_up.y =    start_point.y;
        }
    }
}
 
void mouse( int button, int state, int x, int y) // 마우스 왼쪽버튼과 오른쪽 실행시
 {
    if ( draw_type == DOT && button == GLUT_LEFT_BUTTON && state == GLUT_DOWN )
    {
        start_point.x = x - Window_Width/2;
        start_point.y = Window_Height/2 - y;
        draw_type = RUBBER; // 사각형 그리기 시작
        if ( redraw == 1 )
        {
            end_point.x = start_point.x;
            end_point.y = start_point.y;
            redraw = 0;
            Check_Position();
        }
    }
    if ( draw_type == RUBBER && button == GLUT_LEFT_BUTTON && state == GLUT_UP )
    {
        draw_type = SOLID; // 왼쪽버튼을 눌리다가 때어버리면 왼쪽버튼은 쓸모가 없는 이벤트가 되어 버린다
        end_point.x = x - Window_Width/2;
        end_point.y = Window_Height/2 - y;
    }
    if ( draw_type == SOLID && button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN )
    {
        draw_type = DOT; // 오른쪽 버튼 눌리면 제일처음 상태로 돌아간다 (사각형이 지워진다)
        redraw = 1;
    }
    glutPostRedisplay();
    RenderScene();
}
 
void MyKey (unsigned char key, int x, int y) // 창을 키웠을시에 6번을 누르게 되면 좌표가 작아 지게 됨
 {
    switch( key )
    {
    case '6' :  position_x = 0;
        position_y = 0;
        width = Window_Width;
        height = Window_Height;
        break;
    }
    glViewport(position_x, position_y, width, height);
    glutPostRedisplay();
}
 
 
 
 
void MyMotion(int x, int y) // 마우스의 움직임을 찾는다, 왼쪽버튼을 눌렀을때 진행되는 함수
 {
    if ( draw_type == RUBBER )
    {
        end_point.x = x - Window_Width/2;
        end_point.y = Window_Height/2 - y;
    }
    Check_Position();
    glutPostRedisplay();
}
 
 
void main (int argc, char** argv)
{
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(Window_Width, Window_Height);
    glutCreateWindow ("Motion_Event");
    init();
    glutDisplayFunc (RenderScene);
    glutMotionFunc(MyMotion);
    glutReshapeFunc(MyReshape);
    glutKeyboardFunc(MyKey);
    glutMouseFunc(mouse);
    glutMainLoop();
}
;
Posted by 코딩하는 야구쟁이
,