Advertisement
Guest User

Untitled

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