Advertisement
Guest User

Untitled

a guest
May 24th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.00 KB | None | 0 0
  1. #include <GL/glew.h>
  2. #include <GLFW/glfw3.h>
  3. #include <GL/GLU.h>
  4. #include <cstdlib>
  5. #include <cstdio>
  6. #include <iostream>
  7. #include <vector>
  8. #include <cmath>
  9. #include <time.h>
  10. #include <soil.h>
  11. #include <fstream>
  12. #include <string>
  13.  
  14. using namespace std;
  15.  
  16. #define PI 3.14159265
  17.  
  18. float matr[] = {
  19. 1, 0, 0, 0,
  20. 0, 1, 0, 0,
  21. cos(PI / 4), sin(PI / 4), 1, 0,
  22. 0, 0, 0, 1,
  23. };
  24.  
  25. bool polygonVision = false, Anime = false, TextureVision = false;;
  26. clock_t global_time;
  27. GLuint textureID;
  28. GLfloat material_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  29. GLfloat light0_diffuse[] = { 1, 1, 1 };
  30. GLfloat light0_position[] = { 2.0, 2.0, 2.0, 1.0 };
  31. GLfloat light0_ambient[] = { 3, 3, 3 };
  32. GLfloat light0_spot_direction[] = { -1.0, -1.0, -1.0 };
  33.  
  34. struct point {
  35. GLdouble x, y, z;
  36. };
  37.  
  38. class Cone {
  39. public:
  40. vector<vector<point>> points;
  41. Cone();
  42. void construct();
  43. void draw();
  44.  
  45. float V = 1;
  46. float xalpha = 0, yalpha = 0, zalpha = 0;
  47. float xpos = 0, ypos = 0;
  48. double h = 1;
  49. double speed = 0;
  50. double flour = 0;
  51. int size = 40;
  52. double alpha;
  53. double R = 0.5;
  54. };
  55.  
  56. Cone::Cone(){
  57. construct();
  58. }
  59.  
  60. void Cone::construct() {
  61. points.resize(size);
  62. points[0].resize(size);
  63. for (int i = 0; i < size; i++)
  64. points[0][i] = point{ 0, 0, 0 };
  65.  
  66. for (int j = 1; j < size; j++) {
  67. points[j].resize(size);
  68. double gamma = alpha*size/8;
  69. double sinVal = sin(gamma);
  70. double cosVal = cos(gamma);
  71.  
  72. for (int i = 0; i < size; i++) {
  73. double fi = PI * 2 * i / size;
  74. double xt = R * (1 - (double)(j - 1) / size) * cos(fi);
  75. double zt = R * (1 - (double)(j - 1) / size) * sin(fi);
  76. double yt = (h*(j - 1)) / size;
  77. double xtOld = xt;
  78. xt = xt * cosVal - zt * sinVal;
  79. zt = xtOld * sinVal + zt * cosVal;
  80. points[j][i] = { xt, yt, zt };
  81. }
  82. }
  83. for (int i = 0; i < size; i++)
  84. points[size - 1][i] = point{ 0, h, 0 };
  85. }
  86.  
  87. void Cone::draw() {
  88. for (int i = 0; i < size - 1; i++)
  89. for (int j = 0; j < size; j++) {
  90. glBindTexture(GL_TEXTURE_2D, textureID);
  91. glBegin(GL_POLYGON);
  92.  
  93. double x0 = points[i][j].x, x1 = points[(i + 1) % size][j].x, x2 = points[i][(j + 1) % size].x, x3 = points[i + 1][(j + 1) % size].x,
  94. y0 = points[i][j].y, y1 = points[(i + 1) % size][j].y, y2 = points[i][(j + 1) % size].y, y3 = points[i + 1][(j + 1) % size].y,
  95. z0 = points[i][j].z, z1 = points[(i + 1) % size][j].z, z2 = points[i][(j + 1) % size].z, z3 = points[i + 1][(j + 1) % size].z;
  96.  
  97.  
  98. glNormal3f((y1 - y0)*(z2 - z1) - (z1 - z0)*(y2 - y1),
  99. (x1 - x0)*(z2 - z1) - (z1 - z0)*(x2 - x1),
  100. (x1 - x0)*(y2 - y1) - (y1 - y0)*(x2 - x1));
  101.  
  102. glTexCoord2f(0.0f, 0.0f);
  103. glVertex3f(x0, y0, z0);
  104. glTexCoord2f(1.0f, 0.0f);
  105. glVertex3f(x1, y1, z1);
  106. glTexCoord2f(0.5f, 1.0f);
  107. glVertex3f(x3, y3, z3);
  108. glTexCoord2f(0.5f, 1.0f);
  109. glVertex3f(x2, y2, z2);
  110. glEnd();
  111. }
  112. }
  113.  
  114. auto cone = new Cone();
  115.  
  116. void save() {
  117. std::ofstream ttt;
  118. ttt.open("./position.txt");
  119. if (ttt.is_open()) {
  120. ttt << "flour = (" << cone->flour << ") V = (" << cone->V << ") xalpha = (" << cone->xalpha << ") yalpha = (" << cone->yalpha << ") zalpha = (" << cone->zalpha << ") size = (" << cone->size << ") xpos = (" << cone->xpos << ") ypos = (" << cone->ypos << ") speed = (" << cone->speed << ")";
  121. }
  122. }
  123.  
  124. void load() {
  125. std::ifstream file("./position.txt");
  126. std::string str, word = "";
  127. getline(file, str);
  128. file.close();
  129. int i = 1;
  130.  
  131. for (; str[i] != '('; i++);
  132. i++;
  133. for (; str[i] != ')'; word += str[i++]);
  134. double flour = std::stod(word);
  135. word = "";
  136.  
  137. cone = new Cone();
  138.  
  139. for (vector<vector<point>>::iterator iter = cone->points.begin(); iter != cone->points.end(); ++iter)
  140. for (vector<point>::iterator iter1 = (*iter).begin(); iter1 != (*iter).end(); ++iter1)
  141. (*iter1).y += flour;
  142.  
  143. cone->flour = flour;
  144.  
  145. for (; str[i] != '('; i++);
  146. i++;
  147. for (; str[i] != ')'; word += str[i++]);
  148. cone->V = std::stod(word);
  149. word = "";
  150.  
  151. for (; str[i] != '('; i++);
  152. i++;
  153. for (; str[i] != ')'; word += str[i++]);
  154. cone->xalpha = std::stod(word);
  155. word = "";
  156.  
  157. for (; str[i] != '('; i++);
  158. i++;
  159. for (; str[i] != ')'; word += str[i++]);
  160. cone->yalpha = std::stod(word);
  161. word = "";
  162.  
  163. for (; str[i] != '('; i++);
  164. i++;
  165. for (; str[i] != ')'; word += str[i++]);
  166. cone->zalpha = std::stod(word);
  167. word = "";
  168.  
  169. for (; str[i] != '('; i++);
  170. i++;
  171. for (; str[i] != ')'; word += str[i++]);
  172. cone->size = std::stod(word);
  173. word = "";
  174.  
  175. for (; str[i] != '('; i++);
  176. i++;
  177. for (; str[i] != ')'; word += str[i++]);
  178. cone->xpos = std::stod(word);
  179. word = "";
  180.  
  181. for (; str[i] != '('; i++);
  182. i++;
  183. for (; str[i] != ')'; word += str[i++]);
  184. cone->ypos = std::stod(word);
  185. word = "";
  186.  
  187. for (; str[i] != '('; i++);
  188. i++;
  189. for (; str[i] != ')'; word += str[i++]);
  190. cone->speed = std::stod(word);
  191. word = "";
  192. }
  193.  
  194. void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
  195. if (!action || action == GLFW_REPEAT) {
  196. switch (key) {
  197. case GLFW_KEY_L:
  198. load();
  199. break;
  200. case GLFW_KEY_K:
  201. save();
  202. break;
  203. case GLFW_KEY_ENTER:
  204. global_time = clock();
  205. Anime ^= true;
  206. break;
  207. case GLFW_KEY_UP:
  208. cone->ypos += 0.1;
  209. break;
  210. case GLFW_KEY_DOWN:
  211. cone->ypos -= 0.1;
  212. break;
  213. case GLFW_KEY_LEFT:
  214. cone->xpos -= 0.1;
  215. break;
  216. case GLFW_KEY_RIGHT:
  217. cone->xpos += 0.1;
  218. break;
  219. case GLFW_KEY_R:
  220. if (!polygonVision)
  221. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  222. else
  223. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  224. polygonVision = !polygonVision;
  225. break;
  226. case GLFW_KEY_T:
  227. TextureVision ^= true;
  228. break;
  229. case GLFW_KEY_S:
  230. cone->zalpha -= 4;
  231. break;
  232. case GLFW_KEY_W:
  233. cone->zalpha += 4;
  234. break;
  235. case GLFW_KEY_A:
  236. cone->alpha -= 0.2;
  237. cone->construct();
  238. break;
  239. case GLFW_KEY_D:
  240. cone->alpha += 0.2;
  241. cone->construct();
  242. break;
  243. case GLFW_KEY_Q:
  244. if (cone->size < 64) {
  245. cone->size++;
  246. cone->construct();
  247. }
  248. break;
  249. case GLFW_KEY_E:
  250. if (cone->size > 6) {
  251. cone->size--;
  252. cone->construct();
  253. }
  254. break;
  255. default:
  256. break;
  257. }
  258. }
  259. }
  260.  
  261. void scrollCallback(GLFWwindow* window, double xOffs, double yOffs) {
  262. cone->V += yOffs * 0.1;
  263. }
  264.  
  265. void animation() {
  266. if (cone->flour <= -1)
  267. cone->speed = -cone->speed;
  268. clock_t local_time = clock();
  269. double time = ((double)local_time) / CLOCKS_PER_SEC - ((double)global_time) / CLOCKS_PER_SEC;
  270. global_time = local_time;
  271. cone->speed -= time;
  272. double ypos = time * cone->speed;
  273. for (vector<vector<point>>::iterator iter = cone->points.begin(); iter != cone->points.end(); ++iter)
  274. for (vector<point>::iterator iter1 = (*iter).begin(); iter1 != (*iter).end(); ++iter1)
  275. (*iter1).y += ypos;
  276. cone->flour += ypos;
  277. }
  278.  
  279. void display(GLFWwindow* window) {
  280.  
  281. glClearColor(0.1, 0.1, 0.1, 0.1);
  282. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  283. glMatrixMode(GL_MODELVIEW);
  284. glLoadIdentity();
  285.  
  286. if (Anime) animation();
  287.  
  288. if (TextureVision) glEnable(GL_TEXTURE_2D);
  289. else glDisable(GL_TEXTURE_2D);
  290.  
  291. glTranslatef(cone->xpos, cone->ypos, 0);
  292. glRotated(cone->xalpha, 0, 1, 0);
  293. glRotated(cone->yalpha, 1, 0, 0);
  294. glRotated(cone->zalpha, 0, 0, 1);
  295. glScalef(cone->V, cone->V, cone->V);
  296. cone->draw();
  297. glfwSwapBuffers(window);
  298. glfwPollEvents();
  299. }
  300.  
  301. int main() {
  302. if (!glfwInit()) exit(1);
  303. glfwWindowHint(GLFW_SAMPLES, 4);
  304.  
  305. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  306. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  307. int width = 800, height = 800;
  308.  
  309. GLFWwindow* window = glfwCreateWindow(width, height, "L6", NULL, NULL);
  310.  
  311. if (!window) {
  312. glfwTerminate();
  313. exit(1);
  314. }
  315. glfwMakeContextCurrent(window);
  316. glfwSetKeyCallback(window, keyCallback);
  317. glfwSetScrollCallback(window, scrollCallback);
  318.  
  319. glEnable(GL_NORMALIZE);
  320. glEnable(GL_LIGHTING);
  321. glEnable(GL_LIGHT0);
  322. glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
  323. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse);
  324.  
  325. glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
  326. glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
  327. glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 50);
  328. glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light0_spot_direction);
  329. glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 30.0);
  330.  
  331. int w = 512, h = 512;
  332. unsigned char* image = soil_load_image("./gr.bmp", &w, &h, 0, soil_load_rgb);
  333. glgentextures(1, &textureid);
  334. glbindtexture(gl_texture_2d, textureid);
  335.  
  336. gltexparameteri(gl_texture_2d, gl_texture_wrap_s, gl_repeat);
  337. gltexparameteri(gl_texture_2d, gl_texture_wrap_t, gl_repeat);
  338. gltexparameteri(gl_texture_2d, gl_texture_min_filter, gl_linear);
  339. gltexparameteri(gl_texture_2d, gl_texture_mag_filter, gl_linear);
  340. glteximage2d(gl_texture_2d, 0, gl_rgb, w, h, 0, gl_rgb, gl_unsigned_byte, image);
  341. soil_free_image_data(image);
  342. glbindtexture(gl_texture_2d, 0);
  343.  
  344. glEnable(GL_DEPTH_TEST);
  345. glViewport(0, 0, width, height);
  346.  
  347. glMatrixMode(GL_PROJECTION);
  348. glLoadMatrixf(matr);
  349. gluPerspective(60, (float)width / height, 1, 100);
  350. glTranslated(-1.5, -2, -6);
  351.  
  352. while (!glfwWindowShouldClose(window))
  353. display(window);
  354.  
  355. glfwDestroyWindow(window);
  356. glfwTerminate();
  357. return 0;
  358. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement