Advertisement
DVS_studio

Engine

Nov 21st, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 39.46 KB | None | 0 0
  1. #include "DVSEngine.h"
  2.  
  3. namespace DVSEngine {
  4.  
  5.     MAIN *MC;
  6.  
  7. #pragma region MAIN
  8.  
  9. #pragma region MAINDestructor
  10.  
  11.     MAIN *MAIN::p_instance = 0;
  12.     MAINDestructor MAIN::destroyer;
  13.  
  14.     MAINDestructor::~MAINDestructor() {
  15.         delete p_instance;
  16.     }
  17.     void MAINDestructor::initialize(MAIN *p) {
  18.         p_instance = p;
  19.     }
  20. #pragma endregion
  21.  
  22.     MAIN *MAIN::getInstance() {
  23.         if (!p_instance) {
  24.             p_instance = new MAIN();
  25.             destroyer.initialize(p_instance);
  26.         }
  27.         MC = p_instance;
  28.         return p_instance;
  29.     }
  30.  
  31.     void MAIN::setWindow(int *argcp, char **argv) {
  32.         glutInit_ATEXIT_HACK(argcp, argv);
  33.         glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE);
  34.     }
  35.     char **MAIN::setWindow(char *exeName, LPSTR cmdLine, int *argc) {
  36.         char **ret = CommandLineToArgvA(cmdLine, argc);
  37.         char **argv = new char*[*argc + 1];
  38.         argv[0] = exeName;
  39.         for (int i = 0; i < *argc; ++i)
  40.             argv[i + 1] = ret[i];
  41.         setWindow(argc, argv);
  42.         return ret;
  43.     }
  44.  
  45.     void MAIN::createWindow(char *title, V4 bounds) {
  46.         glutInitWindowPosition((int)bounds.x, (int)bounds.y);
  47.         glutInitWindowSize((int)bounds.z, (int)bounds.w);
  48.         glutCreateWindow_ATEXIT_HACK(title);
  49.     }
  50.     void MAIN::Init(void _In()) {
  51.         glutDisplayFunc(DisplayF);
  52.         glutReshapeFunc(ReshapeF);
  53.         glutMouseFunc(MouseF);
  54.         glutMotionFunc(ActionMouseF);
  55.         glutPassiveMotionFunc(PassiveMouseF);
  56.         glutKeyboardFunc(KeyPressF);
  57.         glutKeyboardUpFunc(KeyReleaseF);
  58.         glutSpecialFunc(KeySpecPressF);
  59.         glutSpecialUpFunc(KeySpecReleaseF);
  60.  
  61.         glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  62.         glClearDepth(1.0);
  63.         glEnable(GL_DEPTH_TEST);
  64.  
  65.         glEnable(GL_TEXTURE_2D);
  66.  
  67.         glDepthFunc(GL_LEQUAL);
  68.         glEnable(GL_LINE_SMOOTH);
  69.         glEnable(GL_BLEND);
  70.         glShadeModel(GL_SMOOTH);
  71.         glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
  72.         glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  73.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  74.  
  75.         ilInit();
  76.         iluInit();
  77.  
  78.         LoadTexture("Default.jpg");
  79.  
  80.         arial_16 = new CFont("arial.ttf", 16, 16.0f);
  81.         camera = new Camera();
  82.         physics = new Physics(9.8f);
  83.         setMaxFPS(60);
  84.  
  85.         if (_In != 0)_In();
  86.     }
  87.     void MAIN::Start() {
  88.         IdleF();
  89.         glutMainLoop();
  90.     }
  91.  
  92. #pragma region GLUT �������
  93.  
  94.     void IdleF() {
  95.         if (MC->Pause) {
  96.  
  97.         }
  98.         else {
  99.             for (Object *o : MC->objs)
  100.                 o->tick();
  101.             MC->time += MC->dT;
  102.         }
  103.         if (MC->I != 0)MC->I();
  104.         glutTimerFunc(MC->ThoudevFps, IdleF, 0);
  105.     }
  106.     void DisplayF() {
  107.         CalculateFrameRate();
  108.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  109.         glLoadIdentity();
  110.         if (MC->D3d != 0)MC->D3d();
  111.  
  112.         for (Object *o : MC->objs)
  113.             o->draw();
  114.  
  115.         to2D();
  116.         MC->cout.draw();
  117.         if (MC->D2d != 0)MC->D2d();
  118.         backTo3D();
  119.  
  120.         glutSwapBuffers();
  121.         glutTimerFunc(MC->ThoudevFps, DisplayF, 0);
  122.     }
  123.  
  124.     void ReshapeF(int width, int height) {
  125.         if (height == 0)
  126.             height = 1;
  127.         MC->Width = width, MC->Height = height;
  128.         float ratio = width * 1.0f / height;
  129.         glMatrixMode(GL_PROJECTION);
  130.         glLoadIdentity();
  131.         glViewport(0, 0, width, height);
  132.         gluPerspective(45.0f, ratio, 0.1f, 100.0f);
  133.         glMatrixMode(GL_MODELVIEW);
  134.         glLoadIdentity();
  135.     }
  136.  
  137.     void MouseF(int button, int state, int x, int y) {
  138.         MC->mouse = { x,y };
  139.         if (!MC->Pause) {
  140.             if (MC->camera->needMouseZoom) {
  141.                 if (button == 3) {
  142.                     MC->camera->Zoom -= 5.0f;
  143.                     MC->camera->Zoom = max(MC->camera->Zoom, 0.0f);
  144.                 }
  145.                 else if (button == 4) {
  146.                     MC->camera->Zoom += 5.0f;
  147.                     MC->camera->Zoom = min(MC->camera->Zoom, MC->camera->ZoomLimit);
  148.                 }
  149.             }
  150.         }
  151.         if (MC->M != 0)MC->M(button, state, x, y);
  152.     }
  153.     void ActionMouseF(int x, int y) {
  154.         MC->mouse = { x,y };
  155.         if (MC->Ma != 0)MC->Ma(x, y);
  156.     }
  157.     void PassiveMouseF(int x, int y) {
  158.         MC->mouse = { x,y };
  159.         if (MC->Mp != 0)MC->Mp(x, y);
  160.     }
  161.  
  162.     void KeyPressF(byte ch, int x, int y) {
  163.         wchar_t c = (ch >= 192 && ch <= 255) ? ch + 848 : (ch <= 26) ? ch + 96 : ch;
  164.         if (MC->Pause) {
  165.             if (ch == 13) {
  166.                 if (MC->tmpStr.empty())
  167.                     return;
  168.                 MC->cout.println(MC->tmpStr);
  169.                 MC->cout.parseln(MC->tmpStr);
  170.                 MC->tmpStr.clear();
  171.             }
  172.             else if (ch == '\b') {
  173.                 MC->tmpStr.pop_back();
  174.             }
  175.             else {
  176.                 MC->tmpStr.push_back(c);
  177.                 if (MC->tmpStr.size() > 100) {
  178.                     for (short i = 1; i <= 100; ++i)
  179.                         MC->tmpStr[i - 1] = MC->tmpStr[i];
  180.                     MC->tmpStr.resize(100);
  181.                 }
  182.             }
  183.         }
  184.         else {
  185.             c = keyboardRuToEng(c);
  186.             MC->kbd[c] = true;
  187.         }
  188.         if (MC->Kp != 0)MC->Kp(ch, x, y);
  189.     }
  190.     void KeyReleaseF(byte ch, int x, int y) {
  191.         wchar_t c = (ch >= 192 && ch <= 255) ? ch + 848 : (ch <= 26) ? ch + 96 : ch;
  192.         if (MC->Pause) {
  193.  
  194.         }
  195.         else {
  196.             c = keyboardRuToEng(c);
  197.             MC->kbd[c] = false;
  198.         }
  199.         if (MC->Kr != 0)MC->Kr(ch, x, y);
  200.     }
  201.  
  202.     void KeySpecPressF(int ch, int x, int y) {
  203.  
  204.         switch (ch) {
  205.         case GLUT_KEY_SHIFT_L:
  206.         case GLUT_KEY_SHIFT_R:
  207.             MC->is_shift = true;
  208.             break;
  209.         case GLUT_KEY_CTRL_L:
  210.         case GLUT_KEY_CTRL_R:
  211.             MC->is_ctrl = true;
  212.             break;
  213.         case GLUT_KEY_ALT_L:
  214.         case GLUT_KEY_ALT_R:
  215.             MC->is_alt = true;
  216.             break;
  217.         }
  218.  
  219.         switch (ch) {
  220.         case GLUT_KEY_UP:
  221.             MC->tmpStr = MC->cout.stack[0];
  222.             break;
  223.         case GLUT_KEY_F4:
  224.             if (MC->is_alt)
  225.                 glutLeaveMainLoop();
  226.             break;
  227.         case GLUT_KEY_F11:
  228.             glutFullScreenToggle();
  229.             break;
  230.         case GLUT_KEY_F12:
  231.             MC->cout.toggle();
  232.             break;
  233.         }
  234.         if (MC->Ksp != 0)MC->Ksp(ch, x, y);
  235.     }
  236.     void KeySpecReleaseF(int ch, int x, int y) {
  237.  
  238.         switch (ch) {
  239.         case GLUT_KEY_SHIFT_L:
  240.         case GLUT_KEY_SHIFT_R:
  241.             MC->is_shift = false;
  242.             break;
  243.         case GLUT_KEY_CTRL_L:
  244.         case GLUT_KEY_CTRL_R:
  245.             MC->is_ctrl = false;
  246.             break;
  247.         case GLUT_KEY_ALT_L:
  248.         case GLUT_KEY_ALT_R:
  249.             MC->is_alt = false;
  250.             break;
  251.         }
  252.  
  253.         if (MC->Ksr != 0)MC->Ksr(ch, x, y);
  254.     }
  255.  
  256. #pragma endregion
  257.  
  258. #pragma region ���. �������
  259.  
  260.     void MAIN::pointerToCenter(bool visibleCursor) {
  261.         glutWarpPointer(Width >> 1, Height >> 1);
  262.         glutSetCursor((visibleCursor) ? GLUT_CURSOR_LEFT_ARROW : GLUT_CURSOR_NONE);
  263.     }
  264.     void MAIN::setMaxFPS(int maxFPS) {
  265.         FPSlimit = maxFPS;
  266.         dT = FPSlimit / 1000.0f;
  267.         ThoudevFps = 1000 / FPSlimit;
  268.     }
  269.  
  270. #pragma endregion
  271.  
  272. #pragma endregion
  273.  
  274. #pragma region ����������
  275.  
  276. #pragma region ������ �������������� �������
  277.  
  278.     short sign(int a) {
  279.         return (a > 0) ? 1 : ((a < 0) ? -1 : a);
  280.     }
  281.  
  282.     float Distance(V4 vPoint1, V4 vPoint2) {
  283.         return sqrt((vPoint2.x - vPoint1.x) * (vPoint2.x - vPoint1.x) +
  284.             (vPoint2.y - vPoint1.y) * (vPoint2.y - vPoint1.y) +
  285.             (vPoint2.z - vPoint1.z) * (vPoint2.z - vPoint1.z));
  286.     }
  287.  
  288.     float PlaneDistance(V4 Normal, V4 Point) {
  289.         return -((Normal.x * Point.x) + (Normal.y * Point.y) + (Normal.z * Point.z));
  290.     }
  291.  
  292.     V4 ClosestPointOnLine(V4 vA, V4 vB, V4 vPoint) {
  293.         V4 vV = vB - vA;
  294.         vV.Normal();
  295.         float d = Distance(vA, vB);
  296.         float t = vV.DotProduct3(vPoint - vA);
  297.         if (t <= 0)
  298.             return vA;
  299.         if (t >= d)
  300.             return vB;
  301.         return vA + vV * t;
  302.     }
  303.  
  304.     V4 ClosestPointOnPlane(PolygonS p, V4 vPoint) {
  305.         return vPoint - p.normal * PlaneDistance(p.normal, vPoint);
  306.     }
  307.  
  308.     float AngleBetweenVectors(V4 Vector1, V4 Vector2) {
  309.         float dotProduct = Vector1.DotProduct3(Vector2);
  310.         float vectorsMagnitude = Vector1.GetLength() * Vector2.GetLength();
  311.  
  312.         float angle = acos(dotProduct / vectorsMagnitude);
  313.         if (_isnan(angle))
  314.             return 0;
  315.  
  316.         return angle;
  317.     }
  318.  
  319.     V4 getBezierPoint(vector<V4> pts, float t) {
  320.         int i, c;
  321.         float p;
  322.         V4 np;
  323.         int n = static_cast<int>(pts.size());
  324.  
  325.         for (i = 0, c = 1; i < n; ++i) {
  326.             pts[i].x = pts[i].x * c;
  327.             pts[i].y = pts[i].y * c;
  328.             pts[i].z = pts[i].z * c;
  329.             c = (n - i)*c / (i + 1);
  330.         }
  331.         for (i = 0, p = 1; i < n; ++i) {
  332.             pts[i].x = pts[i].x * p;
  333.             pts[i].y = pts[i].y * p;
  334.             pts[i].z = pts[i].z * p;
  335.             p = p * t;
  336.         }
  337.         for (i = n - 1, p = 1; i >= 0; --i) {
  338.             pts[i].x = pts[i].x * p;
  339.             pts[i].y = pts[i].y * p;
  340.             pts[i].z = pts[i].z * p;
  341.             p = p * (1 - t);
  342.         }
  343.         for (i = 0; i < n; ++i) {
  344.             np.x = np.x + pts[i].x;
  345.             np.y = np.y + pts[i].y;
  346.             np.z = np.z + pts[i].z;
  347.         }
  348.         return np;
  349.     }
  350.  
  351. #pragma endregion
  352.  
  353. #pragma region ������� ��� ��������
  354.  
  355.     bool InsidePolygon(V4 vIntersection, PLANE poly) {
  356.         const double MATCH_FACTOR = 0.9999;
  357.         double Angle = 0.0;
  358.         Angle += AngleBetweenVectors(poly.p1 - vIntersection, poly.p2 - vIntersection);
  359.         Angle += AngleBetweenVectors(poly.p2 - vIntersection, poly.p3 - vIntersection);
  360.         Angle += AngleBetweenVectors(poly.p3 - vIntersection, poly.p1 - vIntersection);
  361.  
  362.         if (Angle >= (MATCH_FACTOR * (2.0 * M_PI)))
  363.             return true;
  364.         return false;
  365.     }
  366.  
  367.     bool ClassifySphere(SPHERE Sph, PLANE poly, float &distance) {
  368.         float pD = PlaneDistance(poly.normal, poly.p1);
  369.         distance = abs((poly.normal.x * Sph.position.x + poly.normal.y * Sph.position.y + poly.normal.z * Sph.position.z + pD));
  370.         return distance < Sph.radius;
  371.     }
  372.  
  373.     bool ClassifyPoint(V4 TestV, PLANE poly, float &distance) {
  374.         return ClassifySphere({ TestV,0.09f }, poly, distance);
  375.     }
  376.  
  377.     float SquaredDistPointAABB(V4 pointV, BOX aabb) {
  378.         auto check = [&](float pn, float bmin, float bmax) -> float {
  379.             float out = 0;
  380.             if (pn < bmin)
  381.                 out += (bmin - pn)*(bmin - pn);
  382.             if (pn > bmax)
  383.                 out += (pn - bmax)*(pn - bmax);
  384.             return out;
  385.         };
  386.         return check(pointV.x, aabb.minV.x, aabb.maxV.x) +
  387.             check(pointV.y, aabb.minV.y, aabb.maxV.y) +
  388.             check(pointV.z, aabb.minV.z, aabb.maxV.z);
  389.     }
  390.  
  391.     bool LineSphereCollision(V4 linep1, V4 linep2, SPHERE Sph) {
  392.         if (Distance(ClosestPointOnLine(linep1, linep2, Sph.position), Sph.position) < Sph.radius)
  393.             return true;
  394.         return false;
  395.     }
  396.  
  397.     bool EdgeSphereCollision(SPHERE Sph, PLANE Pla) {
  398.  
  399.         if (LineSphereCollision(Pla.p1, Pla.p2, Sph))
  400.             return true;
  401.         if (LineSphereCollision(Pla.p2, Pla.p3, Sph))
  402.             return true;
  403.         if (LineSphereCollision(Pla.p3, Pla.p1, Sph))
  404.             return true;
  405.  
  406.         return false;
  407.     }
  408.  
  409.     bool PointInAABB(V4 PointV, BOX tBox) {
  410.         return(PointV.x > tBox.minV.x && PointV.x < tBox.maxV.x &&
  411.             PointV.y > tBox.minV.y && PointV.y < tBox.maxV.y &&
  412.             PointV.z > tBox.minV.z && PointV.z < tBox.maxV.z);
  413.     }
  414.  
  415. #pragma endregion
  416.  
  417. #pragma endregion
  418.  
  419. #pragma region V4
  420.  
  421.     V4::V4() { x = y = z = 0.0; }
  422.     V4::V4(float X, float Y) { x = X; y = Y; }
  423.     V4::V4(float X, float Y, float Z) { x = X; y = Y; z = Z; }
  424.     V4::V4(int X, int Y) { x = (float)X; y = (float)Y; }
  425.     V4::V4(int X, int Y, int Z) { x = (float)X; y = (float)Y; z = (float)Z; }
  426.     V4::V4(float X, float Y, float Z, float W) { x = X; y = Y; z = Z; w = W; }
  427.     V4::V4(const V4 &v) { x = v.x; y = v.y; z = v.z; w = v.w; }
  428.     V4::V4(const V3 &v) { x = v.x; y = v.y; z = v.z; }
  429.     V4::V4(const POINT &v) { x = (float)v.x; y = (float)v.y; }
  430.  
  431.     V4::operator V3() { return V3(x, y, z); }
  432.     V4::operator float*() { return new float[3]{ x,y,z }; }
  433.  
  434.     void V4::operator =(V4 v) { x = v.x; y = v.y; z = v.z; w = v.w; }
  435.  
  436.     V4 V4::operator -() { return (*this)*-1; }
  437.  
  438.     V4 V4::operator -(V4 v) { return V4(x - v.x, y - v.y, z - v.z); }
  439.     V4 V4::operator +(V4 v) { return V4(x + v.x, y + v.y, z + v.z); }
  440.     V4 V4::operator *(V4 v) { return V4(x * v.x, y * v.y, z * v.z); }
  441.     V4 V4::operator /(V4 v) { return V4(x / v.x, y / v.y, z / v.z); }
  442.  
  443.     V4 V4::operator +(float f) { return V4(x + f, y + f, z + f); }
  444.     V4 V4::operator -(float f) { return V4(x - f, y - f, z - f); }
  445.     V4 V4::operator *(float f) { return V4(x * f, y * f, z * f); }
  446.     V4 V4::operator /(float f) { f = 1 / f; return V4(x * f, y * f, z * f); }
  447.  
  448.     void V4::operator +=(V4 v) { x += v.x; y += v.y; z += v.z; w += v.w; }
  449.     void V4::operator -=(V4 v) { x -= v.x; y -= v.y; z -= v.z; w -= v.w; }
  450.     void V4::operator *=(V4 v) { x *= v.x; y *= v.y; z *= v.z; w *= v.w; }
  451.     void V4::operator /=(V4 v) { x /= v.x; y /= v.y; z /= v.z; w /= v.w; }
  452.  
  453.     void V4::operator +=(float f) { x += f; y += f; z += f; z += f; }
  454.     void V4::operator -=(float f) { x -= f; y -= f; z -= f; z -= f; }
  455.     void V4::operator *=(float f) { x *= f; y *= f; z *= f; z *= f; }
  456.     void V4::operator /=(float f) { f = 1 / f; x *= f; y *= f; z *= f; w *= f; }
  457.  
  458.     bool V4::operator ==(V4 v) { return ((x == v.x) && (y == v.y) && (z == v.z)); }
  459.     bool V4::operator !=(V4 v) { return !((x == v.x) && (y == v.y) && (z == v.z)); }
  460.  
  461.     V4 V4::Cross(V4 v1, V4 v2) {
  462.         x = ((v1.y * v2.z) - (v1.z * v2.y));
  463.         y = ((v1.z * v2.x) - (v1.x * v2.z));
  464.         z = ((v1.x * v2.y) - (v1.y * v2.x));
  465.         return *this;
  466.     }
  467.  
  468.     V4 V4::Cross(V4 v1, V4 v2, V4 v3) {
  469.         x = v1.y * v2.z * v3.w + v1.z * v2.w * v3.y + v1.w * v2.y * v3.z - v1.y * v2.w * v3.z - v1.z * v2.y * v3.w - v1.w * v2.z * v3.y;
  470.         y = v1.x * v2.w * v3.z + v1.z * v2.x * v3.w + v1.w * v2.z * v3.x - v1.x * v2.z * v3.w - v1.z * v2.w * v3.x - v1.w * v2.x * v3.z;
  471.         z = v1.x * v2.y * v3.w + v1.y * v2.w * v3.x + v1.w * v2.x * v3.y - v1.x * v2.w * v3.y - v1.y * v2.x * v3.w - v1.w * v2.y * v3.x;
  472.         w = v1.x * v2.z * v3.y + v1.y * v2.x * v3.z + v1.z * v2.y * v3.x - v1.x * v2.y * v3.z - v1.y * v2.z * v3.x - v1.z * v2.x * v3.y;
  473.         return *this;
  474.     }
  475.  
  476.     float V4::DotProduct3(V4 v1) { return x * v1.x + y * v1.y + z * v1.z; }
  477.  
  478.     float V4::DotProduct4(V4 v1) { return x * v1.x + y * v1.y + z * v1.z + w * v1.w; }
  479.  
  480.     float V4::GetLength() { return (float)sqrt((x * x + y * y + z * z)); }
  481.  
  482.     V4 V4::Normal() {
  483.         float lenght = GetLength();
  484.         if (lenght == 0.0f)
  485.             lenght = 1.0f;
  486.         *this /= lenght;
  487.         return *this;
  488.     }
  489.  
  490.     void V4::Normalize(V4 Triangle[]) {
  491.         V4 v1, v2;
  492.         v1.x = Triangle[0].x - Triangle[1].x;
  493.         v1.y = Triangle[0].y - Triangle[1].y;
  494.         v1.z = Triangle[0].z - Triangle[1].z;
  495.         v1.w = Triangle[0].w - Triangle[1].w;
  496.  
  497.         v2.x = Triangle[1].x - Triangle[2].x;
  498.         v2.y = Triangle[1].y - Triangle[2].y;
  499.         v2.z = Triangle[1].z - Triangle[2].z;
  500.         v2.w = Triangle[1].w - Triangle[2].w;
  501.         Cross(v1, v2);
  502.         Normal();
  503.     }
  504.  
  505.     V4 V4::GetRotatedX(float angle) {
  506.         float sinAngle = (float)sin(M_PI * angle / 180);
  507.         float cosAngle = (float)cos(M_PI * angle / 180);
  508.         return V4(x, y * cosAngle - z * sinAngle, y * sinAngle + z * cosAngle, w);
  509.     }
  510.     V4 V4::GetRotatedY(float angle) {
  511.         float sinAngle = (float)sin(M_PI * angle / 180);
  512.         float cosAngle = (float)cos(M_PI * angle / 180);
  513.         return V4(x * cosAngle + z * sinAngle, y, -x * sinAngle + z * cosAngle, w);
  514.     }
  515.     V4 V4::GetRotatedZ(float angle) {
  516.         float sinAngle = (float)sin(M_PI * angle / 180);
  517.         float cosAngle = (float)cos(M_PI * angle / 180);
  518.         return V4(x * cosAngle - y * sinAngle, x * sinAngle + y * cosAngle, z, w);
  519.     }
  520.  
  521.     V4 V4::GetRotatedAxis(float angle, V4 axis) {
  522.         if (angle == 0.0) return(*this);
  523.  
  524.         axis.Normal();
  525.  
  526.         V4 RotationRow1, RotationRow2, RotationRow3;
  527.  
  528.         double newAngle = M_PI * angle / 180;
  529.         float sinAngle = (float)sin(newAngle);
  530.         float cosAngle = (float)cos(newAngle);
  531.         float oneSubCos = 1.0f - cosAngle;
  532.  
  533.         RotationRow1.x = (axis.x) * (axis.x) + cosAngle * (1 - (axis.x) * (axis.x));
  534.         RotationRow1.y = (axis.x) * (axis.y) * (oneSubCos)-sinAngle * axis.z;
  535.         RotationRow1.z = (axis.x) * (axis.z) * (oneSubCos)+sinAngle * axis.y;
  536.  
  537.         RotationRow2.x = (axis.x) * (axis.y) * (oneSubCos)+sinAngle * axis.z;
  538.         RotationRow2.y = (axis.y) * (axis.y) + cosAngle * (1 - (axis.y) * (axis.y));
  539.         RotationRow2.z = (axis.y) * (axis.z) * (oneSubCos)-sinAngle * axis.x;
  540.  
  541.         RotationRow3.x = (axis.x) * (axis.z) * (oneSubCos)-sinAngle * axis.y;
  542.         RotationRow3.y = (axis.y) * (axis.z) * (oneSubCos)+sinAngle * axis.x;
  543.         RotationRow3.z = (axis.z) * (axis.z) + cosAngle * (1 - (axis.z) * (axis.z));
  544.  
  545.         return V4(this->DotProduct3(RotationRow1),
  546.             this->DotProduct3(RotationRow2),
  547.             this->DotProduct3(RotationRow3));
  548.     }
  549.  
  550.     void V4::maxXYZ(V4 v) { x = max(x, v.x); y = max(y, v.y); z = max(z, v.z); }
  551.     void V4::minXYZ(V4 v) { x = min(x, v.x); y = min(y, v.y); z = min(z, v.z); }
  552.     void V4::set(float f) { x = y = z = w = f; }
  553.  
  554. #pragma endregion
  555.  
  556. #pragma region Quaternion
  557.  
  558. Quaternion::Quaternion() {
  559. }
  560. Quaternion::Quaternion(V4 v3) {
  561.     v.x = v3.x;
  562.     v.y = v3.y;
  563.     v.z = v3.z;
  564.     v.w = 0.0f;
  565. }
  566. Quaternion::Quaternion(V4 v, float degrees, bool is_radian) {
  567.     if (is_radian)CreateFromAxisRadian(v, degrees);
  568.     else CreateFromAxisAngle(v, degrees);
  569. }
  570. void Quaternion::CreateFromAxisAngle(V4 v3, float degrees) {
  571.     float angle = float(degrees / 180.0f * M_PI);
  572.  
  573.     float result = float(sin(angle / 2.0f));
  574.  
  575.     v.x = float(v3.x * result);
  576.     v.y = float(v3.y * result);
  577.     v.z = float(v3.z * result);
  578.     v.w = float(cos(angle / 2.0f));
  579. }
  580. void Quaternion::CreateFromAxisRadian(V4 v3, float angle) {
  581.     float result = float(sin(angle / 2.0f));
  582.  
  583.     v.x = float(v3.x * result);
  584.     v.y = float(v3.y * result);
  585.     v.z = float(v3.z * result);
  586.     v.w = float(cos(angle / 2.0f));
  587. }
  588. Quaternion* Quaternion::mul(Quaternion *q) {
  589.     Quaternion *r = new Quaternion();
  590.  
  591.     float A = (v.w + v.x)*(q->v.w + q->v.x);
  592.     float B = (v.z - v.y)*(q->v.y - q->v.z);
  593.     float C = (v.x - v.w)*(q->v.y + q->v.z);
  594.     float D = (v.y + v.z)*(q->v.x - q->v.w);
  595.     float E = (v.x + v.z)*(q->v.x + q->v.y);
  596.     float F = (v.x - v.z)*(q->v.x - q->v.y);
  597.     float G = (v.w + v.y)*(q->v.w - q->v.z);
  598.     float H = (v.w - v.y)*(q->v.w + q->v.z);
  599.  
  600.     r->v.w = B + (-E - F + G + H) * 0.5f;
  601.     r->v.x = A - (E + F + G + H) * 0.5f;
  602.     r->v.y = -C + (E - F + G - H) * 0.5f;
  603.     r->v.z = -D + (E - F - G + H) * 0.5f;
  604.  
  605.     return r;
  606. }
  607. Quaternion* Quaternion::sum(Quaternion *q) {
  608.     return new Quaternion(v + q->v, v.w + q->v.w);
  609. }
  610. float Quaternion::QMagnitude() {
  611.     return (float)sqrt(v.GetLength() + v.w*v.w);
  612. }
  613. Quaternion* Quaternion::Inverse() {
  614.     return new Quaternion(-v, v.w);/////////////////////////
  615. }
  616. Quaternion::operator V4() { return v; }
  617.  
  618. Quaternion Quaternion::rotate(Quaternion *q, Quaternion *p) {
  619.     Quaternion *mul1 = p->mul(q);
  620.     Quaternion *invQ = p->Inverse();
  621.     Quaternion *mul2 = mul1->mul(invQ);
  622.     Quaternion ret = *mul2;
  623.     delete(invQ);
  624.     delete(mul1);
  625.     delete(mul2);
  626.     return ret;
  627. }
  628.  
  629. Quaternion Quaternion::rotate(V4 qv) {
  630.     Quaternion *q = new Quaternion(qv);
  631.     Quaternion ret = rotate(q, this);
  632.     delete(q);
  633.     return ret;
  634. }
  635. Quaternion Quaternion::rotate(Quaternion q) {
  636.     return rotate(q, this);
  637. }
  638.  
  639. Quaternion Quaternion::rotate(Quaternion *q, V4 pv, float a, bool i) {
  640.     Quaternion *p = new Quaternion(pv, a, i);
  641.     Quaternion ret = rotate(q, p);
  642.     delete(p);
  643.     return ret;
  644. }
  645. Quaternion Quaternion::rotate(V4 qv, Quaternion *p) {
  646.     Quaternion *q = new Quaternion(qv);
  647.     Quaternion ret = rotate(q, p);
  648.     delete(q);
  649.     return ret;
  650. }
  651. Quaternion Quaternion::rotate(V4 qv, V4 pv, float a, bool i) {
  652.     Quaternion *q = new Quaternion(qv);
  653.     Quaternion *p = new Quaternion(pv, a, i);
  654.     Quaternion ret = rotate(q, p);
  655.     delete(q);
  656.     delete(p);
  657.     return ret;
  658. }
  659.  
  660. #pragma endregion
  661.  
  662. #pragma region Frustum
  663.     void Frustum::CalculateFrustum() {
  664.         float   proj[16];           // ������� ��������
  665.         float   modl[16];           // ������� �������
  666.         float   clip[16];           // ��������� ���������
  667.  
  668.         glGetFloatv(GL_PROJECTION_MATRIX, proj);
  669.  
  670.         // ��������� GL_MODELVIEW_MATRIX, �� ����������� ���������� � ������� �������.
  671.         // ��� ����� ���������� � ������� [16].
  672.         glGetFloatv(GL_MODELVIEW_MATRIX, modl);
  673.  
  674.         // ������, ���� ������� �������� � �������, ���� �� �� ������������, �� ������� ���������
  675.         // ���������. ��� ����� �� ������� ������� ���� �� �����.
  676.  
  677.         clip[0] = modl[0] * proj[0] + modl[1] * proj[4] + modl[2] * proj[8] + modl[3] * proj[12];
  678.         clip[1] = modl[0] * proj[1] + modl[1] * proj[5] + modl[2] * proj[9] + modl[3] * proj[13];
  679.         clip[2] = modl[0] * proj[2] + modl[1] * proj[6] + modl[2] * proj[10] + modl[3] * proj[14];
  680.         clip[3] = modl[0] * proj[3] + modl[1] * proj[7] + modl[2] * proj[11] + modl[3] * proj[15];
  681.  
  682.         clip[4] = modl[4] * proj[0] + modl[5] * proj[4] + modl[6] * proj[8] + modl[7] * proj[12];
  683.         clip[5] = modl[4] * proj[1] + modl[5] * proj[5] + modl[6] * proj[9] + modl[7] * proj[13];
  684.         clip[6] = modl[4] * proj[2] + modl[5] * proj[6] + modl[6] * proj[10] + modl[7] * proj[14];
  685.         clip[7] = modl[4] * proj[3] + modl[5] * proj[7] + modl[6] * proj[11] + modl[7] * proj[15];
  686.  
  687.         clip[8] = modl[8] * proj[0] + modl[9] * proj[4] + modl[10] * proj[8] + modl[11] * proj[12];
  688.         clip[9] = modl[8] * proj[1] + modl[9] * proj[5] + modl[10] * proj[9] + modl[11] * proj[13];
  689.         clip[10] = modl[8] * proj[2] + modl[9] * proj[6] + modl[10] * proj[10] + modl[11] * proj[14];
  690.         clip[11] = modl[8] * proj[3] + modl[9] * proj[7] + modl[10] * proj[11] + modl[11] * proj[15];
  691.  
  692.         clip[12] = modl[12] * proj[0] + modl[13] * proj[4] + modl[14] * proj[8] + modl[15] * proj[12];
  693.         clip[13] = modl[12] * proj[1] + modl[13] * proj[5] + modl[14] * proj[9] + modl[15] * proj[13];
  694.         clip[14] = modl[12] * proj[2] + modl[13] * proj[6] + modl[14] * proj[10] + modl[15] * proj[14];
  695.         clip[15] = modl[12] * proj[3] + modl[13] * proj[7] + modl[14] * proj[11] + modl[15] * proj[15];
  696.  
  697.         // ������ ��� ����� �������� ������� frustum'�. ����� ������� ���,
  698.         // ������� ���������� ���������, ���������� ����, � �� ��� ������� �������.
  699.  
  700.         m_Frustum[RIGHT] = V4{ clip[3] - clip[0], clip[7] - clip[4], clip[11] - clip[8], clip[15] - clip[12] }.Normal();
  701.         m_Frustum[LEFT] = V4{ clip[3] + clip[0], clip[7] + clip[4], clip[11] + clip[8], clip[15] + clip[12] }.Normal();
  702.  
  703.         m_Frustum[BOTTOM] = V4{ clip[3] + clip[1], clip[7] + clip[5], clip[11] + clip[9], clip[15] + clip[13] }.Normal();
  704.         m_Frustum[TOP] = V4{ clip[3] - clip[1], clip[7] - clip[5], clip[11] - clip[9], clip[15] - clip[13] }.Normal();
  705.  
  706.         m_Frustum[BACK] = V4{ clip[3] - clip[2], clip[7] - clip[6], clip[11] - clip[10], clip[15] - clip[14] }.Normal();
  707.         m_Frustum[FRONT] = V4{ clip[3] + clip[2], clip[7] + clip[6], clip[11] + clip[10], clip[15] + clip[14] }.Normal();
  708.     }
  709.  
  710.     bool Frustum::PointInFrustum(V4 p) {
  711.         for (int i = 0; i < 6; i++)
  712.             if (m_Frustum[i].x * p.x + m_Frustum[i].y * p.y + m_Frustum[i].z * p.z + m_Frustum[i].w <= 0)
  713.                 return false;
  714.         return true;
  715.     }
  716.  
  717.     bool Frustum::SphereInFrustum(V4 p, float r) {
  718.         for (int i = 0; i < 6; i++)
  719.             if (m_Frustum[i].x * p.x + m_Frustum[i].y * p.y + m_Frustum[i].z * p.z + m_Frustum[i].w <= -r)
  720.                 return false;
  721.         return true;
  722.     }
  723.  
  724.     bool Frustum::CubeInFrustum(V4 p, float size) {
  725.         for (int i = 0; i < 6; i++) {
  726.             if (m_Frustum[i].x * (p.x - size) + m_Frustum[i].y * (p.y - size) +
  727.                 m_Frustum[i].z * (p.z - size) + m_Frustum[i].w > 0)
  728.                 continue;
  729.             if (m_Frustum[i].x * (p.x + size) + m_Frustum[i].y * (p.y - size) +
  730.                 m_Frustum[i].z * (p.z - size) + m_Frustum[i].w > 0)
  731.                 continue;
  732.             if (m_Frustum[i].x * (p.x - size) + m_Frustum[i].y * (p.y + size) +
  733.                 m_Frustum[i].z * (p.z - size) + m_Frustum[i].w > 0)
  734.                 continue;
  735.             if (m_Frustum[i].x * (p.x + size) + m_Frustum[i].y * (p.y + size) +
  736.                 m_Frustum[i].z * (p.z - size) + m_Frustum[i].w > 0)
  737.                 continue;
  738.             if (m_Frustum[i].x * (p.x - size) + m_Frustum[i].y * (p.y - size) +
  739.                 m_Frustum[i].z * (p.z + size) + m_Frustum[i].w > 0)
  740.                 continue;
  741.             if (m_Frustum[i].x * (p.x + size) + m_Frustum[i].y * (p.y - size) +
  742.                 m_Frustum[i].z * (p.z + size) + m_Frustum[i].w > 0)
  743.                 continue;
  744.             if (m_Frustum[i].x * (p.x - size) + m_Frustum[i].y * (p.y + size) +
  745.                 m_Frustum[i].z * (p.z + size) + m_Frustum[i].w > 0)
  746.                 continue;
  747.             if (m_Frustum[i].x * (p.x + size) + m_Frustum[i].y * (p.y + size) +
  748.                 m_Frustum[i].z * (p.z + size) + m_Frustum[i].w > 0)
  749.                 continue;
  750.             return false;
  751.         }
  752.         return true;
  753.     }
  754.  
  755. #pragma endregion
  756.  
  757. #pragma region ������
  758.  
  759.     CFont::CFont(char *ttf, int FSize, float FDepth) {
  760.         char filename[100];
  761.         sprintf(filename, FONT_DIR, ttf);
  762.         if (GetFileAttributesA(filename) == DWORD(-1))
  763.             return;
  764.         this->Font = new FTGLBitmapFont(filename);
  765.         Font->Depth(FDepth);
  766.         Font->CharMap(ft_encoding_unicode);
  767.         Font->FaceSize(FSize);
  768.     }
  769.  
  770.     void CFont::Print(V4 p, V4 c, wstring _text) {
  771.         glPushMatrix();
  772.         glLoadIdentity();
  773.         glDisable(GL_TEXTURE_2D);
  774.         glTranslatef(p.x, p.y, -1);
  775.         glColor3fv(c);
  776.         glRasterPos2f(-1, 0.5);
  777.         Font->Render(_text.c_str());
  778.         glEnable(GL_TEXTURE_2D);
  779.         glPopMatrix();
  780.     }
  781.     void CFont::Printf(V4 p, V4 c, wstring fmt, ...) {
  782.         wchar_t text[256];
  783.         va_list ap;
  784.         va_start(ap, fmt);
  785.         vswprintf(text, fmt.c_str(), ap);
  786.         va_end(ap);
  787.         Print(p, c, text);
  788.     }
  789.  
  790. #pragma endregion
  791.  
  792. #pragma region �������
  793.  
  794.     Console::Console() {
  795.         funcs[L"exit"] = exitA;
  796.         funcs[L"setFPS"] = setFPS;
  797.     }
  798.     void Console::draw() {
  799.         if (!is_open)
  800.             return;
  801.         draw2DTexturedSquare({ 0.0f,0.0f }, { (float)MC->Width, 180.0f }, { 0.7f,0.7f,0.7f });
  802.         int j = 0;
  803.         for (int i = 9; i >= 0; --i)
  804.             if (stack[i].empty())
  805.                 continue;
  806.             else
  807.                 MC->arial_16->Print({ 10.0f, (++j) * 16.0f }, { 1.0f,1.0f,1.0f }, stack[i]);
  808.         MC->arial_16->Print({ 10.0f, (++j) * 16.0f }, { 1.0f,0.0f,1.0f }, MC->tmpStr);
  809.     }
  810.     void Console::toggle() {
  811.         MC->Pause = is_open = !is_open;
  812.         MC->pointerToCenter(is_open);
  813.     }
  814.     void Console::print(wstring s) {
  815.         stack[0].append(s);
  816.     }
  817.     void Console::println(wstring s) {
  818.         for (int i = 8; i >= 0; --i)
  819.             if (stack[i].empty())
  820.                 continue;
  821.             else
  822.                 stack[i + 1] = stack[i];
  823.         stack[0] = s;
  824.     }
  825.     void Console::parseln(wstring s) {
  826.         split(s, L" ", param);
  827.         if (funcs.find(param[0]) != funcs.end())
  828.             (*funcs[param[0]])();
  829.     }
  830.     Console Console::operator<<(wstring string) {
  831.         println(string);
  832.         return *this;
  833.     }
  834.  
  835. #pragma endregion
  836.  
  837. #pragma region ������� �������
  838.  
  839.     void exitA() {
  840.         glutLeaveMainLoop();
  841.     }
  842.     void toggle() {
  843.         MC->cout.toggle();
  844.     }
  845.     void setFPS() {
  846.         MC->setMaxFPS(stoi(MC->cout.param[1]));
  847.     }
  848.  
  849. #pragma endregion
  850.  
  851. #pragma region �����
  852.  
  853.     char **CommandLineToArgvA(PCHAR CmdLine, int* _argc) {
  854.         PCHAR* argv;
  855.         PCHAR  _argv;
  856.         ULONG   len;
  857.         ULONG   argc;
  858.         CHAR   a;
  859.         ULONG   i, j;
  860.  
  861.         BOOLEAN  in_QM;
  862.         BOOLEAN  in_TEXT;
  863.         BOOLEAN  in_SPACE;
  864.  
  865.         len = strlen(CmdLine);
  866.         i = (ULONG)((len + 2) / 2)*sizeof(PVOID) + (ULONG)sizeof(PVOID);
  867.  
  868.         argv = (PCHAR*)GlobalAlloc(GMEM_FIXED,
  869.             i + (len + 2)*sizeof(CHAR));
  870.  
  871.         _argv = (PCHAR)(((PUCHAR)argv) + i);
  872.  
  873.         argc = 0;
  874.         argv[argc] = _argv;
  875.         in_QM = FALSE;
  876.         in_TEXT = FALSE;
  877.         in_SPACE = TRUE;
  878.         i = 0;
  879.         j = 0;
  880.  
  881.         while (a = CmdLine[i]) {
  882.             if (in_QM) {
  883.                 if (a == '\"') {
  884.                     in_QM = FALSE;
  885.                 }
  886.                 else {
  887.                     _argv[j] = a;
  888.                     j++;
  889.                 }
  890.             }
  891.             else
  892.                 switch (a) {
  893.                 case '\"':
  894.                     in_QM = TRUE;
  895.                     in_TEXT = TRUE;
  896.                     if (in_SPACE) {
  897.                         argv[argc] = _argv + j;
  898.                         argc++;
  899.                     }
  900.                     in_SPACE = FALSE;
  901.                     break;
  902.                 case ' ':
  903.                 case '\t':
  904.                 case '\n':
  905.                 case '\r':
  906.                     if (in_TEXT) {
  907.                         _argv[j] = '\0';
  908.                         j++;
  909.                     }
  910.                     in_TEXT = FALSE;
  911.                     in_SPACE = TRUE;
  912.                     break;
  913.                 default:
  914.                     in_TEXT = TRUE;
  915.                     if (in_SPACE) {
  916.                         argv[argc] = _argv + j;
  917.                         argc++;
  918.                     }
  919.                     _argv[j] = a;
  920.                     j++;
  921.                     in_SPACE = FALSE;
  922.                     break;
  923.                 }
  924.             i++;
  925.         }
  926.         _argv[j] = '\0';
  927.         argv[argc] = NULL;
  928.  
  929.         (*_argc) = argc;
  930.         return argv;
  931.     }
  932.  
  933.     wchar_t keyboardRuToEng(wchar_t c) {
  934.         //Ru 2 En
  935.         switch (c) {
  936.         case 1081: c = 113; break;
  937.         case 1094: c = 119; break;
  938.         case 1091: c = 101; break;
  939.         case 1082: c = 114; break;
  940.         case 1077: c = 116; break;
  941.         case 1085: c = 121; break;
  942.         case 1075: c = 117; break;
  943.         case 1096: c = 105; break;
  944.         case 1097: c = 111; break;
  945.         case 1079: c = 112; break;
  946.         case 1093: c = 91; break;
  947.         case 1098: c = 93; break;
  948.  
  949.         case 1092: c = 97; break;
  950.         case 1099: c = 115; break;
  951.         case 1074: c = 100; break;
  952.         case 1072: c = 102; break;
  953.         case 1087: c = 103; break;
  954.         case 1088: c = 104; break;
  955.         case 1086: c = 106; break;
  956.         case 1083: c = 107; break;
  957.         case 1076: c = 108; break;
  958.         case 1078: c = 59; break;
  959.         case 1101: c = 39; break;
  960.  
  961.         case 1103: c = 122; break;
  962.         case 1095: c = 120; break;
  963.         case 1089: c = 99; break;
  964.         case 1084: c = 118; break;
  965.         case 1080: c = 98; break;
  966.         case 1090: c = 110; break;
  967.         case 1100: c = 109; break;
  968.         case 1073: c = 44; break;
  969.         case 1102: c = 46; break;
  970.         }
  971.  
  972.         return c;
  973.     }
  974.  
  975.     void split(wstring &s, const wchar_t* delim, vector<wstring> & v) {
  976.         wchar_t * dup = _wcsdup(s.c_str());
  977.         wchar_t * token = wcstok(dup, delim);
  978.         while (token != NULL) {
  979.             v.push_back(wstring(token));
  980.             token = wcstok(NULL, delim);
  981.         }
  982.         delete(dup);
  983.     }
  984.  
  985.     wstring format(wstring fmt, ...) {
  986.         if (fmt.empty())
  987.             return wstring();
  988.         wchar_t text[256];
  989.         va_list ap;
  990.         va_start(ap, fmt);
  991.         vswprintf(text, fmt.c_str(), ap);
  992.         va_end(ap);
  993.         return wstring(text);
  994.     }
  995.  
  996. #pragma endregion
  997.  
  998. #pragma region �������
  999.  
  1000.     void to2D() {
  1001.         glMatrixMode(GL_PROJECTION);
  1002.         glPushMatrix();
  1003.         glLoadIdentity();
  1004.         gluOrtho2D(0, MC->Width, MC->Height, 0);
  1005.         glMatrixMode(GL_MODELVIEW);
  1006.         glDisable(GL_DEPTH_TEST);
  1007.         glPushMatrix();
  1008.         glLoadIdentity();
  1009.     }
  1010.     void backTo3D() {
  1011.         glPopMatrix();
  1012.         glEnable(GL_DEPTH_TEST);
  1013.         glMatrixMode(GL_PROJECTION);
  1014.         glPopMatrix();
  1015.         glMatrixMode(GL_MODELVIEW);
  1016.     }
  1017.  
  1018.     void CalculateFrameRate() {
  1019.         static float framesPerSecond = 0.0f;
  1020.         static float lastTime = 0.0f;
  1021.         float currentTime = timeGetTime() * 0.001f;
  1022.         ++framesPerSecond;
  1023.         if (currentTime - lastTime > 1.0f) {
  1024.             lastTime = currentTime;
  1025.             MC->FPS = (int)framesPerSecond;
  1026.             framesPerSecond = 0;
  1027.         }
  1028.     }
  1029.  
  1030.     void draw2DTexturedSquare(V4 p1, V4 p2, V4 c, V4 tp1, V4 tp2) {
  1031.         glPushMatrix();
  1032.         glColor3f(c.x, c.y, c.z);
  1033.         glBegin(GL_POLYGON);
  1034.         glTexCoord2f(tp1.x, tp2.y); glVertex2f(p1.x, p1.y);
  1035.         glTexCoord2f(tp2.x, tp2.y); glVertex2f(p2.x, p1.y);
  1036.         glTexCoord2f(tp2.x, tp1.y); glVertex2f(p2.x, p2.y);
  1037.         glTexCoord2f(tp1.x, tp1.y); glVertex2f(p1.x, p2.y);
  1038.         glEnd();
  1039.         glPopMatrix();
  1040.     }
  1041.  
  1042.     GLuint LoadTexture(string dir, string F) {
  1043.         if (MC->textures.count(F))
  1044.             return MC->textures[F];
  1045.         GLuint id;
  1046.         ilGenImages(1, &id);
  1047.         ilBindImage(id);
  1048.  
  1049.         char filename[100];
  1050.         sprintf(filename, "%s\\Textures\\%s", dir.c_str(), F.c_str());
  1051.         if (!ilLoadImage((const wchar_t *)filename))
  1052.             return MC->textures["Default.jpg"];
  1053.  
  1054.         // If the image is flipped (i.e. upside-down and mirrored, flip it the right way up!)
  1055.         ILinfo ImageInfo;
  1056.         iluGetImageInfo(&ImageInfo);
  1057.         if (ImageInfo.Origin == IL_ORIGIN_UPPER_LEFT)
  1058.             iluFlipImage();
  1059.  
  1060.         if (!ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE))
  1061.             return MC->textures["Default.jpg"];
  1062.  
  1063.         glGenTextures(1, &id);
  1064.  
  1065.         glBindTexture(GL_TEXTURE_2D, id);
  1066.  
  1067.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  1068.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  1069.  
  1070.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1071.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  1072.  
  1073.         glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_FORMAT), ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 0,               // Border width in pixels (can either be 1 or 0)
  1074.             ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData());
  1075.  
  1076.         MC->textures[F] = id;
  1077.         return id;
  1078.     }
  1079.  
  1080.     GLuint LoadTexture(string F) {
  1081.         return LoadTexture(RESOURCE_DIR, F);
  1082.     }
  1083. #pragma endregion
  1084.  
  1085. #pragma region ������
  1086.  
  1087.     Camera::Camera() {
  1088.         View = V4{ 0.0f, 0.0f, 1.0f }.Normal();
  1089.         Up = V4{ 0.0f, 1.0f, 0.0f }.Normal();
  1090.         Strafe.Cross(View, Up).Normal();
  1091.         Sensitivity = 75.0f;
  1092.         Zoom = 0.0f, ZoomLimit = 30.0f;
  1093.         locked = needMouseZoom = false;
  1094.  
  1095.         setPosition({ 0.0f, 0.0f, 0.0f });
  1096.     }
  1097.  
  1098.     void Camera::FirstPersonView(int mx, int my) {
  1099.         if (locked)
  1100.             return;
  1101.         int middleX = MC->Width >> 1;
  1102.         int middleY = MC->Height >> 1;
  1103.  
  1104.         if ((mx == middleX) && (my == middleY)) return;
  1105.  
  1106.         glutWarpPointer(middleX, middleY);
  1107.  
  1108.         if (my != middleY) {
  1109.             V4 View1 = V4(Quaternion::rotate(View, Strafe, (float)atan((middleY - my) / Sensitivity))).Normal();
  1110.             float angle = AngleBetweenVectors(View1, Up);
  1111.             if (angle > M_PI_4 / 4 && angle < M_PI - M_PI_4 / 4)
  1112.                 View = View1;
  1113.         }
  1114.         if (mx != middleX) {
  1115.             float ty = View.y;
  1116.             View = V4(Quaternion::rotate(View, Up, (float)atan((middleX - mx) / Sensitivity))).Normal();
  1117.             View.y = ty;
  1118.         }
  1119.         Strafe.Cross(View, Up).Normal();
  1120.         ThirdPersonView();
  1121.     }
  1122.  
  1123.     void Camera::ThirdPersonView() {
  1124.         if (Zoom == 0.0f)
  1125.             LookPos = Position;
  1126.         else
  1127.             LookPos = Position - View*Zoom;
  1128.     }
  1129.  
  1130.     void Camera::setPosition(V4 _position) {
  1131.         Position = _position;
  1132.         ThirdPersonView();
  1133.     }
  1134.     void Camera::setViewTo(V4 viewTo) {
  1135.         View = (viewTo - Position).Normal();
  1136.         ThirdPersonView();
  1137.     }
  1138.  
  1139.     void Camera::lockView() {
  1140.         locked = true;
  1141.     }
  1142.     void Camera::unlockView() {
  1143.         locked = false;
  1144.     }
  1145.  
  1146.     void Camera::Look() {
  1147.         gluLookAt(LookPos.x, LookPos.y, LookPos.z,
  1148.             View.x + LookPos.x, View.y + LookPos.y, View.z + LookPos.z,
  1149.             Up.x, Up.y, Up.z);
  1150.     }
  1151.  
  1152. #pragma endregion
  1153.  
  1154. #pragma region ������
  1155.  
  1156.     struct V2 { float x, y; };
  1157.  
  1158.     Model::Model(char *modelfile, V4 _color) {
  1159.         color = _color;
  1160.         loadModel(modelfile);
  1161.     }
  1162.  
  1163.     void Model::loadModel(char *modelname) {
  1164.         char Mname[100];
  1165.         sprintf(Mname, MODEL_DIR, modelname);
  1166.         FILE *F = fopen(Mname, "rb");
  1167.         if (F == NULL) {
  1168.             div.v1 = { 0.0f,0.0f,0.0f };
  1169.             div.v2 = { 0.0f,0.0f,0.0f };
  1170.             return;
  1171.         }
  1172.         bool fileTypeIsdModel;
  1173.         fread(&fileTypeIsdModel, sizeof(bool), 1, F);
  1174.         if (!fileTypeIsdModel) {
  1175.             div.v1 = { 0.0f,0.0f,0.0f };
  1176.             div.v2 = { 0.0f,0.0f,0.0f };
  1177.             return;
  1178.         }
  1179.         fread(&divType, sizeof(unsigned short), 1, F);
  1180.         fread(&div, sizeof(Div), 1, F);
  1181.         bool isPart;
  1182.         fread(&isPart, sizeof(bool), 1, F);
  1183.         while (isPart) {
  1184.             ModelPart tmpmp;
  1185.             fread(&tmpmp.name, sizeof(char), 40, F);
  1186.             fread(&tmpmp.divType, sizeof(unsigned short), 1, F);
  1187.             fread(&tmpmp.div, sizeof(Div), 1, F);
  1188.             fread(&tmpmp.is_static, sizeof(bool), 1, F);
  1189.             unsigned short polygonCount = 0;
  1190.             fread(&polygonCount, sizeof(unsigned short), 1, F);
  1191.             for (int i = 0; i < polygonCount; ++i) {
  1192.                 PolygonS tmppo;
  1193.  
  1194.                 fread(&tmppo.p1, sizeof(V3), 1, F);
  1195.                 fread(&tmppo.p2, sizeof(V3), 1, F);
  1196.                 fread(&tmppo.p3, sizeof(V3), 1, F);
  1197.  
  1198.                 fread(&tmppo.normal, sizeof(V3), 1, F);
  1199.  
  1200.                 fread(&tmppo.texture, sizeof(char), 40, F);
  1201.  
  1202.                 if (tmppo.texture == "color") {
  1203.                     fread(&tmppo.tp1, sizeof(V3), 1, F);
  1204.                     fread(&tmppo.tp2, sizeof(V3), 1, F);
  1205.                     fread(&tmppo.tp3, sizeof(V3), 1, F);
  1206.                 }
  1207.                 else {
  1208.                     fread(&tmppo.tp1, sizeof(V2), 1, F);
  1209.                     fread(&tmppo.tp2, sizeof(V2), 1, F);
  1210.                     fread(&tmppo.tp3, sizeof(V2), 1, F);
  1211.                 }
  1212.  
  1213.                 tmpmp.polygons.push_back(tmppo);
  1214.             }
  1215.             if (tmpmp.is_static)
  1216.                 buildPart(&tmpmp);
  1217.             parts.push_back(tmpmp);
  1218.             fread(&isPart, sizeof(bool), 1, F);
  1219.         }
  1220.         fclose(F);
  1221.         calculateDiv();
  1222.     }
  1223.  
  1224.     void Model::calculateDiv() {
  1225.         div.v1.set(100.0f);
  1226.         div.v2.set(0.0f);
  1227.         for (int i = 0; i < (int)parts.size(); ++i) {
  1228.             parts[i].div.v1.set(100.0f);
  1229.             parts[i].div.v2.set(0.0f);
  1230.             for (PolygonS poly : parts[i].polygons) {
  1231.                 parts[i].div.v2.maxXYZ(poly.p1); parts[i].div.v1.minXYZ(poly.p1);
  1232.                 parts[i].div.v2.maxXYZ(poly.p2); parts[i].div.v1.minXYZ(poly.p2);
  1233.                 parts[i].div.v2.maxXYZ(poly.p3); parts[i].div.v1.minXYZ(poly.p3);
  1234.             }
  1235.  
  1236.             div.v1.minXYZ(parts[i].div.v1);
  1237.             div.v2.maxXYZ(parts[i].div.v2);
  1238.         }
  1239.         div.v1.w = max3(div.v2.x - div.v1.x, div.v2.y - div.v1.y, div.v2.z - div.v1.z) / 2.0f;
  1240.     }
  1241.  
  1242.     void Model::drawPart(ModelPart *mpart) {
  1243.         glColor3f(V4toxyz(color));
  1244.         glBegin(GL_TRIANGLES);
  1245.         for (PolygonS poly : mpart->polygons) {
  1246.  
  1247.             if (poly.texture == "color") {
  1248.                 glNormal3fv(poly.normal);
  1249.                 glColor3fv(poly.tp1);
  1250.                 glVertex3fv(poly.p1);
  1251.  
  1252.                 glNormal3fv(poly.normal);
  1253.                 glColor3fv(poly.tp2);
  1254.                 glVertex3fv(poly.p2);
  1255.  
  1256.                 glNormal3fv(poly.normal);
  1257.                 glColor3fv(poly.tp3);
  1258.                 glVertex3fv(poly.p3);
  1259.             }
  1260.             else {
  1261.                 //glBindTexture(GL_TEXTURE_2D, Textures[models[modelId]->polygon[i].texId].texID);
  1262.                 glNormal3fv(poly.normal);
  1263.                 glTexCoord2fv(poly.tp1);
  1264.                 glVertex3fv(poly.p1);
  1265.  
  1266.                 glNormal3fv(poly.normal);
  1267.                 glTexCoord2fv(poly.tp2);
  1268.                 glVertex3fv(poly.p2);
  1269.  
  1270.                 glNormal3fv(poly.normal);
  1271.                 glTexCoord2fv(poly.tp3);
  1272.                 glVertex3fv(poly.p3);
  1273.             }
  1274.         }
  1275.         glEnd();
  1276.         glBindTexture(GL_TEXTURE_2D, -1);
  1277.     }
  1278.  
  1279.     void Model::buildPart(ModelPart *mpart) {
  1280.         int list_id = glGenLists(1);
  1281.         glNewList(list_id, GL_COMPILE);
  1282.         drawPart(mpart);
  1283.         glEndList();
  1284.         mpart->listId = list_id;
  1285.     }
  1286.  
  1287.     void Model::drawModel(V4 position) {
  1288.         glPushMatrix();
  1289.         glTranslatef(V4toxyz(position));
  1290.         if (MC->debug.showModel) {
  1291.             for (ModelPart mp : parts)
  1292.                 if (mp.is_static)
  1293.                     glCallList(mp.listId);
  1294.                 else
  1295.                     drawPart(&mp);
  1296.         }
  1297.         if (MC->debug.showNormal) {
  1298.             glBegin(GL_LINES);
  1299.             for (ModelPart mp : parts)
  1300.                 for (PolygonS p : mp.polygons) {
  1301.                     V4 tmpvstart = (p.p1 + p.p2 + p.p3) / 3;
  1302.                     V4 tmlvend = tmpvstart + p.normal;
  1303.                     glColor3f(1.0f, 1.0f, 1.0f);
  1304.                     glVertex3f(V4toxyz(tmpvstart));
  1305.                     glVertex3f(V4toxyz(tmlvend));
  1306.                 }
  1307.             glEnd();
  1308.         }
  1309.         if (MC->debug.showDiv) {
  1310.             glColor3f(1.0f, 1.0f, 1.0f);
  1311.             if (divType == DIV_TYPE_AABB) {
  1312.                 glBegin(GL_LINE_STRIP);
  1313.                 glVertex3f(div.v1.x, div.v1.y, div.v1.z);
  1314.                 glVertex3f(div.v2.x, div.v1.y, div.v1.z);
  1315.                 glVertex3f(div.v2.x, div.v1.y, div.v2.z);
  1316.                 glVertex3f(div.v1.x, div.v1.y, div.v2.z);
  1317.  
  1318.                 glVertex3f(div.v1.x, div.v1.y, div.v1.z);
  1319.                 glVertex3f(div.v1.x, div.v2.y, div.v1.z);
  1320.                 glVertex3f(div.v2.x, div.v2.y, div.v1.z);
  1321.                 glVertex3f(div.v2.x, div.v1.y, div.v1.z);
  1322.                 glEnd();
  1323.  
  1324.                 glBegin(GL_LINE_STRIP);
  1325.                 glVertex3f(div.v2.x, div.v2.y, div.v2.z);
  1326.                 glVertex3f(div.v1.x, div.v2.y, div.v2.z);
  1327.                 glVertex3f(div.v1.x, div.v2.y, div.v1.z);
  1328.                 glVertex3f(div.v2.x, div.v2.y, div.v1.z);
  1329.  
  1330.                 glVertex3f(div.v2.x, div.v2.y, div.v2.z);
  1331.                 glVertex3f(div.v2.x, div.v1.y, div.v2.z);
  1332.                 glVertex3f(div.v1.x, div.v1.y, div.v2.z);
  1333.                 glVertex3f(div.v1.x, div.v2.y, div.v2.z);
  1334.                 glEnd();
  1335.             }
  1336.             else if (divType == DIV_TYPE_SPHERE) {
  1337.                 glutWireSphere(div.v1.w / 2, 10, 10);
  1338.             }
  1339.         }
  1340.         glPopMatrix();
  1341.     }
  1342.     /*
  1343.     bool    fileTypeIsdModel
  1344.     ushort  divType
  1345.     Div div
  1346.  
  1347.     bool    isPart
  1348.     [isPart]{
  1349.         char-40 Part.name
  1350.         ushort  Part.divType
  1351.         Div Part.div
  1352.         bool    is_static
  1353.         ushort  Part.polygonCount{
  1354.             V3  Poly.p1,p2,p3
  1355.             V3  Poly.normal
  1356.             char-40 Poly.texture
  1357.             [Poly.texture == "color"]{
  1358.             V3  Poly.tp1,tp2,tp3
  1359.             }{
  1360.             V2  Poly.tp1,tp2,tp3
  1361.             }
  1362.         }
  1363.         bool    isPart
  1364.     }
  1365.         */
  1366. #pragma endregion
  1367.  
  1368. #pragma region ������
  1369.  
  1370.     Physics::Physics() {
  1371.  
  1372.     }
  1373.     Physics::Physics(float _g) {
  1374.         g = _g;
  1375.     }
  1376.     bool Physics::getIntersection(Object o1, Object o2) {
  1377.  
  1378.         return false;
  1379.     }
  1380.  
  1381. #pragma endregion
  1382.  
  1383. #pragma region ������
  1384.  
  1385.     Object::Object(float _mass, V4 _pos, char *modelfile) {
  1386.         model = new Model(modelfile, { 1.0f,1.0f,0.0f });
  1387.         mass = _mass;
  1388.         position = _pos;
  1389.     }
  1390.  
  1391.     void Object::setPreTickFunc(void f()) { preTick = f; }
  1392.     void Object::setPreDrawFunc(void f()) { preDraw = f; }
  1393.  
  1394.     void Object::applyVelocity(V4 _velocity) {
  1395.         velocity += _velocity;
  1396.     }
  1397.     void Object::applyForce(V4 _force) {
  1398.         force += _force;
  1399.     }
  1400.  
  1401.     void Object::applyGravity() {
  1402.         applyForce({ 0.0f, -mass * MC->physics->g, 0.0f });
  1403.     }
  1404.     void Object::applyFriction() {
  1405.         velocity -= velocity / 20;
  1406.         if (velocity.GetLength() < 0.5f)
  1407.             velocity = { 0.0f,0.0f,0.0f };
  1408.     }
  1409.  
  1410.     void Object::tick(float dt) {
  1411.         if (preTick != 0)preTick();
  1412.         applyGravity();
  1413.         applyFriction();
  1414.  
  1415.         velocity += ((force) / mass) * dt;
  1416.         V4 lastPos = position;
  1417.         position += velocity * dt;
  1418.         force = { 0.0f, 0.0f, 0.0f };
  1419.  
  1420.         if (position.y < 0.0f) {
  1421.             position.y = 0.0f;
  1422.             velocity.y = 0.0f;
  1423.         }
  1424.         moving = position - lastPos;
  1425.     }
  1426.  
  1427.     void Object::tick() { tick(MC->dT); }
  1428.  
  1429.     void Object::draw() {
  1430.         model->drawModel(position);
  1431.     }
  1432.  
  1433. #pragma endregion
  1434.  
  1435. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement