Advertisement
Guest User

Untitled

a guest
Jul 11th, 2018
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 55.67 KB | None | 0 0
  1. #include <iostream>
  2. // #include <map>
  3. // #include <string>
  4.  
  5. // GLEW
  6. #define GLEW_STATIC
  7. #include <GL/glew.h>
  8.  
  9. // GLFW
  10. //#include <GLFW/glfw3.h>
  11.  
  12. #include <SDL.h>
  13. #include <SDL_keyboard.h>
  14. #include <SDL_keysym.h>
  15. #include <stdio.h>
  16. //#include <stdlib.h>
  17.  
  18. // #include <GL/freeglut.h>
  19. #include <glm.hpp>
  20. #include <vector>
  21. //#include <deque>
  22. #include <array>
  23. #include <limits>
  24.  
  25. #define _USE_MATH_DEFINES
  26. #include <math.h>
  27.  
  28. //#include <ft2build.h>
  29.  
  30. //#include FT_FREETYPE_H  
  31. //#include <Shader.h>
  32.  
  33. using namespace std;
  34.  
  35. #define GRAVITY 4.0
  36. #define MAX_NUM_LIGHTS 8
  37. #define PI 3.14159265358979
  38. //#define MAXZ 8.0
  39. //#define MINZ -8.0
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50. // std::map<int, bool> keyboard; // Saves the state(true=pressed; false=released) of each SDL_Key.
  51.  
  52. bool keydown_left, keydown_right, keydown_up, keydown_down, keydown_alt;
  53. const GLuint WIDTH = 1024, HEIGHT = 768;
  54. GLuint VAO, VBO;
  55.  
  56.  
  57. /// Holds all state information relevant to a character as loaded using FreeType
  58. //struct Character {
  59. //  GLuint TextureID;   // ID handle of the glyph texture
  60. //  glm::ivec2 Size;    // Size of glyph
  61. //  glm::ivec2 Bearing;  // Offset from baseline to left/top of glyph
  62. //  GLuint Advance;    // Horizontal offset to advance to next glyph
  63. //};
  64. //std::map<char, Character> Characters;
  65.  
  66.  
  67. struct TColor {
  68.     short R, G, B, A;
  69. };
  70.  
  71. struct TLight {
  72.     double position[4];
  73.     double spotdirection[3];
  74.     short ambient[4];
  75.     short diffusion[4];
  76.     short specular[4];
  77.     float cutoff;
  78.     float exponent;
  79.     short n;
  80.     bool enabled;
  81. };
  82. std::vector<TLight>lights;
  83. unsigned int lights_counter;
  84. unsigned int light_number;
  85.  
  86.  
  87. struct TView {
  88.     glm::vec3 eye;
  89.     glm::vec3 vector;
  90.     glm::vec3 up;
  91.     float angle;
  92.     const char* mode;
  93. };
  94.  
  95.  
  96. //struct szejder {
  97. //  std::string shader1;
  98. //  std::string shader2;
  99. //};
  100.  
  101.  
  102. TColor bkgcolor;
  103. TView view;
  104. glm::vec3 area;
  105.  
  106. //typedef
  107. //int ctrlpoints2[][3];
  108. //ctrlpoints2 ctrlpoints[];
  109.  
  110. //std::vector<float>crosspoints;
  111.  
  112. std::vector<vector<std::array<float,3>>>crosspoints;
  113.  
  114. struct quad {
  115.     std::array<glm::vec3, 4>vertices;
  116.     GLfloat* ambient;
  117.     GLfloat* diffuse;
  118.     GLfloat* specular;
  119.     GLfloat* emission;
  120.     double shininess;
  121.     bool filled;
  122.     bool visible;
  123. };
  124. std::vector<quad>quads;
  125. unsigned int quads_counter;
  126.  
  127. struct ball {
  128.     glm::vec3 position;
  129.     glm::vec3 velocity;
  130.     glm::vec3 rotation_angle;
  131.     glm::vec3 rotation_speed;
  132.     glm::vec3 rotation_dir;
  133.     double radius;
  134.     std::vector<short>color1_RGB;
  135.     std::vector<short>color2_RGB;
  136.     GLuint texture;
  137.     double mass;
  138.     const char* ID;
  139.     GLUquadricObj *sphere;
  140. };
  141. std::vector<ball>balls;
  142. unsigned int balls_counter;
  143.  
  144.  
  145. GLfloat mat_solid[] = { 0.75, 0.75, 0.75, 1.0 };
  146. GLfloat mat_zero[] = { 0.0, 0.0, 0.0, 1.0 };
  147. GLfloat mat_transparent[] = { 0.0, 0.8, 0.8, 0.6 };
  148. GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  149. GLfloat mat_shininess[] = { 50.0 };
  150. GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
  151. GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8f, 1.0f };
  152. GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };
  153.  
  154. float ctrlpoints[9][9][3] {
  155.     { { -3.5, -1.0, -3.5 }, { -2.5, -1.0, -3.5 },
  156.     { -1.5, -1.0, -3.5 }, { -0.5, -1.0, -3.5 },
  157.     { 0.5, -1.0, -3.5 }, { 1.5, -1.0, -3.5 },
  158.     { 2.5, -1.0, -3.5 }, { 3.5, -1.0, -3.5 },
  159.     { 4.5, -1.0, -3.5 } },
  160.     { { -3.5, -1.0, -2.5 }, { -2.5, -1.0, -2.5 },
  161.     { -1.5, -1.0, -2.5 }, { -0.5, -1.0, -2.5 },
  162.     { 0.5, -1.0, -2.5 }, { 1.5, -1.0, -2.5 },
  163.     { 2.5, -1.0, -2.5 }, { 3.5, -1.0, -2.5 },
  164.     { 4.5, -1.0, -2.5 } },
  165.     { { -3.5, -1.0, -1.5 }, { -2.5, -1.0, -1.5 },
  166.     { -1.5, -1.0, -1.5 }, { -0.5, -1.0, -1.5 },
  167.     { 0.5, -1.0, -1.5 }, { 1.5, -1.0, -1.5 },
  168.     { 2.5, -1.0, -1.5 }, { 3.5, -1.0, -1.5 },
  169.     { 4.5, -1.0, -1.5 } },
  170.     { { -3.5, -1.0, -0.5 }, { -2.5, -1.0, -0.5 },
  171.     { -1.5, -1.0, -0.5 }, { -0.5, -1.0, -0.5 },
  172.     { 0.5, -1.0, -0.5 }, { 1.5, -1.0, -0.5 },
  173.     { 2.5, -1.0, -0.5 }, { 3.5, -1.0, -0.5 },
  174.     { 4.5, -1.0, -0.5 } },
  175.     { { -3.5, -1.0, 0.5 }, { -2.5, -1.0, 0.5 },
  176.     { -1.5, -1.0, 0.5 }, { -0.5, -1.0, 0.5 },
  177.     { 0.5, -1.0, 0.5 }, { 1.5, -1.0, 0.5 },
  178.     { 2.5, -1.0, 0.5 }, { 3.5, -1.0, 0.5 },
  179.     { 4.5, -1.0, 0.5 } },
  180.     { { -3.5, -1.0, 1.5 }, { -2.5, -1.0, 1.5 },
  181.     { -1.5, -1.0, 1.5 }, { -0.5, -1.0, 1.5 },
  182.     { 0.5, -1.0, 1.5 }, { 1.5, -1.0, 1.5 },
  183.     { 2.5, -1.0, 1.5 }, { 3.5, -1.0, 1.5 },
  184.     { 4.5, -1.0, 1.5 } },
  185.     { { -3.5, -1.0, 2.5 }, { -2.5, -1.0, 2.5 },
  186.     { -1.5, -1.0, 2.5 }, { -0.5, -1.0, 2.5 },
  187.     { 0.5, -1.0, 2.5 }, { 1.5, -1.0, 2.5 },
  188.     { 2.5, -1.0, 2.5 }, { 3.5, -1.0, 2.5 },
  189.     { 4.5, -1.0, 2.5 } },
  190.     { { -3.5, -1.0, 3.5 }, { -2.5, -1.0, 3.5 },
  191.     { -1.5, -1.0, 3.5 }, { -0.5, -1.0, 3.5 },
  192.     { 0.5, -1.0, 3.5 }, { 1.5, -1.0, 3.5 },
  193.     { 2.5, -1.0, 3.5 }, { 3.5, -1.0, 3.5 },
  194.     { 4.5, -1.0, 3.5 } },
  195.     { { -3.5, -1.0, 4.5 }, { -2.5, -1.0, 4.5 },
  196.     { -1.5, -1.0, 4.5 }, { -0.5, -1.0, 4.5 },
  197.     { 0.5, -1.0, 4.5 }, { 1.5, -1.0, 4.5 },
  198.     { 2.5, -1.0, 4.5 }, { 3.5, -1.0, 4.5 },
  199.     { 4.5, -1.0, 4.5 } }
  200. };
  201.  
  202. unsigned int collision_counter;
  203. bool run;
  204. bool show_coords;
  205. bool bouncing;
  206. bool quit;
  207.  
  208. //static float solidZ = MAXZ;
  209. //static float transparentZ = MINZ;
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224. double sqr(double number) { return (number * number); }
  225. double cotan(double number) { return 1.0 / tan(number); }
  226. //void RenderText(Shader &shader, std::string text, GLfloat x, GLfloat y, GLfloat scale, glm::vec3 color);
  227. static void inputSDL(void);
  228.  
  229. // grawitacja, zmiana pozycji pilek, odbijanie od podlogi i od scian
  230. void RunPhysics3(float dt, ball *object) {
  231.     double obj_newpos_X = object->position.x + object->velocity.x * 1;
  232.     double obj_newpos_Y = object->position.y - object->velocity.y / (double)65536.0;
  233.     double obj_newpos_Z = object->position.z + object->velocity.z * 1;
  234.  
  235.     // odbijanie od scian
  236.     if ((obj_newpos_X + object->radius >= (double)area.x / 2) || (obj_newpos_X - object->radius <= 0 - (double)area.x / 2)) {
  237.         object->velocity.x = -object->velocity.x;
  238.         object->rotation_dir.x = -1.0 * object->rotation_dir.x;
  239.     }
  240.     if ((obj_newpos_Z + object->radius >= (double)area.z / 2) || (obj_newpos_Z - object->radius <= 0 - (double)area.z / 2)) {
  241.         object->velocity.z = -object->velocity.z;
  242.         object->rotation_dir.z = -1.0 * object->rotation_dir.z;
  243.     }
  244.  
  245.     //  cout << "1velocity_y: " << object->velocity.y << "\n";
  246.     //  if (object->position.y - object->radius < -1) {
  247.     if (object->position.y - object->radius > -1) object->velocity.y += GRAVITY;
  248.     //}
  249.     //  cout << "2velocity_y: " << object->velocity.y << "\n";
  250.     object->rotation_angle.x += object->rotation_speed.x * object->rotation_dir.x;
  251.     object->rotation_angle.y += object->rotation_speed.y * object->rotation_dir.y;
  252.     object->rotation_angle.z += object->rotation_speed.z * object->rotation_dir.z;
  253.     object->position.x = object->position.x + object->velocity.x;
  254.     object->position.y = object->position.y - object->velocity.y / (double)65536.0;
  255.     object->position.z = object->position.z + object->velocity.z;
  256.     if (object->position.y - object->radius < -1.0) {
  257.         object->position.y = -1.0 + object->radius;
  258.         if (!bouncing) object->velocity.y = 0.0;
  259.         object->velocity.y = -object->velocity.y;
  260.     }
  261. }
  262.  
  263.  
  264.  
  265.  
  266.  
  267. // detekcja kolizji miedzy pilkami
  268. // ta funkcja wykorzystuje wartosci odleglosci miedzy kulami ze znakiem
  269. // liczy kat fi z arcus tangens cotangensa + 0.5 PI
  270. // liczy katy theta z arcus tangens tangensa plus PI, jesli sinus theta jest mniejszy od 0
  271. // uwzglednia znak predkosci X i Z w obliczeniach skladowych predkosci koncowych
  272. // zakladajac poprawne dzialanie calosci 80% z tego mozna wywalic... (+ pozbyc sie petli)
  273. void CollisionDetect7(ball *object1, ball *object2) {
  274.     // przypuszczalne wspolrzedne srodkow kul w chwili t+1
  275.     double obj1_newpos_X = object1->position.x + object1->velocity.x * 3;
  276.     double obj1_newpos_Y = object1->position.y - object1->velocity.y / (double)65536.0;
  277.     double obj1_newpos_Z = object1->position.z + object1->velocity.z * 3;
  278.     double obj2_newpos_X = object2->position.x + object2->velocity.x * 3;
  279.     double obj2_newpos_Y = object2->position.y - object2->velocity.y / (double)65536.0;
  280.     double obj2_newpos_Z = object2->position.z + object2->velocity.z * 3;
  281.  
  282.     // obliczenie odleglosci miedzy srodkami kul w chwili t+1
  283.     double newdistance_x = abs(obj1_newpos_X - obj2_newpos_X);
  284.     double newdistance_y = abs(obj1_newpos_Y - obj2_newpos_Y);
  285.     double newdistance_z = abs(obj1_newpos_Z - obj2_newpos_Z);
  286.     double newdistance_xyz = sqrt(sqr(newdistance_x) + sqr(newdistance_y) + sqr(newdistance_z));
  287.  
  288.     // jesli odleglosc w chwili t+1 <= sumie promieni obu kul, nastepuje kolizja
  289.     if (newdistance_xyz < object1->radius + object2->radius) {
  290.         collision_counter++;
  291.         cout << collision_counter << ". Kolizja " << object1->ID << " z " << object2->ID << "\n";
  292.  
  293.         double obj1_velocity_X = object1->velocity.x;
  294.         double obj1_velocity_Y = object1->velocity.y;
  295.         double obj1_velocity_Z = object1->velocity.z;
  296.         double obj2_velocity_X = object2->velocity.x;
  297.         double obj2_velocity_Y = object2->velocity.y;
  298.         double obj2_velocity_Z = object2->velocity.z;
  299.         double obj1_currpos_X = object1->position.x;
  300.         double obj1_currpos_Y = object1->position.y;
  301.         double obj1_currpos_Z = object1->position.z;
  302.         double obj2_currpos_X = object2->position.x;
  303.         double obj2_currpos_Y = object2->position.y;
  304.         double obj2_currpos_Z = object2->position.z;
  305.         double currdistance_x = obj1_currpos_X - obj2_currpos_X;
  306.         double currdistance_y = obj1_currpos_Y - obj2_currpos_Y;
  307.         double currdistance_z = obj1_currpos_Z - obj2_currpos_Z;
  308.         double currdistance_xz = sqrt(sqr(currdistance_x) + sqr(currdistance_z));
  309.         double currdistance_xyz = sqrt(sqr(currdistance_x) + sqr(currdistance_y) + sqr(currdistance_z));
  310.  
  311.         double mass = object1->mass / object2->mass;
  312.         double restitution = 1.0;
  313.  
  314.         double sin_fi = currdistance_x / currdistance_xz;
  315.         double cos_fi = currdistance_z / currdistance_xz;
  316.         double tg_fi = currdistance_x / currdistance_z;
  317.         double ctg_fi = currdistance_z / currdistance_x;
  318.         double fi = 0.0 + atan(ctg_fi) + 0.5 * PI;
  319.  
  320.         double psi = acos(currdistance_xz / currdistance_xyz);
  321.  
  322.         double obj1_newpos_X2, obj1_newpos_Y2, obj1_newpos_Z2, obj2_newpos_X2, obj2_newpos_Y2, obj2_newpos_Z2;
  323.         double newdistance2_x, newdistance2_y, newdistance2_z, newdistance2_xyz = 0.0;
  324.         double obj1_velocity_XZ, obj2_velocity_XZ, obj1_velocity_XYZ, obj2_velocity_XYZ;
  325.         double sin_theta1, sin_theta2, cos_theta1, cos_theta2, tg_theta1, tg_theta2, ctg_theta1, ctg_theta2, theta1, theta2;
  326.         double sin_gamma1, sin_gamma2, cos_gamma1, cos_gamma2, tg_gamma1, tg_gamma2, ctg_gamma1, ctg_gamma2, gamma1, gamma2;
  327.         double dzeta_eta_W1x, dzeta_eta_W1y, dzeta_eta_W1z, dzeta_eta_W2x, dzeta_eta_W2y, dzeta_eta_W2z, dzeta_eta_W1xz, dzeta_eta_W2xz;
  328.         double dzeta_eta_W1x_s1, dzeta_eta_W2x_s1, dzeta_eta_W1y_s1, dzeta_eta_W2y_s1, dzeta_eta_W1z_s1, dzeta_eta_W1z_s2, dzeta_eta_W2z_s1, dzeta_eta_W2z_s2;
  329.         double obj1_velocity_Y_s1, obj1_velocity_Y_s2, obj2_velocity_Y_s1, obj2_velocity_Y_s2;
  330.  
  331.         unsigned int collision_counter2 = 0;
  332.  
  333.         while (newdistance2_xyz < newdistance_xyz) {
  334.             collision_counter2++;
  335.             cout << collision_counter << ". #" << collision_counter2 << " " << newdistance2_xyz << ", " << newdistance_xyz << ", " << currdistance_xyz << "\n";
  336.  
  337.             // dlugosci wektorow predkosci wypadkowych na plaszczyznie XZ i w XYZ
  338.             obj1_velocity_XZ = sqrt(sqr(obj1_velocity_X) + sqr(obj1_velocity_Z));
  339.             obj2_velocity_XZ = sqrt(sqr(obj2_velocity_X) + sqr(obj2_velocity_Z));
  340.             obj1_velocity_XYZ = sqrt(sqr(obj1_velocity_X) + sqr(obj1_velocity_Y) + sqr(obj1_velocity_Z));
  341.             obj2_velocity_XYZ = sqrt(sqr(obj2_velocity_X) + sqr(obj2_velocity_Y) + sqr(obj2_velocity_Z));
  342.  
  343.             // obliczanie katow pomiedzy wektorami predkosci wypadkowych kul na plaszczyznie XZ a osia X ukladu
  344.             // wspolrzednych OX
  345.             sin_theta1 = obj1_velocity_Z / obj1_velocity_XZ;
  346.             sin_theta2 = obj2_velocity_Z / obj2_velocity_XZ;
  347.             cos_theta1 = obj1_velocity_X / obj1_velocity_XZ;
  348.             cos_theta2 = obj2_velocity_X / obj2_velocity_XZ;
  349.             tg_theta1 = obj1_velocity_Z / obj1_velocity_X;
  350.             tg_theta2 = obj2_velocity_Z / obj2_velocity_X;
  351.             ctg_theta1 = obj1_velocity_X / obj1_velocity_Z;
  352.             ctg_theta2 = obj2_velocity_X / obj2_velocity_Z;
  353.             // kat theta musi byc liczony z (co)tangensa!! Ze wzgledu na wartosc sinusa/cosinusa,
  354.             // ktory w mianowniku zawsze ma liczbe dodatnia
  355.             theta1 = 0.0 + atan(tg_theta1);
  356.             theta2 = 0.0 + atan(tg_theta2);
  357.             // if (isinf(tg_theta1)) theta1 = 0.0 + atan(ctg_theta1);
  358.             // if (isinf(tg_theta2)) theta2 = 0.0 + atan(ctg_theta2);
  359.             if (sin_theta1 < 0) theta1 = theta1 + PI;
  360.             if (sin_theta2 < 0) theta2 = theta2 + PI;
  361.             // ponizsze jest, zeby miara katow nie byla ujemna. W sumie zbedne.
  362.             if (theta1 < 0) theta1 = 2 * PI + theta1;
  363.             if (theta2 < 0) theta2 = 2 * PI + theta2;
  364.  
  365.             // obliczanie katow GAMMA pomiedzy wektorami predkosci wypadkowych w przestrzeni XYZ a plaszczyzna XZ
  366.             // ukladu wspolrzednych OX
  367.             sin_gamma1 = obj1_velocity_X / obj1_velocity_XYZ;
  368.             sin_gamma2 = obj2_velocity_X / obj2_velocity_XYZ;
  369.             cos_gamma1 = obj1_velocity_XZ / obj1_velocity_XYZ;
  370.             cos_gamma2 = obj2_velocity_XZ / obj2_velocity_XYZ;
  371.             tg_gamma1 = obj1_velocity_X / obj1_velocity_XZ;
  372.             tg_gamma2 = obj2_velocity_X / obj2_velocity_XZ;
  373.             ctg_gamma1 = obj1_velocity_XZ / obj1_velocity_X;
  374.             ctg_gamma2 = obj2_velocity_XZ / obj2_velocity_X;
  375.             if (((obj1_velocity_X < 0) && (obj1_velocity_Z >= 0)) || ((obj1_velocity_X >= 0) && (obj1_velocity_Z < 0))) {
  376.                 tg_gamma1 = -tg_gamma1;
  377.                 ctg_gamma1 = -ctg_gamma1;
  378.             }
  379.             if (((obj2_velocity_X < 0) && (obj2_velocity_Z >= 0)) || ((obj2_velocity_X >= 0) && (obj2_velocity_Z < 0))) {
  380.                 tg_gamma2 = -tg_gamma2;
  381.                 ctg_gamma2 = -ctg_gamma2;
  382.             }
  383.             gamma1 = 0.0 + atan(tg_gamma1);
  384.             gamma2 = 0.0 + atan(tg_gamma2);
  385.             // ponizsze jest, zeby miara katow nie byla ujemna. W sumie zbedne.
  386.             if (gamma1 < 0) gamma1 = 2 * PI + gamma1;
  387.             if (gamma2 < 0) gamma2 = 2 * PI + gamma2;
  388.  
  389.             cout << collision_counter << ". fi=" << fi * 180 / PI << " theta1 (" << object1->ID << ")=" << theta1 * 180 / PI << " theta2 (" << object2->ID << ")=" << theta2 * 180 / PI << "\n";
  390.  
  391.             // obliczanie skladowych predkosci koncowych W obu kul po zderzeniu w ukladzie dzeta-eta.
  392.             // *_s1, *_s2 - skladniki do koncowych rownan
  393.             dzeta_eta_W1x_s1 = obj1_velocity_XZ * cos(theta1 - fi);
  394.             dzeta_eta_W2x_s1 = obj2_velocity_XZ * cos(theta2 - fi);
  395.             dzeta_eta_W1y_s1 = obj1_velocity_XYZ * sin(gamma1 - psi);
  396.             dzeta_eta_W2y_s1 = obj2_velocity_XYZ * sin(gamma1 - psi);
  397.             dzeta_eta_W1z_s1 = obj1_velocity_XZ * ((mass - restitution) / (mass + restitution)) * sin(theta1 - fi);
  398.             dzeta_eta_W1z_s2 = obj2_velocity_XZ * ((restitution + 1) / (mass + 1)) * sin(theta2 - fi);
  399.             dzeta_eta_W2z_s1 = obj1_velocity_XZ * ((mass * (restitution + 1)) / (mass + 1)) * sin(theta1 - fi);
  400.             dzeta_eta_W2z_s2 = obj2_velocity_XZ * ((1 - restitution * mass) / (mass + 1)) * sin(theta2 - fi);
  401.             if (((obj1_velocity_X < 0) && (obj1_velocity_Z >= 0)) || ((obj1_velocity_X >= 0) && (obj1_velocity_Z < 0))) {
  402.                 dzeta_eta_W1x_s1 = -dzeta_eta_W1x_s1;
  403.                 dzeta_eta_W1z_s1 = -dzeta_eta_W1z_s1;
  404.                 dzeta_eta_W2z_s1 = -dzeta_eta_W2z_s1;
  405.             }
  406.             if (((obj2_velocity_X < 0) && (obj2_velocity_Z >= 0)) || ((obj2_velocity_X >= 0) && (obj2_velocity_Z < 0))) {
  407.                 dzeta_eta_W2x_s1 = -dzeta_eta_W2x_s1;
  408.                 dzeta_eta_W1z_s2 = -dzeta_eta_W1z_s2;
  409.                 dzeta_eta_W2z_s2 = -dzeta_eta_W2z_s2;
  410.             }
  411.             dzeta_eta_W1x = dzeta_eta_W1x_s1;
  412.             dzeta_eta_W2x = dzeta_eta_W2x_s1;
  413.             dzeta_eta_W1y = dzeta_eta_W1y_s1;
  414.             dzeta_eta_W2y = dzeta_eta_W2y_s1;
  415.             dzeta_eta_W1z = dzeta_eta_W1z_s1 + dzeta_eta_W1z_s2;
  416.             dzeta_eta_W2z = dzeta_eta_W2z_s1 + dzeta_eta_W2z_s2;
  417.             dzeta_eta_W1xz = sqrt(sqr(dzeta_eta_W1x) + sqr(dzeta_eta_W1z));
  418.             dzeta_eta_W2xz = sqrt(sqr(dzeta_eta_W2x) + sqr(dzeta_eta_W2z));
  419.  
  420.             // obliczanie skladowych wypadkowych predkosci koncowych w ukladzie XZ
  421.             obj1_velocity_Y_s1 = dzeta_eta_W1y * cos(psi);
  422.             obj1_velocity_Y_s2 = dzeta_eta_W1xz * sin(psi);
  423.             obj2_velocity_Y_s1 = dzeta_eta_W2y * cos(psi);
  424.             obj2_velocity_Y_s2 = dzeta_eta_W2xz * sin(psi);
  425.             if (((obj1_velocity_X < 0) && (obj1_velocity_Z >= 0)) || ((obj1_velocity_X >= 0) && (obj1_velocity_Z < 0))) {
  426.                 obj1_velocity_Y_s2 = -obj1_velocity_Y_s2;
  427.             }
  428.             if (((obj2_velocity_X < 0) && (obj2_velocity_Z >= 0)) || ((obj2_velocity_X >= 0) && (obj2_velocity_Z < 0))) {
  429.                 obj2_velocity_Y_s2 = -obj2_velocity_Y_s2;
  430.             }
  431.             obj1_velocity_X = dzeta_eta_W1x * cos(fi) - dzeta_eta_W1z * sin(fi);
  432.             obj1_velocity_Z = dzeta_eta_W1x * sin(fi) + dzeta_eta_W1z * cos(fi);
  433.             obj2_velocity_X = dzeta_eta_W2x * cos(fi) - dzeta_eta_W2z * sin(fi);
  434.             obj2_velocity_Z = dzeta_eta_W2x * sin(fi) + dzeta_eta_W2z * cos(fi);
  435.             // obj1_velocity_Y = dzeta_eta_W1y * cos(psi) + dzeta_eta_W1xz * sin(psi);
  436.             // obj2_velocity_Y = dzeta_eta_W2y * cos(psi) + dzeta_eta_W2xz * sin(psi);
  437.             obj1_velocity_Y = obj1_velocity_Y_s1 + obj1_velocity_Y_s2;
  438.             obj2_velocity_Y = obj2_velocity_Y_s1 + obj2_velocity_Y_s2;
  439.  
  440.             // sprawdzanie kolizji ze scianami...
  441.             if ((obj1_currpos_X + object1->radius >= (double)area.x / 2) || (obj1_currpos_X - object1->radius <= 0 - (double)area.x / 2)) {
  442.                 obj2_velocity_X = -obj2_velocity_X;
  443.                 object2->rotation_dir.x = -1.0 * object2->rotation_dir.x;
  444.             }
  445.             if ((obj1_currpos_Z + object1->radius >= (double)area.z / 2) || (obj1_currpos_Z - object1->radius <= 0 - (double)area.z / 2)) {
  446.                 obj2_velocity_Z = -obj2_velocity_Z;
  447.                 object2->rotation_dir.z = -1.0 * object2->rotation_dir.z;
  448.             }
  449.             if ((obj2_currpos_X + object2->radius >= (double)area.x / 2) || (obj2_currpos_X - object2->radius <= 0 - (double)area.x / 2)) {
  450.                 obj1_velocity_X = -obj1_velocity_X;
  451.                 object1->rotation_dir.x = -1.0 * object1->rotation_dir.x;
  452.             }
  453.             if ((obj2_currpos_Z + object2->radius >= (double)area.z / 2) || (obj2_currpos_Z - object2->radius <= 0 - (double)area.z / 2)) {
  454.                 obj1_velocity_Z = -obj1_velocity_Z;
  455.                 object1->rotation_dir.z = -1.0 * object1->rotation_dir.z;
  456.             }
  457.  
  458.             // zapisanie do zmiennej odleglosci miedzy kulami po przesunieciu o wektor obliczonej predkosci,
  459.             // dla porownania WHILE.
  460.             // przy prawidlowym dzialaniu calosci petla jest zbedna, wiec mozna wywalic.
  461.             obj1_newpos_X2 = obj1_currpos_X + obj1_velocity_X * 3;
  462.             // obj1_newpos_Y2 = obj1_currpos_Y - obj1_velocity_Y / (double)65536.0;
  463.             // obj1_newpos_Y2 = obj1_currpos_Y;
  464.             obj1_newpos_Y2 = obj1_currpos_Y + obj1_velocity_Y * 3;
  465.             obj1_newpos_Z2 = obj1_currpos_Z + obj1_velocity_Z * 3;
  466.             obj2_newpos_X2 = obj2_currpos_X + obj2_velocity_X * 3;
  467.             // obj2_newpos_Y2 = obj2_currpos_Y - obj2_velocity_Y / (double)65536.0;
  468.             // obj2_newpos_Y2 = obj2_currpos_Y;
  469.             obj2_newpos_Y2 = obj2_currpos_Y + obj2_velocity_Y * 3;
  470.             obj2_newpos_Z2 = obj2_currpos_Z + obj2_velocity_Z * 3;
  471.             newdistance2_x = abs(obj2_newpos_X2 - obj1_newpos_X2);
  472.             newdistance2_y = abs(obj2_newpos_Y2 - obj1_newpos_Y2);
  473.             newdistance2_z = abs(obj2_newpos_Z2 - obj1_newpos_Z2);
  474.             newdistance2_xyz = sqrt(sqr(newdistance2_x) + sqr(newdistance2_y) + sqr(newdistance2_z));
  475.         }
  476.         object1->velocity.x = obj1_velocity_X;
  477.         object1->velocity.y = obj1_velocity_Y;
  478.         object1->velocity.z = obj1_velocity_Z;
  479.         object2->velocity.x = obj2_velocity_X;
  480.         object2->velocity.y = obj2_velocity_Y;
  481.         object2->velocity.z = obj2_velocity_Z;
  482.     }
  483. }
  484.  
  485.  
  486.  
  487. // tworzy teksture boing balli
  488. void make_tex(ball *object) {
  489.     unsigned char data[128][128][3];
  490.     for (unsigned short y = 0; y < 128; y++) {
  491.         for (unsigned short x = 0; x < 128; x++) {
  492.             unsigned char *p = data[y][x];
  493.             if ((x ^ y) & 2) {
  494.                 p[0] = object->color1_RGB[0];
  495.                 p[1] = object->color1_RGB[1];
  496.                 p[2] = object->color1_RGB[2];
  497.             }
  498.             else {
  499.                 p[0] = object->color2_RGB[0];
  500.                 p[1] = object->color2_RGB[1];
  501.                 p[2] = object->color2_RGB[2];
  502.             }
  503.         }
  504.     }
  505.     glGenTextures(1, &(object->texture));
  506.     glBindTexture(GL_TEXTURE_2D, object->texture);
  507.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, (const GLvoid *)data);
  508.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  509.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  510. }
  511.  
  512.  
  513.  
  514. // rysowanie wszystkiego
  515. void draw() {
  516.     glClearColor((float)bkgcolor.R/256, (float)bkgcolor.G/256, (float)bkgcolor.B/256, 1);
  517.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  518.  
  519. //  Shader shader("shader.vs", "shader.frag");
  520. //  RenderText(shader, "This is sample text", 25.0f, 25.0f, 1.0f, glm::vec3(0.5, 0.8f, 0.2f));
  521. //  RenderText(shader, "(C) LearnOpenGL.com", 540.0f, 570.0f, 0.5f, glm::vec3(0.3, 0.7f, 0.9f));
  522. //  font.setPenPosition(16, 16);
  523. //  font.draw("Hello, gltext!");
  524.  
  525.     // rysowanie siatki na podlodze
  526.     glMaterialfv(GL_FRONT, GL_EMISSION, mat_zero);
  527.     glLineWidth(0.5);
  528.     unsigned short i, j;
  529.     for (j = 0; j <= 8; j++) {
  530.         glBegin(GL_LINE_STRIP);
  531.         for (i = 0; i <= 30; i++)
  532.             glEvalCoord2f((GLfloat)i / 30.0, (GLfloat)j / 8.0);
  533.         glEnd();
  534.         glBegin(GL_LINE_STRIP);
  535.         for (i = 0; i <= 30; i++)
  536.             glEvalCoord2f((GLfloat)j / 8.0, (GLfloat)i / 30.0);
  537.         glEnd();
  538.     }
  539.    
  540.     // rysowanie kulek
  541.     glMaterialfv(GL_FRONT, GL_EMISSION, mat_zero);
  542.     for (i = 0; i < balls_counter; i++) {
  543.         glMatrixMode(GL_MODELVIEW);
  544.         glLoadIdentity();
  545.         glTranslated(balls[i].position.x, balls[i].position.y, balls[i].position.z);
  546.         glRotated(150, 0.0, 1.0, 1.0);
  547.         glRotated(balls[i].rotation_angle.x, 0.0, 0.0, 2.0);
  548.         gluQuadricDrawStyle(balls[i].sphere, GLU_FILL);
  549.         glBindTexture(GL_TEXTURE_2D, balls[i].texture);
  550.         gluQuadricTexture(balls[i].sphere, GL_TRUE);
  551.         gluQuadricNormals(balls[i].sphere, GLU_SMOOTH);
  552.         gluSphere(balls[i].sphere, balls[i].radius, 32, 16);
  553.     }
  554.    
  555.     // rysowanie scian, sufitu i podlogi
  556.     glMatrixMode(GL_MODELVIEW);
  557.     glLoadIdentity();
  558.     for (int i = 0; i < quads_counter; i++) {
  559.         if (quads[i].visible == true) {
  560.             if (quads[i].filled == true) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  561.             else if (quads[i].filled == false) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  562.             glBegin(GL_QUADS);
  563.             glMaterialfv(GL_FRONT, GL_EMISSION, quads[i].emission);
  564.             glMaterialfv(GL_FRONT, GL_SPECULAR, quads[i].specular);
  565.             glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  566.             if ((quads[i].vertices[0].x == quads[i].vertices[1].x) && (quads[i].vertices[0].x == quads[i].vertices[2].x) && (quads[i].vertices[0].x == quads[i].vertices[3].x)) glNormal3f(1.0, 0.0, 0.0);
  567.             else if ((quads[i].vertices[0].y == quads[i].vertices[1].y) && (quads[i].vertices[0].y == quads[i].vertices[2].y) && (quads[i].vertices[0].y == quads[i].vertices[3].y)) glNormal3f(0.0, 1.0, 0.0);
  568.             else if ((quads[i].vertices[0].z == quads[i].vertices[1].z) && (quads[i].vertices[0].z == quads[i].vertices[2].z) && (quads[i].vertices[0].z == quads[i].vertices[3].z)) glNormal3f(0.0, 0.0, 1.0);
  569.             for (int j = 0; j < 4; j++) {
  570.                 glVertex3d(quads[i].vertices[j].x, quads[i].vertices[j].y, quads[i].vertices[j].z);
  571.             }
  572.             for (int j = 3; j >= 0; j--) {
  573.                 glVertex3d(quads[i].vertices[j].x, quads[i].vertices[j].y, quads[i].vertices[j].z);
  574.             }
  575.             glEnd();
  576.         }
  577.     }
  578.  
  579. //-------------------------- smieciuchy -------------------------------
  580.  
  581.     glMaterialfv(GL_FRONT, GL_EMISSION, mat_transparent);
  582.     //  glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
  583.     glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
  584. //  glColor3f(0.0, 1.0, 1.0);
  585.     //glEnd();
  586.     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  587.     glMatrixMode(GL_MODELVIEW);
  588.     glLoadIdentity();
  589. //  glColor3f(0.0, 1.0, 1.0);
  590.  
  591.  
  592. //  glTranslatef(0.15, 0.15, transparentZ);
  593. //  glRotatef(15.0, 1.0, 1.0, 0.0);
  594. //  glRotatef(30.0, 0.0, 1.0, 0.0);
  595.     glMaterialfv(GL_FRONT, GL_EMISSION, mat_solid);
  596. //  glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_transparent);
  597. //  glEnable(GL_BLEND);
  598.     glDepthMask(GL_FALSE);
  599. //  glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  600. //  glCallList(cubeList);
  601.     glDepthMask(GL_TRUE);
  602. //  glDisable(GL_BLEND);
  603. //  glutWireCube(1.0);
  604.  
  605. //--------------------------- koniec smieciuchow :=( -----------------------------
  606.  
  607.     // ustawienie obserwatora
  608.     glMatrixMode(GL_PROJECTION);
  609.     glLoadIdentity();
  610.     gluPerspective(60.0, (GLdouble)1024 / 768, 1.0, 100);
  611.     gluLookAt(view.eye.x, view.eye.y, view.eye.z, view.eye.x + view.vector.x, view.eye.y + view.vector.y, view.eye.z - view.vector.z, 0.0, view.up.y, 0.0);
  612. //  glutPostRedisplay();
  613. //  glutSwapBuffers();
  614.     SDL_GL_SwapBuffers();
  615. }
  616.  
  617.  
  618.  
  619. // zmiana rozmiarow okna... przy korzystaniu z okienek SDL niepotrzebne.
  620. void resize(unsigned int w, unsigned int h) {
  621.     if (!h) h = 1;
  622.     glViewport(0, 0, w, h);
  623.     glMatrixMode(GL_PROJECTION);
  624.     glLoadIdentity();
  625.     gluPerspective(60.0, 1.00*w / h , 1.0, 100);
  626.     glMatrixMode(GL_MODELVIEW);
  627.     glLoadIdentity();
  628.     gluLookAt(view.eye.x, view.eye.y, view.eye.z, view.eye.x + view.vector.x, view.eye.y + view.vector.y, view.eye.z - view.vector.z, 0.0, view.up.y, 0.0);
  629. }
  630.  
  631.  
  632.  
  633. // zmiana widoku
  634. void changeView(const char* mode) {
  635.     if (mode == "viewAngleX") {
  636.         // przelaczenie na widok pod katem na os X
  637.         view.eye.x = -0.2f;
  638.         view.eye.y = 7.0f;
  639.         view.eye.z = 7.0f;
  640.         view.angle = 0.0f;
  641.         view.up.y = 1.0f;
  642.         view.vector.y = -1.0f;
  643.         view.mode = "angleX";
  644.     }
  645.     else if (mode == "viewAngleZ") {
  646.         // przelaczenie na widok pod katem na os Z
  647.         view.eye.x = -7.0f;
  648.         view.eye.y = 7.0f;
  649.         view.eye.z = -0.2f;
  650.         view.angle = 90.0f;
  651.         view.up.y = 1.0f;
  652.         view.vector.y = -1.0f;
  653.         view.mode = "angleZ";
  654.     }
  655.     else if (mode == "viewTopX") {
  656.         // przelaczenie na widok z gory
  657.         view.eye.x = -0.2f;
  658.         view.eye.y = 10.0f;
  659.         view.eye.z = 2.5f;
  660.         view.angle = 0.0f;
  661.         view.up.y = 1.0f;
  662.         view.vector.y = -5.0f;
  663.         view.mode = "topX";
  664.     }
  665.     else if (mode == "viewTopZ") {
  666.         // przelaczenie na widok z gory
  667.         view.eye.x = -2.5f;
  668.         view.eye.y = 10.0f;
  669.         view.eye.z = -0.2f;
  670.         view.angle = 90.0f;
  671.         view.up.y = 1.0f;
  672.         view.vector.y = -5.0f;
  673.         view.mode = "topZ";
  674.     }
  675.     else if (mode == "viewX") {
  676.         // przelaczenie na widok na os X
  677.         view.eye.x = 0.0f;
  678.         view.eye.y = 0.0f;
  679.         view.eye.z = 10.0f;
  680.         view.angle = 0.0f;
  681.         view.up.y = 1.0f;
  682.         view.vector.y = 0.0f;
  683.         view.mode = "X";
  684.     }
  685.     else if (mode == "viewZ") {
  686.         // przelaczenie na widok na os Z
  687.         view.eye.x = -10.0f;
  688.         view.eye.y = 0.0f;
  689.         view.eye.z = 0.0f;
  690.         view.angle = 90.0f;
  691.         view.up.y = 1.0f;
  692.         view.vector.y = 0.0f;
  693.         view.mode = "Z";
  694.     }
  695.     view.vector.x = (float)sin(view.angle * PI / 180);
  696.     view.vector.z = (float)cos(view.angle * PI / 180);
  697. }
  698.  
  699.  
  700.  
  701. // zmiana pozycji obserwatora
  702. void move(const char* direction) {
  703.     keydown_alt = ((SDL_GetModState() & KMOD_ALT) != 0);
  704.     if (direction == "up") {
  705.         if (view.angle < 90) {
  706.             if ((int)view.angle % 90 == 0) view.eye.z = view.eye.z -= 0.5;
  707.             else {
  708.                 view.eye.z = view.eye.z - view.vector.z;
  709.                 view.eye.x = view.eye.x + view.vector.x;
  710.             }
  711.         }
  712.         else if (view.angle < 180) {
  713.             if ((int)view.angle % 90 == 0) view.eye.x = view.eye.x += 0.5;
  714.             else {
  715.                 view.eye.z = view.eye.z - view.vector.z;
  716.                 view.eye.x = view.eye.x + view.vector.x;
  717.             }
  718.         }
  719.         else if (view.angle < 270) {
  720.             if ((int)view.angle % 90 == 0) view.eye.z = view.eye.z += 0.5;
  721.             else {
  722.                 view.eye.z = view.eye.z - view.vector.z;
  723.                 view.eye.x = view.eye.x + view.vector.x;
  724.             }
  725.         }
  726.         else if (view.angle < 360) {
  727.             if ((int)view.angle % 90 == 0) view.eye.x = view.eye.x -= 0.5;
  728.             else {
  729.                 view.eye.z = view.eye.z - view.vector.z;
  730.                 view.eye.x = view.eye.x + view.vector.x;
  731.             }
  732.         }
  733.     }
  734.     else if (direction == "down") {
  735.         if (view.angle < 90) {
  736.             if ((int)view.angle % 90 == 0) view.eye.z = view.eye.z += 0.5;
  737.             else {
  738.                 view.eye.z = view.eye.z + view.vector.z;
  739.                 view.eye.x = view.eye.x - view.vector.x;
  740.             }
  741.         }
  742.         else if (view.angle < 180) {
  743.             if ((int)view.angle % 90 == 0) view.eye.x = view.eye.x -= 0.5;
  744.             else {
  745.                 view.eye.z = view.eye.z + view.vector.z;
  746.                 view.eye.x = view.eye.x - view.vector.x;
  747.             }
  748.         }
  749.         else if (view.angle < 270) {
  750.             if ((int)view.angle % 90 == 0) view.eye.z = view.eye.z -= 0.5;
  751.             else {
  752.                 view.eye.z = view.eye.z + view.vector.z;
  753.                 view.eye.x = view.eye.x - view.vector.x;
  754.             }
  755.         }
  756.         else if (view.angle < 360) {
  757.             if ((int)view.angle % 90 == 0) view.eye.x = view.eye.x += 0.5;
  758.             else {
  759.                 view.eye.z = view.eye.z + view.vector.z;
  760.                 view.eye.x = view.eye.x - view.vector.x;
  761.             }
  762.         }
  763.     }
  764.     else if (direction == "left") {
  765.         if (keydown_alt == 1) {
  766.             if ((int)view.angle % 90 == 0) {
  767.                 if (view.angle < 90) view.eye.x -= 0.5;
  768.                 else if (view.angle < 180) view.eye.z -= 0.5;
  769.                 else if (view.angle < 270) view.eye.x += 0.5;
  770.                 else if (view.angle < 360) view.eye.z += 0.5;
  771.             }
  772.             else {
  773.                 view.eye.x -= view.vector.z;
  774.                 view.eye.z -= view.vector.x;
  775.             }
  776.         }
  777.         else {
  778.             view.angle -= 3;
  779.             if (view.angle == -3) view.angle = 357;
  780.             view.vector.x = (float)sin(view.angle * PI / 180.0);
  781.             view.vector.z = (float)cos(view.angle * PI / 180.0);
  782.         }
  783.     }
  784.     else if (direction == "right") {
  785.         if (keydown_alt == 1) {
  786.             if ((int)view.angle % 90 == 0) {
  787.                 if (view.angle < 90) view.eye.x += 0.5;
  788.                 else if (view.angle < 180) view.eye.z += 0.5;
  789.                 else if (view.angle < 270) view.eye.x -= 0.5;
  790.                 else if (view.angle < 360) view.eye.z -= 0.5;
  791.             }
  792.             else {
  793.                 view.eye.x += view.vector.z;
  794.                 view.eye.z += view.vector.x;
  795.             }
  796.         }
  797.         else {
  798.             view.angle += 3;
  799.             if (view.angle == 360) view.angle = 0;
  800.             view.vector.x = (float)sin(view.angle * PI / 180);
  801.             view.vector.z = (float)cos(view.angle * PI / 180);
  802.         }
  803.     }
  804.     glMatrixMode(GL_PROJECTION);
  805.     glLoadIdentity();
  806.     gluPerspective(60.0, (GLdouble)1024 / 768, 1.0, 100);
  807.     gluLookAt(view.eye.x, view.eye.y, view.eye.z, view.eye.x + view.vector.x, view.eye.y + view.vector.y, view.eye.z - view.vector.z, 0.0, view.up.y, 0.0);
  808. //  glutPostRedisplay();
  809.     draw();
  810. //  cout << "VIEW angle=" << view.angle << " eye=(" << view.eye.x << "," << view.eye.y << "," << view.eye.z << ") vector=(" << view.vector.x << "," << view.vector.y << "," << view.vector.z << ") center=(" << view.eye.x + view.vector.x << "," << view.eye.y + view.vector.y << "," << view.eye.z - view.vector.z << ")\n";
  811. }
  812.  
  813.  
  814.  
  815. // tworzenie scian
  816. void add_quad(glm::vec3 const (&coords)[4], GLfloat (&emission), GLfloat (&specular), bool fill, bool visibility) {
  817.     quads.resize(quads_counter+1);
  818.     quads[quads_counter].vertices[0] = coords[0];
  819.     quads[quads_counter].vertices[1] = coords[1];
  820.     quads[quads_counter].vertices[2] = coords[2];
  821.     quads[quads_counter].vertices[3] = coords[3];
  822.     quads[quads_counter].emission = &emission;
  823.     quads[quads_counter].specular = &specular;
  824.     quads[quads_counter].filled = fill;
  825.     quads[quads_counter].visible = visibility;
  826.     quads_counter++;
  827. }
  828.  
  829.  
  830.  
  831. // tworzenie pilek
  832. void add_ball(const glm::vec3(&position), const glm::vec3(&velocity), const glm::vec3(&rotation_angle), const glm::vec3(&rotation_speed), const glm::vec3(&rotation_dir),
  833.     double radius, const std::vector<short> &color1RGB, const std::vector<short> &color2RGB, const char* ID) {
  834.     balls.resize(balls_counter + 1);
  835.     balls[balls_counter].position = position;
  836.     balls[balls_counter].velocity = velocity;
  837.     balls[balls_counter].rotation_angle = rotation_angle;
  838.     balls[balls_counter].rotation_speed = rotation_speed;
  839.     balls[balls_counter].rotation_dir = rotation_dir;
  840.     balls[balls_counter].radius = radius;
  841.     balls[balls_counter].color1_RGB = color1RGB;
  842.     balls[balls_counter].color2_RGB = color2RGB;
  843.     balls[balls_counter].mass = 1.25 * PI * sqr(balls[balls_counter].radius) * balls[balls_counter].radius;
  844.     balls[balls_counter].ID = ID;
  845.     make_tex(&(balls[balls_counter]));
  846.     balls[balls_counter].sphere = gluNewQuadric();
  847.     balls_counter++;
  848. }
  849.  
  850.  
  851.  
  852. // tworzenie swiatel
  853. void add_light(const array<double, 4> &position, const array<double, 3> &direction, const array<short, 4> &ambient,
  854.     const array<short, 4> &diffusion, const array<short, 4> &specular, float cutoff, float exponent, bool enable) {
  855.     lights.resize(lights_counter + 1);
  856.     lights[lights_counter].n = light_number;
  857.     lights[lights_counter].enabled = (enable) ? true : false;
  858.     GLfloat lightposition[] = { (float)position[0], (float)position[1], (float)position[2], (float)position[3] };
  859.     GLfloat spotdirection[] = { (float)direction[0], (float)direction[1], (float)direction[2] };
  860.     GLfloat specularRGBA[] = { (float)specular[0] / 256, (float)specular[1] / 256, (float)specular[2] / 256, (float)specular[3] / 256 };
  861.     GLfloat diffusionRGBA[] = { (float)diffusion[0] / 256, (float)diffusion[1] / 256, (float)diffusion[2] / 256, (float)diffusion[3] / 256 };
  862.     GLfloat ambientRGBA[] = { (float)ambient[0] / 256, (float)ambient[1] / 256, (float)ambient[2] / 256, (float)ambient[3] / 256 };
  863.     glLightfv(light_number, GL_POSITION, lightposition);
  864.     glLightfv(light_number, GL_SPOT_DIRECTION, spotdirection);
  865.     glLightfv(light_number, GL_AMBIENT, ambientRGBA);
  866.     glLightfv(light_number, GL_DIFFUSE, diffusionRGBA);
  867.     glLightfv(light_number, GL_SPECULAR, specularRGBA);
  868.     glLightf(light_number, GL_SPOT_CUTOFF, cutoff);
  869.     glLightf(light_number, GL_SPOT_EXPONENT, exponent);
  870.     lights_counter++;
  871.     light_number++;
  872. }
  873.  
  874.  
  875.  
  876. // swiatlo wl/wyl
  877. void switchLight(short light_number) {
  878.     if (lights_counter < light_number) return;
  879.     lights[light_number - 1].enabled = (lights[light_number - 1].enabled) ? false : true;
  880.     (lights[light_number - 1].enabled) ? glEnable(lights[light_number - 1].n) : glDisable(lights[light_number - 1].n);
  881. }
  882.  
  883.  
  884.  
  885. // inicjalizacja...
  886. void init(void) {
  887.     quit = false;
  888.     bouncing = true;
  889.     area = glm::vec3(7.0f, 3.0f, 7.0f);
  890.     bkgcolor.R = 169;
  891.     bkgcolor.G = 167;
  892.     bkgcolor.B = 169;
  893.     quads_counter = 0;
  894.     balls_counter = 0;
  895.     lights_counter = 0;
  896.     light_number = GL_LIGHT0;
  897.  
  898.     // sciana przednia
  899.     add_quad({ glm::vec3(0.0 - area.x / 2.0, -1.0, 0.0 - area.z / 2.0),
  900.         glm::vec3(area.x / 2.0, -1.0, 0.0 - area.z / 2.0),
  901.         glm::vec3(area.x / 2.0, area.y / 2.0, 0.0 - area.z / 2.0),
  902.         glm::vec3(0.0 - area.x / 2.0, area.y / 2.0, 0.0 - area.z / 2.0) },
  903.         *mat_transparent, *mat_specular, true, true);
  904.     // sciana prawa
  905.     add_quad({ glm::vec3(area.x / 2.0, area.y / 2.0, area.z / 2.0),
  906.         glm::vec3(area.x / 2.0, area.y / 2.0, 0.0 - area.z / 2.0),
  907.         glm::vec3(area.x / 2.0, -1.0, 0.0 - area.z / 2.0),
  908.         glm::vec3(area.x / 2.0, -1.0, area.z / 2.0) },
  909.         *mat_transparent, *mat_specular, true, true);
  910.     // sciana tylna
  911.     add_quad({ glm::vec3(0.0 - area.x / 2.0, area.y / 2.0, area.z / 2.0),
  912.         glm::vec3(area.x / 2.0, area.y / 2.0, area.z / 2.0),
  913.         glm::vec3(area.x / 2.0, -1.0, area.z / 2.0),
  914.         glm::vec3(0.0 - area.x / 2.0, -1.0, area.z / 2.0) },
  915.         *mat_transparent, *mat_specular, true, false);
  916.     // sciana lewa
  917.     add_quad({ glm::vec3(0.0 - area.x / 2.0, area.y / 2.0, 0.0 - area.z / 2.0),
  918.         glm::vec3(0.0 - area.x / 2.0, area.y / 2.0, area.z / 2.0),
  919.         glm::vec3(0.0 - area.x / 2.0, -1.0, area.z / 2.0),
  920.         glm::vec3(0.0 - area.x / 2.0, -1.0, 0.0 - area.z / 2.0) },
  921.         *mat_transparent, *mat_specular, true, true);
  922.     // sufit
  923.     add_quad({ glm::vec3(0.0 - area.x / 2.0, area.y / 2.0, 0.0 - area.z / 2.0),
  924.         glm::vec3(area.x / 2.0, area.y / 2.0, 0.0 - area.z / 2.0),
  925.         glm::vec3(area.x / 2.0, area.y / 2.0, area.z / 2.0),
  926.         glm::vec3(0.0 - area.x / 2.0, area.y / 2.0, area.z / 2.0) },
  927.         *mat_transparent, *mat_specular, true, false);
  928.     // podloga
  929.     add_quad({ glm::vec3(0.0 - area.x / 2.0, -1.0, 0.0 - area.z / 2.0),
  930.         glm::vec3(area.x / 2.0, -1.0, 0.0 - area.z / 2.0),
  931.         glm::vec3(area.x / 2.0, -1.0, area.z / 2.0),
  932.         glm::vec3(0.0 - area.x / 2.0, -1.0, area.z / 2.0) },
  933.         *mat_zero, *mat_transparent, false, true);
  934.  
  935.     // utworzenie wspolrzednych siatki podlogi.  Work in progress... zapomnialem juz, jak to dziala. :=)
  936.     GLfloat x = 0.0 - area.x / 2.0;
  937.     GLfloat y = 1.0;
  938.     GLfloat z = 0.0 - area.z / 2.0;
  939.     unsigned int x_slices = (int)area.x;
  940.     unsigned int z_slices = (int)area.z;
  941.     GLfloat row = 0.0;
  942.     GLfloat column = 0.0;
  943.     cout << "x slices, z slices: " << x_slices << " " << z_slices << "\n";
  944.     //  a = new double ctrlpoints*[];
  945.     //  &ctrlpoints[z_slices];
  946.     unsigned int i, j;
  947.     //  for (ctrlpoints[i]::iterator i = ctrlpoints.begin(); i != ctrlpoints.end(); i++)
  948.  
  949.     crosspoints.resize(x_slices + 1);
  950.     crosspoints[0].resize(z_slices + 1);
  951.     crosspoints[1].resize(z_slices + 1);
  952.     crosspoints[2].resize(z_slices + 1);
  953.     crosspoints[3].resize(z_slices + 1);
  954.     crosspoints[4].resize(z_slices + 1);
  955.     crosspoints[5].resize(z_slices + 1);
  956.     crosspoints[6].resize(z_slices + 1);
  957.     crosspoints[7].resize(z_slices + 1);
  958.     /*  for (i = 0; i <= z_slices; i++) {
  959.             crosspoints[i].resize(z_slices + 1);
  960.             for (j = 0; j <= x_slices; j++) {
  961.             crosspoints[i][j][0] = x + column * j;
  962.             crosspoints[i][j][1] = z + row * i;
  963.             crosspoints[i][j][2] = y;
  964.             //          crosspoints[i][j].resize(10);
  965.             //           crosspoints[i][j].push_back(x + column * x);
  966.             //          crosspoints[i][j].push_back(z + row * z);
  967.             //          crosspoints[i][j].push_back(y);
  968.             //          ctrlpoints[i][j][0] = x + column * x;
  969.             //          ctrlpoints[i][j][1] = z + row * z;
  970.             //          ctrlpoints[i][j][2] = y;
  971.             column = column + 1.0;
  972.             j++;
  973.             }
  974.             //ctrlpoints[i][i][i] = 11";
  975.             row = row + 1.0;
  976.             i++;
  977.             }*/
  978.  
  979.     crosspoints[0][0][0] = -3.5;
  980.     crosspoints[0][0][1] = -3.5;
  981.     crosspoints[0][0][2] = 1.0;
  982.     crosspoints[0][1][0] = -2.5;
  983.     crosspoints[0][1][1] = -3.5;
  984.     crosspoints[0][1][2] = 1.0;
  985.     crosspoints[0][2][0] = -1.5;
  986.     crosspoints[0][2][1] = -3.5;
  987.     crosspoints[0][2][2] = 1.0;
  988.     crosspoints[0][3][0] = -0.5;
  989.     crosspoints[0][3][1] = -3.5;
  990.     crosspoints[0][3][2] = 1.0;
  991.     crosspoints[0][4][0] = 0.5;
  992.     crosspoints[0][4][1] = -3.5;
  993.     crosspoints[0][4][2] = 1.0;
  994.     crosspoints[0][5][0] = 1.5;
  995.     crosspoints[0][5][1] = -3.5;
  996.     crosspoints[0][5][2] = 1.0;
  997.     crosspoints[0][6][0] = 2.5;
  998.     crosspoints[0][6][1] = -3.5;
  999.     crosspoints[0][6][2] = 1.0;
  1000.     crosspoints[0][7][0] = 3.5;
  1001.     crosspoints[0][7][1] = -3.5;
  1002.     crosspoints[0][7][2] = 1.0;
  1003.  
  1004.     crosspoints[1][0][0] = -3.5;
  1005.     crosspoints[1][0][1] = -2.5;
  1006.     crosspoints[1][0][2] = 1.0;
  1007.     crosspoints[1][1][0] = -2.5;
  1008.     crosspoints[1][1][1] = -2.5;
  1009.     crosspoints[1][1][2] = 1.0;
  1010.     crosspoints[1][2][0] = -1.5;
  1011.     crosspoints[1][2][1] = -2.5;
  1012.     crosspoints[1][2][2] = 1.0;
  1013.     crosspoints[1][3][0] = -0.5;
  1014.     crosspoints[1][3][1] = -2.5;
  1015.     crosspoints[1][3][2] = 1.0;
  1016.     crosspoints[1][4][0] = 0.5;
  1017.     crosspoints[1][4][1] = -2.5;
  1018.     crosspoints[1][4][2] = 1.0;
  1019.     crosspoints[1][5][0] = 1.5;
  1020.     crosspoints[1][5][1] = -2.5;
  1021.     crosspoints[1][5][2] = 1.0;
  1022.     crosspoints[1][6][0] = 2.5;
  1023.     crosspoints[1][6][1] = -2.5;
  1024.     crosspoints[1][6][2] = 1.0;
  1025.     crosspoints[1][7][0] = 3.5;
  1026.     crosspoints[1][7][1] = -2.5;
  1027.     crosspoints[1][7][2] = 1.0;
  1028.  
  1029.  
  1030.     /*crosspoints.resize(5);
  1031.     crosspoints[0].resize(3);
  1032.     crosspoints[1].resize(3);
  1033.     crosspoints[2].resize(3);
  1034.     crosspoints[3].resize(3);
  1035.     crosspoints[4].resize(3);
  1036.     crosspoints[0][0][0] = -1.5;
  1037.  
  1038.     crosspoints[0][1] = { -0.5, -1.5, 4.0 };
  1039.     crosspoints[0][2] = { 1.5, -1.5, 4.0 };
  1040.     crosspoints[1][0] = { -1.5, -0.5, 4.0 };
  1041.     crosspoints[1][1] = { -0.5, -0.5, 4.0 };
  1042.     crosspoints[1][2] = { 0.5, -0.5, 4.0 };
  1043.     crosspoints[2][0] = { 1.5, -0.5, 4.0 };
  1044.     crosspoints[2][1] = { -1.5, 0.5, 4.0 };
  1045.     crosspoints[2][2] = { -0.5, 0.5, 0.0 };
  1046.     crosspoints[3][0] = { 0.5, 0.5, 3.0 };
  1047.     crosspoints[3][1] = { 1.5, 0.5, 4.0 };
  1048.     crosspoints[3][2] = { -1.5, 1.5, -2.0 };
  1049.     crosspoints[4][1] = { -0.5, 1.5, -2.0 };
  1050.     crosspoints[4][2] = { 0.5, 1.5, 0.0 };
  1051.     crosspoints[4][3] = { 1.5, 1.5, -1.0 };*/
  1052.  
  1053.  
  1054.     //  { { -1.5, -1.5, 4.0 }, { -0.5, -1.5, 4.0 },
  1055.     //  { 0.5, -1.5, 4.0 }, { 1.5, -1.5, 4.0 } },
  1056.     //  { { -1.5, -0.5, 4.0 }, { -0.5, -0.5, 4.0 },
  1057.     //  { 0.5, -0.5, 4.0 }, { 1.5, -0.5, 4.0 } },
  1058.     //  { { -1.5, 0.5, 4.0 }, { -0.5, 0.5, 0.0 },
  1059.     //  { 0.5, 0.5, 3.0 }, { 1.5, 0.5, 4.0 } },
  1060.     //  { { -1.5, 1.5, -2.0 }, { -0.5, 1.5, -2.0 },
  1061.     //  { 0.5, 1.5, 0.0 }, { 1.5, 1.5, -1.0 } }
  1062.  
  1063.     //---------------------------- KONIEC TWORZENIA SIATKI NA PODLODZE ------------------------------
  1064.  
  1065.     // KULKA CZERWONA
  1066.     // rozstawienie na rogach
  1067.     add_ball(glm::vec3 { -2.2f, 1.0f, -2.2f },
  1068.         glm::vec3 { 0.0, 0.0f, -0.003f },
  1069.         glm::vec3 { 0.0f, 0.0f, 0.0f },
  1070.         glm::vec3 { 0.2f, 0.0f, 0.0f },
  1071.         glm::vec3 { -1, 0, 0 },
  1072.         0.5, { 255, 255, 255 }, { 255, 0, 0 }, "red_ball");
  1073.     // ruch wzdluz osi X
  1074.     //  add_ball(glm::vec3(-2.2f, 1.0f, 0.0f),
  1075.     //      glm::vec3(-0.003, 0.0f, 0.0f),
  1076.     //      glm::vec3(0.0f, 0.0f, 0.0f),
  1077.     //      glm::vec3(0.2f, 0.0f, 0.0f),
  1078.     //      glm::vec3(-1, 0, 0),
  1079.     //      0.5, { 255, 255, 255 }, { 255, 0, 0 }, "red_ball");
  1080.     // ruch wzdluz osi Z
  1081.     //  add_ball(glm::vec3(0.0f, 1.0f, -2.2f),
  1082.     //      glm::vec3(0.0, 0.0f, -0.003f),
  1083.     //      glm::vec3(0.0f, 0.0f, 0.0f),
  1084.     //      glm::vec3(0.2f, 0.0f, 0.0f),
  1085.     //      glm::vec3(-1, 0, 0),
  1086.     //      0.5, { 255, 255, 255 }, { 255, 0, 0 }, "red_ball");
  1087.  
  1088.     // KULKA NIEBIESKA
  1089.     // rozstawienie na rogach
  1090.     add_ball(glm::vec3(0.0f, 0.2f, 0.0f),
  1091.         glm::vec3(0.007f, 0.0f, 0.007f),
  1092.         glm::vec3(0.0f, 0.0f, 0.0f),
  1093.         glm::vec3(0.2f, 0.1f, 0.0f),
  1094.         glm::vec3(1, 0, 0),
  1095.         0.5, { 255, 255, 255 }, { 0, 0, 255 }, "blue_ball");
  1096.     // ruch wzdluz osi X
  1097.     //  add_ball(glm::vec3(0.0f, 0.2f, 0.0f),
  1098.     //      glm::vec3(0.007f, 0.0f, 0.0f),
  1099.     //      glm::vec3(0.0f, 0.0f, 0.0f),
  1100.     //      glm::vec3(0.2f, 0.1f, 0.0f),
  1101.     //      glm::vec3(1, 0, 0),
  1102.     //      0.5, { 255, 255, 255 }, { 0, 0, 255 }, "blue_ball");
  1103.     // ruch wzdluz osi Z
  1104.     //      add_ball(glm::vec3(0.0f, 0.2f, 0.0f),
  1105.     //      glm::vec3(0.0f, 0.0f, 0.007f),
  1106.     //      glm::vec3(0.0f, 0.0f, 0.0f),
  1107.     //      glm::vec3(0.2f, 0.1f, 0.0f),
  1108.     //      glm::vec3(1, 0, 0),
  1109.     //      0.5, { 255, 255, 255 }, { 0, 0, 255 }, "blue_ball");
  1110.  
  1111.     // KULKA ZIELONA
  1112.     // rozstawienie na rogach
  1113.     add_ball(glm::vec3(1.5f, 0.3f, 1.5f),
  1114.         glm::vec3(-0.003f, 0.0f, -0.003f),
  1115.         glm::vec3(0.0f, 0.0f, 0.0f),
  1116.         glm::vec3(0.1f, 0.0f, 0.0f),
  1117.         glm::vec3(1, 0, 0),
  1118.         0.5, { 255, 255, 255 }, { 0, 192, 0 }, "green_ball");
  1119.     // ruch wzdluz osi X
  1120.     //  add_ball(glm::vec3(1.5f, 0.3f, 0.0f),
  1121.     //      glm::vec3(-0.003f, 0.0f, 0.0f),
  1122.     //      glm::vec3(0.0f, 0.0f, 0.0f),
  1123.     //      glm::vec3(0.1f, 0.0f, 0.0f),
  1124.     //      glm::vec3(1, 0, 0),
  1125.     //      0.5, { 255, 255, 255 }, { 0, 192, 0 }, "green_ball");
  1126.     // ruch wzdluz osi Z
  1127.     //  add_ball(glm::vec3(0.0f, 0.3f, 1.5f),
  1128.     //      glm::vec3(0.0f, 0.0f, -0.003f),
  1129.     //      glm::vec3(0.0f, 0.0f, 0.0f),
  1130.     //      glm::vec3(0.1f, 0.0f, 0.0f),
  1131.     //      glm::vec3(1, 0, 0),
  1132.     //      0.5, { 255, 255, 255 }, { 0, 192, 0 }, "green_ball");
  1133.  
  1134.     // KULKA JASNONIEBISKA
  1135.     // rozstawienie na rogach
  1136.     add_ball(glm::vec3(-2.2f, 1.0f, 2.2f),
  1137.         glm::vec3(-0.003f, 0.0f, 0.003f),
  1138.         glm::vec3(0.0f, 0.0f, 0.0f),
  1139.         glm::vec3(0.2f, 0.0f, 0.0f),
  1140.         glm::vec3(-1, 0, 0),
  1141.         0.5, { 255, 255, 255 }, { 0, 192, 192 }, "lightblue_ball");
  1142.     // ruch wzdluz osi X
  1143.     //  add_ball(glm::vec3(-2.2f, 1.0f, 0.0f),
  1144.     //      glm::vec3(-0.003f, 0.0f, 0.0f),
  1145.     //      glm::vec3(0.0f, 0.0f, 0.0f),
  1146.     //      glm::vec3(0.2f, 0.0f, 0.0f),
  1147.     //      glm::vec3(-1, 0, 0),
  1148.     //      0.5, { 255, 255, 255 }, { 0, 192, 192 }, "lightblue_ball");
  1149.     // ruch wzdluz osi Z
  1150.     //  add_ball(glm::vec3(0.0f, 1.0f, -2.2f),
  1151.     //      glm::vec3(-0.003f, 0.0f, 0.003f),
  1152.     //      glm::vec3(0.0f, 0.0f, 0.0f),
  1153.     //      glm::vec3(0.2f, 0.0f, 0.0f),
  1154.     //      glm::vec3(-1, 0, 0),
  1155.     //      0.5, { 255, 255, 255 }, { 0, 192, 192 }, "lightblue_ball");
  1156.  
  1157.     // KULKA ZLOTA
  1158.     // rozstawienie na rogach
  1159.     add_ball(glm::vec3(1.5f, 0.3f, -1.5f),
  1160.         glm::vec3(0.003f, 0.0f, -0.003f),
  1161.         glm::vec3(0.0f, 0.0f, 0.0f),
  1162.         glm::vec3(0.1f, 0.0f, 0.0f),
  1163.         glm::vec3(1, 0, 0),
  1164.         0.5, { 255, 255, 255 }, { 255, 192, 0 }, "golden_ball");
  1165.     // ruch wzdluz osi X
  1166.     //  add_ball(glm::vec3(1.5f, 0.3f, 0.0f),
  1167.     //      glm::vec3(0.003f, 0.0f, 0.0f),
  1168.     //      glm::vec3(0.0f, 0.0f, 0.0f),
  1169.     //      glm::vec3(0.1f, 0.0f, 0.0f),
  1170.     //      glm::vec3(1, 0, 0),
  1171.     //      0.5, { 255, 255, 255 }, { 255, 192, 0 }, "golden_ball");
  1172.     // ruch wzdluz osi Z
  1173.     //  add_ball(glm::vec3(0.0f, 0.3f, 1.5f),
  1174.     //      glm::vec3(0.003f, 0.0f, 0.003f),
  1175.     //      glm::vec3(0.0f, 0.0f, 0.0f),
  1176.     //      glm::vec3(0.1f, 0.0f, 0.0f),
  1177.     //      glm::vec3(1, 0, 0),
  1178.     //      0.5, { 255, 255, 255 }, { 255, 192, 0 }, "golden_ball");
  1179.  
  1180.     // SWIATLO 0
  1181.     add_light(array<double, 4> {{ 0.0, 2.0, 3.0, 0.0 }},
  1182.         array<double, 3> {{ 1.0, 1.0, -1.0 }},
  1183.         array<short, 4> {{ 51, 51, 51, 256 }},
  1184.         array<short, 4> {{ 204, 204, 204, 256 }},
  1185.         array<short, 4> {{ 204, 204, 204, 256 }},
  1186.         180.0f, 0.0f, true
  1187.     );
  1188.     // SWIATLO 1
  1189.     add_light(array<double, 4> {{ 0.0, 2.0, -3.0, 0.0 }},
  1190.         array<double, 3> {{ 1.0, 1.0, 11.0 }},
  1191.         array<short, 4> {{ 51, 51, 51, 256 }},
  1192.         array<short, 4> {{ 204, 204, 204, 256 }},
  1193.         array<short, 4> {{ 204, 204, 204, 256 }},
  1194.         180.0f, 0.0f, true
  1195.     );
  1196.  
  1197. //------------- nizej wlaczenie swiatel i jakies smieci ----------------------------
  1198.  
  1199.     glClearColor(0.0, 0.0, 0.0, 0.0);
  1200.     glColor3f(1.0, 0.0, 0.0);
  1201.     glShadeModel(GL_SMOOTH);
  1202.  
  1203.     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  1204.     glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  1205.  
  1206.     glEnable(GL_LIGHTING);
  1207.     for (short i = 0; i < lights_counter; i++) {
  1208.         if (lights[i].enabled == true) glEnable(lights[i].n);
  1209.     }
  1210.  
  1211.     glEnable(GL_DEPTH_TEST);
  1212.  
  1213.     //  cubeList = glGenLists(1);
  1214.     //  glNewList(cubeList, GL_COMPILE);
  1215.     //      glutSolidCube(0.6);
  1216.     //  glEndList();
  1217.  
  1218.     glEnable(GL_TEXTURE_2D);
  1219.  
  1220. //  glMatrixMode(GL_MODELVIEW);
  1221. //  glLoadIdentity();
  1222. //  gluLookAt(view.eye.x, view.eye.y, view.eye.z, view.eye.x + view.vector.x, view.vector.y, view.eye.z - view.vector.z, 0.0, 1.0, 0.0);
  1223.     glColor3d(0, 0, 0);
  1224.     //  glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4,
  1225.     //      0, 1, 12, 4, &*ctrlpoints[0][0][0]);
  1226.     glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 8,
  1227.         0, 1, 27, 8, &ctrlpoints[0][0][0]);
  1228.     glEnable(GL_MAP2_VERTEX_3);
  1229.     glMapGrid2f(20, 0.0, 1.0, 20, 0.0, 1.0);
  1230.     glEnable(GL_DEPTH_TEST);
  1231.     glShadeModel(GL_FLAT);
  1232.  
  1233. //-------------------------- koniec smieci ---------------------------------
  1234.  
  1235.     keydown_left = 0;
  1236.     keydown_right = 0;
  1237.     keydown_up = 0;
  1238.     keydown_down = 0;
  1239.     keydown_alt = 0;
  1240.     changeView("viewAngleX");
  1241.     run = 1;
  1242. }
  1243.  
  1244.  
  1245.  
  1246. void myIdle() {
  1247.     if (run == 1) {
  1248.         short i, j;
  1249.         for (i = 0; i < balls_counter - 1; i++) {
  1250.             for (j = i + 1; j < balls_counter; j++) {
  1251.                 CollisionDetect7(&balls[j], &balls[i]);
  1252.             }
  1253.         }
  1254.         for (i = 0; i < balls_counter; i++) {
  1255.             RunPhysics3(0.003f, &balls[i]);
  1256.         }
  1257.     }
  1258.     inputSDL();
  1259.     draw();
  1260. }
  1261.  
  1262.  
  1263.  
  1264. // eventy klawiszkowe SDL
  1265. static void inputSDL(void) {
  1266.     /* Our SDL event placeholder. */
  1267.     SDL_Event event;
  1268.  
  1269.     /* Grab all the events off the queue. */
  1270.     while (SDL_PollEvent(&event)) {
  1271.         switch (event.type) {
  1272.             case SDL_KEYDOWN: {
  1273.                 /* Handle key presses. */
  1274.                 SDLKey key = event.key.keysym.sym;
  1275.                 switch (key) {
  1276.                     case SDLK_ESCAPE:
  1277.                         quit = true;
  1278.                         break;
  1279.                     case SDLK_1:
  1280.                         (SDL_GetModState() & KMOD_CTRL) ? switchLight(1) : changeView("viewX");
  1281.                         break;
  1282.                     case SDLK_2:
  1283.                         (SDL_GetModState() & KMOD_CTRL) ? switchLight(2) : changeView("viewZ");
  1284.                         break;
  1285.                     case SDLK_3:
  1286.                         (SDL_GetModState() & KMOD_CTRL) ? switchLight(3) : changeView("viewTopX");
  1287.                         break;
  1288.                     case SDLK_4:
  1289.                         (SDL_GetModState() & KMOD_CTRL) ? switchLight(4) : changeView("viewTopZ");
  1290.                         break;
  1291.                     case SDLK_5:
  1292.                         (SDL_GetModState() & KMOD_CTRL) ? switchLight(5) : changeView("viewAngleX");
  1293.                         break;
  1294.                     case SDLK_6:
  1295.                         (SDL_GetModState() & KMOD_CTRL) ? switchLight(6) : changeView("viewAngleZ");
  1296.                         break;
  1297.                     case SDLK_7:
  1298.                         (SDL_GetModState() & KMOD_CTRL) ? switchLight(7) : void();
  1299.                         break;
  1300.                     case SDLK_8:
  1301.                         (SDL_GetModState() & KMOD_CTRL) ? switchLight(8) : void();
  1302.                         break;
  1303.                     case SDLK_LEFT:
  1304.                         keydown_left = 1;
  1305.                         break;
  1306.                     case SDLK_RIGHT:
  1307.                         keydown_right = 1;
  1308.                         break;
  1309.                     case SDLK_UP:
  1310.                         keydown_up = 1;
  1311.                         break;
  1312.                     case SDLK_DOWN:
  1313.                         keydown_down = 1;
  1314.                         break;
  1315.                     case SDLK_s:
  1316.                         run = (run) ? 0 : 1;
  1317.                         break;
  1318.                     case SDLK_b:
  1319.                         bouncing = (bouncing) ? 0 : 1;
  1320.                         break;
  1321.                     default:
  1322.                         break;
  1323.                 }
  1324.             }
  1325.             break;
  1326.             case SDL_KEYUP: {
  1327.                 /* Handle key presses. */
  1328.                 SDLKey key = event.key.keysym.sym;
  1329.                 switch (key) {
  1330.                     case SDLK_LEFT:
  1331.                         keydown_left = 0;
  1332.                         break;
  1333.                     case SDLK_RIGHT:
  1334.                         keydown_right = 0;
  1335.                         break;
  1336.                     case SDLK_UP:
  1337.                         keydown_up = 0;
  1338.                         break;
  1339.                     case SDLK_DOWN:
  1340.                         keydown_down = 0;
  1341.                         break;
  1342.                     default:
  1343.                         break;
  1344.                 }
  1345.             }
  1346.             break;
  1347.             case SDL_QUIT:
  1348.                 quit = true;
  1349.                 break;
  1350.         }
  1351.     }
  1352. }
  1353.  
  1354.  
  1355.  
  1356.  
  1357.  
  1358.  
  1359. /*void RenderText(Shader &shader, std::string text, GLfloat x, GLfloat y, GLfloat scale, glm::vec3 color)
  1360. {
  1361.     // Activate corresponding render state 
  1362.     shader.Use();
  1363.     glUniform3f(glGetUniformLocation(shader.Program, "textColor"), color.x, color.y, color.z);
  1364.     glActiveTexture(GL_TEXTURE0);
  1365.     glBindVertexArray(VAO);
  1366.  
  1367.     // Iterate through all characters
  1368.     std::string::const_iterator c;
  1369.     for (c = text.begin(); c != text.end(); c++)
  1370.     {
  1371.         Character ch = Characters[*c];
  1372.  
  1373.         GLfloat xpos = x + ch.Bearing.x * scale;
  1374.         GLfloat ypos = y - (ch.Size.y - ch.Bearing.y) * scale;
  1375.  
  1376.         GLfloat w = ch.Size.x * scale;
  1377.         GLfloat h = ch.Size.y * scale;
  1378.         // Update VBO for each character
  1379.         GLfloat vertices[6][4] = {
  1380.             { xpos, ypos + h, 0.0, 0.0 },
  1381.             { xpos, ypos, 0.0, 1.0 },
  1382.             { xpos + w, ypos, 1.0, 1.0 },
  1383.  
  1384.             { xpos, ypos + h, 0.0, 0.0 },
  1385.             { xpos + w, ypos, 1.0, 1.0 },
  1386.             { xpos + w, ypos + h, 1.0, 0.0 }
  1387.         };
  1388.         // Render glyph texture over quad
  1389.         glBindTexture(GL_TEXTURE_2D, ch.TextureID);
  1390.         // Update content of VBO memory
  1391.         glBindBuffer(GL_ARRAY_BUFFER, VBO);
  1392.         glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); // Be sure to use glBufferSubData and not glBufferData
  1393.  
  1394.         glBindBuffer(GL_ARRAY_BUFFER, 0);
  1395.         // Render quad
  1396.         glDrawArrays(GL_TRIANGLES, 0, 6);
  1397.         // Now advance cursors for next glyph (note that advance is number of 1/64 pixels)
  1398.         x += (ch.Advance >> 6) * scale; // Bitshift by 6 to get value in pixels (2^6 = 64 (divide amount of 1/64th pixels by 64 to get amount of pixels))
  1399.     }
  1400.     glBindVertexArray(0);
  1401.     glBindTexture(GL_TEXTURE_2D, 0);
  1402. }*/
  1403.  
  1404.  
  1405.  
  1406. // ustawienia FreeType
  1407. void setup_freetype() {
  1408.     /*  glewExperimental = GL_TRUE;
  1409.     glewInit();
  1410.     glEnable(GL_CULL_FACE);
  1411.     glEnable(GL_BLEND);
  1412.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1413.     Shader shader("shader.vs", "shader.frag");
  1414.     glm::mat4 projection = glm::ortho(0.0f, static_cast<GLfloat>(WIDTH), 0.0f, static_cast<GLfloat>(HEIGHT));
  1415.     shader.Use();
  1416.     glUniformMatrix4fv(glGetUniformLocation(shader.Program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
  1417.     // FreeType
  1418.     FT_Library ft;
  1419.     // All functions return a value different than 0 whenever an error occurred
  1420.     if (FT_Init_FreeType(&ft))
  1421.     std::cout << "ERROR::FREETYPE: Could not init FreeType Library" << std::endl;
  1422.  
  1423.     // Load font as face
  1424.     FT_Face face;
  1425.     if (FT_New_Face(ft, "C:/Windows/Fonts/arial.ttf", 0, &face))
  1426.     std::cout << "ERROR::FREETYPE: Failed to load font" << std::endl;
  1427.  
  1428.     // Set size to load glyphs as
  1429.     FT_Set_Pixel_Sizes(face, 0, 48);
  1430.  
  1431.     // Disable byte-alignment restriction
  1432.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  1433.  
  1434.     // Load first 128 characters of ASCII set
  1435.     for (GLubyte c = 0; c < 128; c++)
  1436.     {
  1437.     // Load character glyph
  1438.     if (FT_Load_Char(face, c, FT_LOAD_RENDER))
  1439.     {
  1440.     std::cout << "ERROR::FREETYTPE: Failed to load Glyph" << std::endl;
  1441.     continue;
  1442.     }
  1443.     // Generate texture
  1444.     GLuint texture;
  1445.     glGenTextures(1, &texture);
  1446.     glBindTexture(GL_TEXTURE_2D, texture);
  1447.     glTexImage2D(
  1448.     GL_TEXTURE_2D,
  1449.     0,
  1450.     GL_RED,
  1451.     face->glyph->bitmap.width,
  1452.     face->glyph->bitmap.rows,
  1453.     0,
  1454.     GL_RED,
  1455.     GL_UNSIGNED_BYTE,
  1456.     face->glyph->bitmap.buffer
  1457.     );
  1458.     // Set texture options
  1459.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  1460.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  1461.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  1462.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1463.     // Now store character for later use
  1464.     Character character = {
  1465.     texture,
  1466.     glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
  1467.     glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
  1468.     face->glyph->advance.x
  1469.     };
  1470.     Characters.insert(std::pair<char, Character>(c, character));
  1471.     }
  1472.     glBindTexture(GL_TEXTURE_2D, 0);
  1473.     // Destroy FreeType once we're finished
  1474.     FT_Done_Face(face);
  1475.     FT_Done_FreeType(ft);
  1476.  
  1477.     // Configure VAO/VBO for texture quads
  1478.     glGenVertexArrays(1, &VAO);
  1479.     glGenBuffers(1, &VBO);
  1480.     glBindVertexArray(VAO);
  1481.     glBindBuffer(GL_ARRAY_BUFFER, VBO);
  1482.     glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)* 6 * 4, NULL, GL_DYNAMIC_DRAW);
  1483.     glEnableVertexAttribArray(0);
  1484.     glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), 0);
  1485.     glBindBuffer(GL_ARRAY_BUFFER, 0);
  1486.     glBindVertexArray(0);*/
  1487. }
  1488.  
  1489.  
  1490.  
  1491. // ustawienia openGL
  1492. static void setup_opengl(unsigned int width, unsigned int height)
  1493. {
  1494.     float ratio = (float)width / (float)height;
  1495.  
  1496.     /* Our shading model--Gouraud (smooth). */
  1497.     glShadeModel(GL_SMOOTH);
  1498.  
  1499.     /* Culling. */
  1500.     glCullFace(GL_BACK);
  1501.     glFrontFace(GL_CCW);
  1502.     glEnable(GL_CULL_FACE);
  1503.  
  1504.     /* Set the clear color. */
  1505.     glClearColor(0, 0, 0, 0);
  1506.  
  1507.     /* Setup our viewport. */
  1508.     glViewport(0, 0, width, height);
  1509.  
  1510.     /*
  1511.     * Change to the projection matrix and set
  1512.     * our viewing volume.
  1513.     */
  1514.     glMatrixMode(GL_PROJECTION);
  1515.     glLoadIdentity();
  1516.     /*
  1517.     * EXERCISE:
  1518.     * Replace this with a call to glFrustum.
  1519.     */
  1520.     gluPerspective(60.0, ratio, 1.0, 1024.0);
  1521. }
  1522.  
  1523.  
  1524.  
  1525. // ustawienia SDL
  1526. void setup_SDL() {
  1527.     /* Information about the current video settings. */
  1528.     const SDL_VideoInfo* info = NULL;
  1529.     /* Dimensions of our window. */
  1530.     unsigned int width = 0;
  1531.     unsigned int height = 0;
  1532.     /* Color depth in bits of our window. */
  1533.     unsigned int bpp = 0;
  1534.     /* Flags we will pass into SDL_SetVideoMode. */
  1535.     unsigned int flags = 0;
  1536.  
  1537.     /* First, initialize SDL's video subsystem. */
  1538.     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  1539.         /* Failed, exit. */
  1540.         fprintf(stderr, "Video initialization failed: %s\n",
  1541.             SDL_GetError());
  1542.     }
  1543.  
  1544.     /* Let's get some video information. */
  1545.     info = SDL_GetVideoInfo();
  1546.  
  1547.     if (!info) {
  1548.         /* This should probably never happen. */
  1549.         fprintf(stderr, "Video query failed: %s\n",
  1550.             SDL_GetError());
  1551.     }
  1552.  
  1553.     /*
  1554.     * Set our width/height to 640/480 (you would
  1555.     * of course let the user decide this in a normal
  1556.     * app). We get the bpp we will request from
  1557.     * the display. On X11, VidMode can't change
  1558.     * resolution, so this is probably being overly
  1559.     * safe. Under Win32, ChangeDisplaySettings
  1560.     * can change the bpp.
  1561.     */
  1562.     width = 1024;
  1563.     height = 768;
  1564.     bpp = info->vfmt->BitsPerPixel;
  1565.  
  1566.     /*
  1567.     * Now, we want to setup our requested
  1568.     * window attributes for our OpenGL window.
  1569.     * We want *at least* 5 bits of red, green
  1570.     * and blue. We also want at least a 16-bit
  1571.     * depth buffer.
  1572.     *
  1573.     * The last thing we do is request a double
  1574.     * buffered window. '1' turns on double
  1575.     * buffering, '0' turns it off.
  1576.     *
  1577.     * Note that we do not use SDL_DOUBLEBUF in
  1578.     * the flags to SDL_SetVideoMode. That does
  1579.     * not affect the GL attribute state, only
  1580.     * the standard 2D blitting setup.
  1581.     */
  1582.     SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
  1583.     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
  1584.     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
  1585.     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
  1586.     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  1587.  
  1588.     /*
  1589.     * We want to request that SDL provide us
  1590.     * with an OpenGL window, in a fullscreen
  1591.     * video mode.
  1592.     *
  1593.     * EXERCISE:
  1594.     * Make starting windowed an option, and
  1595.     * handle the resize events properly with
  1596.     * glViewport.
  1597.     */
  1598.     flags = SDL_OPENGL;
  1599.  
  1600.     /*
  1601.     * Set the video mode
  1602.     */
  1603.     if (SDL_SetVideoMode(width, height, bpp, flags) == 0) {
  1604.         /*
  1605.         * This could happen for a variety of reasons,
  1606.         * including DISPLAY not being set, the specified
  1607.         * resolution not being available, etc.
  1608.         */
  1609.         fprintf(stderr, "Video mode set failed: %s\n",
  1610.             SDL_GetError());
  1611.     }
  1612.  
  1613.     /*
  1614.     * At this point, we should have a properly setup
  1615.     * double-buffered window for use with OpenGL.
  1616.     */
  1617.     setup_opengl(width, height);
  1618. }
  1619.  
  1620.  
  1621.  
  1622. int main(int argc, char **argv) {
  1623.     // ponizsze na potrzeby testow
  1624.     cout.precision(std::numeric_limits<double>::max_digits10);
  1625.  
  1626.     setup_SDL();
  1627.  
  1628. //---> GLUT
  1629. //  glutInit(&argc, argv);
  1630.  
  1631. //---> GLFW
  1632. //  GLFWwindow* window = glfwCreateWindow(1024, 768, "My Title", glfwGetPrimaryMonitor(), NULL);
  1633.  
  1634.     setup_freetype();
  1635.  
  1636. //  glutDisplayFunc(draw);
  1637. //  glutIdleFunc(myIdle);
  1638. //  glutReshapeFunc(resize);
  1639.     init();
  1640.     SDL_Init(SDL_INIT_TIMER);
  1641.     SDL_Init(SDL_INIT_EVENTTHREAD);
  1642.     unsigned int lastTime = 0, currentTime;
  1643.     while (quit == false) {
  1644. //      glutMainLoopEvent();
  1645.         myIdle();
  1646.         currentTime = SDL_GetTicks();
  1647.         if (currentTime > lastTime + 40) {
  1648.             lastTime = currentTime;
  1649.             if (keydown_up == 1) move("up");
  1650.             if (keydown_down == 1) move("down");
  1651.             if (keydown_left == 1) move("left");
  1652.             if (keydown_right == 1) move("right");
  1653.         }
  1654.     }
  1655.     return 0;
  1656. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement