Advertisement
Guest User

Untitled

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