8 (꼭지점의 갯수, 정수타입)
-1.0 -1.0 1.0
-1.0 1.0 1.0
1.0 1.0 1.0
1.0 -1.0 1.0
-1.0 -1.0 -1.0
-1.0 1.0 -1.0
1.0 1.0 -1.0
1.0 -1.0 -1.0
* 8개의 줄, 각각의 하나는 꼭지점의 x,y,z 값을 의미한다
6 (면의 갯수, 정수타입)
0 3 2 1
2 3 7 6
3 0 4 7
1 2 6 5
4 5 6 7
5 4 0 1
-1.0 -1.0 1.0
-1.0 1.0 1.0
1.0 1.0 1.0
1.0 -1.0 1.0
-1.0 -1.0 -1.0
-1.0 1.0 -1.0
1.0 1.0 -1.0
1.0 -1.0 -1.0
* 8개의 줄, 각각의 하나는 꼭지점의 x,y,z 값을 의미한다
6 (면의 갯수, 정수타입)
0 3 2 1
2 3 7 6
3 0 4 7
1 2 6 5
4 5 6 7
5 4 0 1
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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | #include <gl/glut.h> #include <stdio.h> #include <math.h> #include <iostream> using namespace std; #define PI 3.1415926 #define M 36 #define N 18 float radius = 1; float theta; float phi; float delta_theta; float delta_phi; float cameraX = 3; float cameraY = 3; float cameraZ = 3; struct POINT3D { float x; float y; float z; }; POINT3D *ver; struct FACE4D { int v1; int v2; int v3; int v4; }; FACE4D *face; int v_no; int f_no; float color[][3] = { (1.0, 1.0, 1.0), //흰색 (1.0, 0.0, 0.0), //빨강색 (0.0, 1.0, 0.0), //초록 (0.0, 0.0, 1.0), //파랑 (1.0, 1.0, 0.0), //노랑 (0.0, 1.0, 1.0), //시안 (1.0, 0.0, 1.0), //퍼플 (0.5, 0.5, 0.5) //회색 }; void File_Load( void ) //파일불러들이기 { char *filename; FILE *fp; int i; filename = "box.txt" ; fp = fopen (filename, "r" ); fscanf (fp, "%d" , &(v_no)); ver = new POINT3D[v_no]; for (i=0;i<v_no;i++) fscanf (fp, "%f %f %f" , &(ver[i].x), &(ver[i].y), &(ver[i].z)); fscanf (fp, "%d" , &(f_no)); face = new FACE4D[f_no]; for (i=0;i<f_no;i++) fscanf (fp, "%d %d %d %d" , &(face[i].v1), &(face[i].v2), &(face[i].v3), &(face[i].v4)); } void init ( void ) { glClearColor (1.0, 1.0, 1.0, 0.0) ; glColor3f(1.0, 1.0, 0.0); glEnable(GL_DEPTH_TEST); // 깊이 활성화 File_Load(); } 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(45.0, 1.0, 1.0, 100.0 ); // 멀고 가까움을 표현함 } void Draw_Box( void ) { int i; for (i=0;i<f_no;i++) { glBegin(GL_POLYGON); glColor3f(color[face[i].v1][0], color[face[i].v1][1], color[face[i].v1][2]); glVertex3f(ver[face[i].v1].x, ver[face[i].v1].y, ver[face[i].v1].z); glColor3f(color[face[i].v2][0], color[face[i].v2][1], color[face[i].v2][2]); glVertex3f(ver[face[i].v2].x, ver[face[i].v2].y, ver[face[i].v2].z); glColor3f(color[face[i].v3][0], color[face[i].v3][1], color[face[i].v3][2]); glVertex3f(ver[face[i].v3].x, ver[face[i].v3].y, ver[face[i].v3].z); glColor3f(color[face[i].v4][0], color[face[i].v4][1], color[face[i].v4][2]); glVertex3f(ver[face[i].v4].x, ver[face[i].v4].y, ver[face[i].v4].z); glEnd(); } } void display ( void ) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode (GL_MODELVIEW); glLoadIdentity(); gluLookAt(5.0, 5.0, 5.0, 0.0, 0.0, 0.0, 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(); Draw_Box(); glFlush (); glutSwapBuffers(); } void key(unsigned char key, int x, int y) { switch (key) { case 27: exit (0); break ; } } void specialKey( int key, int x, int y) { switch (key) { case GLUT_KEY_LEFT: cameraX -= 1; break ; case GLUT_KEY_RIGHT: cameraX += 1; break ; case GLUT_KEY_UP: cameraY += 1; break ; case GLUT_KEY_DOWN: cameraY -= 1; break ; } //glutPostRedisplay(); } 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); glutKeyboardFunc(key); glutSpecialFunc(specialKey); glutIdleFunc(display); glutMainLoop(); } |
'Study > OpenGL' 카테고리의 다른 글
OpenGL 중간고사 (0) | 2011.10.13 |
---|---|
OpenGL txt File 이용해 그래픽 불러 들이기 - bunny (1) | 2011.10.06 |
OpenGL 육면채에 사과무늬 텍스쳐 맵핑하기 (2) | 2011.09.27 |
Opengl 3차원 텍스처 맵핑하기 (0) | 2011.09.27 |
OpenGl 구 표현하기 (카메라 추가) (0) | 2011.09.26 |