정육면체 그리기

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