Advertisement
Anastasia_Lys

Untitled

May 23rd, 2019
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.59 KB | None | 0 0
  1. #include <glfw3.h>
  2. //#include <GL/glu.h>
  3. #include <glaux.h>
  4. #pragma comment(lib, "glaux.lib")
  5. #include <glaux.h>
  6.  
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <iostream>
  10. #include <math.h>
  11. #include <vector>
  12. #include <sstream>
  13. #include <fstream>
  14. //#include "SOIL.h"
  15. using namespace std;
  16.  
  17. static void error_callback(int error, const char* description) {
  18.     fputs(description, stderr);
  19. }
  20.  
  21. struct Point {
  22.     double x;
  23.     double y;
  24.     double z;
  25. };
  26.  
  27. vector<Point> arr;
  28. bool flag = true;
  29. bool flag_light0 = true;
  30. bool flag_light1 = true;
  31. bool flag_texture = false;
  32. float x_pos = 0;
  33. float y_pos = 0;
  34. float z_pos = 0;
  35. float r_cub = 0.65;
  36. float model_x = 0;
  37. float model_y = 0;
  38. float model_z = 0;
  39. float model1_x = 0;
  40. float model1_y = 0;
  41. float model1_z = 0;
  42. GLfloat arg1 = 0;
  43. GLfloat arg2 = 0;
  44. GLfloat arg3 = 0;
  45. float tx = 0.2;
  46. float ty = -0.9;
  47. float tz = -0.3;
  48. GLfloat rx_1 = 0.1;
  49. GLfloat ry_1 = 0;
  50. GLfloat rz_1_ = 0;
  51. GLfloat rx_2 = 0;
  52. GLfloat ry_2 = 0.1;
  53. GLfloat rz_2 = 0;
  54. GLfloat rx_3 = 0;
  55. GLfloat ry_3 = 0;
  56. GLfloat rz_3 = 0.1;
  57. float speed = 0.0003;
  58. bool flag_1 = true;
  59.  
  60. float cub_coord[72] = {-1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, -1,
  61. -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, 1,
  62. -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1, 1, 1, 1,
  63. 1, -1, 1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, 1, -1, -1, -1};
  64.  
  65. GLuint texture[1];
  66.  
  67.  
  68. float pi = 3.1415926535;
  69.  
  70. struct Sphere {
  71.     float R = 0.15;
  72.     int m = 10;
  73.     int n = 15;
  74.     float ***points;
  75. }s, sphere1;
  76.  
  77. void sphere(Sphere *s);
  78. void draw_Sphere(Sphere *s);
  79. void draw();
  80. void animation();
  81. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
  82. void scrollCallback(GLFWwindow *window, double xoffset, double yoffset);
  83. void Texture();
  84. void load();
  85.  
  86. void init() {
  87.     glClearColor(1, 1, 1, 1.0);
  88.     glEnable(GL_LIGHTING);
  89.     glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
  90.     glEnable(GL_NORMALIZE);
  91. }
  92.  
  93.  
  94. void init_l() {
  95.     glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
  96.     float ambient0[4] = { model_x, model_y, model_z, 1 };
  97.     float ambient1[4] = { model1_x, model1_y, model1_z, 1 };
  98.     float light0[] = { 1, 1, 1, 0 };
  99.     float light0_pos[] = { 0, 1, 0, 0 };
  100.     float light1[] = { 1, 0,9, 0,1, 0 };
  101.     float light1_pos1[] = { 1, 0, 1, 0 };
  102.     GLfloat front_color[] = { 0.5, 0.1, 0.6, 1 };
  103.     GLfloat back_color[] = { 0.2, 0.8, 0, 1 };
  104.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  105.     //glEnable(GL_LIGHTING);
  106.     if (flag_light0) {
  107.         glEnable(GL_LIGHT0);
  108.         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient0);
  109.         glMaterialfv(GL_FRONT, GL_AMBIENT, front_color);
  110.         glLightfv(GL_LIGHT0, GL_AMBIENT, light0);
  111.         glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);
  112.     }else
  113.         glDisable(GL_LIGHT0);
  114.  
  115.     if (flag_light1) {
  116.         glEnable(GL_LIGHT1);
  117.         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient1);
  118.         glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, back_color);
  119.         glLightfv(GL_LIGHT1, GL_DIFFUSE, light1);
  120.         glLightfv(GL_LIGHT1, GL_POSITION, light1_pos1);
  121.     }
  122.     else
  123.         glDisable(GL_LIGHT1);
  124.  
  125. }
  126.  
  127.  
  128. int main(void) {
  129.     arr.push_back({ x_pos, y_pos, z_pos });
  130.     GLFWwindow* window;
  131.     glfwSetErrorCallback(error_callback);
  132.     if (!glfwInit())
  133.         exit(EXIT_FAILURE);
  134.     window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
  135.     if (!window)
  136.     {
  137.         glfwTerminate();
  138.         exit(EXIT_FAILURE);
  139.     }
  140.  
  141.  
  142.     glfwMakeContextCurrent(window);
  143.     glfwSetKeyCallback(window, key_callback);
  144.     glfwSetScrollCallback(window, scrollCallback);
  145.     sphere(&s);
  146.     sphere(&sphere1);
  147.     Texture();
  148.  
  149.     //GLuint id = createTexture("C:/Users/Анастасия/Pictures/camomreal.bmp");
  150.     //cout<<id<<endl;
  151.     while (!glfwWindowShouldClose(window)) {
  152.         int screenWidth, screenHeight;
  153.         glfwGetFramebufferSize(window, &screenWidth, &screenHeight);
  154.         float   ratio = screenWidth / (float)screenHeight;
  155.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  156.         glEnable(GL_DEPTH_TEST);
  157.         glDepthFunc(GL_LEQUAL);
  158.         init();
  159.         init_l();
  160.  
  161.         glViewport(0.0f, 0.0f, screenWidth, screenHeight);
  162.         glMatrixMode(GL_PROJECTION);
  163.         glLoadIdentity();
  164.         glOrtho(-ratio, ratio, -1.f, 1.f, 1.f * 3, -1.f * 3);
  165.         glMatrixMode(GL_MODELVIEW);
  166.         glLoadIdentity();
  167.  
  168.         if (flag) {
  169.             animation();
  170.         }
  171.  
  172.         //glGenTextures(1, &id);
  173.         if (flag_texture)
  174.             glEnable(GL_TEXTURE_2D);
  175.         else
  176.             glDisable(GL_TEXTURE_2D);
  177.         glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  178.         glClearDepth(1.0);
  179.         glDepthFunc(GL_LESS);
  180.         glEnable(GL_DEPTH_TEST);
  181.         glShadeModel(GL_SMOOTH);
  182.         glPushMatrix();
  183.         glTranslated(x_pos, y_pos, z_pos);
  184.         glRotated(arg1, rx_1, ry_1, rz_1_);
  185.         glRotated(arg2, rx_2, ry_2, rz_2);
  186.         glRotated(arg3, rx_3, ry_3, rz_3);
  187.        
  188.         //        glBindTexture(GL_TEXTURE_2D, id);
  189.         draw_Sphere(&s);//glScaled(sx, sy, sz);
  190.         glPopMatrix();
  191.         glRotated(arg1, rx_1, ry_1, rz_1_);
  192.         glRotated(arg2, rx_2, ry_2, rz_2);
  193.         glRotated(arg3, rx_3, ry_3, rz_3);
  194.         draw();
  195.         glfwSwapBuffers(window);
  196.         glfwPollEvents();
  197.     }
  198.     glfwDestroyWindow(window);
  199.     glfwTerminate();
  200.     exit(EXIT_SUCCESS);
  201. }
  202.  
  203. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
  204.     if (action == GLFW_PRESS || action == GLFW_REPEAT) {
  205.         switch (key) {
  206.         case GLFW_KEY_SPACE:
  207.             if (flag) {
  208.                 //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  209.                 flag = false;
  210.             }
  211.             else {
  212.                 flag = true;
  213.                 //glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  214.             }
  215.             break;
  216.         case GLFW_KEY_ESCAPE:
  217.             glfwSetWindowShouldClose(window, GL_TRUE);
  218.             break;
  219.         case GLFW_KEY_ENTER:
  220.             if (flag_1)
  221.                 flag_1 = false;
  222.             else {
  223.                 flag_1 = true;
  224.             }
  225.         case GLFW_KEY_UP:
  226.             arg1 -= 10;
  227.             break;
  228.         case GLFW_KEY_DOWN:
  229.             arg1 += 10;
  230.             break;
  231.         case GLFW_KEY_LEFT:
  232.             arg2 += 10;
  233.             break;
  234.         case GLFW_KEY_RIGHT:
  235.             arg2 -= 10;
  236.             break;
  237.         case GLFW_KEY_KP_ADD:
  238.             s.n += 1;
  239.             break;
  240.         case GLFW_KEY_KP_SUBTRACT:
  241.             if (s.n > 1) {
  242.                 s.n -= 1;
  243.             }
  244.             break;
  245.         case GLFW_KEY_MINUS:
  246.             if (s.m > 1) {
  247.                 s.m -= 1;
  248.             }
  249.             break;
  250.         case GLFW_KEY_EQUAL:
  251.             s.m += 1;
  252.             break;
  253.         case GLFW_KEY_Q:
  254.             arg3 += 10;
  255.             break;
  256.         case GLFW_KEY_E:
  257.             arg3 -= 10;
  258.             break;
  259.         case GLFW_KEY_P:
  260.             if (speed < 0.005)
  261.                 speed += 0.0001;
  262.             break;
  263.         case GLFW_KEY_O:
  264.             if (speed > 0.0001)
  265.                 speed -= 0.0001;
  266.             break;
  267.         case GLFW_KEY_Z:
  268.             if (flag_light0) {
  269.                 model_x += 0.1;
  270.                 model_y += 0.1;
  271.                 model_z += 0.1;
  272.             }
  273.             break;
  274.         case GLFW_KEY_X:
  275.             if (flag_light0) {
  276.                 model_x -= 0.1;
  277.                 model_y -= 0.1;
  278.                 model_z -= 0.1;
  279.             }
  280.             break;
  281.         case GLFW_KEY_M:
  282.             if (flag_light1) {
  283.                 model1_x += 0.1;
  284.                 model1_y += 0.1;
  285.                 model1_z += 0.1;
  286.             }
  287.             break;
  288.         case GLFW_KEY_N:
  289.             if (flag_light1) {
  290.                 model1_x -= 0.1;
  291.                 model1_y -= 0.1;
  292.                 model1_z -= 0.1;
  293.             }
  294.             break;
  295.         case GLFW_KEY_1:
  296.             if (flag_light1) {
  297.                 flag_light1 = false;
  298.             }
  299.             else
  300.                 flag_light1 = true;
  301.             break;
  302.         case GLFW_KEY_0:
  303.             if (flag_light0) {
  304.                 flag_light0 = false;
  305.             }
  306.             else
  307.                 flag_light0 = true;
  308.             break;
  309.         case GLFW_KEY_T:
  310.             if (flag_texture)
  311.                 flag_texture = false;
  312.             else
  313.                 flag_texture = true;
  314.             break;
  315.         case GLFW_KEY_C:
  316.             ofstream fout;
  317.             fout.open("./description.txt");
  318.             if (fout.is_open()) {
  319.                 fout << x_pos << endl << y_pos << endl << z_pos << endl <<
  320.                     tx << endl << ty << endl << tz << endl <<
  321.                     model_x << endl << model_y << endl << model_z << endl<<
  322.                     model1_x << endl << model1_y << endl << model1_z << endl <<
  323.                     s.R << endl << s.m << endl << s.n << endl << speed << endl << r_cub << endl <<
  324.                     flag_light0 << endl << flag_light1 << endl << flag_texture << endl << flag_1 << endl << flag;
  325.             }
  326.             fout.close();
  327.             break;
  328.        
  329.         }
  330.         switch (key) {
  331.         case GLFW_KEY_L:
  332.             load();
  333.             break;
  334.         }
  335.     }
  336.     sphere(&s);
  337. }
  338.  
  339. void scrollCallback(GLFWwindow *window, double xoffset, double yoffset)
  340. {
  341.     if (yoffset > 0) {
  342.         s.R -= 0.001;
  343.         r_cub -= 0.001;
  344.     }
  345.     if (yoffset < 0) {
  346.         s.R += 0.001;
  347.         r_cub += 0.001;
  348.     }
  349.     sphere(&s);
  350. }
  351.  
  352. void load() {
  353.     ifstream file("./description.txt");
  354.     file >> x_pos >> y_pos >> z_pos;
  355.     file >> tx >> ty >> tz;
  356.     file >> model_x >> model_y >> model_z;
  357.     file >> model1_x >> model1_y >> model1_z;
  358.     file >> s.R >> s.m >> s.n >> speed >> r_cub;
  359.     file >> flag_light0 >> flag_light1 >> flag_texture >> flag_1 >> flag;
  360.     file.close();
  361. }
  362.  
  363. void Texture() {
  364.     AUX_RGBImageRec *texture1;
  365.     texture1 = auxDIBImageLoad("C:/Users/Анастасия/Downloads/Paver300.bmp");
  366.  
  367.     glGenTextures(1, &texture[0]);
  368.     glBindTexture(GL_TEXTURE_2D, texture[0]);
  369.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  370.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  371.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  372.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  373.     glTexImage2D(GL_TEXTURE_2D, 0, 3, texture1->sizeX, texture1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture1->data);
  374.  
  375.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  376.  
  377. }
  378.  
  379. void sphere(Sphere *s) {
  380.     //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  381.     s->points = new float**[s->m];
  382.     for (int i = 0; i < s->m; i++) {
  383.         s->points[i] = new float*[s->n];
  384.         for (int j = 0; j < s->n; j++) {
  385.             s->points[i][j] = new float[3];
  386.         }
  387.     }
  388.     float dy = 0;
  389.     float dr = 0;
  390.     float teta = ((pi) / (s->m - 1));
  391.     for (int i = 0; i < s->m; i++) {
  392.         dy = s->R*cos(teta*i);
  393.         dr = s->R*sin(teta*i);
  394.         for (int j = 0; j < s->n; ++j) {
  395.             GLfloat fi = ((2 * pi) / s->n)*j;
  396.             GLfloat x = dr * cos(fi);
  397.             GLfloat z = dr * sin(fi);
  398.             GLfloat y = dy;
  399.             s->points[i][j][0] = x;
  400.             s->points[i][j][1] = y;
  401.             s->points[i][j][2] = z;
  402.         }
  403.     }
  404. }
  405.  
  406. void draw_Sphere(Sphere *s) {
  407.     glEnable(GL_DEPTH_TEST);
  408.     glDepthFunc(GL_LESS);
  409.  
  410.     for (int i = 0; i < s->m - 1; ++i) {
  411.         int k = 0;
  412.         for (int j = 0; j < s->n; ++j) {
  413.             glBegin(GL_POLYGON);
  414.             //glColor3f(abs(10000 * (float(k) / s->n - 1)) / 10000.0, float(i) / s->m, 0.5); k++;
  415.             glColor3f(1, 0, 0);
  416.  
  417.             glNormal3f(s->points[i][j][0], s->points[i][j][1], s->points[i][j][2]);
  418.             glTexCoord2f(s->points[i][j][0], s->points[i][j][1]);
  419.             glVertex3f(s->points[i][j][0], s->points[i][j][1], s->points[i][j][2]);
  420.  
  421.             glNormal3f(s->points[i][(j + 1) % s->n][0], s->points[i][(j + 1) % s->n][1], s->points[i][(j + 1) % s->n][2]);
  422.             glTexCoord2f(s->points[i][(j + 1) % s->n][0], s->points[i][(j + 1) % s->n][1]);
  423.             glVertex3f(s->points[i][(j + 1) % s->n][0], s->points[i][(j + 1) % s->n][1], s->points[i][(j + 1) % s->n][2]);
  424.  
  425.             glNormal3f(s->points[i + 1][j][0], s->points[i + 1][j][1], s->points[i + 1][j][2]);
  426.             glTexCoord2f(s->points[i + 1][j][0], s->points[i + 1][j][1]);
  427.             glVertex3f(s->points[i + 1][j][0], s->points[i + 1][j][1], s->points[i + 1][j][2]);
  428.  
  429.             glEnd();
  430.  
  431.             glBegin(GL_POLYGON);
  432.             //glColor3f(abs(10000 * (float(k) / s->n - 1)) / 10000.0, float(i) / s->m, 0.5); k++;
  433.  
  434.             glNormal3f(s->points[i + 1][j][0], s->points[i + 1][j][1], s->points[i + 1][j][2]);
  435.             glTexCoord2f(s->points[i + 1][j][0], s->points[i + 1][j][1]);
  436.             glVertex3f(s->points[i + 1][j][0], s->points[i + 1][j][1], s->points[i + 1][j][2]);
  437.  
  438.             glNormal3f(s->points[i][(j + 1) % s->n][0], s->points[i][(j + 1) % s->n][1], s->points[i][(j + 1) % s->n][2]);
  439.             glTexCoord2f(s->points[i][(j + 1) % s->n][0], s->points[i][(j + 1) % s->n][1]);
  440.             glVertex3f(s->points[i][(j + 1) % s->n][0], s->points[i][(j + 1) % s->n][1], s->points[i][(j + 1) % s->n][2]);
  441.  
  442.             glNormal3f(s->points[i + 1][(j + 1) % s->n][0] , s->points[i + 1][(j + 1) % s->n][1], s->points[i + 1][(j + 1) % s->n][2]);
  443.             glTexCoord2f(s->points[i + 1][(j + 1) % s->n][0], s->points[i + 1][(j + 1) % s->n][1] );
  444.             glVertex3f(s->points[i + 1][(j + 1) % s->n][0], s->points[i + 1][(j + 1) % s->n][1] , s->points[i + 1][(j + 1) % s->n][2]);
  445.  
  446.             glEnd();
  447.         }
  448.     }
  449.     return;
  450. }
  451.  
  452. void draw() {
  453.     /*for (int i = 0; i < arr.size() - 1; ++i) {
  454.         glBegin(GL_LINES);
  455.         glColor3f(1, 0, 0);
  456.         glVertex3f(arr[i].x, arr[i].y, arr[i].z);
  457.         glVertex3f(arr[i + 1].x, arr[i + 1].y, arr[i + 1].z);
  458.         glEnd();
  459.     }*/
  460.  
  461.    
  462.     /*for (int i = 0; i < 96; i+=3) {
  463.         cout << cub_coord[i] << " " << cub_coord[i + 1] << " " << cub_coord[i + 2] << endl;
  464.         glBegin(GL_LINES);
  465.         glColor3f(1, 0, 0);
  466.         glVertex3f(cub_coord[i], cub_coord[i+ 1], cub_coord[i + 2]);
  467.         glVertex3f(cub_coord[i+3], cub_coord[i + 4], cub_coord[i + 6]);
  468.         glEnd();
  469.     }*/
  470.  
  471.     glBegin(GL_LINES);
  472.     glColor3f(1, 0, 0);
  473.     glVertex3f(-r_cub, r_cub, r_cub);
  474.     glVertex3f(r_cub, r_cub, r_cub);
  475.     glEnd();
  476.  
  477.     glBegin(GL_LINES);
  478.     glColor3f(1, 0, 0);
  479.     glVertex3f(r_cub, r_cub, r_cub);
  480.     glVertex3f(r_cub, -r_cub, r_cub);
  481.     glEnd();
  482.  
  483.     glBegin(GL_LINES);
  484.     glColor3f(1, 0, 0);
  485.     glVertex3f(r_cub, -r_cub, r_cub);
  486.     glVertex3f(-r_cub, -r_cub, r_cub);
  487.     glEnd();
  488.  
  489.     glBegin(GL_LINES);
  490.     glColor3f(1, 0, 0);
  491.     glVertex3f(-r_cub, -r_cub, r_cub);
  492.     glVertex3f(-r_cub, r_cub, r_cub);
  493.     glEnd();
  494.  
  495.     glBegin(GL_LINES);
  496.     glColor3f(1, 0, 0);
  497.     glVertex3f(-r_cub, r_cub, -r_cub);
  498.     glVertex3f(r_cub, r_cub, -r_cub);
  499.     glEnd();
  500.  
  501.     glBegin(GL_LINES);
  502.     glColor3f(1, 0, 0);
  503.     glVertex3f(r_cub, r_cub, -r_cub);
  504.     glVertex3f(r_cub, -r_cub, -r_cub);
  505.     glEnd();
  506.  
  507.     glBegin(GL_LINES);
  508.     glColor3f(1, 0, 0);
  509.     glVertex3f(r_cub, -r_cub, -r_cub);
  510.     glVertex3f(-r_cub, -r_cub, -r_cub);
  511.     glEnd();
  512.  
  513.     glBegin(GL_LINES);
  514.     glColor3f(1, 0, 0);
  515.     glVertex3f(-r_cub, -r_cub, -r_cub);
  516.     glVertex3f(-r_cub, r_cub, -r_cub);
  517.     glEnd();
  518.  
  519.     glBegin(GL_LINES);
  520.     glColor3f(1, 0, 0);
  521.     glVertex3f(r_cub, r_cub, r_cub);
  522.     glVertex3f(r_cub, r_cub, -r_cub);
  523.     glEnd();
  524.  
  525.     glBegin(GL_LINES);
  526.     glColor3f(1, 0, 0);
  527.     glVertex3f(r_cub, -r_cub, r_cub);
  528.     glVertex3f(r_cub, -r_cub, -r_cub);
  529.     glEnd();
  530.  
  531.     glBegin(GL_LINES);
  532.     glColor3f(1, 0, 0);
  533.     glVertex3f(-r_cub, r_cub, r_cub);
  534.     glVertex3f(-r_cub, r_cub, -r_cub);
  535.     glEnd();
  536.  
  537.     glBegin(GL_LINES);
  538.     glColor3f(1, 0, 0);
  539.     glVertex3f(-r_cub, -r_cub, r_cub);
  540.     glVertex3f(-r_cub, -r_cub, -r_cub);
  541.     glEnd();
  542. }
  543.  
  544. void animation() {
  545.     if ((x_pos + tx * speed >= -r_cub + s.R && x_pos + tx * speed <= r_cub - s.R) && (y_pos + ty * speed >= -r_cub + s.R && y_pos + ty * speed <= r_cub - s.R) && (z_pos + tz * speed >= -r_cub + s.R && z_pos + tz * speed <= r_cub - s.R)) {
  546.         x_pos += tx * speed;
  547.         y_pos += ty * speed;
  548.         z_pos += tz * speed;
  549.         arr.push_back({ x_pos, y_pos, z_pos });
  550.     }
  551.     else {
  552.         float nx = 0, ny = 0, nz = 0;
  553.         if (x_pos <= -r_cub + s.R) {
  554.             nx = -1;
  555.         }
  556.         else if (x_pos >= r_cub - s.R) {
  557.             nx = 1;
  558.         }
  559.         else if (y_pos <= -r_cub + s.R) {
  560.             ny = -1;
  561.         }
  562.         else if (y_pos >= r_cub - s.R) {
  563.             ny = 1;
  564.         }
  565.         else if (z_pos <= -r_cub + s.R) {
  566.             nz = -1;
  567.         }
  568.         else {
  569.             nz = 1;
  570.         }
  571.         tx = tx - 2 * (tx*nx + ny * ty + nz * tz)*nx;
  572.         ty = ty - 2 * (tx*nx + ny * ty + nz * tz)*ny;
  573.         tz = tz - 2 * (tx*nx + ny * ty + nz * tz)*nz;
  574.         x_pos += tx * speed;
  575.         y_pos += ty * speed;
  576.         z_pos += tz * speed;
  577.         arr.push_back({ x_pos, y_pos, z_pos });
  578.     }
  579. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement