Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
792
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <vector>
  5. #include <GLFW/glfw3.h>
  6. #include <cmath>
  7. #include <math.h>
  8.  
  9. #define PI 3.14159265
  10.  
  11. static double RAD= 180/PI;
  12. static double a=1080;
  13. static double b=1080;
  14. static double Const=a/b;
  15. static int pol= 1;
  16.  
  17. struct cube{
  18. double dx, rx, ry, rz, x, y, z=0, h=1.5 , R=0.07, r =0.05, RT=0.3;
  19. double dy=0.0, sx=1, fi= PI/4.0, teta= PI*35.64/180.0;
  20. int num_p=10, num_elem=10, n=5;
  21. float *proj;
  22. float *polygons;
  23. float f= PI/10;
  24. };
  25.  
  26. static cube C;
  27. static cube C_proj[4];
  28. static double X, Y =1;
  29. static int mod=0;
  30. static double ang=0.0;
  31.  
  32. static void init(cube *t){
  33.  
  34. delete[] (t->polygons);
  35.  
  36. t->polygons=new float[t->num_elem*t->num_p*3];
  37.  
  38. for (int i=0; i< t->num_elem; i++){
  39.  
  40. float A = t->n*2.0*PI* float(i) / float(t->num_elem);
  41. float d=t->h*i/t->num_elem;
  42.  
  43.  
  44. for (int j=0; j<t->num_p; j++){
  45.  
  46. float ang= 2.0*PI* float(j) / float(t->num_p);
  47. float x =t->RT+ (t->R)* cosf(ang);
  48. float y = 0;
  49. float z = (t->r)* sinf(ang);
  50.  
  51. float y1= y*cos(t->f)-z*sin(t->f);
  52. float z1= z*cos(t->f)+y*sin(t->f);
  53.  
  54. float x2= x*cos(A)-y1*sin(A);
  55. float y2= y1*cos(A)+x*sin(A);
  56.  
  57. z= z1+ d;
  58. t->polygons[i*t->num_p*3+j*3]=x2;
  59. t->polygons[i*t->num_p*3+j*3+1]=y2;
  60. t->polygons[i*t->num_p*3+j*3+2]=z;
  61. }
  62. }
  63. }
  64.  
  65. void mouse_button_callback(GLFWwindow* window, int button, int action, int mods){
  66. mod++;
  67. mod%=2;
  68. return;
  69. }
  70.  
  71. static void cursor_pos_callback(GLFWwindow* window, double xpos, double ypos){
  72. xpos=(-1+xpos/(a/2.0));
  73. ypos=(1-ypos/(b/2.0));
  74.  
  75. if (mod==1){
  76. C.rx+=(xpos-X)*70;
  77. C.ry+=(ypos-Y)*70;
  78. }
  79. X= xpos;
  80. Y= ypos;
  81. return;
  82. }
  83.  
  84. static void move(double x, double y){
  85.  
  86. C.dx+=0.05*x;
  87. C.dy+=0.05*y;
  88.  
  89. return;
  90. }
  91.  
  92. static int zoomod =0;
  93. static double zoomv = 0;
  94.  
  95. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods){
  96.  
  97. int t=0; //флаг для переотрисовки объекта при изменении количества полигонов
  98.  
  99. if (action== GLFW_PRESS || action== GLFW_REPEAT){
  100. switch (key){
  101. case GLFW_KEY_ESCAPE:
  102. if (glfwGetInputMode(window, GLFW_CURSOR) != GLFW_CURSOR_DISABLED){
  103. glfwSetWindowShouldClose(window, GLFW_TRUE);
  104. }
  105. break;
  106.  
  107. case 61:
  108. t++;
  109. C.num_elem+=1;
  110. for (int i=0; i<4; i++){
  111. C_proj[i].num_elem+=1;
  112. }
  113. break;
  114. case 45:
  115. if (C.num_elem>1){
  116. t++;
  117. C.num_elem-=1;
  118. for (int i=0; i<4; i++){
  119. C_proj[i].num_elem-=1;
  120. }
  121. }
  122. break;
  123. case 46:
  124. if (C.num_p>1){
  125. t++;
  126. C.num_p-=1;
  127. for (int i=0; i<4; i++){
  128. C_proj[i].num_p-=1;
  129. }
  130. }
  131. break;
  132. case 44:
  133. t++;
  134. C.num_p+=1;
  135. for (int i=0; i<4; i++){
  136. C_proj[i].num_p+=1;
  137. }
  138. break;
  139.  
  140. case 65:
  141. move(-1.0, 0.0);
  142. break;
  143. case 68:
  144. move(1.0, 0.0);
  145. break;
  146. case 87:
  147. move(0.0, 1.0);
  148. break;
  149. case 83:
  150. move(0.0, -1.0);
  151. break;
  152.  
  153. case 265:
  154. zoomod++;
  155. zoomod%=2;
  156. zoomv=0.002;
  157. break;
  158. case 264:
  159. zoomod++;
  160. zoomod%=2;
  161. zoomv=-0.002;
  162. break;
  163. case 263:
  164. C.rz+=4;
  165. break;
  166. case 262:
  167. C.rz-=4;
  168. break;
  169. case GLFW_KEY_R:
  170. pol++;
  171. pol%=2;
  172. break;
  173. default:
  174. //std::cout << key << std::endl;
  175. break;
  176. }
  177. }
  178. if(t){
  179. init(&C);
  180. for (int i=0; i<4; i++){
  181. init(&C_proj[i]);
  182. }
  183. std:: cout << C.num_p << " "<< C.num_elem << std::endl;
  184. }
  185. }
  186.  
  187. void drow( cube t, int k){
  188.  
  189. int j=0;
  190.  
  191. for (int i=0; i<t.num_p+1;i++){
  192.  
  193. glBegin(GL_TRIANGLES);
  194.  
  195. glNormal3f(t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)+1]*t.polygons[(k+1)*t.num_p*3 +(i*3)%(t.num_p*3)+2] -t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)+2]*t.polygons[(k+1)*t.num_p*3 +(i*3)%(t.num_p*3)+1],
  196. -( t.polygons[k*t.num_p*3 +(i*3)%(t.num_p*3)]*t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)+2]-t.polygons[k*t.num_p*3 +(i*3)%(t.num_p*3)+2]*t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)]),
  197. t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)]*t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)+1]-t.polygons[k*t.num_p*3 +(i*3)%(t.num_p*3)+1]*t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)]);
  198. 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++;
  199. glVertex3f(t.polygons[k*t.num_p*3 +(i*3)%(t.num_p*3)], t.polygons[k*t.num_p*3 +(i*3)%(t.num_p*3)+1], t.polygons[k*t.num_p*3 +(i*3)%(t.num_p*3)+2]);
  200. glVertex3f(t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)], t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)+1], t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)+2]);
  201. glVertex3f(t.polygons[(k+1)*t.num_p*3 +(i*3)%(t.num_p*3)], t.polygons[(k+1)*t.num_p*3 +(i*3)%(t.num_p*3)+1], t.polygons[(k+1)*t.num_p*3 +(i*3)%(t.num_p*3)+2]);
  202.  
  203. 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++;
  204. glVertex3f(t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)], t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)+1], t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)+2]);
  205. glVertex3f(t.polygons[(k+1)*t.num_p*3 +((i+1)*3)%(t.num_p*3)], t.polygons[(k+1)*t.num_p*3 +((i+1)*3)%(t.num_p*3)+1], t.polygons[(k+1)*t.num_p*3 +((i+1)*3)%(t.num_p*3)+2]);
  206. glVertex3f(t.polygons[(k+1)*t.num_p*3 +((i)*3)%(t.num_p*3)], t.polygons[(k+1)*t.num_p*3 +((i)*3)%(t.num_p*3)+1], t.polygons[(k+1)*t.num_p*3 +((i)*3)%(t.num_p*3)+2]);
  207. glEnd();
  208. }
  209. }
  210.  
  211. void game_loop(){
  212.  
  213. // обработка поворота
  214. glMatrixMode(GL_PROJECTION);
  215. glLoadIdentity();
  216. glMatrixMode(GL_MODELVIEW);
  217. glLoadIdentity();
  218.  
  219. glTranslatef(C.dx, C.dy, 0.0);
  220. glRotated(C.ry, 1.0, 0.0, 0.0);
  221. glRotated(C.rx, 0.0, 1.0, 0.0);
  222. glRotated(C.rz, 0.0, 0.0, 1.0);
  223. glScaled(C.sx, C.sx, C.sx);
  224.  
  225. //координатные прямые
  226. glLineWidth(2);
  227. glColor3f(0.9,0.6,0.3);
  228. glBegin(GL_LINES);
  229. glVertex3f(0,0,0);
  230. glVertex3f(0.7,0,0);
  231. glEnd();
  232. glBegin(GL_LINES);
  233. glVertex3f(0,0,0);
  234. glVertex3f(0.0,0.7,0);
  235. glEnd();
  236.  
  237. glBegin(GL_LINES);
  238. glVertex3f(0,0,0);
  239. glVertex3f(0.0,0,0.7);
  240. glEnd();
  241. glLineWidth(1);
  242.  
  243. //смещение к центру фигуры
  244. glTranslatef(0.0,0.0,-C.h/2);
  245.  
  246. for (int i =0; i< C.num_elem-1; i++) {
  247. drow(C, i);
  248. }
  249.  
  250. //отрисовка проекций...................
  251.  
  252. glMatrixMode(GL_MODELVIEW);
  253. glLoadIdentity();
  254. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  255.  
  256. for (int k=0; k<4; k++){
  257.  
  258. glMatrixMode(GL_PROJECTION);
  259. glLoadIdentity();
  260. glLoadMatrixf(C_proj[k].proj);
  261. glTranslatef(C_proj[k].dx ,C_proj[k].dy , 0.0);
  262. glScaled(C_proj[k].sx, C_proj[k].sx, C_proj[k].sx);
  263.  
  264. for (float i =0; i< C_proj[k].num_elem-1; i++){
  265. drow(C_proj[k], i);
  266. }
  267. }
  268. }
  269.  
  270.  
  271. int main(int argc, char const *argv[])
  272. {
  273.  
  274. init(&C);
  275.  
  276. for (int i=0;i<4; i++){
  277. init(&C_proj[i]);
  278. }
  279.  
  280. for( int i=0; i<4; i++){
  281. C_proj[i].sx=0.2;
  282. C_proj[i].dy=-0.8;
  283. C_proj[i].dx=-0.8+i*0.54;
  284. }
  285.  
  286. C_proj[0].proj= new float[16] {
  287. cosf(C_proj[1].fi), sinf(C_proj[1].fi)*sinf(C_proj[1].teta), sinf(C_proj[1].fi)*cosf(C_proj[1].teta), 0.0,
  288. 0.0, cosf(C_proj[1].teta),-sinf(C_proj[1].teta), 0.0,
  289. sinf(C_proj[1].fi), -cosf(C_proj[1].fi)*sinf(C_proj[1].teta), -cosf(C_proj[1].fi)*cosf(C_proj[1].teta), 0.0,
  290. -0.2, 0.15, 0.0, 1.0};
  291.  
  292. C_proj[1].proj= new float[16] {
  293. 1.0, 0.0, 0.0, 0.0,
  294. 0.0, 1.0, 0.0, 0.0,
  295. 0.0, 0.0, 0.0, 0.0,
  296. 0.0, 0.0, 0.0, 1.0};
  297.  
  298. C_proj[2].proj= new float[16] {
  299. 0.0, 0.0, -sin(PI/2.0), 0.0,
  300. 0.0, 1.0, 0.0, 0.0,
  301. sin(PI/2.0), 0.0, cos(PI/2.0), 0.0,
  302. 0.27, 0.0, 0.0, 1.0};
  303.  
  304. C_proj[3].proj= new float[16] {
  305. 1.0, 0.0, 0.0, 0.0,
  306. 0.0, 0.0, -sin(PI/2.0), 0.0,
  307. 0.0, sin(PI/2.0), cos(PI/2.0), 0.0,
  308. 0.0, -0.8, 0.0, 1.0};
  309.  
  310. if (!glfwInit()){
  311. std::cout << "faild\n";
  312. return 0;
  313. }
  314.  
  315. GLFWwindow* window = glfwCreateWindow(a, b, "lol", NULL, NULL);
  316.  
  317. if (!window){
  318. std::cout<< "faild\n";
  319. return 0;
  320. }
  321.  
  322. glfwMakeContextCurrent(window);
  323. glClearColor(0.05f, 0.05f, 0.15f, 0.0f);
  324.  
  325. glfwSetCursorPosCallback(window, cursor_pos_callback);
  326. glfwSetKeyCallback(window, key_callback);
  327. glfwSetMouseButtonCallback(window, mouse_button_callback);
  328. glMatrixMode( GL_MODELVIEW );
  329. glLoadIdentity( );
  330. glEnable(GL_LIGHTING);
  331. glEnable(GL_LIGHT0);
  332. while (!glfwWindowShouldClose(window)){
  333. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  334. glEnable(GL_DEPTH_TEST);
  335. if (pol){
  336. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  337. }else {
  338. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  339. }
  340.  
  341. if (zoomod){
  342. C.sx+=zoomv;
  343. }
  344.  
  345. game_loop();
  346.  
  347. glfwSwapBuffers(window);
  348. glfwPollEvents();
  349. }
  350.  
  351. glfwDestroyWindow(window);
  352. glfwTerminate();
  353.  
  354. for (int i=0; i<4;i++){
  355. delete[] (C_proj[i].proj);
  356. delete[] (C_proj[i].polygons);
  357. }
  358. delete[] (C.proj);
  359. delete[] (C.polygons);
  360.  
  361. std::cout << "success\n";
  362. return 0;
  363. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement