Advertisement
Guest User

Untitled

a guest
May 21st, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <vector>
  6. #include <GLFW/glfw3.h>
  7. #include <cmath>
  8. #include <math.h>
  9. #include <time.h>
  10. #include <SOIL/SOIL.h>
  11. #include <fstream>
  12.  
  13. #define PI 3.14159265
  14.  
  15. struct pair{
  16. double x, y;
  17. };
  18.  
  19. struct cube{
  20. double V= 0;
  21. double dx=0, rx, ry, rz, x, y, z=0, h=0.5 , R=0.1, r =0.05, RT=0.15;
  22. double dy=0, sx=0.61;
  23. int num_p=20, num_elem=30, n=3;
  24. float *proj;
  25. float *polygons;
  26. float f= PI/10;
  27. double min_y=1;
  28. };
  29.  
  30. std::vector<pair> base;
  31. static cube C;
  32. static double G= -0.0005;
  33. static double X, Y =0;
  34. static int mod=0;
  35. static double a=1080;
  36. static double b=1080;
  37. static double V[]= {0, 0, 0};
  38. static clock_t global_time;
  39. static int pol= 0;
  40. static int zoomod =0;
  41. static double zoomv = 0;
  42. static bool modA=false, modL=false, modT=false, modI= true;
  43. static GLuint textureID;
  44. GLfloat material_diffuse[] = {1.0, 1.0, 1.0, 1.0};
  45.  
  46. GLfloat light0_diffuse[] = {1,1,1};
  47. GLfloat light0_position[] = {0, 1, 0, 1};
  48. GLfloat light0_ambient[] = {1, 0, 0, 0};
  49.  
  50. GLfloat light1_diffuse[] = {0,1,0};
  51. GLfloat light1_position[] = {1, 0, 0, 1};
  52. GLfloat light1_ambient[] = {1, 0,9, 0,1, 0};
  53.  
  54.  
  55.  
  56. static void animation();
  57. static void game_loop();
  58. static void cursor_pos_callback(GLFWwindow* window, double xpos, double ypos);
  59. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
  60. static void move(double x, double y);
  61. static void init(cube *t);
  62. static void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
  63. static void drow( cube t);
  64. static double* check();
  65.  
  66. int main(int argc, char const *argv[])
  67. {
  68.  
  69. if (!glfwInit()){
  70. std::cout << "faild\n";
  71. return 0;
  72. }
  73.  
  74. GLFWwindow* window = glfwCreateWindow(a, b, "lol", NULL, NULL);
  75.  
  76. if (!window){
  77. std::cout<< "faild\n";
  78. return 0;
  79. }
  80.  
  81. glfwMakeContextCurrent(window);
  82. glClearColor(0.05f, 0.05f, 0.15f, 0.0f);
  83.  
  84. glfwSetCursorPosCallback(window, cursor_pos_callback);
  85. glfwSetKeyCallback(window, key_callback);
  86. glfwSetMouseButtonCallback(window, mouse_button_callback);
  87. glMatrixMode( GL_MODELVIEW );
  88. glLoadIdentity( );
  89.  
  90. glEnable(GL_NORMALIZE);
  91. glEnable(GL_LIGHTING);
  92. glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
  93. glEnable(GL_LIGHT0);
  94. glEnable(GL_LIGHT1);
  95.  
  96. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse);
  97.  
  98.  
  99.  
  100. int width= 512, height= 512;
  101. unsigned char* image = SOIL_load_image("./iye.bmp", &width, &height, 0, SOIL_LOAD_RGB);
  102.  
  103. glGenTextures(1, &textureID);
  104. glBindTexture(GL_TEXTURE_2D, textureID);
  105.  
  106. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  107. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  108. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  109. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  110.  
  111. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
  112. SOIL_free_image_data(image);
  113. glBindTexture(GL_TEXTURE_2D, 0);
  114.  
  115.  
  116.  
  117. while (!glfwWindowShouldClose(window)){
  118. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  119.  
  120. glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
  121. glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
  122. glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
  123.  
  124. glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
  125. glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
  126. glLightfv(GL_LIGHT0, GL_POSITION, light1_position);
  127.  
  128. if (modT){
  129. glEnable(GL_TEXTURE_2D);
  130. } else {
  131. glDisable(GL_TEXTURE_2D);
  132. }
  133.  
  134. glEnable(GL_DEPTH_TEST);
  135. if (pol){
  136. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  137. }else {
  138. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  139. }
  140.  
  141. if (zoomod){
  142. C.sx+=zoomv;
  143. }
  144.  
  145. game_loop();
  146.  
  147. glfwSwapBuffers(window);
  148. glfwPollEvents();
  149. }
  150.  
  151. glfwDestroyWindow(window);
  152. glfwTerminate();
  153.  
  154. delete[] (C.proj);
  155. delete[] (C.polygons);
  156.  
  157. std::cout << "success\n";
  158. return 0;
  159. }
  160.  
  161.  
  162. static void init(cube *t){
  163.  
  164. delete[] (t->polygons);
  165.  
  166. t->polygons=new float[base.size()*t->num_p*3];
  167.  
  168. for (int i=0; i< base.size(); i++){
  169.  
  170. float r = base[i].x, dy = base[i].y;
  171.  
  172. if (dy< t->min_y)
  173. t->min_y=dy;
  174.  
  175. for (int j=0; j<t->num_p; j++){
  176.  
  177. float ang= 2.0*PI* float(j) / float(t->num_p);
  178. float x = r*cosf(ang);
  179. float y = dy;
  180. float z = r*sinf(ang);
  181.  
  182. t->polygons[i*t->num_p*3+j*3]=x;
  183. t->polygons[i*t->num_p*3+j*3+1]=y;
  184. t->polygons[i*t->num_p*3+j*3+2]=z;
  185. }
  186. }
  187. }
  188.  
  189. void mouse_button_callback(GLFWwindow* window, int button, int action, int mods){
  190. if (modI){
  191. std::cout<< X << " " << Y << std::endl;
  192. base.push_back(pair {X, Y});
  193. } else {
  194. mod++;
  195. mod%=2;
  196. }
  197. return;
  198. }
  199.  
  200. static void cursor_pos_callback(GLFWwindow* window, double xpos, double ypos){
  201. xpos= xpos/(a/2)-1;
  202. ypos= 1-(ypos+50)/(a/2);
  203.  
  204.  
  205. if (mod==1){
  206. C.rx+=(xpos-X)*70;
  207. C.ry+=(ypos-Y)*70;
  208. }
  209. X= xpos;
  210. Y= ypos;
  211. return;
  212. }
  213.  
  214. static void move(double x, double y){
  215.  
  216. C.dx+=0.05*x;
  217. C.dy+=0.05*y;
  218.  
  219. return;
  220. }
  221.  
  222. static void animation(){
  223.  
  224. if (C.min_y<=-1){
  225. C.V*=-1;
  226. }
  227.  
  228. clock_t local_time= clock();
  229. double time= ((double)local_time)/CLOCKS_PER_SEC - ((double)global_time)/CLOCKS_PER_SEC ;
  230. global_time= local_time;
  231.  
  232. C.V+= time * G;
  233.  
  234. for (int i=1; i< base.size() * C.num_p*3; i+=3){
  235. C.polygons[i]+= C.V;
  236. }
  237. C.min_y+=C.V;
  238.  
  239. return;
  240. }
  241.  
  242. //rx = (0) ry = (0) ry = (0) count = (8) POSITION = (0 0.765947 0.287019 0.765947 0.451852 0.653704 0.780582 0.227352 0.859259 -0.116667 0.790234 -0.446681 0.688889 -0.566667 0 -0.566667 )
  243.  
  244. static void pars(){
  245. std::ifstream file("./position.txt");
  246. std::string s, t="";
  247. getline(file, s);
  248. //std::cout<< s << std::endl;
  249. file.close();
  250. int i=0;
  251. double v_t[3]= {0,0,0};
  252.  
  253. for (; s[i]!= '('; i++);
  254. i++;
  255. for (; s[i]!= ')'; t+=s[i++]);
  256. C.rx= std::stod(t);
  257. t="";
  258.  
  259. for (; s[i]!= '('; i++);
  260. i++;
  261. for (; s[i]!= ')'; t+=s[i++]);
  262. C.ry= std::stod(t);
  263. t="";
  264.  
  265. for (; s[i]!= '('; i++);
  266. i++;
  267. for (; s[i]!= ')'; t+=s[i++]);
  268. C.rz= std::stod(t);
  269. t="";
  270.  
  271. for (; s[i]!= '('; i++);
  272. i++;
  273. for (; s[i]!= ')'; t+=s[i++]);
  274. C.V= std::stod(t);
  275. t="";
  276.  
  277. for (; s[i]!= '('; i++);
  278. i++;
  279. for (; s[i]!= ')'; t+=s[i++]);
  280. double min_y= std::stod(t);
  281. t="";
  282.  
  283. for (; s[i]!= '('; i++);
  284. i++;
  285. for (; s[i]!= ')'; t+=s[i++]);
  286. int n= std::stod(t);
  287. t="";
  288.  
  289. base.clear();
  290.  
  291. for (; s[i]!= '('; i++);
  292. i++;
  293. for (int k=0; k<n; k++){
  294. while (s[i]!=' ' && s[i]!=')'){
  295. t+=s[i];
  296. i++;
  297. }
  298. double xt=std::stod(t);
  299. i++;
  300. t="";
  301. while (s[i]!=' ' && s[i]!=')'){
  302. t+=s[i];
  303. i++;
  304. }
  305. i++;
  306. double yt=std::stod(t);
  307. t="";
  308. base.push_back(pair {xt, yt});
  309. }
  310.  
  311. std::cout<< C.min_y << " ";
  312. init(&C);
  313. C.min_y = min_y;
  314. return;
  315. }
  316.  
  317. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods){
  318.  
  319. int t=0; //флаг для переотрисовки объекта при изменении количества полигонов
  320. std::ofstream ttt;
  321. if (action== GLFW_PRESS || action== GLFW_REPEAT){
  322. switch (key){
  323. case GLFW_KEY_ESCAPE:
  324. if (glfwGetInputMode(window, GLFW_CURSOR) != GLFW_CURSOR_DISABLED){
  325. glfwSetWindowShouldClose(window, true);
  326. }
  327. break;
  328. case 84: //T
  329. modT^=true;
  330. break;
  331. case 90: //z
  332. ttt.open("./position.txt");
  333. if (ttt.is_open()){
  334. ttt << "rx = (" << C.rx << ") ry = (" << C.ry<< ") rz = (" << C.rz << ") x = (" << C.V << ") min_y = (" << C.min_y;
  335. ttt << ") count = (" << base.size();
  336. ttt << ") POSITION = (";
  337. for (std::vector<pair>::iterator it=base.begin(); it!=base.end(); ++it){
  338. ttt << (*it).x << " "<< (*it).y << " ";
  339. }
  340. }
  341. ttt << ") ";
  342. break;
  343. case 257:
  344. base.push_back(pair {0,base[base.size()-1].y});
  345. base.insert(base.begin(), pair {0, base[0].y});
  346. init(&C);
  347. modI=false;
  348. break;
  349. case 88: //x
  350. modI=false;
  351. pars();
  352. global_time= clock();
  353. break;
  354. case 80: //p
  355. /*for (int i=0; i<3; light0_diffuse[i++]= (rand())%200 /200.0);
  356. for (int i=0; i<3; light0_ambient[i++]= (rand())%200 /200.0);
  357. GL_SPOT_EXPONENT0 = rand()%100;
  358. GL_SPOT_CUTOFF0 = rand()%100;*/
  359. break;
  360. case 76:
  361. modL^=true;
  362. break;
  363. case 61:
  364. t++;
  365. C.num_elem+=1;
  366. break;
  367. case 45:
  368. if (C.num_elem>1){
  369. t++;
  370. C.num_elem-=1;
  371. }
  372. break;
  373. case 46:
  374. if (C.num_p>1){
  375. t++;
  376. C.num_p-=1;
  377. }
  378. break;
  379. case 44:
  380. t++;
  381. C.num_p+=1;
  382. break;
  383. case 65:
  384. move(-1.0, 0.0);
  385. break;
  386. case 68:
  387. move(1.0, 0.0);
  388. break;
  389. case 87:
  390. move(0.0, 1.0);
  391. break;
  392. case 83:
  393. move(0.0, -1.0);
  394. break;
  395. case 265:
  396. C.sx+=0.03;
  397. break;
  398. case 264:
  399. C.sx+=-0.03;
  400. break;
  401. case 263:
  402. C.rz+=4;
  403. break;
  404. case 262:
  405. C.rz-=4;
  406. break;
  407. case GLFW_KEY_R:
  408. pol++;
  409. pol%=2;
  410. break;
  411. case 32:
  412. modA^=true;
  413. global_time= clock();
  414. break;
  415. default:
  416. std::cout << key << std::endl;
  417. break;
  418. }
  419. }
  420. if(t){
  421. init(&C);
  422. }
  423. }
  424.  
  425. void drow( cube t ){
  426.  
  427. for (int k=0; k< base.size()-1; k++){
  428. int j=0;
  429.  
  430. for (int i=0; i<t.num_p+1;i++){
  431.  
  432. glBindTexture(GL_TEXTURE_2D, textureID);
  433. glBegin(GL_TRIANGLES);
  434.  
  435. double x0= t.polygons[k*t.num_p*3 +(i*3)%(t.num_p*3)], x1= t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)], x2=t.polygons[(k+1)*t.num_p*3 +(i*3)%(t.num_p*3)];
  436. double y0= t.polygons[k*t.num_p*3 +(i*3)%(t.num_p*3)+1], y1= t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)+1], y2=t.polygons[(k+1)*t.num_p*3 +(i*3)%(t.num_p*3)+1];
  437. double z0= t.polygons[k*t.num_p*3 +(i*3)%(t.num_p*3)+2], z1= t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)+2], z2= t.polygons[(k+1)*t.num_p*3 +(i*3)%(t.num_p*3)+2];
  438.  
  439. glNormal3f((y1-y0)*(z2-z1) - (z1-z0)*(y2-y1),
  440. (x1-x0)*(z2-z1) - (z1-z0)*(x2-x1),
  441. (x1-x0)*(y2-y1) - (y1-y0)*(x2-x1));
  442.  
  443. glColor3f(abs((float(k)/t.num_elem-1)*1000)/1000.0, abs((float(j)/(t.num_p*2)-0.5)*1000)/1000.0, 0.666); j++;
  444. glTexCoord2f(0.0f, 0.0f);
  445. glVertex3f(x0, y0, z0);
  446. glTexCoord2f(1.0f, 0.0f);
  447. glVertex3f(x1, y1, z1);
  448. glTexCoord2f(0.5f, 1.0f);
  449. glVertex3f(x2, y2, z2);
  450.  
  451. x0=t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)]; x1=t.polygons[(k+1)*t.num_p*3 +((i+1)*3)%(t.num_p*3)]; x2=t.polygons[(k+1)*t.num_p*3 +((i)*3)%(t.num_p*3)];
  452. y0=t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)+1]; y1=t.polygons[(k+1)*t.num_p*3 +((i+1)*3)%(t.num_p*3)+1]; y2=t.polygons[(k+1)*t.num_p*3 +((i)*3)%(t.num_p*3)+1];
  453. z0=t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)+2]; z1=t.polygons[(k+1)*t.num_p*3 +((i+1)*3)%(t.num_p*3)+2]; z2=t.polygons[(k+1)*t.num_p*3 +((i)*3)%(t.num_p*3)+2];
  454.  
  455. glNormal3f((y1-y0)*(z2-z1) - (z1-z0)*(y2-y1),
  456. (x1-x0)*(z2-z1) - (z1-z0)*(x2-x1),
  457. (x1-x0)*(y2-y1) - (y1-y0)*(x2-x1));
  458.  
  459. glColor3f(abs((float(k)/t.num_elem-1)*1000)/1000.0, abs((float(j)/(t.num_p*2)-0.5)*1000)/1000.0, 0.666); j++;
  460. glTexCoord2f(0.0f, 0.0f);
  461. glVertex3f(x0, y0, z0);
  462. glTexCoord2f(1.0f, 0.0f);
  463. glVertex3f(x1, y1, z1);
  464. glTexCoord2f(0.5f, 1.0f);
  465. glVertex3f(x2, y2, z2);
  466. glEnd();
  467. }
  468. }
  469. }
  470.  
  471. void game_loop(){
  472.  
  473. if (modA)
  474. animation();
  475.  
  476. // обработка поворота
  477. glMatrixMode(GL_PROJECTION);
  478. glLoadIdentity();
  479. glMatrixMode(GL_MODELVIEW);
  480. glLoadIdentity();
  481.  
  482. glTranslatef(C.dx, C.dy, 0.0);
  483. glRotated(C.ry, 1.0, 0.0, 0.0);
  484. glRotated(C.rx, 0.0, 1.0, 0.0);
  485. glRotated(C.rz, 0.0, 0.0, 1.0);
  486. if (!modI)
  487. glScaled(C.sx, C.sx, C.sx);
  488. glTranslatef(0.0,0.0,-C.h/2);
  489.  
  490. glColor3f(0,0,0);
  491. glBegin(GL_POLYGON);
  492. glVertex3f(1, -1, 1);
  493. glVertex3f(1, -1, -1);
  494. glVertex3f(-1, -1, -1);
  495. glVertex3f(-1, -1, 1);
  496. glEnd();
  497.  
  498.  
  499. glColor3f(1,1,1);
  500. glBegin(GL_LINE_LOOP);
  501. if (modI){
  502. for (std::vector<pair>::iterator it=base.begin(); it!=base.end(); ++it){
  503. glVertex3f((*it).x, (*it).y, 1);
  504. }
  505. } else {
  506. drow(C);
  507. }
  508. glEnd();
  509.  
  510.  
  511. glMatrixMode(GL_MODELVIEW);
  512. glLoadIdentity();
  513. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  514.  
  515. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement