Study/OpenGL 드래그하면서 점선으로 된 사각형 그리기 코딩하는 야구쟁이 2011. 9. 15. 11:33 #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(); } ;