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 | #include <gl/glut.h> #include <stdio.h> #include <math.h> #define PI 3.1415926 #define M 36 #define N 18 float radius = 5; float theta; float phi; float delta_theta; float delta_phi; struct Point3D { float x; float y; float z; }; Point3D vertex[M+1][N+1]; float cameraX = 10; float cameraY = 10; float cameraZ = 10; 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); gluPerspective(60.0, 1.0, 1.0, 100.0); // 멀고 가까움을 표현함 } void display ( void ) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode (GL_MODELVIEW); glLoadIdentity(); gluLookAt(cameraX, cameraY, cameraZ, 0.0, 0.0, 0.0, 0.0, 1.0, 0.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(); delta_theta = 2 * PI / M; delta_phi = PI / N; int i = 0, j = 0; for (i = 0; i < M; i++) { for (j = 0; j < N; j++) { theta = i * delta_theta; phi = j * delta_phi - ( PI / 2 ); vertex[i][j].x = radius * cos (theta) * cos (phi); vertex[i][j].y = radius * sin (theta) * cos (phi); vertex[i][j].z = radius * sin (phi); } } for (i = 0; i < M; i++) { for (j = 0; j < N; j++) { theta = i * delta_theta; phi = j * delta_phi - ( PI / 2 ); glBegin(GL_POLYGON); glVertex3f(vertex[i][j].x, vertex[i][j].y, vertex[i][j].z); glVertex3f(vertex[i+1][j].x, vertex[i+1][j].y, vertex[i+1][j].z); glVertex3f(vertex[i][j+1].x, vertex[i][j+1].y, vertex[i][j+1].z); glVertex3f(vertex[i+1][j+1].x, vertex[i+1][j+1].y, vertex[i+1][j+1].z); glEnd(); } } glFlush (); glutSwapBuffers(); } void SpecialKey( int key, int x, int y) { switch (key) { case GLUT_KEY_LEFT : theta -= 0.1; break ; case GLUT_KEY_RIGHT : theta += 0.1; break ; case GLUT_KEY_UP : phi += 0.1; break ; case GLUT_KEY_DOWN : phi -= 0.1; break ; case GLUT_KEY_F1 : glMatrixMode (GL_PROJECTION); glLoadIdentity(); glOrtho (-1.0*radius, radius, -1.0*radius, radius, -1.0*radius, radius); break ; case GLUT_KEY_F2 : glMatrixMode (GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, 1.0, 1.0, 100.0); break ; default : break ; } if ( theta > 2.0 * PI ) theta -= (2.0 * PI); else if ( theta < 0.0 ) theta += (2.0 * PI); } 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 3차원 텍스처 맵핑하기 (0) | 2011.09.27 |
---|---|
OpenGl 구 표현하기 (카메라 추가) (0) | 2011.09.26 |
OpenGL 정육면제 회전하기 (8) | 2011.09.19 |
OpenGl 이용한 3차원 정육면체 구현 (2) | 2011.09.15 |
드래그하면서 점선으로 된 원 그리기 (0) | 2011.09.15 |