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
#include <gl/glut.h>
#define width 600
#define height 400
 
int count = 0;
 
struct Point2D
{
    float x;
    float y;
};
 
Point2D point[100];
 
void my_reshape(int w, int h) // 생성된 윈도우창이 움직이거나 변환 되었을때 호출되는 함수
{
    glViewport(0, 0, w, h); // 창 내에서 OpenGL의 렌더링이 진행될 영역을 설정
    // x와 y는 시작되는 위치이고 넓이와 높이는 시작되는 위치에서넓이와 높이를 의미
    glMatrixMode(GL_PROJECTION); // 투영행렬과 모델행렬을 불러오는 함수
    glLoadIdentity(); // 현재 행렬을 초기화 시킨다
    glOrtho(0, 600, 0, 400, -1, 1); // 화면상의 좌표축을 설정하는 함수
}
 
void my_display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glClearColor(0.0, 0.0, 0.0, 1.0);
     
    glColor3f(1.0, 1.0, 0.0); //물체의 색 정의
    glBegin(GL_QUADS); //여러개의 사각형 정의
    glVertex2f(0.0, 0.0); //2차원정수
    glVertex2f(600.0, 0.0);
    glVertex2f(600.0, 400.0);
    glVertex2f(0.0, 400.0);
    glEnd();
     
    glColor3f(1.0, 0.0, 0.0); //물체의 색 정의
    glBegin(GL_LINES); //여러개의 선분 정의
    glVertex2f(0.0, 200.0); //2차원정수
    glVertex2f(600.0, 200.0);
    glEnd();
     
    glColor3f(0.0, 0.0, 1.0); //물체의 색 정의
    glBegin(GL_LINES); //여러개의 선분 정의
    glVertex2f(300.0, .0); //2차원정수
    glVertex2f(300.0, 400.0);
    glEnd();
     
    glColor3f(0.0, 1.0, 0.0); //물체의 색 정의
    glBegin(GL_QUADS); //여러개의 사각형 정의
    glVertex2f(400.0, 350.0); //2차원정수
    glVertex2f(400.0, 250.0);
    glVertex2f(500.0, 250.0);
    glVertex2f(500.0, 350.0);
    glEnd();
     
    glPointSize(5.0); // 점크기 정의
    glColor3f(1.0, 0.0, 0.0); // 점의 색깔 정의
    glBegin(GL_POINTS); // 여러개의 점 정의
    for(int i=0;i<count;i++)
    {
        glVertex2f(point[i].x, point[i].y);
    }
    glEnd();
     
    glFlush(); // 드라이버에게 더 이상 명령어를 쌓지 말고 현재까지 쌓인 명령어 모두를 무조건 프로세서에게 전달하도록 강제하는 명령
}
 
void my_keyboard(unsigned char key, int x, int y)
{
    switch(key)
    {
    case '1': glViewport(width/2, height/2, width/2, height/2);
        break;
    case '2': glViewport(0, height/2, width/2, height/2);
        break;
    case '3': glViewport(0,0, width/2, height/2);
        break;
    case '4': glViewport(width/2,0, width/2, width/2);
        break;
    case '5': glViewport(width/4, height/4, width/2, height/2);
        break;
    case '6': glViewport(0,0,  width, height);
        break;
    default : break;
    }
     
    glutPostRedisplay();
}
 
void my_mouse(int button, int state, int x, int y)
{
    if(button ==GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    {
        point[count].x =x;
        point[count].y =height-y;
        count++;
    }
     
    glutPostRedisplay();
}
 
void main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
    glutInitWindowSize(600, 400);
    glutInitWindowPosition(200,100);
    glutCreateWindow("Viewport_test"); // 새로운 윈도우를 생성하라는 명령
    glutDisplayFunc(my_display); // Mydisply 라는 함수를 디스플레이 이벤트에 대한 콜백함수로 등록
    // 처음 윈도우를 열때, 윈도우 위치를 옮길때, 윈도우 크기를 조절할때
    // 앞 윈도우에 가려져 안 보이던 뒤 운도우가 활성화 되어 앞으로 드러날 때
    glutReshapeFunc(my_reshape); //
    glutKeyboardFunc(my_keyboard); // 문자 및 숫자 키에 대한 콜백함수를 등록하기 위한 것
    glutMouseFunc(my_mouse); // 마우스 버튼을 누를 때 또는 마우스가 움직일 때 발생한다
    glutMainLoop(); // 메인함수의 마지막은 항상 MainLoop
}
 
// 과제 2개
// n각형을 만드는데 1번 눌리면 형태만 그리고
// n각형을 만드는데 2번 눌리면 채워진 도형 그린다
Posted by 코딩하는 야구쟁이
,