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
#include <gl/glut.h>   
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
      
#define PI 3.1415926
#define WIDTH 600
#define HEIGHT 600
     
struct POINT3D{
    float x;
    float y;
    float z;
};
POINT3D *vertex;
    
struct FACE4D{
    int v1;
    int v2;
    int v3;
    //int v4;
};
FACE4D *face;
    
int vertexNumber;
int faceNumber;
float color[][3] = {
    {1.0, 1.0, 1.0},    // white
    {1.0, 0.0, 0.0},    // red
    {0.0, 1.0, 0.0},    // green
    {0.0, 0.0, 1.0},    // blue
    {1.0, 1.0, 0.0},    // yellow
    {0.0, 1.0, 1.0},    // cyan
    {1.0, 0.0, 1.0},    // pupple
    {0.5, 0.5, 0.5}     // gray
};
     
float cameraX = 0;
float cameraY = 0;
float cameraZ = 0;
float radius = 200;
float phi = 0;
float theta = 0;
      
    
void fileLoad(void){
    char *filename;             // 파일 이름
    FILE *fp;                   // 읽어온 파일을 저장할 객체
    int i;
        
    filename = "bunny.txt";
    fp = fopen(filename, "r");  // 읽기형식(r)으로 파일 자체를 읽어온다.
    
    fscanf(fp, "%d", &vertexNumber);        // 파일에 저장되어 있는 데이터를 읽어온다.(첫번째 정수를 읽어 vertexNumber에 저장)
    vertex = new POINT3D[vertexNumber];     // 읽어온 숫자 크기만큼 구조체 배열 생성
    for(i = 0; i < vertexNumber; i++)
    {
        fscanf(fp, "%f %f %f",          // vertex를 읽어온다.
            &vertex[i].x, &vertex[i].y, &vertex[i].z);
    }
    
    fscanf(fp, "%d", &faceNumber);
    face = new FACE4D[faceNumber];
    for(i = 0; i < faceNumber; i++)
    {   
        fscanf(fp, "%d %d %d",           // 면의 정보를 읽어온다.
            &face[i].v1, &face[i].v2, &face[i].v3);
    }
}
        
void init (void)
{
    glClearColor (1.0, 1.0, 1.0, 0.0) ;    
    glColor3f(1.0, 1.0, 0.0);
    glEnable(GL_DEPTH_TEST);    // 깊이 활성화
    
    fileLoad();                 // 그래픽 데이터 파일 로드
}
        
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, WIDTH/HEIGHT, 1.0, 500.0);  // 멀고 가까움을 표현.
}
    
void drawBox(void)
{
    int i;
        
    for(i = 0; i < faceNumber; i++)
    {
        glBegin(GL_POLYGON);
            glColor3f(1.0, 0.5, 0.0);
            glVertex3f(vertex[face[i].v1].x, vertex[face[i].v1].y, vertex[face[i].v1].z);
            glVertex3f(vertex[face[i].v2].x, vertex[face[i].v2].y, vertex[face[i].v2].z);
            glVertex3f(vertex[face[i].v3].x, vertex[face[i].v3].y, vertex[face[i].v3].z);
        glEnd();
   
        glBegin(GL_LINE_LOOP);
            glColor3f(0.0, 0.0, 0.0);
            glVertex3f(vertex[face[i].v1].x, vertex[face[i].v1].y, vertex[face[i].v1].z);
            glVertex3f(vertex[face[i].v2].x, vertex[face[i].v2].y, vertex[face[i].v2].z);
            glVertex3f(vertex[face[i].v3].x, vertex[face[i].v3].y, vertex[face[i].v3].z);
        glEnd();
    }
}
     
void display (void)
    cameraX = radius * cos(phi) * cos(theta);
    cameraY = radius * cos(phi) * sin(theta);
    cameraZ = radius * sin(phi);
        
    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, 0.0, 1.0);
    // (앞의 세 인자는 카메라의 위치, 중간의 세 인자는 장면의 중앙,
    //  뒤의 세 인자는 법선벡터 방향.
     
    glPushMatrix();
        glRotatef(90.0, 1.0, 0.0, 0.0);
        glRotatef(-270.0, 0.0, 1.0, 0.0);
        drawBox();
    glPopMatrix();
    
    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:
        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;
    }
       
       
}
        
void main (int argc, char** argv)
{
    glutInit (&argc, argv);    
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );  
    glutInitWindowPosition (100, 100);    
    glutInitWindowSize (WIDTH, HEIGHT);   
    glutCreateWindow ("Bunny");  
    init(); 
    glutDisplayFunc (display);   
    glutReshapeFunc (reshape);  
    glutKeyboardFunc(key);
    glutSpecialFunc(specialKey);
    glutIdleFunc(display);
    glutMainLoop();    
}




Posted by 코딩하는 야구쟁이
,