Advertisement
Guest User

Untitled

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