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 | #include <gl/glut.h> #include <stdio.h> #include <math.h> GLfloat vertices[][3] = { { -1.0, -1.0, 1.0 }, // 0 { -1.0, 1.0, 1.0 }, // 1 { 1.0, 1.0, 1.0 }, // 2 { 1.0, -1.0, 1.0 }, // 3 { -1.0, -1.0, -1.0 }, // 4 { -1.0, 1.0, -1.0 }, // 5 { 1.0, 1.0, -1.0 }, // 6 { 1.0, -1.0, -1.0 }}; // 7 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 { 1.0, 1.0, 1.0 }, // white { 1.0, 0.0, 1.0 }}; // magenta void polygon( int a, int b, int c, int d) { glColor3fv(colors[a]); glBegin(GL_POLYGON); glVertex3fv(vertices[a]); glVertex3fv(vertices[b]); glVertex3fv(vertices[c]); glVertex3fv(vertices[d]); glEnd(); } // 6개의 면을 만든다. void createCube( void ) { polygon(0, 3, 2, 1); // front polygon(2, 3, 7, 6); // right polygon(3, 0, 4, 7); // bottom polygon(4, 5, 6, 7); // back polygon(1, 2, 6, 5); // top polygon(5, 4, 0, 1); // right } void init ( void ) { glClearColor (0.0, 0.0, 0.0, 0.0) ; glColor3f(1.0, 1.0, 0.0); glEnable(GL_DEPTH_TEST); // 깊이 활성화 } void reshape ( int w, int h) { glViewport(0, 0, w, h); glMatrixMode (GL_PROJECTION); glLoadIdentity(); glOrtho (-5.0, 5.0, -5.0, 5.0, -5.0, 5.0); } void display ( void ) { float x, y, theta; theta = 1.0; x = 2.0 * cos (theta); y = 2.0 * sin (theta); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode (GL_MODELVIEW); glLoadIdentity(); gluLookAt(x, y, 2.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); // (앞의 세 인자는 카메라의 위치, 중간의 세 인자는 카메라의 초점, // 뒤의 세 인자는 법선벡터 방향 (0, 1, 0))으로 해줘야 세워져 보인다. /* glBegin(GL_LINES); // X, Y, Z 선 표시 glColor3f(1.0, 0.0, 0.0); // X축 glVertex3f(0.0, 0.0, 0.0); glVertex3f(10.0, 0.0, 0.0); glColor3f(0.0, 1.0, 0.0); // Y축 glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 10.0, 0.0); glColor3f(0.0, 0.0, 1.0); // Z축 glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 0.0, 10.0); glEnd(); */ createCube(); // 큐브 생성 glFlush (); glutSwapBuffers(); } void main ( int argc, char ** argv) { glutInit (&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH ); glutInitWindowPosition (100, 100); glutInitWindowSize (500, 500); glutCreateWindow ( "Cube" ); init(); glutDisplayFunc (display); glutReshapeFunc (reshape); glutIdleFunc(display); glutMainLoop(); } ; |
'Study > OpenGL' 카테고리의 다른 글
Opengl 구 그리기 (1) | 2011.09.22 |
---|---|
OpenGL 정육면제 회전하기 (8) | 2011.09.19 |
드래그하면서 점선으로 된 원 그리기 (0) | 2011.09.15 |
드래그하면서 점선으로 된 사각형 그리기 (0) | 2011.09.15 |
glLineStipple 선그리기 (0) | 2011.09.15 |