그래픽스 : Solar System

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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// SolarSystem.cpp : Defines the entry point for the console application.
//
 
//self rotation 자전
//orbiting 공전
 
/*
* self rotation표현하기 위해
* rotation axis : +y  (0,1,0)
* rotation angle : 얼마나 돌았나
*/
 
#define _USE_MATH_DEFINES
#include <gl glut.h="">
#include <cmath>
 
void init();
void display();
void idle();
void draw_axes();
void draw_cube();
 
float moon_selfrot_angle;
float moon_radius,earth_radius ;
float moon_orbit_angle,earth_selfrot_angle;
float moon_orbit_raidus;
float   sun_radius,earth_orbit_raidus,earth_orbit_angle;
 
void main(int argc, char* argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); 
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(640, 640);
    glutCreateWindow("GLUT tutorial");
    glutDisplayFunc(display);
    glutIdleFunc(idle);
    init();
    glutMainLoop();
}
 
 
void init()
{
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    glEnable(GL_DEPTH_TEST);
 
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity(); 
    gluPerspective(45.0f, 1.0f, 0.001f, 10000.0f);    // don't care at this moment
 
    sun_radius=10.0f;
    earth_orbit_raidus=26.0f;
    earth_orbit_angle= 0.0f;
    moon_radius=0.7f;
    moon_selfrot_angle = 0.0f;
    moon_orbit_angle=0.0f;
    moon_orbit_raidus=3.5f;
    earth_radius=1.2f;
    earth_selfrot_angle=0.0f;
}
 
 
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity(); 
    gluLookAt(50.0, 50.0, 50.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
 
    draw_axes();
    // DRAW the SUn
 
    glPushMatrix();
 
    glScalef(sun_radius,sun_radius,sun_radius);
    glColor3f(1.0f,0.0f,0.0f);
    draw_cube();
    glPopMatrix();
 
 
 
    glTranslatef(earth_orbit_raidus*cos(2*M_PI*earth_orbit_angle/360),0.0f,
        earth_orbit_raidus*sin(2*M_PI*earth_orbit_angle/360));
 
 
    glPushMatrix(); // 지구랑 달을 묶어서 위의 트렌스레이션을 하기위해
    // 중괄호 같이 묶는다는 느낌.
    //지구
     
    glPushMatrix(); /////////////////////////////////////
    //현재 MV메트릭스를 저장하고 POP때 불러온다
    // 이게 계층척 그래픽스의 핵심. 모델간의 계층관계를 표현하기 위해.
    // 기존의 I 메트릭스를 PUSH
    glRotatef(earth_selfrot_angle,0.5,0.5,0);
    glScalef(earth_radius,earth_radius,earth_radius);
    glColor3f(0.0f,1.0f,0.0f);
    draw_cube();
    glPopMatrix();  ///////////////////////////////////
    // 기존의 pUSH 했던 I매트릭스를 pOP
 
     
 
    //dRAW THE MOON
    // 360 : 2*M_PI = Moon_orbit_angle : x ==> 2*M_PI*moon_orbit_angle/360
    glTranslatef(moon_orbit_raidus*cos(2*M_PI*moon_orbit_angle/360),0.0f,
        moon_orbit_raidus*sin(2*M_PI*moon_orbit_angle/360));
 
    glScalef(moon_radius,moon_radius,moon_radius);
    glColor3f(0.5f, 0.5f, 0.5f);
    glRotatef(moon_selfrot_angle,0,-1,0);
    draw_cube();
 
    glPopMatrix();
    glutSwapBuffers();
}
 
 
void draw_axes()
{
    glBegin(GL_LINES);
    // x axis
    glColor3f(0.2f, 0.2f, 0.2f);
    glVertex3f(-10.0f, 0.0f, 0.0f);
    glColor3f(1.0f, 0.0f, 0.0f);
    glVertex3f( 10.0f, 0.0f, 0.0f);
    // y axis
    glColor3f(0.2f, 0.2f, 0.2f);
    glVertex3f(0.0f, -10.0f, 0.0f);
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex3f(0.0f,  10.0f, 0.0f);
    // z axis
    glColor3f(0.2f, 0.2f, 0.2f);
    glVertex3f(0.0f, 0.0f, -10.0f);
    glColor3f(0.0f, 0.0f, 1.0f);
    glVertex3f(0.0f, 0.0f,  10.0f); 
    glEnd();
}
 
void draw_cube()
{
    glBegin(GL_QUADS);
    // front
    glVertex3f( 0.5f,  0.5f,  0.5f);
    glVertex3f(-0.5f,  0.5f,  0.5f);
    glVertex3f(-0.5f, -0.5f,  0.5f);
    glVertex3f( 0.5f, -0.5f,  0.5f);
    // back
    glVertex3f( 0.5f,  0.5f, -0.5f);
    glVertex3f( 0.5f, -0.5f, -0.5f);
    glVertex3f(-0.5f, -0.5f, -0.5f);
    glVertex3f(-0.5f,  0.5f, -0.5f);
    // top
    glVertex3f( 0.5f,  0.5f,  0.5f);
    glVertex3f( 0.5f,  0.5f, -0.5f);
    glVertex3f(-0.5f,  0.5f, -0.5f);
    glVertex3f(-0.5f,  0.5f,  0.5f);
    // bottom
    glVertex3f( 0.5f, -0.5f,  0.5f);
    glVertex3f(-0.5f, -0.5f,  0.5f);
    glVertex3f(-0.5f, -0.5f, -0.5f);
    glVertex3f( 0.5f, -0.5f, -0.5f);
    // left
    glVertex3f(-0.5f,  0.5f,  0.5f);
    glVertex3f(-0.5f,  0.5f, -0.5f);
    glVertex3f(-0.5f, -0.5f, -0.5f);
    glVertex3f(-0.5f, -0.5f,  0.5f);
    // right
    glVertex3f( 0.5f,  0.5f,  0.5f);
    glVertex3f( 0.5f, -0.5f,  0.5f);
    glVertex3f( 0.5f, -0.5f, -0.5f);
    glVertex3f( 0.5f,  0.5f, -0.5f);
    glEnd();
}
 
 
void idle()
{
    // TODO: ...
    earth_orbit_angle+=0.01f;
    if(earth_orbit_angle > 360){
        earth_orbit_angle = 0.0f;
    }
 
    moon_selfrot_angle+=0.01;//degree (도)
 
    if(moon_selfrot_angle > 360){
        moon_selfrot_angle = 0.0f;
    }
    moon_orbit_angle+=0.04; // 
    if(moon_orbit_angle > 360){
        moon_orbit_angle= 0.0f;
    }
    earth_selfrot_angle+=0.06f;
    if(earth_selfrot_angle > 360)
        earth_selfrot_angle=0.0f;
 
    glutPostRedisplay();
}
 
 
 
</cmath></gl>

'Sorce Bank' 카테고리의 다른 글

JOGL로 한 그래픽스 실습 <Lighitng>  (0) 2010.06.01
B-Tree  (0) 2010.05.15
Python BST + AVL  (0) 2010.05.08
Python HW 2번문제  (0) 2010.05.06
Python HW 1번문제  (0) 2010.05.06