Advertisement
Guest User

cycki

a guest
Jun 26th, 2018
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 23.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <GL/glut.h>
  3.  
  4. #define _USE_MATH_DEFINES
  5. #include <math.h>
  6. //#include <gltext.hpp>
  7. //#include <SOIL.h>
  8.  
  9. using namespace std;
  10.  
  11.  
  12. struct TVector {
  13.     float x, y, z;
  14. };
  15.  
  16. struct TColors {
  17.     short R, G, B;
  18. };
  19.  
  20. struct TObject3D {
  21.     TVector position;
  22.     TVector velocity;
  23.     TVector rotation_angle;
  24.     TVector rotation;
  25.     TVector rotation_dir;
  26.     float radius;
  27.     float vector = 0.0;
  28.     TColors color1;
  29.     TColors color2;
  30.     GLuint texture;
  31. //  std::string name;
  32. };
  33.  
  34. struct TLight {
  35.     TVector position;
  36.     TVector direction;
  37.     float cutoff;
  38. };
  39.  
  40. struct TView {
  41.     TVector eye, vector;
  42.     float angle;
  43. };
  44.  
  45. // TObject3D objects[];
  46. TObject3D sphereOBJ1, sphereOBJ2, sphereOBJ3;
  47. TLight light0;
  48. TColors bkg;
  49. TView view;
  50. // short objects_count = 0;
  51.  
  52. //GLuint tex;
  53. //GLUquadric* sphere1;
  54. //GLUquadric* sphere2;
  55. GLUquadricObj *sphere1;
  56. GLUquadricObj *sphere2;
  57. GLUquadricObj *sphere3;
  58.  
  59. std::string tekst;
  60.  
  61. #define GRAVITY 4.0
  62. #define MAX_NUM_LIGHTS 8
  63. #define PI 3.14159265
  64.  
  65. void RunPhysics(float dt, TObject3D *object) {
  66.     if ((object->position.x >= 7.0) || (object->position.x <= -7.0)) {
  67.         object->velocity.x = -object->velocity.x;
  68.         object->rotation_dir.x = -1 * object->rotation_dir.x;
  69.     }
  70.     if ((object->position.z >= 7.0) || (object->position.z <= -7.0)) {
  71.         object->velocity.z = -object->velocity.z;
  72.         object->rotation_dir.z = -1 * object->rotation_dir.z;
  73.     }
  74.     object->velocity.y += GRAVITY;
  75.     object->rotation_angle.x += object->rotation.x * object->rotation_dir.x;
  76.     object->rotation_angle.y += object->rotation.y * object->rotation_dir.y;
  77.     object->rotation_angle.z += object->rotation.z * object->rotation_dir.z;
  78.     object->position.x = object->position.x + object->velocity.x;
  79.     object->position.y = object->position.y - object->velocity.y / 65536.0;
  80.     object->position.z = object->position.z + object->velocity.z;
  81.     if (object->position.y - object->radius < -1) {
  82.         object->position.y = -1 + object->radius;
  83.         object->velocity.y = -object->velocity.y;
  84.     }
  85.     object->vector = sqrt(object->velocity.x * object->velocity.x + object->velocity.z * object->velocity.z);
  86. //  cout << object->position.y << "\n";
  87.     //  cout << object->vector << "     " << object->position.x << " " << object->position.y << " " << object->position.z << "\n";
  88. }
  89.  
  90.  
  91. void CollisionDetect3(TObject3D *object1, TObject3D *object2) {
  92.     // obliczenie odleglosci miedzy kulami
  93.     float dx = abs(object1->position.x - object2->position.x);
  94.     float dy = abs(object1->position.y - object2->position.y);
  95.     float dz = abs(object1->position.z - object2->position.z);
  96.     float distance = sqrt(dx*dx + dy*dy + dz*dz);
  97.  
  98.     // jesli odleglosc <= sumie promieni obu kul, nastepuje kolizja
  99.     if (distance <= object1->radius + object2->radius) {
  100.         // nadanie wartosci zmiennym pomocniczym...
  101.         // wspolrzedne srodkow kul w chwili T0
  102.         float obj1xT0 = object1->position.x - object1->velocity.x * 2;
  103.         float obj1yT0 = object1->position.y - object1->velocity.y * 2;
  104.         float obj1zT0 = object1->position.z - object1->velocity.z * 2;
  105.         float obj2xT0 = object2->position.x - object2->velocity.x * 2;
  106.         float obj2yT0 = object2->position.y - object2->velocity.y * 2;
  107.         float obj2zT0 = object2->position.z - object2->velocity.z * 2;
  108.         // wspolrzedne srodkow kul w chwili T
  109.         float obj1xT = object1->position.x - object1->velocity.x;
  110.         float obj1yT = object1->position.y - object1->velocity.y;
  111.         float obj1zT = object1->position.z - object1->velocity.z;
  112.         float obj2xT = object2->position.x - object2->velocity.x;
  113.         float obj2yT = object2->position.y - object2->velocity.y;
  114.         float obj2zT = object2->position.z - object2->velocity.z;
  115.         // wspolrzedne srodkow kul w chwili zderzenia
  116.         float obj1xZD = object1->position.x;
  117.         float obj1yZD = object1->position.y;
  118.         float obj1zZD = object1->position.z;
  119.         float obj2xZD = object2->position.x;
  120.         float obj2yZD = object2->position.y;
  121.         float obj2zZD = object2->position.z;
  122.         // skladowe predkosci kul
  123.         float dvx1 = object1->velocity.x;
  124.         float dvy1 = object1->velocity.y;
  125.         float dvz1 = object1->velocity.z;
  126.         float dvx2 = object2->velocity.x;
  127.         float dvy2 = object2->velocity.y;
  128.         float dvz2 = object2->velocity.z;
  129.  
  130.         // dlugosci wektorow predkosci wypadkowych w plaszczyznie XZ
  131.         float obj1_velocity_xz = sqrt(dvx1*dvx1 + dvz1*dvz1);
  132.         float obj2_velocity_xz = sqrt(dvx2*dvx2 + dvz2*dvz2);
  133.  
  134.         cout << "dlugosci wektorow: " << obj1_velocity_xz << " " << obj2_velocity_xz << "\n";
  135.  
  136.         // obliczanie kata pomiedzy osia X ukladu wpsolrzednych OX a osia dzeta ulkadu dzeta-eta
  137. //      float sin_fi = (obj2xZD - obj1xZD) / sqrt((obj2xZD - obj1xZD) * (obj2xZD - obj1xZD) + (obj1zZD - obj2zZD) * (obj1zZD - obj2zZD));
  138.         float cos_fi = (obj1zZD - obj2zZD) / sqrt((obj2xZD - obj1xZD) * (obj2xZD - obj1xZD) + (obj1zZD - obj2zZD) * (obj1zZD - obj2zZD));
  139.         float fi = acos(cos_fi);
  140.  
  141.         cout << "kat miedzy X a dzeta: " << fi << "\n";
  142.  
  143.         // obliczanie katow pomiedzy wektorami predkosci kul a osia X ukladu wspolrzednych OX
  144.         float cos_theta1 = abs(dvx1) / obj1_velocity_xz;
  145.         float cos_theta2 = abs(dvx2) / obj2_velocity_xz;
  146.         float theta1 = acos(cos_theta1);
  147.         float theta2 = acos(cos_theta2);
  148.  
  149.         cout << "cosinusy theta: " << cos_theta1 << " " << cos_theta2 << "\n";
  150.         cout << "katy miedzy predkosciami a osia X: " << theta1 << " " << theta2 << "\n";
  151.  
  152.         // obliczanie wektora stycznej obu kul
  153.         double styczna_vec_x = dx * cos(90 * M_PI / 180) - dz * sin(90 * M_PI / 180);
  154.         double styczna_vec_z = dx * sin(90 * M_PI / 180) + dz * cos(90 * M_PI / 180);
  155.         double styczna_vec_len = sqrt((styczna_vec_x * styczna_vec_x) + (styczna_vec_z * styczna_vec_z));
  156.  
  157.         // skladowe predkosci obu kul po zderzeniu w ukladzie dzeta-eta. Wspolczynnik restytucji = 1, masa = 1
  158.         float dzeta_eta_W1x = obj1_velocity_xz * cos(theta1 - fi);
  159.         float dzeta_eta_W2x = obj2_velocity_xz * cos(theta2 - fi);
  160.         float dzeta_eta_W1z = obj1_velocity_xz * ((1 - 1) / (1 + 1)) * sin(theta1 - fi) + obj2_velocity_xz * ((1 + 1) / (1 + 1)) * sin(theta2 - fi);
  161.         float dzeta_eta_W2z = obj1_velocity_xz * ((1 * (1 + 1)) / (1 + 1)) * sin(theta1 - fi) + obj2_velocity_xz * ((1 - 1 * 1) / (1 + 1)) * sin(theta2 - fi);
  162.  
  163.         cout << "skladowe predkosci W w dzeta-eta: " << dzeta_eta_W1x << "," << dzeta_eta_W1z << "   " << dzeta_eta_W2x << "," << dzeta_eta_W2z << "\n";
  164.  
  165.         // skladowe predkosci obu kul w chwili zderzenia w ukladzie dzeta-eta
  166.         float dzeta_eta_V1x = abs(dvx1) * cos(fi) + abs(dvz1) * sin(fi);
  167.         float dzeta_eta_V1z = abs(dvz1) * cos(fi) - abs(dvx1) * sin(fi);
  168.         float dzeta_eta_V2x = abs(dvx2) * cos(fi) + abs(dvz2) * sin(fi);
  169.         float dzeta_eta_V2z = abs(dvz2) * cos(fi) - abs(dvx2) * sin(fi);
  170.  
  171.         // obliczanie skladowych X i Z predkosci koncowych W1 i W2
  172.         float W1x = dzeta_eta_W1x * cos(fi) - dzeta_eta_W1z * sin(fi);
  173.         float W1z = dzeta_eta_W1x * sin(fi) + dzeta_eta_W1z * cos(fi);
  174.         float W2x = dzeta_eta_W2x * cos(fi) - dzeta_eta_W2z * sin(fi);
  175.         float W2z = dzeta_eta_W2x * sin(fi) + dzeta_eta_W2z * cos(fi);
  176.  
  177.         cout << "skladowe predkosci W: " << W1x << "," << W1z << "   " << W2x << "," << W2z << "\n";
  178.        
  179.         // obliczanie predkosci koncowych W1, W2
  180.         float W1 = sqrt(W1x * W1x + W1z * W1z);
  181.         float W2 = sqrt(W2x * W2x + W2z * W2z);
  182.  
  183.         // obliczanie katow miedzy wektorami W2, W1 a osia X
  184.         float cos_psi1 = W1x / W1;
  185.         float cos_psi2 = W2x / W2;
  186.         float psi1 = acos(cos_psi1);
  187.         float psi2 = acos(cos_psi2);
  188.  
  189.         // obliczanie kata miedzy wektorem W2 a osia dzeta
  190.         float alpha = psi2 - fi;
  191.  
  192.        
  193.  
  194. //      cout << "predkosci koncowe: " << W1 << " " << W2 << "\n\n";
  195.        
  196.         // koniec
  197.         float dvx1_new = W1x;
  198.         float dvz1_new = W1z;
  199.         float dvx2_new = W2x;
  200.         float dvz2_new = W2z;
  201.  
  202.         object1->velocity.x = dvx1_new;
  203.         object1->velocity.z = dvz1_new;
  204.         object2->velocity.x = dvx2_new;
  205.         object2->velocity.z = dvz2_new;
  206.         if (object2->position.y < object1->position.y) object2->velocity.y = -object2->velocity.y;
  207.         else if (object1->position.y <= object2->position.y) object1->velocity.y = -object1->velocity.y;
  208.  
  209.         /*object1->position.x += object1->velocity.x;
  210.         object1->position.z += object1->velocity.z;
  211.         object1->position.y += object1->velocity.y;
  212.         object2->position.x += object2->velocity.x;
  213.         object2->position.z += object2->velocity.z;
  214.         object2->position.y += object2->velocity.y; */
  215.  
  216. //      cout << "kolizja " << "predkosc W1: " << W1 << " " << W1x << " " << W1z << " ... W2: " << W2 << " " << W2x << " " << W2z << "\n";
  217. //      cout << "fi " << fi << " theta1: " << theta1 << " theta2: " << theta2 << "\n";
  218.     }
  219. }
  220.  
  221. void make_tex(TObject3D *object) {
  222.     unsigned char data[128][128][3];
  223.     for (int y = 0; y < 128; y++) {
  224.         for (int x = 0; x < 128; x++) {
  225.             unsigned char *p = data[y][x];
  226.             if ((x ^ y) & 2) {
  227.                 p[0] = object->color1.R;
  228.                 p[1] = object->color1.G;
  229.                 p[2] = object->color1.B;
  230.             }
  231.             else {
  232.                 p[0] = object->color2.R;
  233.                 p[1] = object->color2.G;
  234.                 p[2] = object->color2.B;
  235.             }
  236.         }
  237.     }
  238.     glGenTextures(1, &(object->texture));
  239.     glBindTexture(GL_TEXTURE_2D, object->texture);
  240.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, (const GLvoid *)data);
  241.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  242.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  243. }
  244.  
  245. void draw() {
  246.     glClearColor((float)bkg.R/256, (float)bkg.G/256, (float)bkg.B/256, 1);
  247.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  248.  
  249.     glMatrixMode(GL_MODELVIEW);
  250.     glLoadIdentity();
  251.     glTranslatef(sphereOBJ1.position.x, sphereOBJ1.position.y, sphereOBJ1.position.z);
  252.     glRotatef(150, 0.0, 1.0, 1.0);
  253.     glRotatef(sphereOBJ1.rotation_angle.x, 0.0, 0.0, 2.0);
  254.     glColor3f(1.0, 1.0, 1.0);
  255.     gluQuadricDrawStyle(sphere1, GLU_FILL);
  256.     glBindTexture(GL_TEXTURE_2D, sphereOBJ1.texture);
  257.     gluQuadricTexture(sphere1, GL_TRUE);
  258.     gluQuadricNormals(sphere1, GLU_SMOOTH);
  259.     gluSphere(sphere1, sphereOBJ1.radius, 32, 16);
  260.  
  261.     glMatrixMode(GL_MODELVIEW);
  262.     glLoadIdentity();
  263.     glTranslatef(sphereOBJ2.position.x, sphereOBJ2.position.y, sphereOBJ2.position.z);
  264.     glRotatef(150, 0.0, 1.0, 1.0);
  265.     glRotatef(sphereOBJ2.rotation_angle.x, 0.0, 0.0, 2.0);
  266.     glColor3f(1.0, 1.0, 1.0);
  267.     gluQuadricDrawStyle(sphere2, GLU_FILL);
  268.     glBindTexture(GL_TEXTURE_2D, sphereOBJ2.texture);
  269.     gluQuadricTexture(sphere2, GL_TRUE);
  270.     gluQuadricNormals(sphere2, GLU_SMOOTH);
  271.     gluSphere(sphere2, sphereOBJ2.radius, 32, 16);
  272.  
  273.     glMatrixMode(GL_MODELVIEW);
  274.     glLoadIdentity();
  275.     glTranslatef(sphereOBJ3.position.x, sphereOBJ3.position.y, sphereOBJ3.position.z);
  276.     glRotatef(150, 0.0, 1.0, 1.0);
  277.     glRotatef(sphereOBJ3.rotation_angle.x, 0.0, 0.0, 2.0);
  278.     glColor3f(1.0, 1.0, 1.0);
  279.     gluQuadricDrawStyle(sphere3, GLU_FILL);
  280.     glBindTexture(GL_TEXTURE_2D, sphereOBJ3.texture);
  281.     gluQuadricTexture(sphere3, GL_TRUE);
  282.     gluQuadricNormals(sphere3, GLU_SMOOTH);
  283.     gluSphere(sphere3, sphereOBJ3.radius, 32, 16);
  284.  
  285.     glutSwapBuffers();
  286. }
  287.  
  288. void resize(int w, int h) {
  289.     if (!h) h = 1;
  290.     glViewport(0, 0, w, h);
  291.     glMatrixMode(GL_PROJECTION);
  292.     glLoadIdentity();
  293.     gluPerspective(60.0, 1.00*w / h , 1.0, 100);
  294.     glMatrixMode(GL_MODELVIEW);
  295.     glLoadIdentity();
  296.     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);
  297. }
  298.  
  299. void changeView(bool symbol1, bool symbol2) {
  300.     view.eye.x = (symbol1 == 1) ? view.eye.x + view.vector.z : view.eye.x - view.vector.z;
  301.     view.eye.z = (symbol2 == 1) ? view.eye.z + view.vector.x : view.eye.z - view.vector.x;
  302. }
  303.  
  304. void inputSpecial(int key, int x, int y) {
  305.     cout << key << "\n";
  306.     if (key == GLUT_KEY_UP) {
  307. //      cout << "up" << "\n";
  308. //      if ((view.angle >= 0) && (view.angle < 90)) ((int)view.angle % 90) == 0 ? view.eye.z -= 1.0 : changeView(1, 0);
  309. //      else if ((view.angle >= 90) && (view.angle < 180)) ((int)view.angle % 90) == 0 ? view.eye.x += 1.0 : changeView(1, 0);
  310. //      else if ((view.angle >= 180) && (view.angle < 270)) ((int)view.angle % 90) == 0 ? view.eye.z += 1.0 : changeView(1, 0);
  311. //      else if ((view.angle >= 270) && (view.angle < 360)) ((int)view.angle % 90) == 0 ? view.eye.x -= 1.0 : changeView(1, 0);
  312.         if ((view.angle >= 0) && (view.angle < 90)) {
  313.             if (((int)view.angle % 90) == 0) {
  314.                 view.eye.z = view.eye.z -= 1.0;
  315.             }
  316.             else {
  317.                 view.eye.z = view.eye.z - view.vector.z;
  318.                 view.eye.x = view.eye.x + view.vector.x;
  319.             }
  320.         }
  321.         else if ((view.angle >= 90) && (view.angle < 180)) {
  322.             if (((int)view.angle % 90) == 0) {
  323.                 view.eye.x = view.eye.x += 1.0;
  324.             }
  325.             else {
  326.                 view.eye.z = view.eye.z - view.vector.z;
  327.                 view.eye.x = view.eye.x + view.vector.x;
  328.             }
  329.         }
  330.         else if ((view.angle >= 180) && (view.angle < 270)) {
  331.             if (((int)view.angle % 90) == 0) {
  332.                 view.eye.z = view.eye.z += 1.0;
  333.             }
  334.             else {
  335.                 view.eye.z = view.eye.z - view.vector.z;
  336.                 view.eye.x = view.eye.x + view.vector.x;
  337.             }
  338.         }
  339.         else if ((view.angle >= 270) && (view.angle < 360)) {
  340.             if (((int)view.angle % 90) == 0) {
  341.                 view.eye.x = view.eye.x -= 1.0;
  342.             }
  343.             else {
  344.                 view.eye.z = view.eye.z - view.vector.z;
  345.                 view.eye.x = view.eye.x + view.vector.x;
  346.             }
  347.         }
  348.     }
  349.     else if (key == GLUT_KEY_DOWN) {
  350. //      cout << "down" << "\n";
  351. //      if ((view.angle >= 0) && (view.angle < 90)) ((int)view.angle % 90) == 0 ? view.eye.z += 1.0 : changeView(0, 1);
  352. //      else if ((view.angle >= 90) && (view.angle < 180)) ((int)view.angle % 90) == 0 ? view.eye.x -= 1.0 : changeView(0, 0);
  353. //      else if ((view.angle >= 180) && (view.angle < 270)) ((int)view.angle % 90) == 0 ? view.eye.z -= 1.0 : changeView(0, 1);
  354. //      else if ((view.angle >= 270) && (view.angle < 360)) ((int)view.angle % 90) == 0 ? view.eye.x += 1.0 : changeView(0, 1);
  355.         if ((view.angle >= 0) && (view.angle < 90)) {
  356.             if (((int)view.angle % 90) == 0) {
  357.                 view.eye.z = view.eye.z += 1.0;
  358.             }
  359.             else {
  360.                 view.eye.z = view.eye.z + view.vector.z;
  361.                 view.eye.x = view.eye.x - view.vector.x;
  362.             }
  363.         }
  364.         else if ((view.angle >= 90) && (view.angle < 180)) {
  365.             if (((int)view.angle % 90) == 0) {
  366.                 view.eye.x = view.eye.x -= 1.0;
  367.             }
  368.             else {
  369.                 view.eye.z = view.eye.z + view.vector.z;
  370.                 view.eye.x = view.eye.x - view.vector.x;
  371.             }
  372.         }
  373.         else if ((view.angle >= 180) && (view.angle < 270)) {
  374.             if (((int)view.angle % 90) == 0) {
  375.                 view.eye.z = view.eye.z -= 1.0;
  376.             }
  377.             else {
  378.                 view.eye.z = view.eye.z + view.vector.z;
  379.                 view.eye.x = view.eye.x - view.vector.x;
  380.             }
  381.         }
  382.         else if ((view.angle >= 270) && (view.angle < 360)) {
  383.             if (((int)view.angle % 90) == 0) {
  384.                 view.eye.x = view.eye.x += 1.0;
  385.             }
  386.             else {
  387.                 view.eye.z = view.eye.z + view.vector.z;
  388.                 view.eye.x = view.eye.x - view.vector.x;
  389.             }
  390.         }
  391.     }
  392.     else if (key == GLUT_KEY_LEFT) {
  393.         if (glutGetModifiers() & GLUT_ACTIVE_ALT) {
  394.             cout << "left" << "\n";
  395.             if ((view.angle >= 0) && (view.angle < 90)) ((int)view.angle % 90) == 0 ? view.eye.x -= 1.0 : changeView(0, 0);
  396.             else if ((view.angle >= 90) && (view.angle < 180)) ((int)view.angle % 90) == 0 ? view.eye.z -= 1.0 : changeView(0, 0);
  397.             else if ((view.angle >= 180) && (view.angle < 270)) ((int)view.angle % 90) == 0 ? view.eye.x += 1.0 : changeView(0, 0);
  398.             else if ((view.angle >= 270) && (view.angle < 360)) ((int)view.angle % 90) == 0 ? view.eye.z += 1.0 : changeView(0, 0);
  399.         }
  400.         else {
  401.             cout << "left2" << "\n";
  402.             view.angle -= 3;
  403.             if (view.angle == -3) view.angle = 357;
  404.             view.vector.x = sin(view.angle * PI / 180);
  405.             view.vector.z = cos(view.angle * PI / 180);
  406.         }
  407.     }
  408.     else if (key == GLUT_KEY_RIGHT) {
  409.         if (glutGetModifiers() & GLUT_ACTIVE_ALT) {
  410.             cout << "right" << "\n";
  411.             if ((view.angle >= 0) && (view.angle < 90)) ((int)view.angle % 90) == 0 ? view.eye.x += 1.0 : changeView(1, 1);
  412.             else if ((view.angle >= 90) && (view.angle < 180)) ((int)view.angle % 90) == 0 ? view.eye.z += 1.0 : changeView(1, 1);
  413.             else if ((view.angle >= 180) && (view.angle < 270)) ((int)view.angle % 90) == 0 ? view.eye.x -= 1.0 : changeView(1, 1);
  414.             else if ((view.angle >= 270) && (view.angle < 360)) ((int)view.angle % 90) == 0 ? view.eye.z -= 1.0 : changeView(1, 1);
  415.         }
  416.         else {
  417.             cout << "right2" << "\n";
  418.             view.angle += 3;
  419.             if (view.angle == 360) view.angle = 0;
  420.             view.vector.x = sin(view.angle * PI / 180);
  421.             view.vector.z = cos(view.angle * PI / 180);
  422.         }
  423.     }
  424.     glMatrixMode(GL_PROJECTION);
  425.     glLoadIdentity();
  426.     gluPerspective(60.0, (GLdouble)1024 / 768, 1.0, 100);
  427.     gluLookAt(view.eye.x, view.eye.y, view.eye.z, view.eye.x + view.vector.x, view.eye.y, view.eye.z - view.vector.z, 0.0, 1.0, 0.0);
  428.     glutPostRedisplay();
  429. }
  430.  
  431. void inputStd(unsigned char key, int x, int y) {
  432.     switch (key) {
  433.         case 27:
  434.             exit(0);
  435.             break;
  436.         default:
  437.             break;
  438.     }
  439. }
  440.  
  441.  
  442. void addObject(
  443.                 float posx,float posy,float posz,
  444.                 float velx,float vely,float velz,
  445.                 float rotangx, float rotangy, float rotangz,
  446.                 float rotx, float roty, float rotz,
  447.                 float rotdirx, float rotdiry, float rotdirz,
  448.                 float rad, float vec,
  449.                 short col1r, short col1g, short col1b,
  450.                 short col2r, short col2g, short col2b,
  451.                 GLuint tex) {
  452.     //objects_count++;
  453.     //  objects[objects_count].position.y = "abc";
  454. //  objects[objects_count] = { { posx, posy, posz }, { velx, vely, velz }, { rotangx, rotangy, rotangz }, { rotx, roty, rotz }, { rotdirx, rotdiry, rotdirz }, rad, vec, { col1r, col1g, col1b }, { col2r, col2g, col2b }, tex };
  455. //  { 26, "Jim Smith" } };
  456.         /*object->position.y = posy;
  457.     object->position.z = posz;
  458.     object->velocity.x = velx;
  459.     object->velocity.y = vely;
  460.     object->velocity.z = velz;
  461.     object->rotation_angle.x = rotangx;
  462.     object->rotation_angle.y = rotangy;
  463.     object->rotation_angle.z = rotangz;
  464.     object->rotation.x = rotx;
  465.     object->rotation.y = roty;
  466.     object->rotation.z = rotz;
  467.     object->rotation_dir.x = rotdirx;
  468.     object->rotation_dir.y = rotdiry;
  469.     object->rotation_dir.z = rotdirz;
  470.     object->radius = rad;
  471.     object->color1.R = col1r;
  472.     object->color1.G = col1g;
  473.     object->color1.B = col1b;
  474.     object->color2.R = col2r;
  475.     object->color2.G = col2g;
  476.     object->color2.B = col2b;*/
  477. }
  478.  
  479.  
  480.  
  481.  
  482.  
  483.  
  484. void init(void) {
  485.     bkg.R = 169;
  486.     bkg.G = 167;
  487.     bkg.B = 169;
  488.  
  489. //  addObject(0.0, 0.5, 0.0, 0.005, 0, 0.005, 0.0, 0.0, 0.0, 0.2, 0.0, 0, -1, 0, 0, 1.0, 0, 255, 255, 255, 255, 0, 0, 1);
  490. //  addObject(2.0, 0.1, 1.0, 0.003, 0, 0.009, 0.0, 0.0, 0.0, 0.2, 0.1, 0, 1, 0, 0, 0.25, 0, 255, 255, 255, 0, 255, 255, 2);
  491. //  addObject(2.0, 0.1, 1.0, 0.003, 0, 0.009, 0.0, 0.0, 0.0, 0.2, 0.1, 0, 1, 0, 0, 0.25, 0, 255, 255, 255, 0, 255, 255, 3);
  492.  
  493.     sphereOBJ1.position.x = 0.0;
  494.     sphereOBJ1.position.y = 0.5;
  495.     sphereOBJ1.position.z = 0.0;
  496.     sphereOBJ1.velocity.x = 0.003;
  497.     sphereOBJ1.velocity.y = 0.0;
  498.     sphereOBJ1.velocity.z = 0.02;
  499.     sphereOBJ1.rotation_angle.x = 0.0;
  500.     sphereOBJ1.rotation_angle.y = 0.0;
  501.     sphereOBJ1.rotation_angle.z = 0.0;
  502.     sphereOBJ1.rotation.x = 0.2;
  503.     sphereOBJ1.rotation.y = 0;
  504.     sphereOBJ1.rotation.z = 0;
  505.     sphereOBJ1.rotation_dir.x = -1;
  506.     sphereOBJ1.rotation_dir.y = 0;
  507.     sphereOBJ1.rotation_dir.z = 0;
  508.     sphereOBJ1.radius = 1.0;
  509.     sphereOBJ1.color1.R = 255;
  510.     sphereOBJ1.color1.G = 255;
  511.     sphereOBJ1.color1.B = 255;
  512.     sphereOBJ1.color2.R = 255;
  513.     sphereOBJ1.color2.G = 0;
  514.     sphereOBJ1.color2.B = 0;
  515.     //addObject(&sphereOBJ2, 2.0, 0.1, 1.0, 0.003, 0, 0.009, 0.0, 0.0, 0.0, 0.2, 0.1, 0, 1, 0, 0, 0.25, 255, 255, 255, 0, 255, 255);
  516.     sphereOBJ2.position.x = 2.0;
  517.     sphereOBJ2.position.y = 0.1;
  518.     sphereOBJ2.position.z = 2.0;
  519.     sphereOBJ2.velocity.x = 0.005;
  520.     sphereOBJ2.velocity.y = 0.0;
  521.     sphereOBJ2.velocity.z = -0.010;
  522.     sphereOBJ2.rotation_angle.x = 0.0;
  523.     sphereOBJ2.rotation_angle.y = 0.0;
  524.     sphereOBJ2.rotation_angle.z = 0.0;
  525.     sphereOBJ2.rotation.x = 0.2;
  526.     sphereOBJ2.rotation.y = 0.1;
  527.     sphereOBJ2.rotation.z = 0;
  528.     sphereOBJ2.rotation_dir.x = 1;
  529.     sphereOBJ2.rotation_dir.y = 0;
  530.     sphereOBJ2.rotation_dir.z = 0;
  531.     sphereOBJ2.radius = 0.5;
  532.     sphereOBJ2.color1.R = 255;
  533.     sphereOBJ2.color1.G = 255;
  534.     sphereOBJ2.color1.B = 255;
  535.     sphereOBJ2.color2.R = 0;
  536.     sphereOBJ2.color2.G = 0;
  537.     sphereOBJ2.color2.B = 255;
  538.    
  539.     sphereOBJ3.position.x = 4.0;
  540.     sphereOBJ3.position.y = 0.3;
  541.     sphereOBJ3.position.z = 5.0;
  542.     sphereOBJ3.velocity.x = 0.004;
  543.     sphereOBJ3.velocity.y = 0.0;
  544.     sphereOBJ3.velocity.z = 0.004;
  545.     sphereOBJ3.rotation_angle.x = 0.0;
  546.     sphereOBJ3.rotation_angle.y = 0.0;
  547.     sphereOBJ3.rotation_angle.z = 0.0;
  548.     sphereOBJ3.rotation.x = 0.1;
  549.     sphereOBJ3.rotation.y = 0;
  550.     sphereOBJ3.rotation.z = 0;
  551.     sphereOBJ3.rotation_dir.x = 1;
  552.     sphereOBJ3.rotation_dir.y = 0;
  553.     sphereOBJ3.rotation_dir.z = 0;
  554.     sphereOBJ3.radius = 0.75;
  555.     sphereOBJ3.color1.R = 255;
  556.     sphereOBJ3.color1.G = 255;
  557.     sphereOBJ3.color1.B = 255;
  558.     sphereOBJ3.color2.R = 255;
  559.     sphereOBJ3.color2.G = 0;
  560.     sphereOBJ3.color2.B = 255;
  561.    
  562.     view.eye.x = 0.0;
  563. //  view.eye.y = 5.0;
  564.     view.eye.y = 0.0;
  565.     view.eye.z = 10.0;
  566.     view.vector.x = 0.0;
  567. //  view.vector.y = -10.0;
  568.     view.vector.y = 0.0;
  569.     view.vector.z = 1.0;
  570.     view.angle = 0.0;
  571.  
  572.     light0.position.x = 0.0;
  573.     light0.position.y = 2.0;
  574.     light0.position.z = 3.0;
  575.     light0.direction.x = 1.0;
  576.     light0.direction.y = 1.0;
  577.     light0.direction.z = -1.0;
  578.  
  579.     tekst = "ala";
  580.  
  581. //  light1.position.x = 0.0;
  582. //  light1.position.y = -2.0;
  583. //  light1.position.z = 3.0;
  584. //  light1.direction.x = 1.0;
  585. //  light1.direction.y = 1.0;
  586. //  light1.direction.z = -1.0;
  587.  
  588.     GLfloat light0_position[] = { light0.position.x, light0.position.y, light0.position.z, 0.0 };
  589.     GLfloat spot0_direction[] = { light0.direction.x, light0.direction.y, light0.direction.z };
  590.     GLfloat spot0_cutoff = 180;
  591. //  GLfloat light1_position[] = { light1.position.x, light1.position.y, light1.position.z, 0.0 };
  592. //  GLfloat spot1_direction[] = { light1.direction.x, light1.direction.y, light1.direction.z };
  593. //  GLfloat spot1_cutoff = 180;
  594.  
  595.     GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  596.     GLfloat mat_shininess[] = { 50.0 };
  597.     GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
  598.     GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8, 1.0f };
  599.     GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };
  600.  
  601.  
  602.     glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
  603.     glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spot0_direction);
  604.     glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, spot0_cutoff);
  605. //  glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
  606. //  glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, spot1_direction);
  607. //  glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, spot1_cutoff);
  608.  
  609.     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  610.     glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  611.     // Assign created components to GL_LIGHT0
  612.     glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
  613.     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
  614.     glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
  615.  
  616.     glEnable(GL_LIGHTING);
  617.     glEnable(GL_LIGHT0);
  618. //  glEnable(GL_LIGHT1);
  619.  
  620.     glEnable(GL_DEPTH_TEST);
  621.  
  622. //  for (short i = 0; ++i; i < objects_count) {
  623.         make_tex(&sphereOBJ1);
  624.         make_tex(&sphereOBJ2);
  625.         make_tex(&sphereOBJ3);
  626. //  }
  627.     sphere1 = gluNewQuadric();
  628.     sphere2 = gluNewQuadric();
  629.     sphere3 = gluNewQuadric();
  630.  
  631.     glEnable(GL_TEXTURE_2D);
  632.  
  633.     glMatrixMode(GL_MODELVIEW);
  634.     glLoadIdentity();
  635.     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);
  636.     glColor3d(0, 0, 0);
  637. }
  638.  
  639. void myIdle() {
  640.     CollisionDetect3(&sphereOBJ1, &sphereOBJ2);
  641.     CollisionDetect3(&sphereOBJ1, &sphereOBJ3);
  642.     CollisionDetect3(&sphereOBJ2, &sphereOBJ3);
  643.     RunPhysics(0.003, &sphereOBJ1);
  644.     RunPhysics(0.003, &sphereOBJ2);
  645.     RunPhysics(0.003, &sphereOBJ3);
  646.     draw();
  647. }
  648.  
  649.  
  650. int main(int argc, char **argv) {
  651.     glutInit(&argc, argv);
  652.  
  653.     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  654.     glutInitWindowSize(1024,768);
  655.  
  656.     glutCreateWindow("Test");
  657.  
  658.     glutDisplayFunc(draw);
  659.     glutKeyboardFunc(inputStd);
  660.     glutSpecialFunc(inputSpecial);
  661.     glutIdleFunc(myIdle);
  662.     glutReshapeFunc(resize);
  663.  
  664.     init();
  665.  
  666.     glutMainLoop();
  667. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement