include stdlib include GL glut include iostream include clocale using

  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
#include <stdlib.h>
#include <GL/glut.h>
#include <iostream>
#include <clocale>
using namespace std;
//Инициализация
void init1(void)
{
glClearColor(0.0,0.0,0.0,0.0);
glShadeModel(GL_FLAT);
}
//Отображение
void display1(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);
//Очистить матрицу
glLoadIdentity();
//Видовая трансформация(камера)
gluLookAt(0.0,0.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0);
//Модельная трансформация
glScalef(1.0,2.0,1.0);
glutWireCube(1.0);
glFlush();
}
//Изменение размеров окна
void reshape1(int w, int h)
{
glViewport(0,0,(GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0,1.0,-1.0,1.0,1.5,20.0);
glMatrixMode(GL_MODELVIEW);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Инициализация
void init2(void)
{
glClearColor(0.0,0.0,0.0,0.0);
glShadeModel(GL_FLAT);
}
//Изменение размеров окна
void reshape2(int w, int h)
{
glViewport(0,0,(GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0,(GLfloat) w/(GLfloat) h,1.0,20.0); glMatrixMode(GL_MODELVIEW);
}
//Отображение
void display2(void)
{
GLdouble eqn[4]={0.0,1.0,0.0,0.0};
GLdouble eqn2[4]={1.0,0.0,0.0,0.0};
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);
glPushMatrix();
glTranslatef(0.0,0.0,-5.0);
//Отсечь нижнюю половину (y<0)
glClipPlane(GL_CLIP_PLANE0,eqn);
glEnable(GL_CLIP_PLANE0);
//Отсечь левую половину (x<0)
glClipPlane(GL_CLIP_PLANE1,eqn2);
glEnable(GL_CLIP_PLANE1);
glRotatef(90.0,1.0,0.0,0.0);
glutWireSphere(2.0,20,16);
glPopMatrix();
glFlush();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int year=0, day=0;
//Инициализация
void init3(void)
{
glClearColor(0.0,0.0,0.0,0.0);
glShadeModel(GL_FLAT);
}
//Изменение размеров окна
void reshape3(int w, int h)
{
glViewport(0,0,(GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0,(GLfloat) w/ (GLfloat) h,1.0,20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0);
}
//Отображение
void display3(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);
glPushMatrix();
//Рисуем солнце
glutWireSphere(1.0,20,16);
glRotatef((GLfloat)year,0.0,1.0,0.0);
glTranslatef(2.0,0.0,0.0);
glRotatef((GLfloat)day,0.0,1.0,0.0);
//Рисуем планету
glutWireSphere(0.2,10,8);
glPopMatrix();
glutSwapBuffers();
}
//Реакция на клавиатуру
void keyboard(unsigned char key,int x, int y)
{
switch(key)
{
case 'd':
day=(day+10)%360;
glutPostRedisplay();
break;
case 'D':
day=(day-10)%360;
glutPostRedisplay();
break;
case 'y':
year=(year+5)%360;
glutPostRedisplay();
break;
case 'Y':
year=(year-5)%360;
glutPostRedisplay();
break;
default:
break;
}
}
int main(int argc, char **argv)
{
setlocale(LC_CTYPE, "rus");
int number;
cout <<"1 - Куб" <<endl;
cout <<"2 - Усечонная проволочная сфера" <<endl;
cout <<"3 - Планетарная система" <<endl;
cin >>number;
if (number == 1) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("Transformed Cube");
init1();
glutDisplayFunc(display1);
glutReshapeFunc(reshape1);
glutMainLoop();
} else if (number == 2) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("Wireframe Sphere with Two Clipping Planes");
init2();
glutDisplayFunc(display2);
glutReshapeFunc(reshape2);
glutMainLoop();
} else if (number == 3) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("Planetary System");
init3();
glutDisplayFunc(display3);
glutReshapeFunc(reshape3);
glutKeyboardFunc(keyboard);
glutMainLoop();
} else {
return 0;
}
return 0;
}