Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.40 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 cube{
  17. double dx=0, rx, ry, rz, x, y, z=0, h=0.5 , R=0.1, r =0.05, RT=0.15;
  18. double dy=0, sx=0.65, fi= PI/4.0, teta= PI*35.64/180.0;
  19. int num_p=20, num_elem=30, n=3;
  20. float *proj;
  21. float *polygons;
  22. float f= PI/10;
  23. };
  24.  
  25.  
  26. std::vector<double> O_o;
  27. 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};
  28. static cube C;
  29. static double X, Y =1;
  30. static int mod=0;
  31. static double ang=0.0;
  32. static double RAD= 180/PI;
  33. static double a=1080;
  34. static double b=1080;
  35. static double V[]= {700/a, 200/a, 200/a};
  36. static double Const=a/b;
  37. static clock_t global_time;
  38. static int pol= 1;
  39. static int zoomod =0;
  40. static double zoomv = 0;
  41. static bool modA=false, modL=false, modT=false;
  42. static GLuint textureID;
  43. static GLfloat light0_diffuse[] = {1,1,1};
  44. static GLfloat light0_position[] = {1.0, 1.0, 1.0, 1.0};
  45. static GLfloat light0_ambient[] = {1.0, 1, 1 };
  46. static GLfloat light0_spot_direction[] = {-1.0, -1.0, -1.0};
  47. static int GL_SPOT_CUTOFF0 = 70;
  48. static int GL_SPOT_EXPONENT0 = 100;
  49. static int fps =0;
  50. static clock_t fps_global_time;
  51.  
  52.  
  53. static void animation();
  54. static void game_loop();
  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(cube *t);
  59. static void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
  60. static void drow( cube t, int k);
  61. static double* check();
  62.  
  63. int main(int argc, char const *argv[])
  64. {
  65.  
  66. init(&C);
  67.  
  68. if (!glfwInit()){
  69. std::cout << "faild\n";
  70. return 0;
  71. }
  72.  
  73. GLFWwindow* window = glfwCreateWindow(a, b, "lol", NULL, NULL);
  74.  
  75. if (!window){
  76. std::cout<< "faild\n";
  77. return 0;
  78. }
  79.  
  80. glfwMakeContextCurrent(window);
  81. glClearColor(0.05f, 0.05f, 0.15f, 0.0f);
  82.  
  83. glfwSetCursorPosCallback(window, cursor_pos_callback);
  84. glfwSetKeyCallback(window, key_callback);
  85. glfwSetMouseButtonCallback(window, mouse_button_callback);
  86. glMatrixMode( GL_MODELVIEW );
  87. glLoadIdentity( );
  88.  
  89. glEnable(GL_NORMALIZE);
  90. glEnable(GL_LIGHTING);
  91. glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
  92. glEnable(GL_LIGHT0);
  93. GLfloat material_diffuse[] = {1.0, 1.0, 1.0, 1.0};
  94. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse);
  95.  
  96. int width= 512, height= 512;
  97. unsigned char* image = SOIL_load_image("./iye.bmp", &width, &height, 0, SOIL_LOAD_RGB);
  98.  
  99. glGenTextures(1, &textureID);
  100. glBindTexture(GL_TEXTURE_2D, textureID);
  101.  
  102. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  103. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  104. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  105. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  106.  
  107. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
  108. SOIL_free_image_data(image);
  109. glBindTexture(GL_TEXTURE_2D, 0);
  110.  
  111.  
  112. while (!glfwWindowShouldClose(window)){
  113. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  114.  
  115. if (modT){
  116. glEnable(GL_TEXTURE_2D);
  117. } else {
  118. glDisable(GL_TEXTURE_2D);
  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. glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, GL_SPOT_CUTOFF0);
  124. glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light0_spot_direction);
  125. glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, GL_SPOT_EXPONENT0);
  126.  
  127. glEnable(GL_DEPTH_TEST);
  128. if (pol){
  129. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  130. }else {
  131. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  132. }
  133.  
  134. if (zoomod){
  135. C.sx+=zoomv;
  136. }
  137.  
  138. game_loop();
  139.  
  140. glfwSwapBuffers(window);
  141. glfwPollEvents();
  142. }
  143.  
  144. glfwDestroyWindow(window);
  145. glfwTerminate();
  146.  
  147. delete[] (C.proj);
  148. delete[] (C.polygons);
  149.  
  150. std::cout << "success\n";
  151. return 0;
  152. }
  153.  
  154.  
  155. static void init(cube *t){
  156.  
  157. delete[] (t->polygons);
  158.  
  159. t->polygons=new float[t->num_elem*t->num_p*3];
  160.  
  161. for (int i=0; i< t->num_elem; i++){
  162.  
  163. float A = t->n*2.0*PI* float(i) / float(t->num_elem);
  164. float d=t->h*i/t->num_elem;
  165.  
  166.  
  167. for (int j=0; j<t->num_p; j++){
  168.  
  169. float ang= 2.0*PI* float(j) / float(t->num_p);
  170. float x =t->RT+ (t->R)* cosf(ang);
  171. float y = 0;
  172. float z = (t->r)* sinf(ang);
  173.  
  174. float y1= y*cos(t->f)-z*sin(t->f);
  175. float z1= z*cos(t->f)+y*sin(t->f);
  176.  
  177. float x2= x*cos(A)-y1*sin(A);
  178. float y2= y1*cos(A)+x*sin(A);
  179.  
  180. z= z1+ d;
  181. t->polygons[i*t->num_p*3+j*3]=x2;
  182. t->polygons[i*t->num_p*3+j*3+1]=y2;
  183. t->polygons[i*t->num_p*3+j*3+2]=z;
  184. }
  185. }
  186. }
  187.  
  188. void mouse_button_callback(GLFWwindow* window, int button, int action, int mods){
  189. mod++;
  190. mod%=2;
  191. return;
  192. }
  193.  
  194. static void cursor_pos_callback(GLFWwindow* window, double xpos, double ypos){
  195. xpos=(-1+xpos/(a/2.0));
  196. ypos=(1-ypos/(b/2.0));
  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 88: //x
  411. pars();
  412. break;
  413. case 80: //p
  414. for (int i=0; i<3; light0_diffuse[i++]= (rand())%200 /200.0);
  415. for (int i=0; i<3; light0_ambient[i++]= (rand())%200 /200.0);
  416. GL_SPOT_EXPONENT0 = rand()%100;
  417. GL_SPOT_CUTOFF0 = rand()%100;
  418. break;
  419. case 76:
  420. modL^=true;
  421. break;
  422. case 61:
  423. t++;
  424. C.num_elem+=1;
  425. break;
  426. case 45:
  427. if (C.num_elem>1){
  428. t++;
  429. C.num_elem-=1;
  430. }
  431. break;
  432. case 46:
  433. if (C.num_p>1){
  434. t++;
  435. C.num_p-=1;
  436. }
  437. break;
  438. case 44:
  439. t++;
  440. C.num_p+=1;
  441. break;
  442. case 65:
  443. move(-1.0, 0.0);
  444. break;
  445. case 68:
  446. move(1.0, 0.0);
  447. break;
  448. case 87:
  449. move(0.0, 1.0);
  450. break;
  451. case 83:
  452. move(0.0, -1.0);
  453. break;
  454. case 265:
  455. C.sx+=0.03;
  456. break;
  457. case 264:
  458. C.sx+=-0.03;
  459. break;
  460. case 263:
  461. C.rz+=4;
  462. break;
  463. case 262:
  464. C.rz-=4;
  465. break;
  466. case GLFW_KEY_R:
  467. pol++;
  468. pol%=2;
  469. break;
  470. case 32:
  471. modA^=true;
  472. global_time= clock();
  473. break;
  474. default:
  475. std::cout << key << std::endl;
  476. break;
  477. }
  478. }
  479. if(t){
  480. init(&C);
  481. }
  482. }
  483.  
  484. void drow( cube t, int k){
  485.  
  486. int j=0;
  487.  
  488. for (int i=0; i<t.num_p+1;i++){
  489.  
  490. glBindTexture(GL_TEXTURE_2D, textureID);
  491. glBegin(GL_TRIANGLES);
  492.  
  493. 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)];
  494. 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];
  495. 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];
  496.  
  497. glNormal3f((y1-y0)*(z2-z1) - (z1-z0)*(y2-y1),
  498. (x1-x0)*(z2-z1) - (z1-z0)*(x2-x1),
  499. (x1-x0)*(y2-y1) - (y1-y0)*(x2-x1));
  500.  
  501. 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++;
  502. glTexCoord2f(0.0f, 0.0f);
  503. glVertex3f(x0, y0, z0);
  504. glTexCoord2f(1.0f, 0.0f);
  505. glVertex3f(x1, y1, z1);
  506. glTexCoord2f(0.5f, 1.0f);
  507. glVertex3f(x2, y2, z2);
  508.  
  509. 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)];
  510. 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];
  511. 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];
  512.  
  513. glNormal3f((y1-y0)*(z2-z1) - (z1-z0)*(y2-y1),
  514. (x1-x0)*(z2-z1) - (z1-z0)*(x2-x1),
  515. (x1-x0)*(y2-y1) - (y1-y0)*(x2-x1));
  516.  
  517. 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++;
  518. glTexCoord2f(0.0f, 0.0f);
  519. glVertex3f(x0, y0, z0);
  520. glTexCoord2f(1.0f, 0.0f);
  521. glVertex3f(x1, y1, z1);
  522. glTexCoord2f(0.5f, 1.0f);
  523. glVertex3f(x2, y2, z2);
  524. glEnd();
  525. }
  526. }
  527.  
  528. void game_loop(){
  529.  
  530. if (modA)
  531. animation();
  532.  
  533. // обработка поворота
  534. glMatrixMode(GL_PROJECTION);
  535. glLoadIdentity();
  536. glMatrixMode(GL_MODELVIEW);
  537. glLoadIdentity();
  538.  
  539. glTranslatef(C.dx, C.dy, 0.0);
  540. glRotated(C.ry, 1.0, 0.0, 0.0);
  541. glRotated(C.rx, 0.0, 1.0, 0.0);
  542. glRotated(C.rz, 0.0, 0.0, 1.0);
  543. glScaled(C.sx, C.sx, C.sx);
  544. glTranslatef(0.0,0.0,-C.h/2);
  545.  
  546. for (int i =0; i< C.num_elem-1; i++) {
  547. drow(C, i);
  548. }
  549.  
  550. //ограничителя...................
  551.  
  552. glColor3f(1,0,0);
  553.  
  554. glBegin(GL_LINES);
  555. for (int i=0; i<32; i++){
  556. glVertex3f(delim[i*3],delim[i*3+1],delim[i*3+2]);
  557. }
  558.  
  559. glColor3f(1, 1, 1);
  560. glBegin(GL_LINE_LOOP);
  561. glVertex3f(0,0,0);
  562. for (int i=0; i< O_o.size()/3; i++){
  563. glVertex3f(O_o[i*3], O_o[i*3+1], O_o[i*3+2]);
  564. glVertex3f(O_o[i*3], O_o[i*3+1], O_o[i*3+2]);
  565. }
  566. glEnd();
  567. glLineWidth(1);
  568.  
  569. glEnd();
  570.  
  571. glMatrixMode(GL_MODELVIEW);
  572. glLoadIdentity();
  573. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  574.  
  575. fps++;
  576. clock_t fps_time = clock();
  577. double time = ((double)fps_time)/CLOCKS_PER_SEC - ((double)fps_global_time)/CLOCKS_PER_SEC;
  578. if (time >= 1) {
  579. std::cout << "fps : " << fps << std::endl;
  580. fps_global_time = fps_time;
  581. fps = 0;
  582. }
  583.  
  584. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement