Advertisement
bulletmys

Untitled

Jun 6th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.52 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <string>
  5. #include <glew.h>
  6. #include <vector>
  7. #include <cmath>
  8. #include <time.h>
  9. #include <fstream>
  10. #include <glfw3.h>
  11.  
  12. #include "SOIL.h"
  13.  
  14. using namespace std;
  15.  
  16. #define PI 3.14159265
  17.  
  18. static int fps = 0;
  19. static clock_t fps_global_time;
  20.  
  21. bool leftButtonIsPressed = false;
  22. bool polygonMode = false;
  23. double xPosOld = 0, yPosOld = 0;
  24. double G = -1.5;
  25. clock_t global_time;
  26. bool modA = false, modT = false;
  27. GLfloat material_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  28. GLuint textureID;
  29. GLfloat light0_diffuse[] = { 1,1,1 };
  30. GLfloat light0_position[] = { 0.0, 0.0, 0.0, 1.0 };
  31. GLfloat light0_ambient[] = { 1, 1, 1 };
  32. GLfloat light0_spot_direction[] = { 1.0, 1.0, -1.0 };
  33.  
  34. void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
  35. void cursorPos(GLFWwindow* window, double xpos, double ypos);
  36. void mouseCallback(GLFWwindow *window, int button, int action, int mods);
  37. void scrollCallback(GLFWwindow* window, double xOffs, double yOffs);
  38.  
  39. const GLfloat cabinet[] = {
  40. 1 ,0 , 0, 0,
  41. 0 ,1 ,0 , 0,
  42. -0.5*cos(PI / 4),-0.5*sin(PI / 4),1, 0,
  43. 0 ,0 ,0 ,1 };
  44.  
  45. const GLfloat cabinet2[] = {
  46. 1 ,0 , 0, 0,
  47. 0 ,1 ,0 , 0,
  48. -0.5*cos(PI / 6),-0.5*sin(PI / 6),1, 0,
  49. 0 ,0 ,0 ,1 };
  50.  
  51. void perspectiveGL(GLdouble fovY, GLdouble aspect, GLdouble zNear, GLdouble zFar)
  52. {
  53. const GLdouble pi = 3.1415926535897932384626433832795;
  54. GLdouble fW, fH;
  55.  
  56. fH = tan(fovY / 360 * pi) * zNear;
  57. fW = fH * aspect;
  58.  
  59. glFrustum(-fW, fW, -fH, fH, zNear, zFar);
  60. }
  61.  
  62. struct Point {
  63. GLdouble x;
  64. GLdouble y;
  65. GLdouble z;
  66. };
  67.  
  68. class Pyramid {
  69. public:
  70. vector<vector<Point>> points;
  71. Pyramid(double r, double h, int n, int m);
  72.  
  73. void construct();
  74. void draw();
  75.  
  76. float scaleValue = 1.5;
  77. float xAngle = 0, yAngle = 0, zAngle = 0;
  78. float dx = 0, dy = 0, dz = 0;
  79. double h;
  80. double V = 0;
  81. double min_y = 0;
  82. int n;
  83. int m;
  84. double R = 0.5;
  85.  
  86. };
  87.  
  88. Pyramid::Pyramid(double r, double h, int n, int m) :
  89. R(r), h(h), n(n + 1), m(m) {
  90. construct();
  91. }
  92.  
  93. void Pyramid::construct() {
  94. points.resize(n);
  95. points[0].resize(m);
  96.  
  97. for (int j = 1; j < n; j++) {
  98. points[j].resize(m);
  99.  
  100.  
  101. for (int i = 0; i < m; i++) {
  102. double fi = PI * 2 * i / m;
  103. double xt = R * (1 - (double)(j - 2) / n) * cos(fi);
  104. double zt = R * (1 - (double)(j - 2) / n) * sin(fi);
  105. double yt = (h*(j - 2)) / (n - 4);
  106.  
  107. points[j][i] = { xt, yt, zt };
  108. }
  109. }
  110. for (int i = 0; i < m; i++) {
  111. points[n - 1][i] = Point{ 0, h, 0 };
  112. points[0][i] = Point{ 0, 0, 0 };
  113. points[1][i] = Point{ 0, 0, 0 };
  114. }
  115. }
  116.  
  117. void Pyramid::draw() {
  118. glFrontFace(GL_CW);
  119. //glBegin(GL_TRIANGLE_STRIP);
  120.  
  121. for (int i = 0; i < n - 1; i++) {
  122.  
  123.  
  124. for (int j = 0; j < m; j++) {
  125.  
  126. glBindTexture(GL_TEXTURE_2D, textureID);
  127. glBegin(GL_TRIANGLES);
  128.  
  129. double x0 = points[i][j].x, x1 = points[(i + 1) % n][j].x, x2 = points[i][(j + 1) % m].x;
  130. double y0 = points[i][j].y, y1 = points[(i + 1) % n][j].y, y2 = points[i][(j + 1) % m].y;
  131. double z0 = points[i][j].z, z1 = points[(i + 1) % n][j].z, z2 = points[i][(j + 1) % m].z;
  132.  
  133. glNormal3f((y1 - y0)*(z2 - z1) - (z1 - z0)*(y2 - y1),
  134. (x1 - x0)*(z2 - z1) - (z1 - z0)*(x2 - x1),
  135. (x1 - x0)*(y2 - y1) - (y1 - y0)*(x2 - x1));
  136.  
  137. glTexCoord2f(0.0f, 0.0f);
  138. glVertex3f(x0, y0, z0);
  139. glTexCoord2f(1.0f, 0.0f);
  140. glVertex3f(x1, y1, z1);
  141. glTexCoord2f(0.5f, 1.0f);
  142. glVertex3f(x2, y2, z2);
  143.  
  144. x0 = points[i][(j + 1) % m].x, x1 = points[(i + 1) % n][j].x, x2 = points[i + 1][(j + 1) % m].x;
  145. y0 = points[i][(j + 1) % m].y, y1 = points[(i + 1) % n][j].y, y2 = points[i + 1][(j + 1) % m].y;
  146. z0 = points[i][(j + 1) % m].z, z1 = points[(i + 1) % n][j].z, z2 = points[i + 1][(j + 1) % m].z;
  147.  
  148. glTexCoord2f(0.0f, 0.0f);
  149. glVertex3f(x0, y0, z0);
  150. glTexCoord2f(1.0f, 0.0f);
  151. glVertex3f(x1, y1, z1);
  152. glTexCoord2f(0.5f, 1.0f);
  153. glVertex3f(x2, y2, z2);
  154. glEnd();
  155.  
  156. }
  157. }
  158. //glEnd();
  159.  
  160. fps++;
  161. clock_t fps_time = clock();
  162. double time = ((double)fps_time) / CLOCKS_PER_SEC - ((double)fps_global_time) / CLOCKS_PER_SEC;
  163. if (time >= 1) {
  164. std::cout << "fps : " << fps << std::endl;
  165. fps_global_time = fps_time;
  166. fps = 0;
  167. }
  168. }
  169.  
  170. auto MyPyramid = new Pyramid(0.5, 1, 15, 5);
  171. auto smallPyramid = new Pyramid(0.2, 1, 15, 5);
  172.  
  173.  
  174. void animation() {
  175. if (MyPyramid->min_y <= -1) {
  176. MyPyramid->V *= -1;
  177. }
  178.  
  179. clock_t local_time = clock();
  180. double time = ((double)local_time) / CLOCKS_PER_SEC - ((double)global_time) / CLOCKS_PER_SEC;
  181. global_time = local_time;
  182.  
  183. MyPyramid->V += G * time;
  184.  
  185. double dy = time * MyPyramid->V;
  186.  
  187. for (vector<vector<Point>>::iterator it = MyPyramid->points.begin(); it != MyPyramid->points.end(); ++it) {
  188. for (vector<Point>::iterator it2 = (*it).begin(); it2 != (*it).end(); ++it2) {
  189. (*it2).y += dy;
  190. }
  191. }
  192. MyPyramid->min_y += dy;
  193. }
  194.  
  195.  
  196. int main()
  197. {
  198. if (!glfwInit()) {
  199. exit(EXIT_FAILURE);
  200. }
  201. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  202. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  203. int width = 800, height = 600;
  204.  
  205. GLFWwindow* window = glfwCreateWindow(width, height, "noname", nullptr, nullptr);
  206.  
  207. if (!window) {
  208. glfwTerminate();
  209. exit(EXIT_FAILURE);
  210. }
  211.  
  212.  
  213. glfwMakeContextCurrent(window);
  214. glfwSetKeyCallback(window, keyCallback);
  215. glfwSetCursorPosCallback(window, cursorPos);
  216. glfwSetMouseButtonCallback(window, mouseCallback);
  217. glfwSetScrollCallback(window, scrollCallback);
  218.  
  219. glEnable(GL_CULL_FACE);
  220.  
  221. glEnable(GL_NORMALIZE);
  222.  
  223. glEnable(GL_LIGHTING);
  224. glEnable(GL_LIGHT0);
  225. glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
  226. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse);
  227.  
  228. glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
  229. glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
  230. glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 100);
  231. glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light0_spot_direction);
  232. glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 300.0);
  233.  
  234. int w = 512, h = 512;
  235. unsigned char* image = SOIL_load_image("./met.bmp", &w, &h, 0, SOIL_LOAD_RGB);
  236. glGenTextures(1, &textureID);
  237. glBindTexture(GL_TEXTURE_2D, textureID);
  238.  
  239. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  240. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  241. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  242. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  243. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
  244. SOIL_free_image_data(image);
  245. glBindTexture(GL_TEXTURE_2D, 0);
  246.  
  247.  
  248.  
  249.  
  250. glEnable(GL_DEPTH_TEST);
  251. glViewport(0, 0, width, height);
  252.  
  253. glMatrixMode(GL_PROJECTION);
  254. // glMatrixMode(GL_PROJECTION);
  255. glLoadIdentity();
  256. // glOrtho(-width, width, -height, height, -1000, 1000);
  257. // FLAG ? glMultMatrixf(cabinet) : glMultMatrixf(cabinet2);
  258. // glRotatef(90, 1, 0, 0);
  259. glMultMatrixf(cabinet);
  260. glScalef(0.5, 0.5, 0.5);
  261. //perspectiveGL(60, (float)width / height, 1, 100);
  262. /*glTranslated(0, 0, -6);
  263. glRotated(45, 0, 1, 0);*/
  264.  
  265. while (!glfwWindowShouldClose(window)) {
  266.  
  267. if (modT) {
  268. glEnable(GL_TEXTURE_2D);
  269. }
  270. else {
  271. glDisable(GL_TEXTURE_2D);
  272. }
  273.  
  274. glClearColor(0.2, 0.3, 0.2, 1);
  275. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  276. glMatrixMode(GL_MODELVIEW);
  277. glLoadIdentity();
  278. glTranslatef(MyPyramid->dz, MyPyramid->dy, MyPyramid->dx);
  279.  
  280. glRotated(MyPyramid->xAngle, 0, 1, 0);
  281. glRotated(MyPyramid->yAngle, 1, 0, 0);
  282. glRotated(MyPyramid->zAngle, 0, 0, 1);
  283. glScalef(MyPyramid->scaleValue, MyPyramid->scaleValue, MyPyramid->scaleValue);
  284.  
  285. if (modA) animation();
  286. MyPyramid->draw();
  287.  
  288.  
  289. glfwSwapBuffers(window);
  290. glfwPollEvents();
  291. }
  292.  
  293. glfwDestroyWindow(window);
  294. glfwTerminate();
  295. exit(EXIT_SUCCESS);
  296. }
  297.  
  298. void save() {
  299. std::ofstream ttt;
  300. ttt.open("./position.txt");
  301. if (ttt.is_open()) {
  302. ttt << "min_y = (" << MyPyramid->min_y << ") V = (" << MyPyramid->V << ") scaleValue = (" << MyPyramid->scaleValue << ") xAngle = (" << MyPyramid->xAngle << ") yAngle = (" << MyPyramid->yAngle << ") zAngle = (" << MyPyramid->zAngle << ")";
  303. }
  304. }
  305.  
  306.  
  307. void load() {
  308. //std::ifstream file("./position.txt");
  309. std::string s, t = "";
  310. //getline(file, s);
  311. //file.close();
  312. int i = 0;
  313.  
  314. for (; s[i] != '('; i++);
  315. i++;
  316. for (; s[i] != ')'; t += s[i++]);
  317. double min_y = std::stod(t);
  318. t = "";
  319.  
  320. MyPyramid = new Pyramid(0.5, 1, 15, 5);
  321.  
  322. for (vector<vector<Point>>::iterator it = MyPyramid->points.begin(); it != MyPyramid->points.end(); ++it) {
  323. for (vector<Point>::iterator it2 = (*it).begin(); it2 != (*it).end(); ++it2) {
  324. (*it2).y += min_y;
  325. }
  326. }
  327.  
  328. MyPyramid->min_y = min_y;
  329.  
  330.  
  331.  
  332. for (; s[i] != '('; i++);
  333. i++;
  334. for (; s[i] != ')'; t += s[i++]);
  335. MyPyramid->V = std::stod(t);
  336. t = "";
  337.  
  338. for (; s[i] != '('; i++);
  339. i++;
  340. for (; s[i] != ')'; t += s[i++]);
  341. MyPyramid->scaleValue = std::stod(t);
  342. t = "";
  343.  
  344. for (; s[i] != '('; i++);
  345. i++;
  346. for (; s[i] != ')'; t += s[i++]);
  347. MyPyramid->xAngle = std::stod(t);
  348. t = "";
  349.  
  350. for (; s[i] != '('; i++);
  351. i++;
  352. for (; s[i] != ')'; t += s[i++]);
  353. MyPyramid->yAngle = std::stod(t);
  354. t = "";
  355.  
  356. for (; s[i] != '('; i++);
  357. i++;
  358. for (; s[i] != ')'; t += s[i++]);
  359. MyPyramid->zAngle = std::stod(t);
  360. t = "";
  361.  
  362.  
  363.  
  364. }
  365.  
  366.  
  367. void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
  368. if (!action || action == GLFW_REPEAT) {
  369. switch (key) {
  370. case GLFW_KEY_ESCAPE:
  371. glfwSetWindowShouldClose(window, GLFW_TRUE);
  372. break;
  373. case GLFW_KEY_2:
  374. load();
  375. break;
  376. case GLFW_KEY_1:
  377. save();
  378. break;
  379. case GLFW_KEY_SPACE:
  380. global_time = clock();
  381. modA ^= true;
  382. break;
  383. case GLFW_KEY_UP:
  384. MyPyramid->dy += 0.1;
  385. break;
  386. case GLFW_KEY_DOWN:
  387. MyPyramid->dy -= 0.1;
  388. break;
  389. case GLFW_KEY_LEFT:
  390. MyPyramid->dx += 0.1;
  391. break;
  392. case GLFW_KEY_RIGHT:
  393. MyPyramid->dx -= 0.1;
  394. break;
  395. case GLFW_KEY_C:
  396. if (!polygonMode) {
  397. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  398. polygonMode = true;
  399. }
  400. else {
  401. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  402. polygonMode = false;
  403. }
  404. break;
  405. case GLFW_KEY_T:
  406. modT ^= true;
  407. break;
  408. case GLFW_KEY_S:
  409. MyPyramid->yAngle += 4;
  410. break;
  411. case GLFW_KEY_W:
  412. MyPyramid->yAngle -= 4;
  413. break;
  414. case GLFW_KEY_A:
  415. MyPyramid->zAngle += 4;
  416. break;
  417. case GLFW_KEY_D:
  418. MyPyramid->zAngle -= 4;
  419. break;
  420. case GLFW_KEY_Z:
  421. MyPyramid->dz -= 0.1;
  422. break;
  423. case GLFW_KEY_X:
  424. MyPyramid->dz += 0.1;
  425. break;
  426. case GLFW_KEY_P:
  427. MyPyramid->m++;
  428. smallPyramid->m++;
  429. MyPyramid->construct();
  430. smallPyramid->construct();
  431. break;
  432. case GLFW_KEY_O:
  433. MyPyramid->m--;
  434. smallPyramid->m--;
  435. MyPyramid->construct();
  436. smallPyramid->construct();
  437. break;
  438. default:
  439. break;
  440. }
  441. }
  442. }
  443.  
  444. void mouseCallback(GLFWwindow *window, int button, int action, int mods) {
  445. leftButtonIsPressed = static_cast<bool>(action);
  446. }
  447.  
  448.  
  449. void cursorPos(GLFWwindow* window, double xpos, double ypos) {
  450. if (leftButtonIsPressed) {
  451. MyPyramid->xAngle -= (xpos - xPosOld) * 0.6;
  452. MyPyramid->yAngle -= (ypos - yPosOld) * 0.6;
  453. }
  454. xPosOld = xpos;
  455. yPosOld = ypos;
  456. }
  457.  
  458. void scrollCallback(GLFWwindow* window, double xOffs, double yOffs) {
  459. MyPyramid->scaleValue += yOffs * 0.05;
  460. }
  461.  
  462. void draw_arr() {
  463. glEnableClientState(GL_NORMAL_ARRAY);
  464. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  465. glEnableClientState(GL_VERTEX_ARRAY);
  466. //glNormalPointer(GL_FLOAT, 0, C.normal);
  467. //glTexCoordPointer(2, GL_FLOAT, 0, C.texture);
  468. //glVertexPointer(3, GL_FLOAT, 0, points);
  469. //glDrawArrays(GL_TRIANGLE_STRIP, 0, MyPyramid->points.begin);
  470. glDisableClientState(GL_NORMAL_ARRAY);
  471. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  472. glDisableClientState(GL_VERTEX_ARRAY);
  473. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement