Advertisement
Anastasia_Lys

Untitled

May 23rd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.75 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_TRUE);
  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, 0, 0, 0 };
  99.     float light0_pos[] = { 0, 1, 0, 1 };
  100.     float light1[] = { 1, 0,9, 0,1, 0 };
  101.     float light1_pos1[] = { 1, 0, 0, 1 };
  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.  
  183.         glRotated(arg1, rx_1, ry_1, rz_1_);
  184.         glRotated(arg2, rx_2, ry_2, rz_2);
  185.         glRotated(arg3, rx_3, ry_3, rz_3);
  186.        
  187.         //        glBindTexture(GL_TEXTURE_2D, id);
  188.         draw_Sphere(&s);//glScaled(sx, sy, sz);
  189.         draw();
  190.         glfwSwapBuffers(window);
  191.         glfwPollEvents();
  192.     }
  193.     glfwDestroyWindow(window);
  194.     glfwTerminate();
  195.     exit(EXIT_SUCCESS);
  196. }
  197.  
  198. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
  199.     if (action == GLFW_PRESS || action == GLFW_REPEAT) {
  200.         switch (key) {
  201.         case GLFW_KEY_SPACE:
  202.             if (flag) {
  203.                 //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  204.                 flag = false;
  205.             }
  206.             else {
  207.                 flag = true;
  208.                 //glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  209.             }
  210.             break;
  211.         case GLFW_KEY_ESCAPE:
  212.             glfwSetWindowShouldClose(window, GL_TRUE);
  213.             break;
  214.         case GLFW_KEY_ENTER:
  215.             if (flag_1)
  216.                 flag_1 = false;
  217.             else {
  218.                 flag_1 = true;
  219.             }
  220.         case GLFW_KEY_UP:
  221.             arg1 -= 10;
  222.             break;
  223.         case GLFW_KEY_DOWN:
  224.             arg1 += 10;
  225.             break;
  226.         case GLFW_KEY_LEFT:
  227.             arg2 += 10;
  228.             break;
  229.         case GLFW_KEY_RIGHT:
  230.             arg2 -= 10;
  231.             break;
  232.         case GLFW_KEY_KP_ADD:
  233.             s.n += 1;
  234.             break;
  235.         case GLFW_KEY_KP_SUBTRACT:
  236.             if (s.n > 1) {
  237.                 s.n -= 1;
  238.             }
  239.             break;
  240.         case GLFW_KEY_MINUS:
  241.             if (s.m > 1) {
  242.                 s.m -= 1;
  243.             }
  244.             break;
  245.         case GLFW_KEY_EQUAL:
  246.             s.m += 1;
  247.             break;
  248.         case GLFW_KEY_Q:
  249.             arg3 += 10;
  250.             break;
  251.         case GLFW_KEY_E:
  252.             arg3 -= 10;
  253.             break;
  254.         case GLFW_KEY_P:
  255.             if (speed < 0.005)
  256.                 speed += 0.0001;
  257.             break;
  258.         case GLFW_KEY_O:
  259.             if (speed > 0.0001)
  260.                 speed -= 0.0001;
  261.             break;
  262.         case GLFW_KEY_Z:
  263.             if (flag_light0) {
  264.                 model_x += 0.1;
  265.                 model_y += 0.1;
  266.                 model_z += 0.1;
  267.             }
  268.             break;
  269.         case GLFW_KEY_X:
  270.             if (flag_light0) {
  271.                 model_x -= 0.1;
  272.                 model_y -= 0.1;
  273.                 model_z -= 0.1;
  274.             }
  275.             break;
  276.         case GLFW_KEY_M:
  277.             if (flag_light1) {
  278.                 model1_x += 0.1;
  279.                 model1_y += 0.1;
  280.                 model1_z += 0.1;
  281.             }
  282.             break;
  283.         case GLFW_KEY_N:
  284.             if (flag_light1) {
  285.                 model1_x -= 0.1;
  286.                 model1_y -= 0.1;
  287.                 model1_z -= 0.1;
  288.             }
  289.             break;
  290.         case GLFW_KEY_1:
  291.             if (flag_light1) {
  292.                 flag_light1 = false;
  293.             }
  294.             else
  295.                 flag_light1 = true;
  296.             break;
  297.         case GLFW_KEY_0:
  298.             if (flag_light0) {
  299.                 flag_light0 = false;
  300.             }
  301.             else
  302.                 flag_light0 = true;
  303.             break;
  304.         case GLFW_KEY_T:
  305.             if (flag_texture)
  306.                 flag_texture = false;
  307.             else
  308.                 flag_texture = true;
  309.             break;
  310.         case GLFW_KEY_C:
  311.             ofstream fout;
  312.             fout.open("./description.txt");
  313.             if (fout.is_open()) {
  314.                 fout << x_pos << endl << y_pos << endl << z_pos << endl <<
  315.                     tx << endl << ty << endl << tz << endl <<
  316.                     model_x << endl << model_y << endl << model_z << endl<<
  317.                     model1_x << endl << model1_y << endl << model1_z << endl <<
  318.                     s.R << endl << s.m << endl << s.n << endl << speed << endl << r_cub << endl <<
  319.                     flag_light0 << endl << flag_light1 << endl << flag_texture << endl << flag_1 << endl << flag;
  320.             }
  321.             fout.close();
  322.             break;
  323.        
  324.         }
  325.         switch (key) {
  326.         case GLFW_KEY_L:
  327.             load();
  328.             break;
  329.         }
  330.     }
  331.     sphere(&s);
  332. }
  333.  
  334. void scrollCallback(GLFWwindow *window, double xoffset, double yoffset)
  335. {
  336.     if (yoffset > 0) {
  337.         s.R -= 0.001;
  338.         r_cub -= 0.001;
  339.     }
  340.     if (yoffset < 0) {
  341.         s.R += 0.001;
  342.         r_cub += 0.001;
  343.     }
  344.     sphere(&s);
  345. }
  346.  
  347. void load() {
  348.     ifstream file("./description.txt");
  349.     file >> x_pos >> y_pos >> z_pos;
  350.     file >> tx >> ty >> tz;
  351.     file >> model_x >> model_y >> model_z;
  352.     file >> model1_x >> model1_y >> model1_z;
  353.     file >> s.R >> s.m >> s.n >> speed >> r_cub;
  354.     file >> flag_light0 >> flag_light1 >> flag_texture >> flag_1 >> flag;
  355.     file.close();
  356. }
  357.  
  358. void Texture() {
  359.     AUX_RGBImageRec *texture1;
  360.     texture1 = auxDIBImageLoad("C:/Users/Анастасия/Downloads/Paver300.bmp");
  361.  
  362.     glGenTextures(1, &texture[0]);
  363.     glBindTexture(GL_TEXTURE_2D, texture[0]);
  364.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  365.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  366.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  367.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  368.     glTexImage2D(GL_TEXTURE_2D, 0, 3, texture1->sizeX, texture1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture1->data);
  369.  
  370.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  371.  
  372. }
  373.  
  374. void sphere(Sphere *s) {
  375.     //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  376.     s->points = new float**[s->m];
  377.     for (int i = 0; i < s->m; i++) {
  378.         s->points[i] = new float*[s->n];
  379.         for (int j = 0; j < s->n; j++) {
  380.             s->points[i][j] = new float[3];
  381.         }
  382.     }
  383.     float dy = 0;
  384.     float dr = 0;
  385.     float teta = ((pi) / (s->m - 1));
  386.     for (int i = 0; i < s->m; i++) {
  387.         dy = s->R*cos(teta*i);
  388.         dr = s->R*sin(teta*i);
  389.         for (int j = 0; j < s->n; ++j) {
  390.             GLfloat fi = ((2 * pi) / s->n)*j;
  391.             GLfloat x = dr * cos(fi);
  392.             GLfloat z = dr * sin(fi);
  393.             GLfloat y = dy;
  394.             s->points[i][j][0] = x;
  395.             s->points[i][j][1] = y;
  396.             s->points[i][j][2] = z;
  397.         }
  398.     }
  399. }
  400.  
  401. void draw_Sphere(Sphere *s) {
  402.     glEnable(GL_DEPTH_TEST);
  403.     glDepthFunc(GL_LESS);
  404.  
  405.     for (int i = 0; i < s->m - 1; ++i) {
  406.         int k = 0;
  407.         for (int j = 0; j < s->n; ++j) {
  408.             glBegin(GL_POLYGON);
  409.             glColor3f(abs(10000 * (float(k) / s->n - 1)) / 10000.0, float(i) / s->m, 0.5); k++;
  410.  
  411.  
  412.             glNormal3f(s->points[i][j][0] + x_pos, s->points[i][j][1] + y_pos, s->points[i][j][2] + z_pos);
  413.             glTexCoord2f(s->points[i][j][0] + x_pos, s->points[i][j][1] + y_pos);
  414.             glVertex3f(s->points[i][j][0] + x_pos, s->points[i][j][1] + y_pos, s->points[i][j][2] + z_pos);
  415.  
  416.             glNormal3f(s->points[i][(j + 1) % s->n][0] + x_pos, s->points[i][(j + 1) % s->n][1] + y_pos, s->points[i][(j + 1) % s->n][2] + z_pos);
  417.             glTexCoord2f(s->points[i][(j + 1) % s->n][0] + x_pos, s->points[i][(j + 1) % s->n][1] + y_pos);
  418.             glVertex3f(s->points[i][(j + 1) % s->n][0] + x_pos, s->points[i][(j + 1) % s->n][1] + y_pos, s->points[i][(j + 1) % s->n][2] + z_pos);
  419.  
  420.             glNormal3f(s->points[i + 1][j][0] + x_pos, s->points[i + 1][j][1] + y_pos, s->points[i + 1][j][2] + z_pos);
  421.             glTexCoord2f(s->points[i + 1][j][0] + x_pos, s->points[i + 1][j][1] + y_pos);
  422.             glVertex3f(s->points[i + 1][j][0] + x_pos, s->points[i + 1][j][1] + y_pos, s->points[i + 1][j][2] + z_pos);
  423.  
  424.             glEnd();
  425.  
  426.             glBegin(GL_POLYGON);
  427.             glColor3f(abs(10000 * (float(k) / s->n - 1)) / 10000.0, float(i) / s->m, 0.5); k++;
  428.  
  429.             glNormal3f(s->points[i + 1][j][0] + x_pos, s->points[i + 1][j][1] + y_pos, s->points[i + 1][j][2] + z_pos);
  430.             glTexCoord2f(s->points[i + 1][j][0] + x_pos, s->points[i + 1][j][1] + y_pos);
  431.             glVertex3f(s->points[i + 1][j][0] + x_pos, s->points[i + 1][j][1] + y_pos, s->points[i + 1][j][2] + z_pos);
  432.  
  433.             glNormal3f(s->points[i][(j + 1) % s->n][0] + x_pos, s->points[i][(j + 1) % s->n][1] + y_pos, s->points[i][(j + 1) % s->n][2] + z_pos);
  434.             glTexCoord2f(s->points[i][(j + 1) % s->n][0] + x_pos, s->points[i][(j + 1) % s->n][1] + y_pos);
  435.             glVertex3f(s->points[i][(j + 1) % s->n][0] + x_pos, s->points[i][(j + 1) % s->n][1] + y_pos, s->points[i][(j + 1) % s->n][2] + z_pos);
  436.  
  437.             glNormal3f(s->points[i + 1][(j + 1) % s->n][0] + x_pos, s->points[i + 1][(j + 1) % s->n][1] + y_pos, s->points[i + 1][(j + 1) % s->n][2] + z_pos);
  438.             glTexCoord2f(s->points[i + 1][(j + 1) % s->n][0] + x_pos, s->points[i + 1][(j + 1) % s->n][1] + y_pos);
  439.             glVertex3f(s->points[i + 1][(j + 1) % s->n][0] + x_pos, s->points[i + 1][(j + 1) % s->n][1] + y_pos, s->points[i + 1][(j + 1) % s->n][2] + z_pos);
  440.  
  441.             glEnd();
  442.         }
  443.     }
  444.     return;
  445. }
  446.  
  447. void draw() {
  448.     /*for (int i = 0; i < arr.size() - 1; ++i) {
  449.         glBegin(GL_LINES);
  450.         glColor3f(1, 0, 0);
  451.         glVertex3f(arr[i].x, arr[i].y, arr[i].z);
  452.         glVertex3f(arr[i + 1].x, arr[i + 1].y, arr[i + 1].z);
  453.         glEnd();
  454.     }*/
  455.  
  456.    
  457.     /*for (int i = 0; i < 96; i+=3) {
  458.         cout << cub_coord[i] << " " << cub_coord[i + 1] << " " << cub_coord[i + 2] << endl;
  459.         glBegin(GL_LINES);
  460.         glColor3f(1, 0, 0);
  461.         glVertex3f(cub_coord[i], cub_coord[i+ 1], cub_coord[i + 2]);
  462.         glVertex3f(cub_coord[i+3], cub_coord[i + 4], cub_coord[i + 6]);
  463.         glEnd();
  464.     }*/
  465.  
  466.     glBegin(GL_LINES);
  467.     glColor3f(1, 0, 0);
  468.     glVertex3f(-r_cub, r_cub, r_cub);
  469.     glVertex3f(r_cub, r_cub, r_cub);
  470.     glEnd();
  471.  
  472.     glBegin(GL_LINES);
  473.     glColor3f(1, 0, 0);
  474.     glVertex3f(r_cub, r_cub, r_cub);
  475.     glVertex3f(r_cub, -r_cub, r_cub);
  476.     glEnd();
  477.  
  478.     glBegin(GL_LINES);
  479.     glColor3f(1, 0, 0);
  480.     glVertex3f(r_cub, -r_cub, r_cub);
  481.     glVertex3f(-r_cub, -r_cub, r_cub);
  482.     glEnd();
  483.  
  484.     glBegin(GL_LINES);
  485.     glColor3f(1, 0, 0);
  486.     glVertex3f(-r_cub, -r_cub, r_cub);
  487.     glVertex3f(-r_cub, r_cub, r_cub);
  488.     glEnd();
  489.  
  490.     glBegin(GL_LINES);
  491.     glColor3f(1, 0, 0);
  492.     glVertex3f(-r_cub, r_cub, -r_cub);
  493.     glVertex3f(r_cub, r_cub, -r_cub);
  494.     glEnd();
  495.  
  496.     glBegin(GL_LINES);
  497.     glColor3f(1, 0, 0);
  498.     glVertex3f(r_cub, r_cub, -r_cub);
  499.     glVertex3f(r_cub, -r_cub, -r_cub);
  500.     glEnd();
  501.  
  502.     glBegin(GL_LINES);
  503.     glColor3f(1, 0, 0);
  504.     glVertex3f(r_cub, -r_cub, -r_cub);
  505.     glVertex3f(-r_cub, -r_cub, -r_cub);
  506.     glEnd();
  507.  
  508.     glBegin(GL_LINES);
  509.     glColor3f(1, 0, 0);
  510.     glVertex3f(-r_cub, -r_cub, -r_cub);
  511.     glVertex3f(-r_cub, r_cub, -r_cub);
  512.     glEnd();
  513.  
  514.     glBegin(GL_LINES);
  515.     glColor3f(1, 0, 0);
  516.     glVertex3f(r_cub, r_cub, r_cub);
  517.     glVertex3f(r_cub, r_cub, -r_cub);
  518.     glEnd();
  519.  
  520.     glBegin(GL_LINES);
  521.     glColor3f(1, 0, 0);
  522.     glVertex3f(r_cub, -r_cub, r_cub);
  523.     glVertex3f(r_cub, -r_cub, -r_cub);
  524.     glEnd();
  525.  
  526.     glBegin(GL_LINES);
  527.     glColor3f(1, 0, 0);
  528.     glVertex3f(-r_cub, r_cub, r_cub);
  529.     glVertex3f(-r_cub, r_cub, -r_cub);
  530.     glEnd();
  531.  
  532.     glBegin(GL_LINES);
  533.     glColor3f(1, 0, 0);
  534.     glVertex3f(-r_cub, -r_cub, r_cub);
  535.     glVertex3f(-r_cub, -r_cub, -r_cub);
  536.     glEnd();
  537. }
  538.  
  539. void animation() {
  540.     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)) {
  541.         x_pos += tx * speed;
  542.         y_pos += ty * speed;
  543.         z_pos += tz * speed;
  544.         arr.push_back({ x_pos, y_pos, z_pos });
  545.     }
  546.     else {
  547.         float nx = 0, ny = 0, nz = 0;
  548.         if (x_pos <= -r_cub + s.R) {
  549.             nx = -1;
  550.         }
  551.         else if (x_pos >= r_cub - s.R) {
  552.             nx = 1;
  553.         }
  554.         else if (y_pos <= -r_cub + s.R) {
  555.             ny = -1;
  556.         }
  557.         else if (y_pos >= r_cub - s.R) {
  558.             ny = 1;
  559.         }
  560.         else if (z_pos <= -r_cub + s.R) {
  561.             nz = -1;
  562.         }
  563.         else {
  564.             nz = 1;
  565.         }
  566.         tx = tx - 2 * (tx*nx + ny * ty + nz * tz)*nx;
  567.         ty = ty - 2 * (tx*nx + ny * ty + nz * tz)*ny;
  568.         tz = tz - 2 * (tx*nx + ny * ty + nz * tz)*nz;
  569.         x_pos += tx * speed;
  570.         y_pos += ty * speed;
  571.         z_pos += tz * speed;
  572.         arr.push_back({ x_pos, y_pos, z_pos });
  573.     }
  574. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement