include stdafx include Windows include fstream using namespace std def

  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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include "stdafx.h"
#include <Windows.h>
#include <fstream>
using namespace std;
#define PI (3.141592653)
struct cord{
double x, y, z;
};
struct color{
GLfloat r, g, b;
};
int ifi=100, ipsi=1000; // параметры тора: количество точку малой и большой окружностей
cord p1, p2; // промежудочные точки анимации
cord *ver[5]; // массивы вершин анимации: начальный, конечный, текущий
cord *norm; // массив нормалей к вершинам
double R=300, r=80; // параметры тора: радиусы большой и малой окружностей
int f = 0; // флаг движения анимации
float alpha = 10; // угол поворота фигуры
double t = 0; // параметр анимации
int s_height = 600, s_width = 800; // высота и ширина экрана
color *text; // текстура
int texture = 0;
static void cursor_callback(GLFWwindow* window, double x, double y){
}
static void mouse_callback(GLFWwindow* window, int button, int action, int mods){
}
static void keyboard_callback(GLFWwindow* window, int key, int scancode, int action, int mods){
if (action == GLFW_RELEASE) return;
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
if (key == GLFW_KEY_SPACE && action == GLFW_PRESS){
glLoadIdentity();
t = 0;
}
if (key == GLFW_KEY_ENTER && action == GLFW_PRESS){
if (!f)
t = 0.001;
else
t = 0.999;
f = !f;
}
if (key == GLFW_KEY_A && action == GLFW_PRESS){
glRotated(alpha, 0, 1, 0);
}
if (key == GLFW_KEY_D && action == GLFW_PRESS){
glRotated(-alpha, 0, 1, 0);
}
if (key == GLFW_KEY_W && action == GLFW_PRESS){
glRotated(alpha, 1, 0, 0);
}
if (key == GLFW_KEY_S && action == GLFW_PRESS){
glRotated(-alpha, 1, 0, 0);
}
if (key == GLFW_KEY_Q && action == GLFW_PRESS){
glScalef(GLfloat(0.9), GLfloat(0.9), GLfloat(0.9));
}
if (key == GLFW_KEY_E && action == GLFW_PRESS){
glScalef(GLfloat(1.1), GLfloat(1.1), GLfloat(1.1));
}
if (key == GLFW_KEY_UP && action == GLFW_PRESS){
glTranslatef(0, 10, 0);
}
if (key == GLFW_KEY_DOWN && action == GLFW_PRESS){
glTranslatef(0, -10, 0);
}
if (key == GLFW_KEY_LEFT && action == GLFW_PRESS){
glTranslatef(-10, 0, 0);
}
if (key == GLFW_KEY_RIGHT && action == GLFW_PRESS){
glTranslatef(10, 0, 0);
}
if (key == GLFW_KEY_7 && action == GLFW_PRESS){
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
if (key == GLFW_KEY_8 && action == GLFW_PRESS){
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
if (key == GLFW_KEY_9 && action == GLFW_PRESS){
texture = !texture;
}
if (key == GLFW_KEY_G && action == GLFW_PRESS){
float a[16];
glGetFloatv(GL_MODELVIEW_MATRIX, a);
ofstream file;
file.open("D:\\save.txt");
for (int i = 0; i < 4; i++){
for (int j = 0; j < 4; j++)
file << a[i * 4 + j] << " ";
file << endl;
}
file << f << endl;
file << t << endl;
file << texture << endl;
file.close();
}
if (key == GLFW_KEY_H && action == GLFW_PRESS){
float a[16];
setlocale(LC_ALL, "RUS");
fstream file;
file.open("D:\\save.txt");
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
file >> a[i * 4 + j];
file >> f;
file >> t;
file >> texture;
glLoadMatrixf(a);
file.close();
}
}
static void resize_callback(GLFWwindow* window, int width, int height){
double fi = 45;
GLdouble m[16] = {
1, 0, 0, 0,
0, 1, 0, 0,
-0.5*cos(fi*PI/180), -0.5*sin(fi*PI/180), 1, 0,
0, 0, 0, 1
};
glViewport(0, 0, width, width);
glMatrixMode(GL_PROJECTION);
glLoadMatrixd(m);
glOrtho(-(GLdouble)1000, (GLdouble)1000, -(GLdouble)1000, (GLdouble)1000, -(GLdouble)1000, (GLdouble)1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
s_width = width;
s_height = height;
}
void anim(){
double v = 1 - t;
for (int i = 0; i < ipsi*ifi; i++){
ver[2][i].x = v*v*v*(ver[0][i].x) + 3 * t*v*v*(p1.x) + 3 * t*t*v*(p2.x) + t*t*t*(ver[1][i].x);
ver[2][i].y = v*v*v*(ver[0][i].y) + 3 * t*v*v*(p1.y) + 3 * t*t*v*(p2.y) + t*t*t*(ver[1][i].y);
ver[2][i].z = v*v*v*(ver[0][i].z) + 3 * t*v*v*(p1.z) + 3 * t*t*v*(p2.z) + t*t*t*(ver[1][i].z);
}
}
void put(int i,int j){
int k = i*ifi + j;
glNormal3d(norm[k].x, norm[k].y, norm[k].z);
glTexCoord2f(float(j) / float(ifi), float(i) / float(ipsi));
glVertex3d(ver[2][k].x, ver[2][k].y, ver[2][k].z);
}
cord normal(cord a1, cord a0, cord b1, cord b0){
cord c;
c.x = (a1.y - a0.y)*(b1.z - b0.z) - (b1.y - b0.y)*(a1.z - a0.z);
c.y = (a1.z - a0.z)*(b1.x - b0.x) - (a1.x - a0.x)*(b1.z - b0.z);
c.z = (a1.x - a0.x)*(b1.y - b0.y) - (a1.y - a0.y)*(b1.x - b0.x);
return c;
}
void normalize(){
for (int i = 0; i < ipsi; i++)
for (int j = 0; j < ifi; j++){
cord p0 = ver[2][i*ifi + j];
cord p1 = ver[2][((i + 1) % ipsi)*ifi + j];
cord p2 = ver[2][i*ifi + (j + 1) % ifi];
cord p3 = ver[2][((i - 1 + ipsi) % ipsi)*ifi + j];
cord p4 = ver[2][i*ifi + (j - 1 + ifi) % ifi];
cord v1 = normal(p1, p0, p2, p0);
cord v2 = normal(p2, p0, p3, p0);
cord v3 = normal(p3, p0, p4, p0);
cord v4 = normal(p4, p0, p1, p0);
norm[i*ifi + j].x = (v1.x + v2.x + v3.x + v4.x) / 4.0;
norm[i*ifi + j].y = (v1.y + v2.y + v3.y + v4.y) / 4.0;
norm[i*ifi + j].z = (v1.z + v2.z + v3.z + v4.z) / 4.0;
}
}
void draw_A(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//light
glPushMatrix();
glLoadIdentity();
GLfloat light_diffuse[] = { 1, 1, 1 };
GLfloat light_position[] = { -1000.0, -1000.0, 0.0, 1.0 };
GLfloat light_spot_direction[] = { 1.0, 1.0, 0.0 };
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 30);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light_spot_direction);
glPopMatrix();
//model
if (texture)
glEnable(GL_TEXTURE_2D);
else
glDisable(GL_TEXTURE_2D);
anim();
normalize();
glColor3f(0.5, 0, 0.5);
for (int i = 0; i < ipsi; i++){
for (int j = 0; j < ifi; j++){
glBegin(GL_POLYGON);
put(i,j);
put(i,(j + 1) % ifi);
put(((i + 1) % ipsi),(j + 1) % ifi);
put(((i + 1) % ipsi),j);
glEnd();
}
}
if (t < 1 && t>0)
if (f)
t += 0.01;
else
t -= 0.01;
glDisable(GL_LIGHT0);
}
void predraw(){
int k = 0;
double fi, psi;
for (int i = 0; i < 3; i++)
ver[i] = (cord*)malloc(sizeof(cord)*ifi*ipsi);
norm = (cord*)malloc(sizeof(cord)*ifi*ipsi);
for (int i = 0; i < ipsi; i++){
psi = 2 * i* PI / ipsi;
for (int j = 0; j < ifi; j++){
fi = 2 * j* PI / ifi;
ver[0][k].x = (R + r*cos(fi))*cos(psi);
ver[0][k].y = (R + r*cos(fi))*sin(psi);
ver[0][k].z = r*sin(fi);
k++;
}
}
k = 0;
p1.x = 0; p1.y = 0; p1.z = 0;
p2.x = 100; p2.y = 100; p2.z = 0;
for (int i = 0; i < ipsi; i++){
psi = 2 * i* PI / ipsi;
for (int j = 0; j < ifi; j++){
fi = 2 * j* PI / ifi;
ver[1][k].x = (R + r*cos(fi))*cos(psi);
ver[1][k].y = (R * 1.5 + r*cos(fi))*sin(psi);
ver[1][k].z = 2*r*sin(fi);
k++;
}
}
// текстура
text = new color[ipsi*ifi*3];
for (int i=0;i<ipsi;i++)
for (int j = 0; j < ifi; j++){
text[i*ifi + j].r = 0.5;
text[i*ifi + j].g = 0;
text[i*ifi + j].b = float(i) / float(ipsi);
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, 3, ipsi, ifi, 0, GL_RGB, GL_FLOAT, text);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
}
int main(int argc, _TCHAR* argv[]){
if (!glfwInit()){
printf("glfwInit failed\n");
return -1;
}
GLFWwindow* window;
window = glfwCreateWindow(s_width, s_height, "Test app", NULL, NULL);
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, keyboard_callback);
glfwSetFramebufferSizeCallback(window, resize_callback);
glfwSetMouseButtonCallback(window, mouse_callback);
glfwSetCursorPosCallback(window, cursor_callback);
resize_callback(window, s_width, s_height);
predraw();
glEnable(GL_LIGHTING);
while (!glfwWindowShouldClose(window)){
draw_A();
glfwSwapBuffers(window);
if ((t<=0) || (t>=1))
glfwWaitEvents();
}
for (int i = 0; i<5; i++)
free(ver[i]);
free(norm);
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}