Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.56 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. //#include <jpeglib.h>
  13.  
  14. #define PI 3.14159265
  15.  
  16. struct con{
  17. double dx=0, rx, ry, rz, x, y, z=0, h=1 , R=0.2, r =0.1, RT=0.5;
  18. double dy=-0.5, sx=0.65, fi= PI/4.0, teta= PI*35.64/180.0;
  19. double bias = 0.2, t=0;
  20. int num_p=20, num_elem=20, n=3;
  21. float *proj;
  22. float *polygons;
  23. float f= PI/10;
  24. };
  25.  
  26.  
  27. static con C;
  28. static con start;
  29. static con end;
  30. static con P1;
  31.  
  32.  
  33. static double X, Y =1;
  34. static int mod=0;
  35. static double V = 0.5;
  36. static double a=1080;
  37. static double b=1080;
  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, modTwo= false, modLocal= false;
  43. GLuint textureID;
  44. GLfloat material_diffuse[] = {1.0, 1.0, 1.0, 1.0};
  45. GLfloat light0_diffuse[] = {0.4,0,1};
  46. GLfloat light0_position[] = {1.0, 1.0, 1.0, 1.0};
  47. GLfloat light0_ambient[] = {1, 1, 1 };
  48. GLfloat ambient[] = {0.2, 0.5, 0.5, 1};
  49.  
  50.  
  51.  
  52. static void animation();
  53. static void game_loop();
  54. static void save();
  55. static void cursor_pos_callback(GLFWwindow* window, double xpos, double ypos);
  56. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
  57. static void move(double x, double y);
  58. static void init(con *t);
  59. static void init1(con *t);
  60. static void init2(con *t);
  61. static void init3(con *t);
  62. static void init4(con *t);
  63. static void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
  64. static void drow( con t);
  65.  
  66. int main(int argc, char const *argv[])
  67. {
  68.  
  69. init(&C);
  70. init(&start);
  71. init1(&P1);
  72. init2(&end);
  73.  
  74. if (!glfwInit()){
  75. std::cout << "faild\n";
  76. return 0;
  77. }
  78.  
  79. GLFWwindow* window = glfwCreateWindow(a, b, "lol", NULL, NULL);
  80.  
  81. if (!window){
  82. std::cout<< "faild\n";
  83. return 0;
  84. }
  85.  
  86. glfwMakeContextCurrent(window);
  87. glClearColor(0.05f, 0.05f, 0.15f, 0.0f);
  88.  
  89. glfwSetCursorPosCallback(window, cursor_pos_callback);
  90. glfwSetKeyCallback(window, key_callback);
  91. glfwSetMouseButtonCallback(window, mouse_button_callback);
  92.  
  93. glEnable(GL_NORMALIZE);
  94. glEnable(GL_LIGHTING);
  95. glEnable(GL_LIGHT0);
  96. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse);
  97. glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
  98. glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
  99. glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
  100.  
  101.  
  102. int width= 512, height= 512;
  103. unsigned char* image = SOIL_load_image("./iye.bmp", &width, &height, 0, SOIL_LOAD_RGB);
  104. glGenTextures(1, &textureID);
  105. glBindTexture(GL_TEXTURE_2D, textureID);
  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. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ALPHA);
  111. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE);
  112. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
  113.  
  114. SOIL_free_image_data(image);
  115. glBindTexture(GL_TEXTURE_2D, 0);
  116.  
  117.  
  118. while (!glfwWindowShouldClose(window)){
  119. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  120.  
  121.  
  122.  
  123. if(modTwo)
  124. glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
  125. else
  126. glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
  127. if(modLocal)
  128. glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
  129. else
  130. glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_FALSE);
  131. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
  132.  
  133. if (modT){
  134. glEnable(GL_TEXTURE_2D);
  135. } else {
  136. glDisable(GL_TEXTURE_2D);
  137. }
  138.  
  139. glEnable(GL_DEPTH_TEST);
  140. if (pol){
  141. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  142. }else {
  143. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  144. }
  145.  
  146. if (zoomod){
  147. C.sx+=zoomv;
  148. }
  149.  
  150. game_loop();
  151.  
  152. glfwSwapBuffers(window);
  153. glfwPollEvents();
  154. }
  155.  
  156. glfwDestroyWindow(window);
  157. glfwTerminate();
  158.  
  159. delete[] (C.proj);
  160. delete[] (C.polygons);
  161.  
  162. std::cout << "success\n";
  163. return 0;
  164. }
  165.  
  166.  
  167. static void init1(con *t){
  168. delete[] (t->polygons);
  169.  
  170. t->polygons=new float[t->num_elem*t->num_p*3];
  171. t->h=1;
  172. t->R=0.02;
  173. t->r=0.01;
  174.  
  175. for (int i=0; i< t->num_elem; i++){
  176.  
  177. float A = t->n*2.0*PI* float(i) / float(t->num_elem);
  178. float d=t->h*i/t->num_elem;
  179.  
  180.  
  181. for (int j=0; j<t->num_p; j++){
  182.  
  183. float ang= 2.0*PI* float(j) / float(t->num_p);
  184. float x =t->RT+ (t->R)* cosf(ang);
  185. float y = 0;
  186. float z = (t->r)* sinf(ang);
  187.  
  188. float y1= y*cos(t->f)-z*sin(t->f);
  189. float z1= z*cos(t->f)+y*sin(t->f);
  190.  
  191. float x2= x*cos(A)-y1*sin(A);
  192. float y2= y1*cos(A)+x*sin(A);
  193.  
  194. z= z1+ d;
  195. t->polygons[i*t->num_p*3+j*3]=x2;
  196. t->polygons[i*t->num_p*3+j*3+1]=y2;
  197. t->polygons[i*t->num_p*3+j*3+2]=z;
  198. }
  199. }
  200. }
  201.  
  202. static void init2(con *t){
  203. delete[] (t->polygons);
  204.  
  205. t->R=0.7;
  206.  
  207. t->polygons=new float[(t->num_elem)*t->num_p*3];
  208.  
  209. for (int i=0; i< t->num_elem; i++){
  210.  
  211. float fi = (float)(PI*i)/(t->num_elem);
  212.  
  213. for (int j=0; j<t->num_p; j++){
  214.  
  215. float ang= 2.0*PI* float(j) / float(t->num_p);
  216.  
  217. float x = t->R * sinf(fi) * cosf(ang);
  218. float y = t->R * cosf(fi);
  219. float z = t->R * sinf(fi) * sinf(ang);
  220.  
  221. t->polygons[i*t->num_p*3+j*3]=x;
  222. t->polygons[i*t->num_p*3+j*3+1]=y;
  223. t->polygons[i*t->num_p*3+j*3+2]=z;
  224. }
  225. }
  226. }
  227.  
  228. static void init3(con *t){
  229. delete[] (t->polygons);
  230.  
  231. t->h=1.2;
  232. t->R=0.6;
  233. t->r=0.5;
  234.  
  235. t->polygons=new float[(t->num_elem)*t->num_p*3];
  236.  
  237. for (int i=0; i< t->num_elem; i++){
  238.  
  239. float dy = (float)(t->h*(i))/(t->num_elem), dr = (float)(i)/(t->num_elem);
  240.  
  241. for (int j=0; j<t->num_p; j++){
  242.  
  243. float ang= 2.0*PI* float(j) / float(t->num_p);
  244.  
  245. float x = (t->R * dr)* cosf(ang);
  246. float y = dy;
  247. float z = (t->r * dr)* sinf(ang);
  248.  
  249. t->polygons[i*t->num_p*3+j*3]=x;
  250. t->polygons[i*t->num_p*3+j*3+1]=y+0.1;
  251. t->polygons[i*t->num_p*3+j*3+2]=z;
  252. }
  253. }
  254. }
  255.  
  256. static void init(con *t){
  257. delete[] (t->polygons);
  258.  
  259. t->h=0.9;
  260. t->R=0.4;
  261. t->r=0.7;
  262.  
  263. t->polygons=new float[(t->num_elem)*t->num_p*3];
  264.  
  265. for (int i=0; i< t->num_elem; i++){
  266.  
  267. float dy = (float)(t->h*(i))/(t->num_elem), dr = 1-(float)(i)/(t->num_elem);
  268.  
  269. for (int j=0; j<t->num_p; j++){
  270.  
  271. float ang= 2.0*PI* float(j) / float(t->num_p);
  272.  
  273. float x = (t->R * dr)* cosf(ang);
  274. float y = dy;
  275. float z = (t->r * dr)* sinf(ang);
  276.  
  277. t->polygons[i*t->num_p*3+j*3]=x;
  278. t->polygons[i*t->num_p*3+j*3+1]=y+0.1;
  279. t->polygons[i*t->num_p*3+j*3+2]=z;
  280. }
  281. }
  282. }
  283.  
  284. void mouse_button_callback(GLFWwindow* window, int button, int action, int mods){
  285. mod++;
  286. mod%=2;
  287. return;
  288. }
  289.  
  290. static void cursor_pos_callback(GLFWwindow* window, double xpos, double ypos){
  291. xpos=(-1+xpos/(a/2.0));
  292. ypos=(1-ypos/(b/2.0));
  293.  
  294. if (mod==1){
  295. C.rx+=(xpos-X)*70;
  296. C.ry+=(ypos-Y)*70;
  297. }
  298. X= xpos;
  299. Y= ypos;
  300. return;
  301. }
  302.  
  303. static void move(double x, double y){
  304. C.dx+=0.05*x;
  305. C.dy+=0.05*y;
  306.  
  307. return;
  308. }
  309.  
  310. //P = (1-t)^2 * P0 + 2*t*(1-t)P1+ t^2 * P2
  311.  
  312. static void animation(){
  313.  
  314. if (C.t<0 || C.t>1){
  315. V*=-1;
  316. }
  317. double x0 = pow((1 - C.t),2), x1 = 2*C.t*(1-C.t), x2 = C.t*C.t;
  318.  
  319. for (int i=0; i< C.num_elem * C.num_p * 3; i++){
  320. C.polygons[i] = start.polygons[i] * x0 + P1.polygons[i] * x1 + end.polygons[i] * x2;
  321. }
  322.  
  323. clock_t local_time= clock();
  324. double time= ((double)local_time)/CLOCKS_PER_SEC - ((double)global_time)/CLOCKS_PER_SEC ;
  325. global_time= local_time;
  326.  
  327. C.t+= V * time;
  328.  
  329.  
  330. return;
  331. }
  332.  
  333. static void pars(){
  334. std::ifstream file("./position.txt");
  335. std::string s, t="";
  336. getline(file, s);
  337.  
  338. file.close();
  339. int i=0;
  340. double v_t[3]= {0,0,0};
  341.  
  342. for (; s[i]!= '{'; i++);
  343. i++;
  344. for (; s[i]!= '}'; t+=s[i++]);
  345. C.sx= std::stod(t);
  346. t="";
  347.  
  348. for (; s[i]!= '{'; i++);
  349. i++;
  350. for (; s[i]!= '}'; t+=s[i++]);
  351. V= std::stod(t);
  352. t="";
  353.  
  354. for (; s[i]!= '{'; i++);
  355. i++;
  356. for (; s[i]!= '}'; t+=s[i++]);
  357. C.t= std::stod(t);
  358. t="";
  359.  
  360. for (; s[i]!= '{'; i++);
  361. i++;
  362. for (; s[i]!= '}'; t+=s[i++]);
  363. modTwo= std::stod(t);
  364. t="";
  365.  
  366. for (; s[i]!= '{'; i++);
  367. i++;
  368. for (; s[i]!= '}'; t+=s[i++]);
  369. modLocal= std::stod(t);
  370. t="";
  371.  
  372. for (; s[i]!= '{'; i++);
  373. i++;
  374. for (; s[i]!= '}'; t+=s[i++]);
  375. C.rx= std::stod(t);
  376. t="";
  377.  
  378. for (; s[i]!= '{'; i++);
  379. i++;
  380. for (; s[i]!= '}'; t+=s[i++]);
  381. C.ry= std::stod(t);
  382. t="";
  383.  
  384. for (; s[i]!= '{'; i++);
  385. i++;
  386. for (; s[i]!= '}'; t+=s[i++]);
  387. C.rz= std::stod(t);
  388. t="";
  389.  
  390. for (; s[i]!= '{'; i++);
  391. i++;
  392. for (int k=0; k>4; k++){
  393. while (s[i]!=' ' && s[i]!=')'){
  394. t+=s[i];
  395. i++;
  396. }
  397. ambient[k]=std::stod(t);
  398. i++;
  399. t="";
  400. }
  401.  
  402. return;
  403. }
  404.  
  405. void save(){
  406. std::ofstream ttt;
  407. ttt.open("./position.txt");
  408. if (ttt.is_open()){
  409. ttt << "sx : {" << C.sx << "} V : {" << V << "} t : {"<< C.t <<"} modTwo : {"<< modTwo<< "} modLocal : {"<< modLocal << "} rx : {"<< C.rx<< "} ry : {" << C.ry<< "} rz : {"<< C.rz << "} ambient : {" << ambient[0] << " "<< ambient[1] << " " << ambient[2] << " " << ambient[3] << "}";
  410. }
  411. }
  412.  
  413. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods){
  414.  
  415. int t=0; //флаг для переотрисовки объекта при изменении количества полигонов
  416. std::ofstream ttt;
  417. if (action== GLFW_PRESS || action== GLFW_REPEAT){
  418. switch (key){
  419. case GLFW_KEY_ESCAPE:
  420. if (glfwGetInputMode(window, GLFW_CURSOR) != GLFW_CURSOR_DISABLED){
  421. glfwSetWindowShouldClose(window, true);
  422. }
  423. break;
  424. case GLFW_KEY_P:
  425. for (int i = 0; i < 4; ++i){
  426. ambient[i] = rand()%100/100.0;
  427. }
  428. break;
  429. case GLFW_KEY_1:
  430. modLocal^=true;
  431. break;
  432. case GLFW_KEY_2:
  433. modTwo^=true;
  434. break;
  435. case 84: //T
  436. modT^=true;
  437. break;
  438. case 90: //z
  439. save();
  440. break;
  441. case 88: //x
  442. pars();
  443. break;
  444. case 76:
  445. modL^=true;
  446. break;
  447. case 61:
  448. t++;
  449. C.num_elem+=1;
  450. start.num_elem+=1;
  451. P1.num_elem+=1;
  452. end.num_elem+=1;
  453. break;
  454. case 45:
  455. if (C.num_elem>1){
  456. t++;
  457. C.num_elem-=1;
  458. start.num_elem-=1;
  459. P1.num_elem-=1;
  460. end.num_elem-=1;
  461. }
  462. break;
  463. case 46:
  464. if (C.num_p>1){
  465. t++;
  466. C.num_p-=1;
  467. start.num_p-=1;
  468. P1.num_p-=1;
  469. end.num_p-=1;
  470. }
  471. break;
  472. case 44:
  473. t++;
  474. C.num_p+=1;
  475. start.num_p+=1;
  476. P1.num_p+=1;
  477. end.num_p+=1;
  478. break;
  479. case 65:
  480. move(-1.0, 0.0);
  481. break;
  482. case 68:
  483. move(1.0, 0.0);
  484. break;
  485. case 87:
  486. move(0.0, 1.0);
  487. break;
  488. case 83:
  489. move(0.0, -1.0);
  490. break;
  491. case 265:
  492. C.sx+=0.03;
  493. break;
  494. case 264:
  495. C.sx+=-0.03;
  496. break;
  497. case 263:
  498. C.rz+=4;
  499. break;
  500. case 262:
  501. C.rz-=4;
  502. break;
  503. case GLFW_KEY_R:
  504. pol++;
  505. pol%=2;
  506. break;
  507. case 32:
  508. modA^=true;
  509. global_time= clock();
  510. break;
  511. default:
  512. std::cout << key << std::endl;
  513. break;
  514. }
  515. }
  516. if(t){
  517. init(&C);
  518. init(&start);
  519. init1(&end);
  520. init3(&P1);
  521. }
  522. }
  523.  
  524. void drow( con t ){
  525.  
  526. for (int k =0; k< C.num_elem-1; k++){
  527. int j=0;
  528. for (int i=0; i<t.num_p+1;i++){
  529.  
  530. glBindTexture(GL_TEXTURE_2D, textureID);
  531. glBegin(GL_TRIANGLES);
  532.  
  533. 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)];
  534. 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];
  535. 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];
  536.  
  537. glNormal3f((y1-y0)*(z2-z1) - (z1-z0)*(y2-y1),
  538. (x1-x0)*(z2-z1) - (z1-z0)*(x2-x1),
  539. (x1-x0)*(y2-y1) - (y1-y0)*(x2-x1));
  540.  
  541. 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++;
  542. glTexCoord2f(0.0f, 0.0f);
  543. glVertex3f(x0, y0, z0);
  544. glTexCoord2f(1.0f, 0.0f);
  545. glVertex3f(x1, y1, z1);
  546. glTexCoord2f(0.5f, 1.0f);
  547. glVertex3f(x2, y2, z2);
  548.  
  549. 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)];
  550. 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];
  551. 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];
  552.  
  553. glNormal3f((y1-y0)*(z2-z1) - (z1-z0)*(y2-y1),
  554. (x1-x0)*(z2-z1) - (z1-z0)*(x2-x1),
  555. (x1-x0)*(y2-y1) - (y1-y0)*(x2-x1));
  556.  
  557. 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++;
  558. glTexCoord2f(0.0f, 0.0f);
  559. glVertex3f(x0, y0, z0);
  560. glTexCoord2f(1.0f, 0.0f);
  561. glVertex3f(x1, y1, z1);
  562. glTexCoord2f(0.5f, 1.0f);
  563. glVertex3f(x2, y2, z2);
  564. glEnd();
  565. }
  566. }
  567. }
  568.  
  569. void game_loop(){
  570.  
  571. if (modA)
  572. animation();
  573.  
  574. glMatrixMode(GL_PROJECTION);
  575. glLoadIdentity();
  576. glMatrixMode(GL_MODELVIEW);
  577. glLoadIdentity();
  578.  
  579. glTranslatef(C.dx, C.dy, 0.0);
  580. glRotated(C.ry, 1.0, 0.0, 0.0);
  581. glRotated(C.rx, 0.0, 1.0, 0.0);
  582. glRotated(C.rz, 0.0, 0.0, 1.0);
  583. glScaled(C.sx, C.sx, C.sx);
  584. glTranslatef(0.0,0.0,-C.h/2);
  585.  
  586. drow(C);
  587.  
  588. glMatrixMode(GL_MODELVIEW);
  589. glLoadIdentity();
  590. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  591. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement