Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "DVSEngine.h"
- namespace DVSEngine {
- MAIN *MC;
- #pragma region MAIN
- #pragma region MAINDestructor
- MAIN *MAIN::p_instance = 0;
- MAINDestructor MAIN::destroyer;
- MAINDestructor::~MAINDestructor() {
- delete p_instance;
- }
- void MAINDestructor::initialize(MAIN *p) {
- p_instance = p;
- }
- #pragma endregion
- MAIN *MAIN::getInstance() {
- if (!p_instance) {
- p_instance = new MAIN();
- destroyer.initialize(p_instance);
- }
- MC = p_instance;
- return p_instance;
- }
- void MAIN::setWindow(int *argcp, char **argv) {
- glutInit_ATEXIT_HACK(argcp, argv);
- glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE);
- }
- char **MAIN::setWindow(char *exeName, LPSTR cmdLine, int *argc) {
- char **ret = CommandLineToArgvA(cmdLine, argc);
- char **argv = new char*[*argc + 1];
- argv[0] = exeName;
- for (int i = 0; i < *argc; ++i)
- argv[i + 1] = ret[i];
- setWindow(argc, argv);
- return ret;
- }
- void MAIN::createWindow(char *title, V4 bounds) {
- glutInitWindowPosition((int)bounds.x, (int)bounds.y);
- glutInitWindowSize((int)bounds.z, (int)bounds.w);
- glutCreateWindow_ATEXIT_HACK(title);
- }
- void MAIN::Init(void _In()) {
- glutDisplayFunc(DisplayF);
- glutReshapeFunc(ReshapeF);
- glutMouseFunc(MouseF);
- glutMotionFunc(ActionMouseF);
- glutPassiveMotionFunc(PassiveMouseF);
- glutKeyboardFunc(KeyPressF);
- glutKeyboardUpFunc(KeyReleaseF);
- glutSpecialFunc(KeySpecPressF);
- glutSpecialUpFunc(KeySpecReleaseF);
- glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- glClearDepth(1.0);
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_TEXTURE_2D);
- glDepthFunc(GL_LEQUAL);
- glEnable(GL_LINE_SMOOTH);
- glEnable(GL_BLEND);
- glShadeModel(GL_SMOOTH);
- glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
- glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- ilInit();
- iluInit();
- LoadTexture("Default.jpg");
- arial_16 = new CFont("arial.ttf", 16, 16.0f);
- camera = new Camera();
- physics = new Physics(9.8f);
- setMaxFPS(60);
- if (_In != 0)_In();
- }
- void MAIN::Start() {
- IdleF();
- glutMainLoop();
- }
- #pragma region GLUT �������
- void IdleF() {
- if (MC->Pause) {
- }
- else {
- for (Object *o : MC->objs)
- o->tick();
- MC->time += MC->dT;
- }
- if (MC->I != 0)MC->I();
- glutTimerFunc(MC->ThoudevFps, IdleF, 0);
- }
- void DisplayF() {
- CalculateFrameRate();
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- if (MC->D3d != 0)MC->D3d();
- for (Object *o : MC->objs)
- o->draw();
- to2D();
- MC->cout.draw();
- if (MC->D2d != 0)MC->D2d();
- backTo3D();
- glutSwapBuffers();
- glutTimerFunc(MC->ThoudevFps, DisplayF, 0);
- }
- void ReshapeF(int width, int height) {
- if (height == 0)
- height = 1;
- MC->Width = width, MC->Height = height;
- float ratio = width * 1.0f / height;
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glViewport(0, 0, width, height);
- gluPerspective(45.0f, ratio, 0.1f, 100.0f);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- void MouseF(int button, int state, int x, int y) {
- MC->mouse = { x,y };
- if (!MC->Pause) {
- if (MC->camera->needMouseZoom) {
- if (button == 3) {
- MC->camera->Zoom -= 5.0f;
- MC->camera->Zoom = max(MC->camera->Zoom, 0.0f);
- }
- else if (button == 4) {
- MC->camera->Zoom += 5.0f;
- MC->camera->Zoom = min(MC->camera->Zoom, MC->camera->ZoomLimit);
- }
- }
- }
- if (MC->M != 0)MC->M(button, state, x, y);
- }
- void ActionMouseF(int x, int y) {
- MC->mouse = { x,y };
- if (MC->Ma != 0)MC->Ma(x, y);
- }
- void PassiveMouseF(int x, int y) {
- MC->mouse = { x,y };
- if (MC->Mp != 0)MC->Mp(x, y);
- }
- void KeyPressF(byte ch, int x, int y) {
- wchar_t c = (ch >= 192 && ch <= 255) ? ch + 848 : (ch <= 26) ? ch + 96 : ch;
- if (MC->Pause) {
- if (ch == 13) {
- if (MC->tmpStr.empty())
- return;
- MC->cout.println(MC->tmpStr);
- MC->cout.parseln(MC->tmpStr);
- MC->tmpStr.clear();
- }
- else if (ch == '\b') {
- MC->tmpStr.pop_back();
- }
- else {
- MC->tmpStr.push_back(c);
- if (MC->tmpStr.size() > 100) {
- for (short i = 1; i <= 100; ++i)
- MC->tmpStr[i - 1] = MC->tmpStr[i];
- MC->tmpStr.resize(100);
- }
- }
- }
- else {
- c = keyboardRuToEng(c);
- MC->kbd[c] = true;
- }
- if (MC->Kp != 0)MC->Kp(ch, x, y);
- }
- void KeyReleaseF(byte ch, int x, int y) {
- wchar_t c = (ch >= 192 && ch <= 255) ? ch + 848 : (ch <= 26) ? ch + 96 : ch;
- if (MC->Pause) {
- }
- else {
- c = keyboardRuToEng(c);
- MC->kbd[c] = false;
- }
- if (MC->Kr != 0)MC->Kr(ch, x, y);
- }
- void KeySpecPressF(int ch, int x, int y) {
- switch (ch) {
- case GLUT_KEY_SHIFT_L:
- case GLUT_KEY_SHIFT_R:
- MC->is_shift = true;
- break;
- case GLUT_KEY_CTRL_L:
- case GLUT_KEY_CTRL_R:
- MC->is_ctrl = true;
- break;
- case GLUT_KEY_ALT_L:
- case GLUT_KEY_ALT_R:
- MC->is_alt = true;
- break;
- }
- switch (ch) {
- case GLUT_KEY_UP:
- MC->tmpStr = MC->cout.stack[0];
- break;
- case GLUT_KEY_F4:
- if (MC->is_alt)
- glutLeaveMainLoop();
- break;
- case GLUT_KEY_F11:
- glutFullScreenToggle();
- break;
- case GLUT_KEY_F12:
- MC->cout.toggle();
- break;
- }
- if (MC->Ksp != 0)MC->Ksp(ch, x, y);
- }
- void KeySpecReleaseF(int ch, int x, int y) {
- switch (ch) {
- case GLUT_KEY_SHIFT_L:
- case GLUT_KEY_SHIFT_R:
- MC->is_shift = false;
- break;
- case GLUT_KEY_CTRL_L:
- case GLUT_KEY_CTRL_R:
- MC->is_ctrl = false;
- break;
- case GLUT_KEY_ALT_L:
- case GLUT_KEY_ALT_R:
- MC->is_alt = false;
- break;
- }
- if (MC->Ksr != 0)MC->Ksr(ch, x, y);
- }
- #pragma endregion
- #pragma region ���. �������
- void MAIN::pointerToCenter(bool visibleCursor) {
- glutWarpPointer(Width >> 1, Height >> 1);
- glutSetCursor((visibleCursor) ? GLUT_CURSOR_LEFT_ARROW : GLUT_CURSOR_NONE);
- }
- void MAIN::setMaxFPS(int maxFPS) {
- FPSlimit = maxFPS;
- dT = FPSlimit / 1000.0f;
- ThoudevFps = 1000 / FPSlimit;
- }
- #pragma endregion
- #pragma endregion
- #pragma region ����������
- #pragma region ������ �������������� �������
- short sign(int a) {
- return (a > 0) ? 1 : ((a < 0) ? -1 : a);
- }
- float Distance(V4 vPoint1, V4 vPoint2) {
- return sqrt((vPoint2.x - vPoint1.x) * (vPoint2.x - vPoint1.x) +
- (vPoint2.y - vPoint1.y) * (vPoint2.y - vPoint1.y) +
- (vPoint2.z - vPoint1.z) * (vPoint2.z - vPoint1.z));
- }
- float PlaneDistance(V4 Normal, V4 Point) {
- return -((Normal.x * Point.x) + (Normal.y * Point.y) + (Normal.z * Point.z));
- }
- V4 ClosestPointOnLine(V4 vA, V4 vB, V4 vPoint) {
- V4 vV = vB - vA;
- vV.Normal();
- float d = Distance(vA, vB);
- float t = vV.DotProduct3(vPoint - vA);
- if (t <= 0)
- return vA;
- if (t >= d)
- return vB;
- return vA + vV * t;
- }
- V4 ClosestPointOnPlane(PolygonS p, V4 vPoint) {
- return vPoint - p.normal * PlaneDistance(p.normal, vPoint);
- }
- float AngleBetweenVectors(V4 Vector1, V4 Vector2) {
- float dotProduct = Vector1.DotProduct3(Vector2);
- float vectorsMagnitude = Vector1.GetLength() * Vector2.GetLength();
- float angle = acos(dotProduct / vectorsMagnitude);
- if (_isnan(angle))
- return 0;
- return angle;
- }
- V4 getBezierPoint(vector<V4> pts, float t) {
- int i, c;
- float p;
- V4 np;
- int n = static_cast<int>(pts.size());
- for (i = 0, c = 1; i < n; ++i) {
- pts[i].x = pts[i].x * c;
- pts[i].y = pts[i].y * c;
- pts[i].z = pts[i].z * c;
- c = (n - i)*c / (i + 1);
- }
- for (i = 0, p = 1; i < n; ++i) {
- pts[i].x = pts[i].x * p;
- pts[i].y = pts[i].y * p;
- pts[i].z = pts[i].z * p;
- p = p * t;
- }
- for (i = n - 1, p = 1; i >= 0; --i) {
- pts[i].x = pts[i].x * p;
- pts[i].y = pts[i].y * p;
- pts[i].z = pts[i].z * p;
- p = p * (1 - t);
- }
- for (i = 0; i < n; ++i) {
- np.x = np.x + pts[i].x;
- np.y = np.y + pts[i].y;
- np.z = np.z + pts[i].z;
- }
- return np;
- }
- #pragma endregion
- #pragma region ������� ��� ��������
- bool InsidePolygon(V4 vIntersection, PLANE poly) {
- const double MATCH_FACTOR = 0.9999;
- double Angle = 0.0;
- Angle += AngleBetweenVectors(poly.p1 - vIntersection, poly.p2 - vIntersection);
- Angle += AngleBetweenVectors(poly.p2 - vIntersection, poly.p3 - vIntersection);
- Angle += AngleBetweenVectors(poly.p3 - vIntersection, poly.p1 - vIntersection);
- if (Angle >= (MATCH_FACTOR * (2.0 * M_PI)))
- return true;
- return false;
- }
- bool ClassifySphere(SPHERE Sph, PLANE poly, float &distance) {
- float pD = PlaneDistance(poly.normal, poly.p1);
- distance = abs((poly.normal.x * Sph.position.x + poly.normal.y * Sph.position.y + poly.normal.z * Sph.position.z + pD));
- return distance < Sph.radius;
- }
- bool ClassifyPoint(V4 TestV, PLANE poly, float &distance) {
- return ClassifySphere({ TestV,0.09f }, poly, distance);
- }
- float SquaredDistPointAABB(V4 pointV, BOX aabb) {
- auto check = [&](float pn, float bmin, float bmax) -> float {
- float out = 0;
- if (pn < bmin)
- out += (bmin - pn)*(bmin - pn);
- if (pn > bmax)
- out += (pn - bmax)*(pn - bmax);
- return out;
- };
- return check(pointV.x, aabb.minV.x, aabb.maxV.x) +
- check(pointV.y, aabb.minV.y, aabb.maxV.y) +
- check(pointV.z, aabb.minV.z, aabb.maxV.z);
- }
- bool LineSphereCollision(V4 linep1, V4 linep2, SPHERE Sph) {
- if (Distance(ClosestPointOnLine(linep1, linep2, Sph.position), Sph.position) < Sph.radius)
- return true;
- return false;
- }
- bool EdgeSphereCollision(SPHERE Sph, PLANE Pla) {
- if (LineSphereCollision(Pla.p1, Pla.p2, Sph))
- return true;
- if (LineSphereCollision(Pla.p2, Pla.p3, Sph))
- return true;
- if (LineSphereCollision(Pla.p3, Pla.p1, Sph))
- return true;
- return false;
- }
- bool PointInAABB(V4 PointV, BOX tBox) {
- return(PointV.x > tBox.minV.x && PointV.x < tBox.maxV.x &&
- PointV.y > tBox.minV.y && PointV.y < tBox.maxV.y &&
- PointV.z > tBox.minV.z && PointV.z < tBox.maxV.z);
- }
- #pragma endregion
- #pragma endregion
- #pragma region V4
- V4::V4() { x = y = z = 0.0; }
- V4::V4(float X, float Y) { x = X; y = Y; }
- V4::V4(float X, float Y, float Z) { x = X; y = Y; z = Z; }
- V4::V4(int X, int Y) { x = (float)X; y = (float)Y; }
- V4::V4(int X, int Y, int Z) { x = (float)X; y = (float)Y; z = (float)Z; }
- V4::V4(float X, float Y, float Z, float W) { x = X; y = Y; z = Z; w = W; }
- V4::V4(const V4 &v) { x = v.x; y = v.y; z = v.z; w = v.w; }
- V4::V4(const V3 &v) { x = v.x; y = v.y; z = v.z; }
- V4::V4(const POINT &v) { x = (float)v.x; y = (float)v.y; }
- V4::operator V3() { return V3(x, y, z); }
- V4::operator float*() { return new float[3]{ x,y,z }; }
- void V4::operator =(V4 v) { x = v.x; y = v.y; z = v.z; w = v.w; }
- V4 V4::operator -() { return (*this)*-1; }
- V4 V4::operator -(V4 v) { return V4(x - v.x, y - v.y, z - v.z); }
- V4 V4::operator +(V4 v) { return V4(x + v.x, y + v.y, z + v.z); }
- V4 V4::operator *(V4 v) { return V4(x * v.x, y * v.y, z * v.z); }
- V4 V4::operator /(V4 v) { return V4(x / v.x, y / v.y, z / v.z); }
- V4 V4::operator +(float f) { return V4(x + f, y + f, z + f); }
- V4 V4::operator -(float f) { return V4(x - f, y - f, z - f); }
- V4 V4::operator *(float f) { return V4(x * f, y * f, z * f); }
- V4 V4::operator /(float f) { f = 1 / f; return V4(x * f, y * f, z * f); }
- void V4::operator +=(V4 v) { x += v.x; y += v.y; z += v.z; w += v.w; }
- void V4::operator -=(V4 v) { x -= v.x; y -= v.y; z -= v.z; w -= v.w; }
- void V4::operator *=(V4 v) { x *= v.x; y *= v.y; z *= v.z; w *= v.w; }
- void V4::operator /=(V4 v) { x /= v.x; y /= v.y; z /= v.z; w /= v.w; }
- void V4::operator +=(float f) { x += f; y += f; z += f; z += f; }
- void V4::operator -=(float f) { x -= f; y -= f; z -= f; z -= f; }
- void V4::operator *=(float f) { x *= f; y *= f; z *= f; z *= f; }
- void V4::operator /=(float f) { f = 1 / f; x *= f; y *= f; z *= f; w *= f; }
- bool V4::operator ==(V4 v) { return ((x == v.x) && (y == v.y) && (z == v.z)); }
- bool V4::operator !=(V4 v) { return !((x == v.x) && (y == v.y) && (z == v.z)); }
- V4 V4::Cross(V4 v1, V4 v2) {
- x = ((v1.y * v2.z) - (v1.z * v2.y));
- y = ((v1.z * v2.x) - (v1.x * v2.z));
- z = ((v1.x * v2.y) - (v1.y * v2.x));
- return *this;
- }
- V4 V4::Cross(V4 v1, V4 v2, V4 v3) {
- 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;
- 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;
- 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;
- 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;
- return *this;
- }
- float V4::DotProduct3(V4 v1) { return x * v1.x + y * v1.y + z * v1.z; }
- float V4::DotProduct4(V4 v1) { return x * v1.x + y * v1.y + z * v1.z + w * v1.w; }
- float V4::GetLength() { return (float)sqrt((x * x + y * y + z * z)); }
- V4 V4::Normal() {
- float lenght = GetLength();
- if (lenght == 0.0f)
- lenght = 1.0f;
- *this /= lenght;
- return *this;
- }
- void V4::Normalize(V4 Triangle[]) {
- V4 v1, v2;
- v1.x = Triangle[0].x - Triangle[1].x;
- v1.y = Triangle[0].y - Triangle[1].y;
- v1.z = Triangle[0].z - Triangle[1].z;
- v1.w = Triangle[0].w - Triangle[1].w;
- v2.x = Triangle[1].x - Triangle[2].x;
- v2.y = Triangle[1].y - Triangle[2].y;
- v2.z = Triangle[1].z - Triangle[2].z;
- v2.w = Triangle[1].w - Triangle[2].w;
- Cross(v1, v2);
- Normal();
- }
- V4 V4::GetRotatedX(float angle) {
- float sinAngle = (float)sin(M_PI * angle / 180);
- float cosAngle = (float)cos(M_PI * angle / 180);
- return V4(x, y * cosAngle - z * sinAngle, y * sinAngle + z * cosAngle, w);
- }
- V4 V4::GetRotatedY(float angle) {
- float sinAngle = (float)sin(M_PI * angle / 180);
- float cosAngle = (float)cos(M_PI * angle / 180);
- return V4(x * cosAngle + z * sinAngle, y, -x * sinAngle + z * cosAngle, w);
- }
- V4 V4::GetRotatedZ(float angle) {
- float sinAngle = (float)sin(M_PI * angle / 180);
- float cosAngle = (float)cos(M_PI * angle / 180);
- return V4(x * cosAngle - y * sinAngle, x * sinAngle + y * cosAngle, z, w);
- }
- V4 V4::GetRotatedAxis(float angle, V4 axis) {
- if (angle == 0.0) return(*this);
- axis.Normal();
- V4 RotationRow1, RotationRow2, RotationRow3;
- double newAngle = M_PI * angle / 180;
- float sinAngle = (float)sin(newAngle);
- float cosAngle = (float)cos(newAngle);
- float oneSubCos = 1.0f - cosAngle;
- RotationRow1.x = (axis.x) * (axis.x) + cosAngle * (1 - (axis.x) * (axis.x));
- RotationRow1.y = (axis.x) * (axis.y) * (oneSubCos)-sinAngle * axis.z;
- RotationRow1.z = (axis.x) * (axis.z) * (oneSubCos)+sinAngle * axis.y;
- RotationRow2.x = (axis.x) * (axis.y) * (oneSubCos)+sinAngle * axis.z;
- RotationRow2.y = (axis.y) * (axis.y) + cosAngle * (1 - (axis.y) * (axis.y));
- RotationRow2.z = (axis.y) * (axis.z) * (oneSubCos)-sinAngle * axis.x;
- RotationRow3.x = (axis.x) * (axis.z) * (oneSubCos)-sinAngle * axis.y;
- RotationRow3.y = (axis.y) * (axis.z) * (oneSubCos)+sinAngle * axis.x;
- RotationRow3.z = (axis.z) * (axis.z) + cosAngle * (1 - (axis.z) * (axis.z));
- return V4(this->DotProduct3(RotationRow1),
- this->DotProduct3(RotationRow2),
- this->DotProduct3(RotationRow3));
- }
- void V4::maxXYZ(V4 v) { x = max(x, v.x); y = max(y, v.y); z = max(z, v.z); }
- void V4::minXYZ(V4 v) { x = min(x, v.x); y = min(y, v.y); z = min(z, v.z); }
- void V4::set(float f) { x = y = z = w = f; }
- #pragma endregion
- #pragma region Quaternion
- Quaternion::Quaternion() {
- }
- Quaternion::Quaternion(V4 v3) {
- v.x = v3.x;
- v.y = v3.y;
- v.z = v3.z;
- v.w = 0.0f;
- }
- Quaternion::Quaternion(V4 v, float degrees, bool is_radian) {
- if (is_radian)CreateFromAxisRadian(v, degrees);
- else CreateFromAxisAngle(v, degrees);
- }
- void Quaternion::CreateFromAxisAngle(V4 v3, float degrees) {
- float angle = float(degrees / 180.0f * M_PI);
- float result = float(sin(angle / 2.0f));
- v.x = float(v3.x * result);
- v.y = float(v3.y * result);
- v.z = float(v3.z * result);
- v.w = float(cos(angle / 2.0f));
- }
- void Quaternion::CreateFromAxisRadian(V4 v3, float angle) {
- float result = float(sin(angle / 2.0f));
- v.x = float(v3.x * result);
- v.y = float(v3.y * result);
- v.z = float(v3.z * result);
- v.w = float(cos(angle / 2.0f));
- }
- Quaternion* Quaternion::mul(Quaternion *q) {
- Quaternion *r = new Quaternion();
- float A = (v.w + v.x)*(q->v.w + q->v.x);
- float B = (v.z - v.y)*(q->v.y - q->v.z);
- float C = (v.x - v.w)*(q->v.y + q->v.z);
- float D = (v.y + v.z)*(q->v.x - q->v.w);
- float E = (v.x + v.z)*(q->v.x + q->v.y);
- float F = (v.x - v.z)*(q->v.x - q->v.y);
- float G = (v.w + v.y)*(q->v.w - q->v.z);
- float H = (v.w - v.y)*(q->v.w + q->v.z);
- r->v.w = B + (-E - F + G + H) * 0.5f;
- r->v.x = A - (E + F + G + H) * 0.5f;
- r->v.y = -C + (E - F + G - H) * 0.5f;
- r->v.z = -D + (E - F - G + H) * 0.5f;
- return r;
- }
- Quaternion* Quaternion::sum(Quaternion *q) {
- return new Quaternion(v + q->v, v.w + q->v.w);
- }
- float Quaternion::QMagnitude() {
- return (float)sqrt(v.GetLength() + v.w*v.w);
- }
- Quaternion* Quaternion::Inverse() {
- return new Quaternion(-v, v.w);/////////////////////////
- }
- Quaternion::operator V4() { return v; }
- Quaternion Quaternion::rotate(Quaternion *q, Quaternion *p) {
- Quaternion *mul1 = p->mul(q);
- Quaternion *invQ = p->Inverse();
- Quaternion *mul2 = mul1->mul(invQ);
- Quaternion ret = *mul2;
- delete(invQ);
- delete(mul1);
- delete(mul2);
- return ret;
- }
- Quaternion Quaternion::rotate(V4 qv) {
- Quaternion *q = new Quaternion(qv);
- Quaternion ret = rotate(q, this);
- delete(q);
- return ret;
- }
- Quaternion Quaternion::rotate(Quaternion q) {
- return rotate(q, this);
- }
- Quaternion Quaternion::rotate(Quaternion *q, V4 pv, float a, bool i) {
- Quaternion *p = new Quaternion(pv, a, i);
- Quaternion ret = rotate(q, p);
- delete(p);
- return ret;
- }
- Quaternion Quaternion::rotate(V4 qv, Quaternion *p) {
- Quaternion *q = new Quaternion(qv);
- Quaternion ret = rotate(q, p);
- delete(q);
- return ret;
- }
- Quaternion Quaternion::rotate(V4 qv, V4 pv, float a, bool i) {
- Quaternion *q = new Quaternion(qv);
- Quaternion *p = new Quaternion(pv, a, i);
- Quaternion ret = rotate(q, p);
- delete(q);
- delete(p);
- return ret;
- }
- #pragma endregion
- #pragma region Frustum
- void Frustum::CalculateFrustum() {
- float proj[16]; // ������� ��������
- float modl[16]; // ������� �������
- float clip[16]; // ��������� ���������
- glGetFloatv(GL_PROJECTION_MATRIX, proj);
- // ��������� GL_MODELVIEW_MATRIX, �� ����������� ���������� � ������� �������.
- // ��� ����� ���������� � ������� [16].
- glGetFloatv(GL_MODELVIEW_MATRIX, modl);
- // ������, ���� ������� �������� � �������, ���� �� �� ������������, �� ������� ���������
- // ���������. ��� ����� �� ������� ������� ���� �� �����.
- clip[0] = modl[0] * proj[0] + modl[1] * proj[4] + modl[2] * proj[8] + modl[3] * proj[12];
- clip[1] = modl[0] * proj[1] + modl[1] * proj[5] + modl[2] * proj[9] + modl[3] * proj[13];
- clip[2] = modl[0] * proj[2] + modl[1] * proj[6] + modl[2] * proj[10] + modl[3] * proj[14];
- clip[3] = modl[0] * proj[3] + modl[1] * proj[7] + modl[2] * proj[11] + modl[3] * proj[15];
- clip[4] = modl[4] * proj[0] + modl[5] * proj[4] + modl[6] * proj[8] + modl[7] * proj[12];
- clip[5] = modl[4] * proj[1] + modl[5] * proj[5] + modl[6] * proj[9] + modl[7] * proj[13];
- clip[6] = modl[4] * proj[2] + modl[5] * proj[6] + modl[6] * proj[10] + modl[7] * proj[14];
- clip[7] = modl[4] * proj[3] + modl[5] * proj[7] + modl[6] * proj[11] + modl[7] * proj[15];
- clip[8] = modl[8] * proj[0] + modl[9] * proj[4] + modl[10] * proj[8] + modl[11] * proj[12];
- clip[9] = modl[8] * proj[1] + modl[9] * proj[5] + modl[10] * proj[9] + modl[11] * proj[13];
- clip[10] = modl[8] * proj[2] + modl[9] * proj[6] + modl[10] * proj[10] + modl[11] * proj[14];
- clip[11] = modl[8] * proj[3] + modl[9] * proj[7] + modl[10] * proj[11] + modl[11] * proj[15];
- clip[12] = modl[12] * proj[0] + modl[13] * proj[4] + modl[14] * proj[8] + modl[15] * proj[12];
- clip[13] = modl[12] * proj[1] + modl[13] * proj[5] + modl[14] * proj[9] + modl[15] * proj[13];
- clip[14] = modl[12] * proj[2] + modl[13] * proj[6] + modl[14] * proj[10] + modl[15] * proj[14];
- clip[15] = modl[12] * proj[3] + modl[13] * proj[7] + modl[14] * proj[11] + modl[15] * proj[15];
- // ������ ��� ����� �������� ������� frustum'�. ����� ������� ���,
- // ������� ���������� ���������, ���������� ����, � �� ��� ������� �������.
- m_Frustum[RIGHT] = V4{ clip[3] - clip[0], clip[7] - clip[4], clip[11] - clip[8], clip[15] - clip[12] }.Normal();
- m_Frustum[LEFT] = V4{ clip[3] + clip[0], clip[7] + clip[4], clip[11] + clip[8], clip[15] + clip[12] }.Normal();
- m_Frustum[BOTTOM] = V4{ clip[3] + clip[1], clip[7] + clip[5], clip[11] + clip[9], clip[15] + clip[13] }.Normal();
- m_Frustum[TOP] = V4{ clip[3] - clip[1], clip[7] - clip[5], clip[11] - clip[9], clip[15] - clip[13] }.Normal();
- m_Frustum[BACK] = V4{ clip[3] - clip[2], clip[7] - clip[6], clip[11] - clip[10], clip[15] - clip[14] }.Normal();
- m_Frustum[FRONT] = V4{ clip[3] + clip[2], clip[7] + clip[6], clip[11] + clip[10], clip[15] + clip[14] }.Normal();
- }
- bool Frustum::PointInFrustum(V4 p) {
- for (int i = 0; i < 6; i++)
- if (m_Frustum[i].x * p.x + m_Frustum[i].y * p.y + m_Frustum[i].z * p.z + m_Frustum[i].w <= 0)
- return false;
- return true;
- }
- bool Frustum::SphereInFrustum(V4 p, float r) {
- for (int i = 0; i < 6; i++)
- if (m_Frustum[i].x * p.x + m_Frustum[i].y * p.y + m_Frustum[i].z * p.z + m_Frustum[i].w <= -r)
- return false;
- return true;
- }
- bool Frustum::CubeInFrustum(V4 p, float size) {
- for (int i = 0; i < 6; i++) {
- if (m_Frustum[i].x * (p.x - size) + m_Frustum[i].y * (p.y - size) +
- m_Frustum[i].z * (p.z - size) + m_Frustum[i].w > 0)
- continue;
- if (m_Frustum[i].x * (p.x + size) + m_Frustum[i].y * (p.y - size) +
- m_Frustum[i].z * (p.z - size) + m_Frustum[i].w > 0)
- continue;
- if (m_Frustum[i].x * (p.x - size) + m_Frustum[i].y * (p.y + size) +
- m_Frustum[i].z * (p.z - size) + m_Frustum[i].w > 0)
- continue;
- if (m_Frustum[i].x * (p.x + size) + m_Frustum[i].y * (p.y + size) +
- m_Frustum[i].z * (p.z - size) + m_Frustum[i].w > 0)
- continue;
- if (m_Frustum[i].x * (p.x - size) + m_Frustum[i].y * (p.y - size) +
- m_Frustum[i].z * (p.z + size) + m_Frustum[i].w > 0)
- continue;
- if (m_Frustum[i].x * (p.x + size) + m_Frustum[i].y * (p.y - size) +
- m_Frustum[i].z * (p.z + size) + m_Frustum[i].w > 0)
- continue;
- if (m_Frustum[i].x * (p.x - size) + m_Frustum[i].y * (p.y + size) +
- m_Frustum[i].z * (p.z + size) + m_Frustum[i].w > 0)
- continue;
- if (m_Frustum[i].x * (p.x + size) + m_Frustum[i].y * (p.y + size) +
- m_Frustum[i].z * (p.z + size) + m_Frustum[i].w > 0)
- continue;
- return false;
- }
- return true;
- }
- #pragma endregion
- #pragma region ������
- CFont::CFont(char *ttf, int FSize, float FDepth) {
- char filename[100];
- sprintf(filename, FONT_DIR, ttf);
- if (GetFileAttributesA(filename) == DWORD(-1))
- return;
- this->Font = new FTGLBitmapFont(filename);
- Font->Depth(FDepth);
- Font->CharMap(ft_encoding_unicode);
- Font->FaceSize(FSize);
- }
- void CFont::Print(V4 p, V4 c, wstring _text) {
- glPushMatrix();
- glLoadIdentity();
- glDisable(GL_TEXTURE_2D);
- glTranslatef(p.x, p.y, -1);
- glColor3fv(c);
- glRasterPos2f(-1, 0.5);
- Font->Render(_text.c_str());
- glEnable(GL_TEXTURE_2D);
- glPopMatrix();
- }
- void CFont::Printf(V4 p, V4 c, wstring fmt, ...) {
- wchar_t text[256];
- va_list ap;
- va_start(ap, fmt);
- vswprintf(text, fmt.c_str(), ap);
- va_end(ap);
- Print(p, c, text);
- }
- #pragma endregion
- #pragma region �������
- Console::Console() {
- funcs[L"exit"] = exitA;
- funcs[L"setFPS"] = setFPS;
- }
- void Console::draw() {
- if (!is_open)
- return;
- draw2DTexturedSquare({ 0.0f,0.0f }, { (float)MC->Width, 180.0f }, { 0.7f,0.7f,0.7f });
- int j = 0;
- for (int i = 9; i >= 0; --i)
- if (stack[i].empty())
- continue;
- else
- MC->arial_16->Print({ 10.0f, (++j) * 16.0f }, { 1.0f,1.0f,1.0f }, stack[i]);
- MC->arial_16->Print({ 10.0f, (++j) * 16.0f }, { 1.0f,0.0f,1.0f }, MC->tmpStr);
- }
- void Console::toggle() {
- MC->Pause = is_open = !is_open;
- MC->pointerToCenter(is_open);
- }
- void Console::print(wstring s) {
- stack[0].append(s);
- }
- void Console::println(wstring s) {
- for (int i = 8; i >= 0; --i)
- if (stack[i].empty())
- continue;
- else
- stack[i + 1] = stack[i];
- stack[0] = s;
- }
- void Console::parseln(wstring s) {
- split(s, L" ", param);
- if (funcs.find(param[0]) != funcs.end())
- (*funcs[param[0]])();
- }
- Console Console::operator<<(wstring string) {
- println(string);
- return *this;
- }
- #pragma endregion
- #pragma region ������� �������
- void exitA() {
- glutLeaveMainLoop();
- }
- void toggle() {
- MC->cout.toggle();
- }
- void setFPS() {
- MC->setMaxFPS(stoi(MC->cout.param[1]));
- }
- #pragma endregion
- #pragma region �����
- char **CommandLineToArgvA(PCHAR CmdLine, int* _argc) {
- PCHAR* argv;
- PCHAR _argv;
- ULONG len;
- ULONG argc;
- CHAR a;
- ULONG i, j;
- BOOLEAN in_QM;
- BOOLEAN in_TEXT;
- BOOLEAN in_SPACE;
- len = strlen(CmdLine);
- i = (ULONG)((len + 2) / 2)*sizeof(PVOID) + (ULONG)sizeof(PVOID);
- argv = (PCHAR*)GlobalAlloc(GMEM_FIXED,
- i + (len + 2)*sizeof(CHAR));
- _argv = (PCHAR)(((PUCHAR)argv) + i);
- argc = 0;
- argv[argc] = _argv;
- in_QM = FALSE;
- in_TEXT = FALSE;
- in_SPACE = TRUE;
- i = 0;
- j = 0;
- while (a = CmdLine[i]) {
- if (in_QM) {
- if (a == '\"') {
- in_QM = FALSE;
- }
- else {
- _argv[j] = a;
- j++;
- }
- }
- else
- switch (a) {
- case '\"':
- in_QM = TRUE;
- in_TEXT = TRUE;
- if (in_SPACE) {
- argv[argc] = _argv + j;
- argc++;
- }
- in_SPACE = FALSE;
- break;
- case ' ':
- case '\t':
- case '\n':
- case '\r':
- if (in_TEXT) {
- _argv[j] = '\0';
- j++;
- }
- in_TEXT = FALSE;
- in_SPACE = TRUE;
- break;
- default:
- in_TEXT = TRUE;
- if (in_SPACE) {
- argv[argc] = _argv + j;
- argc++;
- }
- _argv[j] = a;
- j++;
- in_SPACE = FALSE;
- break;
- }
- i++;
- }
- _argv[j] = '\0';
- argv[argc] = NULL;
- (*_argc) = argc;
- return argv;
- }
- wchar_t keyboardRuToEng(wchar_t c) {
- //Ru 2 En
- switch (c) {
- case 1081: c = 113; break;
- case 1094: c = 119; break;
- case 1091: c = 101; break;
- case 1082: c = 114; break;
- case 1077: c = 116; break;
- case 1085: c = 121; break;
- case 1075: c = 117; break;
- case 1096: c = 105; break;
- case 1097: c = 111; break;
- case 1079: c = 112; break;
- case 1093: c = 91; break;
- case 1098: c = 93; break;
- case 1092: c = 97; break;
- case 1099: c = 115; break;
- case 1074: c = 100; break;
- case 1072: c = 102; break;
- case 1087: c = 103; break;
- case 1088: c = 104; break;
- case 1086: c = 106; break;
- case 1083: c = 107; break;
- case 1076: c = 108; break;
- case 1078: c = 59; break;
- case 1101: c = 39; break;
- case 1103: c = 122; break;
- case 1095: c = 120; break;
- case 1089: c = 99; break;
- case 1084: c = 118; break;
- case 1080: c = 98; break;
- case 1090: c = 110; break;
- case 1100: c = 109; break;
- case 1073: c = 44; break;
- case 1102: c = 46; break;
- }
- return c;
- }
- void split(wstring &s, const wchar_t* delim, vector<wstring> & v) {
- wchar_t * dup = _wcsdup(s.c_str());
- wchar_t * token = wcstok(dup, delim);
- while (token != NULL) {
- v.push_back(wstring(token));
- token = wcstok(NULL, delim);
- }
- delete(dup);
- }
- wstring format(wstring fmt, ...) {
- if (fmt.empty())
- return wstring();
- wchar_t text[256];
- va_list ap;
- va_start(ap, fmt);
- vswprintf(text, fmt.c_str(), ap);
- va_end(ap);
- return wstring(text);
- }
- #pragma endregion
- #pragma region �������
- void to2D() {
- glMatrixMode(GL_PROJECTION);
- glPushMatrix();
- glLoadIdentity();
- gluOrtho2D(0, MC->Width, MC->Height, 0);
- glMatrixMode(GL_MODELVIEW);
- glDisable(GL_DEPTH_TEST);
- glPushMatrix();
- glLoadIdentity();
- }
- void backTo3D() {
- glPopMatrix();
- glEnable(GL_DEPTH_TEST);
- glMatrixMode(GL_PROJECTION);
- glPopMatrix();
- glMatrixMode(GL_MODELVIEW);
- }
- void CalculateFrameRate() {
- static float framesPerSecond = 0.0f;
- static float lastTime = 0.0f;
- float currentTime = timeGetTime() * 0.001f;
- ++framesPerSecond;
- if (currentTime - lastTime > 1.0f) {
- lastTime = currentTime;
- MC->FPS = (int)framesPerSecond;
- framesPerSecond = 0;
- }
- }
- void draw2DTexturedSquare(V4 p1, V4 p2, V4 c, V4 tp1, V4 tp2) {
- glPushMatrix();
- glColor3f(c.x, c.y, c.z);
- glBegin(GL_POLYGON);
- glTexCoord2f(tp1.x, tp2.y); glVertex2f(p1.x, p1.y);
- glTexCoord2f(tp2.x, tp2.y); glVertex2f(p2.x, p1.y);
- glTexCoord2f(tp2.x, tp1.y); glVertex2f(p2.x, p2.y);
- glTexCoord2f(tp1.x, tp1.y); glVertex2f(p1.x, p2.y);
- glEnd();
- glPopMatrix();
- }
- GLuint LoadTexture(string dir, string F) {
- if (MC->textures.count(F))
- return MC->textures[F];
- GLuint id;
- ilGenImages(1, &id);
- ilBindImage(id);
- char filename[100];
- sprintf(filename, "%s\\Textures\\%s", dir.c_str(), F.c_str());
- if (!ilLoadImage((const wchar_t *)filename))
- return MC->textures["Default.jpg"];
- // If the image is flipped (i.e. upside-down and mirrored, flip it the right way up!)
- ILinfo ImageInfo;
- iluGetImageInfo(&ImageInfo);
- if (ImageInfo.Origin == IL_ORIGIN_UPPER_LEFT)
- iluFlipImage();
- if (!ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE))
- return MC->textures["Default.jpg"];
- glGenTextures(1, &id);
- glBindTexture(GL_TEXTURE_2D, id);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- 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)
- ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData());
- MC->textures[F] = id;
- return id;
- }
- GLuint LoadTexture(string F) {
- return LoadTexture(RESOURCE_DIR, F);
- }
- #pragma endregion
- #pragma region ������
- Camera::Camera() {
- View = V4{ 0.0f, 0.0f, 1.0f }.Normal();
- Up = V4{ 0.0f, 1.0f, 0.0f }.Normal();
- Strafe.Cross(View, Up).Normal();
- Sensitivity = 75.0f;
- Zoom = 0.0f, ZoomLimit = 30.0f;
- locked = needMouseZoom = false;
- setPosition({ 0.0f, 0.0f, 0.0f });
- }
- void Camera::FirstPersonView(int mx, int my) {
- if (locked)
- return;
- int middleX = MC->Width >> 1;
- int middleY = MC->Height >> 1;
- if ((mx == middleX) && (my == middleY)) return;
- glutWarpPointer(middleX, middleY);
- if (my != middleY) {
- V4 View1 = V4(Quaternion::rotate(View, Strafe, (float)atan((middleY - my) / Sensitivity))).Normal();
- float angle = AngleBetweenVectors(View1, Up);
- if (angle > M_PI_4 / 4 && angle < M_PI - M_PI_4 / 4)
- View = View1;
- }
- if (mx != middleX) {
- float ty = View.y;
- View = V4(Quaternion::rotate(View, Up, (float)atan((middleX - mx) / Sensitivity))).Normal();
- View.y = ty;
- }
- Strafe.Cross(View, Up).Normal();
- ThirdPersonView();
- }
- void Camera::ThirdPersonView() {
- if (Zoom == 0.0f)
- LookPos = Position;
- else
- LookPos = Position - View*Zoom;
- }
- void Camera::setPosition(V4 _position) {
- Position = _position;
- ThirdPersonView();
- }
- void Camera::setViewTo(V4 viewTo) {
- View = (viewTo - Position).Normal();
- ThirdPersonView();
- }
- void Camera::lockView() {
- locked = true;
- }
- void Camera::unlockView() {
- locked = false;
- }
- void Camera::Look() {
- gluLookAt(LookPos.x, LookPos.y, LookPos.z,
- View.x + LookPos.x, View.y + LookPos.y, View.z + LookPos.z,
- Up.x, Up.y, Up.z);
- }
- #pragma endregion
- #pragma region ������
- struct V2 { float x, y; };
- Model::Model(char *modelfile, V4 _color) {
- color = _color;
- loadModel(modelfile);
- }
- void Model::loadModel(char *modelname) {
- char Mname[100];
- sprintf(Mname, MODEL_DIR, modelname);
- FILE *F = fopen(Mname, "rb");
- if (F == NULL) {
- div.v1 = { 0.0f,0.0f,0.0f };
- div.v2 = { 0.0f,0.0f,0.0f };
- return;
- }
- bool fileTypeIsdModel;
- fread(&fileTypeIsdModel, sizeof(bool), 1, F);
- if (!fileTypeIsdModel) {
- div.v1 = { 0.0f,0.0f,0.0f };
- div.v2 = { 0.0f,0.0f,0.0f };
- return;
- }
- fread(&divType, sizeof(unsigned short), 1, F);
- fread(&div, sizeof(Div), 1, F);
- bool isPart;
- fread(&isPart, sizeof(bool), 1, F);
- while (isPart) {
- ModelPart tmpmp;
- fread(&tmpmp.name, sizeof(char), 40, F);
- fread(&tmpmp.divType, sizeof(unsigned short), 1, F);
- fread(&tmpmp.div, sizeof(Div), 1, F);
- fread(&tmpmp.is_static, sizeof(bool), 1, F);
- unsigned short polygonCount = 0;
- fread(&polygonCount, sizeof(unsigned short), 1, F);
- for (int i = 0; i < polygonCount; ++i) {
- PolygonS tmppo;
- fread(&tmppo.p1, sizeof(V3), 1, F);
- fread(&tmppo.p2, sizeof(V3), 1, F);
- fread(&tmppo.p3, sizeof(V3), 1, F);
- fread(&tmppo.normal, sizeof(V3), 1, F);
- fread(&tmppo.texture, sizeof(char), 40, F);
- if (tmppo.texture == "color") {
- fread(&tmppo.tp1, sizeof(V3), 1, F);
- fread(&tmppo.tp2, sizeof(V3), 1, F);
- fread(&tmppo.tp3, sizeof(V3), 1, F);
- }
- else {
- fread(&tmppo.tp1, sizeof(V2), 1, F);
- fread(&tmppo.tp2, sizeof(V2), 1, F);
- fread(&tmppo.tp3, sizeof(V2), 1, F);
- }
- tmpmp.polygons.push_back(tmppo);
- }
- if (tmpmp.is_static)
- buildPart(&tmpmp);
- parts.push_back(tmpmp);
- fread(&isPart, sizeof(bool), 1, F);
- }
- fclose(F);
- calculateDiv();
- }
- void Model::calculateDiv() {
- div.v1.set(100.0f);
- div.v2.set(0.0f);
- for (int i = 0; i < (int)parts.size(); ++i) {
- parts[i].div.v1.set(100.0f);
- parts[i].div.v2.set(0.0f);
- for (PolygonS poly : parts[i].polygons) {
- parts[i].div.v2.maxXYZ(poly.p1); parts[i].div.v1.minXYZ(poly.p1);
- parts[i].div.v2.maxXYZ(poly.p2); parts[i].div.v1.minXYZ(poly.p2);
- parts[i].div.v2.maxXYZ(poly.p3); parts[i].div.v1.minXYZ(poly.p3);
- }
- div.v1.minXYZ(parts[i].div.v1);
- div.v2.maxXYZ(parts[i].div.v2);
- }
- div.v1.w = max3(div.v2.x - div.v1.x, div.v2.y - div.v1.y, div.v2.z - div.v1.z) / 2.0f;
- }
- void Model::drawPart(ModelPart *mpart) {
- glColor3f(V4toxyz(color));
- glBegin(GL_TRIANGLES);
- for (PolygonS poly : mpart->polygons) {
- if (poly.texture == "color") {
- glNormal3fv(poly.normal);
- glColor3fv(poly.tp1);
- glVertex3fv(poly.p1);
- glNormal3fv(poly.normal);
- glColor3fv(poly.tp2);
- glVertex3fv(poly.p2);
- glNormal3fv(poly.normal);
- glColor3fv(poly.tp3);
- glVertex3fv(poly.p3);
- }
- else {
- //glBindTexture(GL_TEXTURE_2D, Textures[models[modelId]->polygon[i].texId].texID);
- glNormal3fv(poly.normal);
- glTexCoord2fv(poly.tp1);
- glVertex3fv(poly.p1);
- glNormal3fv(poly.normal);
- glTexCoord2fv(poly.tp2);
- glVertex3fv(poly.p2);
- glNormal3fv(poly.normal);
- glTexCoord2fv(poly.tp3);
- glVertex3fv(poly.p3);
- }
- }
- glEnd();
- glBindTexture(GL_TEXTURE_2D, -1);
- }
- void Model::buildPart(ModelPart *mpart) {
- int list_id = glGenLists(1);
- glNewList(list_id, GL_COMPILE);
- drawPart(mpart);
- glEndList();
- mpart->listId = list_id;
- }
- void Model::drawModel(V4 position) {
- glPushMatrix();
- glTranslatef(V4toxyz(position));
- if (MC->debug.showModel) {
- for (ModelPart mp : parts)
- if (mp.is_static)
- glCallList(mp.listId);
- else
- drawPart(&mp);
- }
- if (MC->debug.showNormal) {
- glBegin(GL_LINES);
- for (ModelPart mp : parts)
- for (PolygonS p : mp.polygons) {
- V4 tmpvstart = (p.p1 + p.p2 + p.p3) / 3;
- V4 tmlvend = tmpvstart + p.normal;
- glColor3f(1.0f, 1.0f, 1.0f);
- glVertex3f(V4toxyz(tmpvstart));
- glVertex3f(V4toxyz(tmlvend));
- }
- glEnd();
- }
- if (MC->debug.showDiv) {
- glColor3f(1.0f, 1.0f, 1.0f);
- if (divType == DIV_TYPE_AABB) {
- glBegin(GL_LINE_STRIP);
- glVertex3f(div.v1.x, div.v1.y, div.v1.z);
- glVertex3f(div.v2.x, div.v1.y, div.v1.z);
- glVertex3f(div.v2.x, div.v1.y, div.v2.z);
- glVertex3f(div.v1.x, div.v1.y, div.v2.z);
- glVertex3f(div.v1.x, div.v1.y, div.v1.z);
- glVertex3f(div.v1.x, div.v2.y, div.v1.z);
- glVertex3f(div.v2.x, div.v2.y, div.v1.z);
- glVertex3f(div.v2.x, div.v1.y, div.v1.z);
- glEnd();
- glBegin(GL_LINE_STRIP);
- glVertex3f(div.v2.x, div.v2.y, div.v2.z);
- glVertex3f(div.v1.x, div.v2.y, div.v2.z);
- glVertex3f(div.v1.x, div.v2.y, div.v1.z);
- glVertex3f(div.v2.x, div.v2.y, div.v1.z);
- glVertex3f(div.v2.x, div.v2.y, div.v2.z);
- glVertex3f(div.v2.x, div.v1.y, div.v2.z);
- glVertex3f(div.v1.x, div.v1.y, div.v2.z);
- glVertex3f(div.v1.x, div.v2.y, div.v2.z);
- glEnd();
- }
- else if (divType == DIV_TYPE_SPHERE) {
- glutWireSphere(div.v1.w / 2, 10, 10);
- }
- }
- glPopMatrix();
- }
- /*
- bool fileTypeIsdModel
- ushort divType
- Div div
- bool isPart
- [isPart]{
- char-40 Part.name
- ushort Part.divType
- Div Part.div
- bool is_static
- ushort Part.polygonCount{
- V3 Poly.p1,p2,p3
- V3 Poly.normal
- char-40 Poly.texture
- [Poly.texture == "color"]{
- V3 Poly.tp1,tp2,tp3
- }{
- V2 Poly.tp1,tp2,tp3
- }
- }
- bool isPart
- }
- */
- #pragma endregion
- #pragma region ������
- Physics::Physics() {
- }
- Physics::Physics(float _g) {
- g = _g;
- }
- bool Physics::getIntersection(Object o1, Object o2) {
- return false;
- }
- #pragma endregion
- #pragma region ������
- Object::Object(float _mass, V4 _pos, char *modelfile) {
- model = new Model(modelfile, { 1.0f,1.0f,0.0f });
- mass = _mass;
- position = _pos;
- }
- void Object::setPreTickFunc(void f()) { preTick = f; }
- void Object::setPreDrawFunc(void f()) { preDraw = f; }
- void Object::applyVelocity(V4 _velocity) {
- velocity += _velocity;
- }
- void Object::applyForce(V4 _force) {
- force += _force;
- }
- void Object::applyGravity() {
- applyForce({ 0.0f, -mass * MC->physics->g, 0.0f });
- }
- void Object::applyFriction() {
- velocity -= velocity / 20;
- if (velocity.GetLength() < 0.5f)
- velocity = { 0.0f,0.0f,0.0f };
- }
- void Object::tick(float dt) {
- if (preTick != 0)preTick();
- applyGravity();
- applyFriction();
- velocity += ((force) / mass) * dt;
- V4 lastPos = position;
- position += velocity * dt;
- force = { 0.0f, 0.0f, 0.0f };
- if (position.y < 0.0f) {
- position.y = 0.0f;
- velocity.y = 0.0f;
- }
- moving = position - lastPos;
- }
- void Object::tick() { tick(MC->dT); }
- void Object::draw() {
- model->drawModel(position);
- }
- #pragma endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement