루빅스 큐브 만들기2

Posted by 백창
2014. 12. 8. 20:42 개발/Open GL
반응형


 개요

 

 cube 객체를 생성하고 27개의 작은 정육면체로 큰 정육면체를 만들기



 간단한 설명

 

클래스를 작성하여 반복문으로 각 위치를 설정해주고 정육면체를 그린다.


생각나는데로 만들어서 노가다성 코드가 많음.







 좌표설정 및 칼라 설정

 

가장 간단한 노가다로 처리


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
static GLfloat vertices[3][9][8][3] = {
    //Front
    {
        //Top
        {
            { -0.5, 0.5, 0.2 }, { -0.5, 0.5, 0.5 }, { -0.2, 0.5, 0.5 }, { -0.2, 0.5, 0.2 }, { -0.5, 0.2, 0.2 }, { -0.5, 0.2, 0.5 }, { -0.2, 0.2, 0.5 }, { -0.2, 0.2, 0.2 }
        },
        {
            { -0.15, 0.5, 0.2 }, { -0.15, 0.5, 0.5 }, { 0.15, 0.5, 0.5 }, { 0.15, 0.5, 0.2 }, { -0.15, 0.2, 0.2 }, { -0.15, 0.2, 0.5 }, { 0.15, 0.2, 0.5 }, { 0.15, 0.2, 0.2 }
        },
        {
            { 0.2, 0.5, 0.2 }, { 0.2, 0.5, 0.5 }, { 0.5, 0.5, 0.5 }, { 0.5, 0.5, 0.2 }, { 0.2, 0.2, 0.2 }, { 0.2, 0.2, 0.5 }, { 0.5, 0.2, 0.5 }, { 0.5, 0.2, 0.2 }
        },
            //Mid
        {
            { -0.5, 0.5, -0.15 }, { -0.5, 0.5, 0.15 }, { -0.2, 0.5, 0.15 }, { -0.2, 0.5, -0.15 }, { -0.5, 0.2, -0.15 }, { -0.5, 0.2, 0.15 }, { -0.2, 0.2, 0.15 }, { -0.2, 0.2, -0.15 }
        },
        {
            { -0.15, 0.5, -0.15 }, { -0.15, 0.5, 0.15 }, { 0.15, 0.5, 0.15 }, { 0.15, 0.5, -0.15 }, { -0.15, 0.2, -0.15 }, { -0.15, 0.2, 0.15 }, { 0.15, 0.2, 0.15 }, { 0.15, 0.2, -0.15 }
        },
        {
            { 0.2, 0.5, -0.15 }, { 0.2, 0.5, 0.15 }, { 0.5, 0.5, 0.15 }, { 0.5, 0.5, -0.15 }, { 0.2, 0.2, -0.15 }, { 0.2, 0.2, 0.15 }, { 0.5, 0.2, 0.15 }, { 0.5, 0.2, -0.15 }
        },
            //Bot
        {
            { -0.5, 0.5, -0.5 }, { -0.5, 0.5, -0.2 }, { -0.2, 0.5, -0.2 }, { -0.2, 0.5, -0.5 }, { -0.5, 0.2, -0.5 }, { -0.5, 0.2, -0.2 }, { -0.2, 0.2, -0.2 }, { -0.2, 0.2, -0.5 }
            
        },
        {
            { -0.15, 0.5, -0.5 }, { -0.15, 0.5, -0.2 }, { 0.15, 0.5, -0.2 }, { 0.15, 0.5, -0.5 }, { -0.15, 0.2, -0.5 }, { -0.15, 0.2, -0.2 }, { 0.15, 0.2, -0.2 }, { 0.15, 0.2, -0.5 }
        },
        {
            { 0.2, 0.5, -0.5 }, { 0.2, 0.5, -0.2 }, { 0.5, 0.5, -0.2 }, { 0.5, 0.5, -0.5 }, { 0.2, 0.2, -0.5 }, { 0.2, 0.2, -0.2 }, { 0.5, 0.2, -0.2 }, { 0.5, 0.2, -0.5 }
        }
    },
        //Middle
    {
        //Top
        {
            { -0.5, 0.15, 0.2 }, { -0.5, 0.15, 0.5 }, { -0.2, 0.15, 0.5 }, { -0.2, 0.15, 0.2 }, { -0.5, -0.15, 0.2 }, { -0.5, -0.15, 0.5 }, { -0.2, -0.15, 0.5 }, { -0.2, -0.15, 0.2 }
        },
        {
            { -0.15, 0.15, 0.2 }, { -0.15, 0.15, 0.5 }, { 0.15, 0.15, 0.5 }, { 0.15, 0.15, 0.2 }, { -0.15, -0.15, 0.2 }, { -0.15, -0.15, 0.5 }, { 0.15, -0.15, 0.5 }, { 0.15, -0.15, 0.2 }
        },
        {
            { 0.2, 0.15, 0.2 }, { 0.2, 0.15, 0.5 }, { 0.5, 0.15, 0.5 }, { 0.5, 0.15, 0.2 }, { 0.2, -0.15, 0.2 }, { 0.2, -0.15, 0.5 }, { 0.5, -0.15, 0.5 }, { 0.5, -0.15, 0.2 }
        },
            //Mid
        {
            { -0.5, 0.15, -0.15 }, { -0.5, 0.15, 0.15 }, { -0.2, 0.15, 0.15 }, { -0.2, 0.15, -0.15 }, { -0.5, -0.15, -0.15 }, { -0.5, -0.15, 0.15 }, { -0.2, -0.15, 0.15 }, { -0.2, -0.15, -0.15 }
        },
        { //Black One - Center Cube
            { -0.35, 0.35, -0.35 }, { -0.35, 0.35, 0.35 }, { 0.35, 0.35, 0.35 }, { 0.35, 0.35, -0.35 }, { -0.35, -0.35, -0.35 }, { -0.35, -0.35, 0.35 }, { 0.35, -0.35, 0.35 }, { 0.35, -0.35, -0.35 }
        },
        {
            { 0.2, 0.15, -0.15 }, { 0.2, 0.15, 0.15 }, { 0.5, 0.15, 0.15 }, { 0.5, 0.15, -0.15 }, { 0.2, -0.15, -0.15 }, { 0.2, -0.15, 0.15 }, { 0.5, -0.15, 0.15 }, { 0.5, -0.15, -0.15 }
        },
            //Bot
        {
            { -0.5, 0.15, -0.5 }, { -0.5, 0.15, -0.2 }, { -0.2, 0.15, -0.2 }, { -0.2, 0.15, -0.5 }, { -0.5, -0.15, -0.5 }, { -0.5, -0.15, -0.2 }, { -0.2, -0.15, -0.2 }, { -0.2, -0.15, -0.5 }
        },
        {
            { -0.15, 0.15, -0.5 }, { -0.15, 0.15, -0.2 }, { 0.15, 0.15, -0.2 }, { 0.15, 0.15, -0.5 }, { -0.15, -0.15, -0.5 }, { -0.15, -0.15, -0.2 }, { 0.15, -0.15, -0.2 }, { 0.15, -0.15, -0.5 }
        },
        {
            { 0.2, 0.15, -0.5 }, { 0.2, 0.15, -0.2 }, { 0.5, 0.15, -0.2 }, { 0.5, 0.15, -0.5 }, { 0.2, -0.15, -0.5 }, { 0.2, -0.15, -0.2 }, { 0.5, -0.15, -0.2 }, { 0.5, -0.15, -0.5 }
            
        }
    },
        //Back
    {
        //Top
        {
            { -0.5, -0.2, 0.2 }, { -0.5, -0.2, 0.5 }, { -0.2, -0.2, 0.5 }, { -0.2, -0.2, 0.2 }, { -0.5, -0.5, 0.2 }, { -0.5, -0.5, 0.5 }, { -0.2, -0.5, 0.5 }, { -0.2, -0.5, 0.2 }
        },
        {
            { -0.15, -0.2, 0.2 }, { -0.15, -0.2, 0.5 }, { 0.15, -0.2, 0.5 }, { 0.15, -0.2, 0.2 }, { -0.15, -0.5, 0.2 }, { -0.15, -0.5, 0.5 }, { 0.15, -0.5, 0.5 }, { 0.15, -0.5, 0.2 }
        },
        {
            { 0.2, -0.2, 0.2 }, { 0.2, -0.2, 0.5 }, { 0.5, -0.2, 0.5 }, { 0.5, -0.2, 0.2 }, { 0.2, -0.5, 0.2 }, { 0.2, -0.5, 0.5 }, { 0.5, -0.5, 0.5 }, { 0.5, -0.5, 0.2 }
        },
            //Mid
        {
            { -0.5, -0.2, -0.15 }, { -0.5, -0.2, 0.15 }, { -0.2, -0.2, 0.15 }, { -0.2, -0.2, -0.15 }, { -0.5, -0.5, -0.15 }, { -0.5, -0.5, 0.15 }, { -0.2, -0.5, 0.15 }, { -0.2, -0.5, -0.15 }
        },
        {
            { -0.15, -0.2, -0.15 }, { -0.15, -0.2, 0.15 }, { 0.15, -0.2, 0.15 }, { 0.15, -0.2, -0.15 }, { -0.15, -0.5, -0.15 }, { -0.15, -0.5, 0.15 }, { 0.15, -0.5, 0.15 }, { 0.15, -0.5, -0.15 }
        },
        {
            { 0.2, -0.2, -0.15 }, { 0.2, -0.2, 0.15 }, { 0.5, -0.2, 0.15 }, { 0.5, -0.2, -0.15 }, { 0.2, -0.5, -0.15 }, { 0.2, -0.5, 0.15 }, { 0.5, -0.5, 0.15 }, { 0.5, -0.5, -0.15 }
        },
 
            //Bot
        {
            { -0.5, -0.2, -0.5 }, { -0.5, -0.2, -0.2 }, { -0.2, -0.2, -0.2 }, { -0.2, -0.2, -0.5 }, { -0.5, -0.5, -0.5 }, { -0.5, -0.5, -0.2 }, { -0.2, -0.5, -0.2 }, { -0.2, -0.5, -0.5 }
        },
        {
            { -0.15, -0.2, -0.5 }, { -0.15, -0.2, -0.2 }, { 0.15, -0.2, -0.2 }, { 0.15, -0.2, -0.5 }, { -0.15, -0.5, -0.5 }, { -0.15, -0.5, -0.2 }, { 0.15, -0.5, -0.2 }, { 0.15, -0.5, -0.5 }
        },
        {
            { 0.2, -0.2, -0.5 }, { 0.2, -0.2, -0.2 }, { 0.5, -0.2, -0.2 }, { 0.5, -0.2, -0.5 }, { 0.2, -0.5, -0.5 }, { 0.2, -0.5, -0.2 }, { 0.5, -0.5, -0.2 }, { 0.5, -0.5, -0.5 }
            
        }
    }
};
 
GLfloat primary_colors[][3] = {
    { 1.0, 0.0, 0.0 },      // red  
    { 1.0, 1.0, 0.0 },      // yellow  
    { 0.0, 1.0, 0.0 },      // green  
    { 0.0, 0.0, 1.0 },      // blue  
    { 1.0, 1.0, 1.0 },      // gold
    { 1.0, 0.0, 1.0 }        // magenta  
};     




 클래스 생성

 

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
 
class Cube{
private:
    GLfloat vertices[8][3];
    GLfloat colors[6][3];
    int location[3];
    
 
public:
    void setPosition(GLfloat set[8][3]);
 
    void setLocation(int loc[3]);
    void getLocation(int *ptr);
 
    void setColor(GLfloat set[6][3]);
    void getColor(GLfloat get[6][3]);
 
    void setAngle(int new_angleX, int new_angleY, int new_angleZ);
 
    void drawCube();
    void drawBlackCube();
        
};
 
 
void Cube::setColor(GLfloat set[6][3]){
    for (int i = 0; i<6; i++){
        for (int j = 0; j<3; j++){
            colors[i][j] = set[i][j];
        }
    }
}
 
 
void Cube::getColor(GLfloat get[6][3]){
    for (int i = 0; i < 6; i++){
        for (int j = 0; j < 3; j++){
            get[i][j] = colors[i][j];
        }
    }
}
 
/**
* Cube 위치 설정
*/
void Cube::setLocation(int loc[3]){
    for (int i = 0; i < 3; i++){
        location[i] = loc[i];
    }
}
 
/**
* Cube 위치 반환
*/
void Cube::getLocation(int *ptr){
    for (int i = 0; i < 3; i++){
        ptr[i] = location[i];
    }
 
}
 
/**
* Cube 위치 좌표 설정
*/
void Cube::setPosition(GLfloat set[8][3]){
    for (int i = 0; i < 8; i++){
        for (int j = 0; j < 3; j++){
            vertices[i][j] = set[i][j];
        }
    }
 
}
/**
* Cube 그리기
*/
void Cube::drawCube(){
    
    glBegin(GL_QUADS);
    
    glColor3fv(colors[0]);
    glNormal3f(0.0f, 1.0f, 0.0f);//top
    glVertex3fv(vertices[0]);
    glVertex3fv(vertices[3]);
    glVertex3fv(vertices[2]);
    glVertex3fv(vertices[1]);
    
    glColor3fv(colors[1]);
    glNormal3f(0.0f, 0.0f, 1.0f);//front
    glVertex3fv(vertices[1]);
    glVertex3fv(vertices[2]);
    glVertex3fv(vertices[6]);
    glVertex3fv(vertices[5]);
    
    glColor3fv(colors[2]);
    glNormal3f(1.0f, 0.0f, 0.0f);//right
    glVertex3fv(vertices[3]);
    glVertex3fv(vertices[7]);
    glVertex3fv(vertices[6]);
    glVertex3fv(vertices[2]);
    
    
    glColor3fv(colors[3]);
    glNormal3f(0.0f, 0.0f, -1.0f);//back
    glVertex3fv(vertices[0]);
    glVertex3fv(vertices[3]);
    glVertex3fv(vertices[7]);
    glVertex3fv(vertices[4]);
    
    glColor3fv(colors[4]);
    glNormal3f(0.0f, -1.0f, 0.0f);//buttom
    glVertex3fv(vertices[7]);
    glVertex3fv(vertices[4]);
    glVertex3fv(vertices[5]);
    glVertex3fv(vertices[6]);
    
    glColor3fv(colors[5]);
    glNormal3f(-1.0f, 0.0f, 0.0f);//left 
    glVertex3fv(vertices[4]);
    glVertex3fv(vertices[0]);
    glVertex3fv(vertices[1]);
    glVertex3fv(vertices[5]);
    
    glEnd();            // End Drawing The Cube
}
/**
* Center Cube 그리기
*/
void Cube::drawBlackCube(){
 
    glBegin(GL_QUADS);
    glColor3f(0.5f, 0.5f, 0.5f);
    glVertex3fv(vertices[0]);
    glVertex3fv(vertices[3]);
    glVertex3fv(vertices[2]);
    glVertex3fv(vertices[1]);
 
    glColor3f(0.5f, 0.5f, 0.5f);
    glVertex3fv(vertices[1]);
    glVertex3fv(vertices[2]);
    glVertex3fv(vertices[6]);
    glVertex3fv(vertices[5]);
 
    glColor3f(0.5f, 0.5f, 0.5f);
    glVertex3fv(vertices[3]);
    glVertex3fv(vertices[7]);
    glVertex3fv(vertices[6]);
    glVertex3fv(vertices[2]);
 
    glColor3f(0.5f, 0.5f, 0.5f);
    glVertex3fv(vertices[0]);
    glVertex3fv(vertices[3]);
    glVertex3fv(vertices[7]);
    glVertex3fv(vertices[4]);
 
    glColor3f(0.5f, 0.5f, 0.5f);
    glVertex3fv(vertices[7]);
    glVertex3fv(vertices[4]);
    glVertex3fv(vertices[5]);
    glVertex3fv(vertices[6]);
 
    glColor3f(0.5f, 0.5f, 0.5f);
    glVertex3fv(vertices[4]);
    glVertex3fv(vertices[0]);
    glVertex3fv(vertices[1]);
    glVertex3fv(vertices[5]);
 
    glEnd();            // End Drawing The Cube
}




 Display Func

 

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
 
void setAllCube(){
 
    /**************** 큐브 초기 좌표 설정 *****************/
    for (int i = 0; i < 3; i++){
        for (int j = 0; j < 9; j++){
            cube[i][j].setPosition(vertices[i][j]);
            cube[i][j].setLocation(primaryLocation[i][j]);
            cube[i][j].setColor(primary_colors);
        }
    }
}
 
void drawAllCube(){
 
    for (int a = 0; a < 3; a++){
        for (int b = 0; b < 9; b++){
 
            for (int i = 0; i < 3; i++){
                for (int j = 0; j < 9; j++){
 
                    int save[3];
                    int cnt = 0;
 
                    cube[i][j].getLocation(save);
                    
                    //위치에 맞는 큐브를 선택
                    for (int k = 0; k < 3; k++){
                        if (primaryLocation[a][b][k] == save[k]){
                            cnt++;
                        }
                    }
                    if (cnt == 3){
 
                        glPushMatrix();
                        if (i == 1 && j == 4)
                            cube[i][j].drawBlackCube();
                        else
                            cube[i][j].drawCube();
 
                        glPopMatrix();
                    }
                }
            }
        }
    }
 
    
}
 
 
void Init(){
 
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(700, 700);
    glutInitWindowPosition(0, 0);
 
 
    glutCreateWindow("Rubik\'s Cube");
 
 
//    glClearColor(1.0, 1.0, 1.0, 0.0);
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
 
    glEnable(GL_DEPTH_TEST);
 
    setAllCube();
}
 
 
void MyDisplay(){
    
    
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
 
    glPopMatrix();
    glRotatef(g_fSpinY, 1.0f, 0.0f, 0.0f);
    glRotatef(g_fSpinX, 0.0f, 0.0f, 1.0f);
 
    drawAllCube();
 
    glPopMatrix(); 
    glutSwapBuffers();
    glFlush();
    
}
 


반응형

'개발 > Open GL' 카테고리의 다른 글

루빅스 큐브 만들기3  (3) 2014.12.24
루빅스 큐브 만들기1  (0) 2014.11.22
이미지 불러오기  (0) 2014.11.21
정육면체 그리기  (0) 2014.11.21
도형 그리기 및 이동  (0) 2014.11.03

루빅스 큐브 만들기1

Posted by 백창
2014. 11. 22. 15:03 개발/Open GL
반응형


 개요

 

 Open GL을 사용하여 어릴때 가지고 놀던 장난감, 루빅스 큐브를 만들어 보도록 하자.




 설계

 

 각각의 정육면체를 Class로 생성, 각각의 객체에 3차원 좌표와 각 면의 색깔을 지정해준다.


 그리고 회전하고자 하는 면의 좌표를 선택하여 아래와 같은 규칙을 따라 회전 시킨다. 


(나는 좌표 값을 사용하지 않고 각 객체가 위치한 좌표를 식별하기 편하게 1 , 2 , 3 으로 주었다.)


(반대 방향으로 회전하고자 하는 경우 규칙을 반대로 적용하면 된다.)




단면을 봤을 때 위와 같은 규칙으로 회전하고 전체적인 그림을 봤을때 색깔의 변경은 아래와 같다.



반응형

'개발 > Open GL' 카테고리의 다른 글

루빅스 큐브 만들기3  (3) 2014.12.24
루빅스 큐브 만들기2  (0) 2014.12.08
이미지 불러오기  (0) 2014.11.21
정육면체 그리기  (0) 2014.11.21
도형 그리기 및 이동  (0) 2014.11.03

정육면체 그리기

Posted by 백창
2014. 11. 21. 10:56 개발/Open GL
반응형


 개요

 

 사각형을 이용해 정육면체 즉, 큐브를 그려보자



 코드

 

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
#include <gl/glut.h>
 
GLfloat vertices[][3] = {
    { -0.5, 0.5, -0.5 }, 
    { -0.5, 0.5, 0.5 }, 
    { 0.5, 0.5, 0.5 }, 
    { 0.5, 0.5, -0.5 }, 
    { -0.5, -0.5, -0.5 }, 
    { -0.5, -0.5, 0.5 }, 
    { 0.5, -0.5, 0.5 }, 
    { 0.5, -0.5, -0.5 }
};
 
GLfloat colors[][3] = {
    { 1.0, 0.0, 0.0 },      // red  
    { 1.0, 1.0, 0.0 },      // yellow  
    { 0.0, 1.0, 0.0 },      // green  
    { 0.0, 0.0, 1.0 },      // blue  
    { 0.5, 0.5, 0.0 },      // gold
    { 1.0, 0.0, 1.0 }        // magenta  
};
 
 
void display(){
 
 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
    glTranslatef(0.0f, 0.0f, 0.0f);
    glRotatef(30, 1.0f, 0.0f, 0.0f);
    glRotatef(30, 0.0f, 0.0f, 1.0f);
 
    glBegin(GL_QUADS);
 
    glColor3fv(colors[0]);
    glVertex3fv(vertices[0]);
    glVertex3fv(vertices[3]);
    glVertex3fv(vertices[2]);
    glVertex3fv(vertices[1]);
 
    glColor3fv(colors[1]);
    glVertex3fv(vertices[1]);
    glVertex3fv(vertices[2]);
    glVertex3fv(vertices[6]);
    glVertex3fv(vertices[5]);
 
    glColor3fv(colors[2]);
    glVertex3fv(vertices[3]);
    glVertex3fv(vertices[7]);
    glVertex3fv(vertices[6]);
    glVertex3fv(vertices[2]);
 
    glColor3fv(colors[3]);
    glVertex3fv(vertices[0]);
    glVertex3fv(vertices[3]);
    glVertex3fv(vertices[7]);
    glVertex3fv(vertices[4]);
 
    glColor3fv(colors[4]);
    glVertex3fv(vertices[7]);
    glVertex3fv(vertices[4]);
    glVertex3fv(vertices[5]);
    glVertex3fv(vertices[6]);
 
    glColor3fv(colors[5]);
    glVertex3fv(vertices[4]);
    glVertex3fv(vertices[0]);
    glVertex3fv(vertices[1]);
    glVertex3fv(vertices[5]);
 
    glEnd();            // End Drawing The Cube
 
    glutSwapBuffers();
}
 
 
void Init(){
 
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(700, 700);
    glutInitWindowPosition(0, 0);
 
    glutCreateWindow("Cube");
 
    glClearColor(1.0, 1.0, 1.0, 1.0);
 
    glEnable(GL_DEPTH_TEST);
 
}
 
int main(int argc, char **argv){
    glutInit(&argc, argv);
 
    Init();
 
    glutDisplayFunc(display);
    glutMainLoop();
 
    return 0;
}



 결과

 




 참고

 

 glEnable(GL_DEPTH_TEST) 를 사용하지 않으면 어떤 도형이 화면과 가까운지 체크할 수 없어 앞면과 뒷면이 구분되지 않는 문제가 발생한다.

반응형

'개발 > Open GL' 카테고리의 다른 글

루빅스 큐브 만들기1  (0) 2014.11.22
이미지 불러오기  (0) 2014.11.21
도형 그리기 및 이동  (0) 2014.11.03
[빌드 오류] failure during conversion to COFF: file invalid or corrupt  (0) 2014.10.17
Menu & SubMenu 생성  (11) 2014.10.09