diff --git a/common/camera.cpp b/common/camera.cpp index c2b3665a..591748fb 100644 --- a/common/camera.cpp +++ b/common/camera.cpp @@ -1,13 +1,13 @@ // Camera object. #include "lc_global.h" +#include "lc_math.h" #include "lc_colors.h" #include #include #include #include "opengl.h" #include "globals.h" -#include "vector.h" #include "matrix.h" #include "lc_file.h" #include "camera.h" @@ -108,20 +108,21 @@ Camera::Camera (const float *eye, const float *target, const float *up, Camera* : Object (LC_OBJECT_CAMERA) { // Fix the up vector - Vector upvec(up), frontvec(eye[0]-target[0], eye[1]-target[1], eye[2]-target[2]), sidevec; - frontvec.Normalize(); - sidevec.Cross(frontvec, upvec); - upvec.Cross(sidevec, frontvec); - upvec.Normalize(); + lcVector3 UpVector(up[0], up[1], up[2]); + lcVector3 FrontVector(eye[0] - target[0], eye[1] - target[1], eye[2] - target[2]), SideVector; + FrontVector.Normalize(); + SideVector = lcCross(FrontVector, UpVector); + UpVector = lcCross(SideVector, FrontVector); + UpVector.Normalize(); Initialize(); ChangeKey (1, false, true, eye, LC_CK_EYE); ChangeKey (1, false, true, target, LC_CK_TARGET); - ChangeKey (1, false, true, upvec, LC_CK_UP); + ChangeKey (1, false, true, UpVector, LC_CK_UP); ChangeKey (1, true, true, eye, LC_CK_EYE); ChangeKey (1, true, true, target, LC_CK_TARGET); - ChangeKey (1, true, true, upvec, LC_CK_UP); + ChangeKey (1, true, true, UpVector, LC_CK_UP); int i, max = 0; @@ -150,14 +151,14 @@ Camera::Camera (float ex, float ey, float ez, float tx, float ty, float tz, Came : Object (LC_OBJECT_CAMERA) { // Fix the up vector - Vector upvec(0,0,1), frontvec(ex-tx, ey-ty, ez-tz), sidevec; - frontvec.Normalize(); - if (frontvec == upvec) - sidevec = Vector(1,0,0); + lcVector3 UpVector(0, 0, 1), FrontVector(ex - tx, ey - ty, ez - tz), SideVector; + FrontVector.Normalize(); + if (FrontVector == UpVector) + SideVector = lcVector3(1, 0, 0); else - sidevec.Cross(frontvec, upvec); - upvec.Cross(sidevec, frontvec); - upvec.Normalize(); + SideVector = lcCross(FrontVector, UpVector); + UpVector = lcCross(SideVector, FrontVector); + UpVector.Normalize(); Initialize(); @@ -165,10 +166,10 @@ Camera::Camera (float ex, float ey, float ez, float tx, float ty, float tz, Came ChangeKey (1, false, true, eye, LC_CK_EYE); ChangeKey (1, false, true, target, LC_CK_TARGET); - ChangeKey (1, false, true, upvec, LC_CK_UP); + ChangeKey (1, false, true, UpVector, LC_CK_UP); ChangeKey (1, true, true, eye, LC_CK_EYE); ChangeKey (1, true, true, target, LC_CK_TARGET); - ChangeKey (1, true, true, upvec, LC_CK_UP); + ChangeKey (1, true, true, UpVector, LC_CK_UP); int i, max = 0; @@ -446,12 +447,12 @@ void Camera::Move (unsigned short nTime, bool bAnimation, bool bAddKey, float dx } // Fix the up vector - Vector upvec(m_fUp), sidevec; - Vector frontvec(m_fTarget[0]-m_fEye[0], m_fTarget[1]-m_fEye[1], m_fTarget[2]-m_fEye[2]); - sidevec.Cross(frontvec, upvec); - upvec.Cross(sidevec, frontvec); - upvec.Normalize(); - upvec.ToFloat(m_fUp); + lcVector3 UpVector(m_fUp[0], m_fUp[1], m_fUp[2]), SideVector; + lcVector3 FrontVector(m_fTarget[0] - m_fEye[0], m_fTarget[1] - m_fEye[1], m_fTarget[2] - m_fEye[2]); + SideVector = lcCross(FrontVector, UpVector); + UpVector = lcCross(SideVector, FrontVector); + UpVector.Normalize(); + memcpy(m_fUp, UpVector, sizeof(m_fUp)); ChangeKey(nTime, bAnimation, bAddKey, m_fUp, LC_CK_UP); } @@ -519,15 +520,15 @@ void Camera::UpdatePosition(unsigned short nTime, bool bAnimation) void Camera::UpdateBoundingBox() { // Fix the up vector - Vector frontvec(m_fEye[0]-m_fTarget[0], m_fEye[1]-m_fTarget[1], m_fEye[2]-m_fTarget[2]); - Vector upvec(m_fUp), sidevec; + lcVector3 FrontVector(m_fEye[0] - m_fTarget[0], m_fEye[1] - m_fTarget[1], m_fEye[2] - m_fTarget[2]); + lcVector3 UpVector(m_fUp[0], m_fUp[1], m_fUp[2]), SideVector; - sidevec.Cross(frontvec, upvec); - upvec.Cross(sidevec, frontvec); - upvec.Normalize(); - upvec.ToFloat(m_fUp); + SideVector = lcCross(FrontVector, UpVector); + UpVector = lcCross(SideVector, FrontVector); + UpVector.Normalize(); + memcpy(m_fUp, UpVector, sizeof(m_fUp)); - float len = frontvec.Length(); + float len = FrontVector.Length(); Matrix mat; mat.CreateLookat (m_fEye, m_fTarget, m_fUp); @@ -656,8 +657,8 @@ void Camera::Render(float fLineWidth) if (IsSelected()) { Matrix projection, modelview; - Vector frontvec(m_fTarget[0]-m_fEye[0], m_fTarget[1]-m_fEye[1], m_fTarget[2]-m_fEye[2]); - float len = frontvec.Length(); + lcVector3 FrontVector(m_fTarget[0] - m_fEye[0], m_fTarget[1] - m_fEye[1], m_fTarget[2] - m_fEye[2]); + float len = FrontVector.Length(); glPushMatrix (); @@ -736,17 +737,17 @@ void Camera::LoadProjection(float fAspect) void Camera::DoZoom(int dy, int mouse, unsigned short nTime, bool bAnimation, bool bAddKey) { - Vector frontvec(m_fEye[0]-m_fTarget[0], m_fEye[1]-m_fTarget[1], m_fEye[2]-m_fTarget[2]); - frontvec.Normalize(); - frontvec *= 2.0f*dy/(21-mouse); + lcVector3 FrontVector(m_fEye[0] - m_fTarget[0], m_fEye[1] - m_fTarget[1], m_fEye[2] - m_fTarget[2]); + FrontVector.Normalize(); + FrontVector *= 2.0f * dy / (21 - mouse); // TODO: option to move eye, target or both - m_fEye[0] += frontvec[0]; - m_fEye[1] += frontvec[1]; - m_fEye[2] += frontvec[2]; - m_fTarget[0] += frontvec[0]; - m_fTarget[1] += frontvec[1]; - m_fTarget[2] += frontvec[2]; + m_fEye[0] += FrontVector[0]; + m_fEye[1] += FrontVector[1]; + m_fEye[2] += FrontVector[2]; + m_fTarget[0] += FrontVector[0]; + m_fTarget[1] += FrontVector[1]; + m_fTarget[2] += FrontVector[2]; ChangeKey(nTime, bAnimation, bAddKey, m_fEye, LC_CK_EYE); ChangeKey(nTime, bAnimation, bAddKey, m_fTarget, LC_CK_TARGET); @@ -755,19 +756,19 @@ void Camera::DoZoom(int dy, int mouse, unsigned short nTime, bool bAnimation, bo void Camera::DoPan(int dx, int dy, int mouse, unsigned short nTime, bool bAnimation, bool bAddKey) { - Vector upvec(m_fUp), frontvec(m_fEye[0]-m_fTarget[0], m_fEye[1]-m_fTarget[1], m_fEye[2]-m_fTarget[2]), sidevec; - sidevec.Cross(frontvec, upvec); - sidevec.Normalize(); - sidevec *= 2.0f*dx/(21-mouse); - upvec.Normalize(); - upvec *= -2.0f*dy/(21-mouse); + lcVector3 UpVector(m_fUp[0], m_fUp[1], m_fUp[2]), FrontVector(m_fEye[0] - m_fTarget[0], m_fEye[1] - m_fTarget[1], m_fEye[2] - m_fTarget[2]), SideVector; + SideVector = lcCross(FrontVector, UpVector); + SideVector.Normalize(); + SideVector *= 2.0f*dx/(21-mouse); + UpVector.Normalize(); + UpVector *= -2.0f*dy/(21-mouse); - m_fEye[0] += upvec[0] + sidevec[0]; - m_fEye[1] += upvec[1] + sidevec[1]; - m_fEye[2] += upvec[2] + sidevec[2]; - m_fTarget[0] += upvec[0] + sidevec[0]; - m_fTarget[1] += upvec[1] + sidevec[1]; - m_fTarget[2] += upvec[2] + sidevec[2]; + m_fEye[0] += UpVector[0] + SideVector[0]; + m_fEye[1] += UpVector[1] + SideVector[1]; + m_fEye[2] += UpVector[2] + SideVector[2]; + m_fTarget[0] += UpVector[0] + SideVector[0]; + m_fTarget[1] += UpVector[1] + SideVector[1]; + m_fTarget[2] += UpVector[2] + SideVector[2]; ChangeKey(nTime, bAnimation, bAddKey, m_fEye, LC_CK_EYE); ChangeKey(nTime, bAnimation, bAddKey, m_fTarget, LC_CK_TARGET); @@ -776,28 +777,28 @@ void Camera::DoPan(int dx, int dy, int mouse, unsigned short nTime, bool bAnimat void Camera::DoRotate(int dx, int dy, int mouse, unsigned short nTime, bool bAnimation, bool bAddKey, float* /*center*/) { - Vector upvec(m_fUp), frontvec(m_fEye[0]-m_fTarget[0], m_fEye[1]-m_fTarget[1], m_fEye[2]-m_fTarget[2]), sidevec; - sidevec.Cross(frontvec, upvec); - sidevec.Normalize(); - sidevec *= 2.0f*dx/(21-mouse); - upvec.Normalize(); - upvec *= -2.0f*dy/(21-mouse); + lcVector3 UpVector(m_fUp[0], m_fUp[1], m_fUp[2]), FrontVector(m_fEye[0] - m_fTarget[0], m_fEye[1] - m_fTarget[1], m_fEye[2] - m_fTarget[2]), SideVector; + SideVector = lcCross(FrontVector, UpVector); + SideVector.Normalize(); + SideVector *= 2.0f*dx/(21-mouse); + UpVector.Normalize(); + UpVector *= -2.0f*dy/(21-mouse); // TODO: option to move eye or target - float len = frontvec.Length(); - frontvec += Vector(upvec[0] + sidevec[0], upvec[1] + sidevec[1], upvec[2] + sidevec[2]); - frontvec.Normalize(); - frontvec *= len; - frontvec += Vector(m_fTarget); - frontvec.ToFloat(m_fEye); + float len = FrontVector.Length(); + FrontVector += lcVector3(UpVector[0] + SideVector[0], UpVector[1] + SideVector[1], UpVector[2] + SideVector[2]); + FrontVector.Normalize(); + FrontVector *= len; + FrontVector += lcVector3(m_fTarget[0], m_fTarget[1], m_fTarget[2]); + memcpy(m_fEye, FrontVector, sizeof(m_fEye)); // Calculate new up - upvec = Vector(m_fUp[0], m_fUp[1], m_fUp[2]); - frontvec = Vector(m_fEye[0]-m_fTarget[0], m_fEye[1]-m_fTarget[1], m_fEye[2]-m_fTarget[2]); - sidevec.Cross(frontvec, upvec); - upvec.Cross(sidevec, frontvec); - upvec.Normalize(); - upvec.ToFloat(m_fUp); + UpVector = lcVector3(m_fUp[0], m_fUp[1], m_fUp[2]); + FrontVector = lcVector3(m_fEye[0] - m_fTarget[0], m_fEye[1] - m_fTarget[1], m_fEye[2] - m_fTarget[2]); + SideVector = lcCross(FrontVector, UpVector); + UpVector = lcCross(SideVector, FrontVector); + UpVector.Normalize(); + memcpy(m_fUp, UpVector, sizeof(m_fUp)); ChangeKey(nTime, bAnimation, bAddKey, m_fEye, LC_CK_EYE); ChangeKey(nTime, bAnimation, bAddKey, m_fUp, LC_CK_UP); diff --git a/common/curve.cpp b/common/curve.cpp index 068bbcbe..212ced51 100755 --- a/common/curve.cpp +++ b/common/curve.cpp @@ -2,6 +2,7 @@ // #include "lc_global.h" +#include "lc_math.h" #include "lc_colors.h" #include #include @@ -9,7 +10,6 @@ #include "curve.h" #include "opengl.h" #include "matrix.h" -#include "vector.h" #define LC_CURVE_SAVE_VERSION 1 // LeoCAD 0.73 #define LC_CURVE_POINT_SAVE_VERSION 1 // LeoCAD 0.73 @@ -545,29 +545,29 @@ void Curve::TesselateHose () b = 3*cy[0]*t2 + 2*cy[1]*t + cy[2]; c = 3*cz[0]*t2 + 2*cz[1]*t + cz[2]; - Vector side, front (a, b, c); - Vector up (u); - side.Cross (front, up); - up.Cross (side, front); - up.Normalize (); - front.Normalize (); - side.Normalize (); + lcVector3 SideVector, FrontVector(a, b, c); + lcVector3 UpVector(u[0], u[1], u[2]); + SideVector = lcCross(FrontVector, UpVector); + UpVector = lcCross(SideVector, FrontVector); + UpVector.Normalize(); + FrontVector.Normalize(); + SideVector.Normalize(); if (angle_step != 0) { Matrix rot; - rot.FromAxisAngle (front, angle_step); - rot.TransformPoint (u, up); + rot.FromAxisAngle(FrontVector, angle_step); +// rot.TransformPoint(u, UpVector); } - else - up.ToFloat (u); +// else +// UpVector.ToFloat(u); float f[16]; #define M(row,col) f[col*4+row] - M(0,0) = side[0]; M(0,1) = up[0]; M(0,2) = front[0]; M(0,3) = x; - M(1,0) = side[1]; M(1,1) = up[1]; M(1,2) = front[1]; M(1,3) = y; - M(2,0) = side[2]; M(2,1) = up[2]; M(2,2) = front[2]; M(2,3) = z; - M(3,0) = 0.0; M(3,1) = 0.0; M(3,2) = 0.0; M(3,3) = 1.0; + M(0,0) = SideVector[0]; M(0,1) = UpVector[0]; M(0,2) = FrontVector[0]; M(0,3) = x; + M(1,0) = SideVector[1]; M(1,1) = UpVector[1]; M(1,2) = FrontVector[1]; M(1,3) = y; + M(2,0) = SideVector[2]; M(2,1) = UpVector[2]; M(2,2) = FrontVector[2]; M(2,3) = z; + M(3,0) = 0.0; M(3,1) = 0.0; M(3,2) = 0.0; M(3,3) = 1.0; #undef M float v[3]; diff --git a/common/lc_math.h b/common/lc_math.h new file mode 100644 index 00000000..56fe8037 --- /dev/null +++ b/common/lc_math.h @@ -0,0 +1,190 @@ +#ifndef _LC_MATH_H_ +#define _LC_MATH_H_ + +#include + +class lcVector3; +class lcVector4; +class lcMatrix33; +class lcMatrix44; + +class lcVector3 +{ +public: + lcVector3() + { + } + + lcVector3(const float _x, const float _y, const float _z) + : x(_x), y(_y), z(_z) + { + } + + lcVector3(const lcVector3& a) + : x(a.x), y(a.y), z(a.z) + { + } + + operator const float*() const + { + return (const float*)this; + } + + float& operator[](int i) const + { + return ((float*)this)[i]; + } + + void Normalize(); + void Dot(); + float Length() const; + + float x, y, z; +}; + +class lcVector4 +{ +public: + lcVector4() + { + } + + lcVector4(const float _x, const float _y, const float _z, const float _w) + : x(_x), y(_y), z(_z), w(_w) + { + } + + operator const float*() const + { + return (const float*)this; + } + + float& operator[](int i) const + { + return ((float*)this)[i]; + } + + float x, y, z, w; +}; + +inline lcVector3 operator+(const lcVector3& a, const lcVector3& b) +{ + return lcVector3(a.x + b.x, a.y + b.y, a.z + b.z); +} + +inline lcVector3 operator-(const lcVector3& a, const lcVector3& b) +{ + return lcVector3(a.x - b.x, a.y - b.y, a.z - b.z); +} + +inline lcVector3 operator*(const lcVector3& a, const lcVector3& b) +{ + return lcVector3(a.x * b.x, a.y * b.y, a.z * b.z); +} + +inline lcVector3 operator/(const lcVector3& a, const lcVector3& b) +{ + return lcVector3(a.x / b.x, a.y / b.y, a.z / b.z); +} + +inline lcVector3 operator*(const lcVector3& a, float b) +{ + return lcVector3(a.x * b, a.y * b, a.z * b); +} + +inline lcVector3 operator/(const lcVector3& a, float b) +{ + return lcVector3(a.x / b, a.y / b, a.z / b); +} + +inline lcVector3 operator-(const lcVector3& a) +{ + return lcVector3(-a.x, -a.y, -a.z); +} + +inline lcVector3& operator+=(lcVector3& a, const lcVector3& b) +{ + a.x += b.x; + a.y += b.y; + a.z += b.z; + + return a; +} + +inline lcVector3& operator-=(lcVector3& a, const lcVector3& b) +{ + a.x -= b.x; + a.y -= b.y; + a.z -= b.z; + + return a; +} + +inline lcVector3& operator*=(lcVector3& a, const lcVector3& b) +{ + a.x *= b.x; + a.y *= b.y; + a.z *= b.z; + + return a; +} + +inline lcVector3& operator/=(lcVector3& a, const lcVector3& b) +{ + a.x /= b.x; + a.y /= b.y; + a.z /= b.z; + + return a; +} + +inline lcVector3& operator*=(lcVector3& a, float b) +{ + a.x *= b; + a.y *= b; + a.z *= b; + + return a; +} + +inline lcVector3& operator/=(lcVector3& a, float b) +{ + a.x /= b; + a.y /= b; + a.z /= b; + + return a; +} + +inline void lcVector3::Normalize() +{ + float InvLength = 1.0f / Length(); + + x *= InvLength; + y *= InvLength; + z *= InvLength; +} + +inline float lcVector3::Length() const +{ + return sqrtf(x * x + y * y + z * z); +} + +inline lcVector3 lcNormalize(const lcVector3& a) +{ + lcVector3 Ret(a); + Ret.Normalize(); + return Ret; +} + +inline float lcDot(const lcVector3& a, const lcVector3& b) +{ + return a.x * b.x + a.y * b.y + a.z * b.z; +} + +inline lcVector3 lcCross(const lcVector3& a, const lcVector3& b) +{ + return lcVector3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x); +} + +#endif // _LC_MATH_H_ diff --git a/common/light.cpp b/common/light.cpp index eca0337f..d4f03fba 100644 --- a/common/light.cpp +++ b/common/light.cpp @@ -1,6 +1,7 @@ // Light object. #include "lc_global.h" +#include "lc_math.h" #include "lc_colors.h" #include #include @@ -8,7 +9,6 @@ #include #include "light.h" #include "globals.h" -#include "vector.h" #include "matrix.h" GLuint Light::m_nSphereList = 0; @@ -268,45 +268,46 @@ void Light::Move (unsigned short nTime, bool bAnimation, bool bAddKey, float dx, void Light::UpdatePosition (unsigned short nTime, bool bAnimation) { - CalculateKeys (nTime, bAnimation); - BoundingBoxCalculate (m_fPos); + CalculateKeys(nTime, bAnimation); + BoundingBoxCalculate(m_fPos); - if (m_pTarget != NULL) - { - m_pTarget->BoundingBoxCalculate (m_fTarget); + if (m_pTarget != NULL) + { + m_pTarget->BoundingBoxCalculate(m_fTarget); - if (m_nList == 0) - m_nList = glGenLists(1); + if (m_nList == 0) + m_nList = glGenLists(1); - glNewList (m_nList, GL_COMPILE); + glNewList(m_nList, GL_COMPILE); - glPushMatrix (); - glTranslatef (m_fPos[0], m_fPos[1], m_fPos[2]); + glPushMatrix(); + glTranslatef(m_fPos[0], m_fPos[1], m_fPos[2]); - Vector frontvec (m_fTarget[0]-m_fPos[0], m_fTarget[1]-m_fPos[1], m_fTarget[2]-m_fPos[2]); - float len = frontvec.Length (), up[3] = { 1, 1, 1 }; + lcVector3 FrontVector(m_fTarget[0] - m_fPos[0], m_fTarget[1] - m_fPos[1], m_fTarget[2] - m_fPos[2]); + lcVector3 UpVector(1, 1, 1); + float Length = FrontVector.Length(); - if (fabs (frontvec[0]) < fabs (frontvec[1])) - { - if (fabs (frontvec[0]) < fabs (frontvec[2])) - up[0] = -(up[1]*frontvec[1] + up[2]*frontvec[2]); - else - up[2] = -(up[0]*frontvec[0] + up[1]*frontvec[1]); - } - else - { - if (fabs (frontvec[1]) < fabs (frontvec[2])) - up[1] = -(up[0]*frontvec[0] + up[2]*frontvec[2]); - else - up[2] = -(up[0]*frontvec[0] + up[1]*frontvec[1]); - } + if (fabs (FrontVector[0]) < fabs (FrontVector[1])) + { + if (fabs(FrontVector[0]) < fabs(FrontVector[2])) + UpVector[0] = -(UpVector[1] * FrontVector[1] + UpVector[2] * FrontVector[2]); + else + UpVector[2] = -(UpVector[0] * FrontVector[0] + UpVector[1] * FrontVector[1]); + } + else + { + if (fabs(FrontVector[1]) < fabs(FrontVector[2])) + UpVector[1] = -(UpVector[0] * FrontVector[0] + UpVector[2] * FrontVector[2]); + else + UpVector[2] = -(UpVector[0] * FrontVector[0] + UpVector[1] * FrontVector[1]); + } - Matrix mat; - mat.CreateLookat (m_fPos, m_fTarget, up); - mat.Invert (); - mat.SetTranslation (0, 0, 0); + Matrix mat; + mat.CreateLookat(m_fPos, m_fTarget, UpVector); + mat.Invert(); + mat.SetTranslation(0, 0, 0); - glMultMatrixf (mat.m); + glMultMatrixf(mat.m); glEnableClientState (GL_VERTEX_ARRAY); float verts[16*3]; @@ -331,7 +332,7 @@ void Light::UpdatePosition (unsigned short nTime, bool bAnimation) glVertex3f (-0.5f, 0.5f, -0.3f); glEnd (); - glTranslatef(0, 0, -len); + glTranslatef(0, 0, -Length); glEndList(); if (m_nTargetList == 0) @@ -471,31 +472,32 @@ void Light::Render (float fLineWidth) if (IsSelected()) { Matrix projection, modelview; - Vector frontvec(m_fTarget[0]-m_fPos[0], m_fTarget[1]-m_fPos[1], m_fTarget[2]-m_fPos[2]); - float len = frontvec.Length (), up[3] = { 1, 1, 1 }; + lcVector3 FrontVector(m_fTarget[0] - m_fPos[0], m_fTarget[1] - m_fPos[1], m_fTarget[2] - m_fPos[2]); + lcVector3 UpVector(1, 1, 1); + float Length = FrontVector.Length(); - if (fabs (frontvec[0]) < fabs (frontvec[1])) + if (fabs(FrontVector[0]) < fabs(FrontVector[1])) { - if (fabs (frontvec[0]) < fabs (frontvec[2])) - up[0] = -(up[1]*frontvec[1] + up[2]*frontvec[2]); + if (fabs(FrontVector[0]) < fabs(FrontVector[2])) + UpVector[0] = -(UpVector[1] * FrontVector[1] + UpVector[2] * FrontVector[2]); else - up[2] = -(up[0]*frontvec[0] + up[1]*frontvec[1]); + UpVector[2] = -(UpVector[0] * FrontVector[0] + UpVector[1] * FrontVector[1]); } else { - if (fabs (frontvec[1]) < fabs (frontvec[2])) - up[1] = -(up[0]*frontvec[0] + up[2]*frontvec[2]); + if (fabs(FrontVector[1]) < fabs(FrontVector[2])) + UpVector[1] = -(UpVector[0] * FrontVector[0] + UpVector[2] * FrontVector[2]); else - up[2] = -(up[0]*frontvec[0] + up[1]*frontvec[1]); + UpVector[2] = -(UpVector[0] * FrontVector[0] + UpVector[1] * FrontVector[1]); } - glPushMatrix (); + glPushMatrix(); - modelview.CreateLookat (m_fPos, m_fTarget, up); + modelview.CreateLookat (m_fPos, m_fTarget, UpVector); modelview.Invert (); glMultMatrixf (modelview.m); - projection.CreatePerspective (2*m_fCutoff, 1.0f, 0.01f, len); + projection.CreatePerspective (2*m_fCutoff, 1.0f, 0.01f, Length); projection.Invert (); glMultMatrixf (projection.m); @@ -573,11 +575,11 @@ void Light::Setup (int index) if (m_pTarget != NULL) { - Vector dir (m_fTarget[0]-m_fPos[0], m_fTarget[1]-m_fPos[1], m_fTarget[2]-m_fPos[2]); - dir.Normalize (); + lcVector3 Dir(m_fTarget[0] - m_fPos[0], m_fTarget[1] - m_fPos[1], m_fTarget[2] - m_fPos[2]); + Dir.Normalize(); - glLightf (light, GL_SPOT_CUTOFF, m_fCutoff); - glLightf (light, GL_SPOT_EXPONENT, m_fExponent); - glLightfv (light, GL_SPOT_DIRECTION, dir); + glLightf(light, GL_SPOT_CUTOFF, m_fCutoff); + glLightf(light, GL_SPOT_EXPONENT, m_fExponent); + glLightfv(light, GL_SPOT_DIRECTION, Dir); } } diff --git a/common/module.mk b/common/module.mk index c599d8dd..31ad8606 100644 --- a/common/module.mk +++ b/common/module.mk @@ -4,7 +4,7 @@ SRC += common/algebra.cpp common/camera.cpp common/console.cpp common/curve.cpp common/matrix.cpp common/message.cpp common/minifig.cpp common/object.cpp common/opengl.cpp \ common/piece.cpp common/pieceinf.cpp common/preview.cpp common/project.cpp common/quant.cpp \ common/str.cpp common/terrain.cpp common/texfont.cpp common/texture.cpp common/tr.cpp \ - common/vector.cpp common/view.cpp + common/view.cpp ifeq ($(HAVE_JPEGLIB), yes) LIBS += -ljpeg diff --git a/common/object.cpp b/common/object.cpp index c37449ac..07acd5c7 100755 --- a/common/object.cpp +++ b/common/object.cpp @@ -2,6 +2,7 @@ // #include "lc_global.h" +#include "lc_math.h" #include #include #include @@ -9,7 +10,6 @@ #include "project.h" #include "object.h" #include "matrix.h" -#include "vector.h" #include "lc_file.h" #include "lc_application.h" @@ -34,11 +34,11 @@ static void GetPolyCoeffs (float x1, float y1, float z1, float x2, float y2, flo double LC_CLICKLINE::PointDistance (float *point) { - Vector op ((float)(point[0] - a1), (float)(point[1] - b1), (float)(point[2] - c1)); - Vector d ((float)a2, (float)b2, (float)c2); - float len = d.Length (); - d.Normalize (); - float t = op.Dot (d); + lcVector3 op((float)(point[0] - a1), (float)(point[1] - b1), (float)(point[2] - c1)); + lcVector3 d((float)a2, (float)b2, (float)c2); + float len = d.Length(); + d.Normalize(); + float t = lcDot(op, d); if (t > 0) { diff --git a/common/pieceinf.cpp b/common/pieceinf.cpp index ed9826aa..10706f1f 100644 --- a/common/pieceinf.cpp +++ b/common/pieceinf.cpp @@ -2,6 +2,7 @@ // #include "lc_global.h" +#include "lc_math.h" #include #include #include @@ -12,7 +13,6 @@ #include "project.h" #include "globals.h" #include "matrix.h" -#include "vector.h" #include "library.h" #include "lc_application.h" @@ -1711,27 +1711,27 @@ void PieceInfo::ZoomExtents(float Fov, float Aspect, float* EyePos) const EyePos[2] = NewEye[2]; } - Vector FrontVec, RightVec, UpVec; + lcVector3 FrontVec, RightVec, UpVec; // Calculate view matrix. - UpVec = Vector(Top[0], Top[1], Top[2]); + UpVec = lcVector3(Top[0], Top[1], Top[2]); UpVec.Normalize(); - FrontVec = Vector(Front[0], Front[1], Front[2]); + FrontVec = lcVector3(Front[0], Front[1], Front[2]); FrontVec.Normalize(); - RightVec = Vector(Side[0], Side[1], Side[2]); + RightVec = lcVector3(Side[0], Side[1], Side[2]); RightVec.Normalize(); - float ViewMat[16]; - ViewMat[0] = -RightVec[0]; ViewMat[4] = -RightVec[1]; ViewMat[8] = -RightVec[2]; ViewMat[12] = 0.0; - ViewMat[1] = UpVec[0]; ViewMat[5] = UpVec[1]; ViewMat[9] = UpVec[2]; ViewMat[13] = 0.0; - ViewMat[2] = -FrontVec[0]; ViewMat[6] = -FrontVec[1]; ViewMat[10] = -FrontVec[2]; ViewMat[14] = 0.0; - ViewMat[3] = 0.0; ViewMat[7] = 0.0; ViewMat[11] = 0.0; ViewMat[15] = 1.0; + float ViewMat[16]; + ViewMat[0] = -RightVec[0]; ViewMat[4] = -RightVec[1]; ViewMat[8] = -RightVec[2]; ViewMat[12] = 0.0; + ViewMat[1] = UpVec[0]; ViewMat[5] = UpVec[1]; ViewMat[9] = UpVec[2]; ViewMat[13] = 0.0; + ViewMat[2] = -FrontVec[0]; ViewMat[6] = -FrontVec[1]; ViewMat[10] = -FrontVec[2]; ViewMat[14] = 0.0; + ViewMat[3] = 0.0; ViewMat[7] = 0.0; ViewMat[11] = 0.0; ViewMat[15] = 1.0; - // Load ViewMatrix - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - glMultMatrixf(ViewMat); - glTranslatef(-NewEye[0], -NewEye[1], -NewEye[2]); + // Load ViewMatrix + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glMultMatrixf(ViewMat); + glTranslatef(-NewEye[0], -NewEye[1], -NewEye[2]); } // Used by the print catalog and HTML instructions functions. diff --git a/common/project.cpp b/common/project.cpp index 42afc6ff..85bfacc5 100644 --- a/common/project.cpp +++ b/common/project.cpp @@ -2,6 +2,7 @@ // #include "lc_global.h" +#include "lc_math.h" #include #include #include @@ -9,7 +10,6 @@ #include #include #include "opengl.h" -#include "vector.h" #include "matrix.h" #include "pieceinf.h" #include "texture.h" @@ -418,17 +418,15 @@ bool Project::FileLoad(lcFile* file, bool bUndo, bool bMerge) pCam->ChangeKey(1, true, false, tmp, LC_CK_TARGET); // Create up vector - Vector upvec(0,0,1), frontvec((float)(eye[0]-target[0]), (float)(eye[1]-target[1]), (float)(eye[2]-target[2])), sidevec; - frontvec.Normalize(); - if (frontvec == upvec) - sidevec = Vector(1,0,0); + lcVector3 UpVector(0, 0, 1), FrontVector((float)(eye[0] - target[0]), (float)(eye[1] - target[1]), (float)(eye[2] - target[2])), SideVector; + FrontVector.Normalize(); + if (FrontVector == UpVector) + SideVector = lcVector3(1, 0, 0); else - sidevec.Cross(frontvec, upvec); - upvec.Cross(sidevec, frontvec); - upvec.Normalize(); - upvec.ToFloat(tmp); - pCam->ChangeKey(1, false, false, tmp, LC_CK_UP); - pCam->ChangeKey(1, true, false, tmp, LC_CK_UP); + SideVector = lcCross(FrontVector, UpVector); + UpVector = lcNormalize(lcCross(SideVector, FrontVector)); + pCam->ChangeKey(1, false, false, UpVector, LC_CK_UP); + pCam->ChangeKey(1, true, false, UpVector, LC_CK_UP); } if (bMerge) diff --git a/common/vector.cpp b/common/vector.cpp deleted file mode 100644 index b992322e..00000000 --- a/common/vector.cpp +++ /dev/null @@ -1,105 +0,0 @@ -#include "lc_global.h" -#include -#include "vector.h" - -////////////////////////////////////////////////////////////////////// -// Construction/Destruction - -Vector::Vector() -{ - m_fPoint[0] = 0; - m_fPoint[1] = 0; - m_fPoint[2] = 0; -} - -Vector::Vector(float x, float y, float z) -{ - m_fPoint[0] = x; - m_fPoint[1] = y; - m_fPoint[2] = z; -} - -Vector::Vector(const float *point) -{ - m_fPoint[0] = point[0]; - m_fPoint[1] = point[1]; - m_fPoint[2] = point[2]; -} - -Vector::Vector(const float *p1, const float *p2) -{ - m_fPoint[0] = p2[0] - p1[0]; - m_fPoint[1] = p2[1] - p1[1]; - m_fPoint[2] = p2[2] - p1[2]; -} - -////////////////////////////////////////////////////////////////////// -// Operations - -Vector& Vector::operator*=(float scalar) -{ - m_fPoint[0] *= scalar; - m_fPoint[1] *= scalar; - m_fPoint[2] *= scalar; - return *this; -} - -Vector& Vector::operator+=(const Vector& add) -{ - m_fPoint[0] += add.m_fPoint[0]; - m_fPoint[1] += add.m_fPoint[1]; - m_fPoint[2] += add.m_fPoint[2]; - return *this; -} - -Vector& Vector::operator-=(const Vector& sub) -{ - m_fPoint[0] -= sub.m_fPoint[0]; - m_fPoint[1] -= sub.m_fPoint[1]; - m_fPoint[2] -= sub.m_fPoint[2]; - return *this; -} - -float Vector::Length() -{ - return (float)sqrt(m_fPoint[0]*m_fPoint[0] + m_fPoint[1]*m_fPoint[1] + m_fPoint[2]*m_fPoint[2]); -} - -void Vector::Normalize() -{ - float inv = 1.0f / Length(); - m_fPoint[0] *= inv; - m_fPoint[1] *= inv; - m_fPoint[2] *= inv; -} - -Vector& Vector::Cross(const Vector& v1, const Vector& v2) -{ - m_fPoint[0] = v1.m_fPoint[1]*v2.m_fPoint[2] - v1.m_fPoint[2]*v2.m_fPoint[1]; - m_fPoint[1] = v1.m_fPoint[2]*v2.m_fPoint[0] - v1.m_fPoint[0]*v2.m_fPoint[2]; - m_fPoint[2] = v1.m_fPoint[0]*v2.m_fPoint[1] - v1.m_fPoint[1]*v2.m_fPoint[0]; - return *this; -} - -float Vector::Angle(const Vector& vec) -{ - double d, m1, m2; - - d = m_fPoint[0]*vec.m_fPoint[0]+m_fPoint[1]*vec.m_fPoint[1]+m_fPoint[2]*vec.m_fPoint[2]; - m1 = sqrt(m_fPoint[0]*m_fPoint[0]+m_fPoint[1]*m_fPoint[1]+m_fPoint[2]*m_fPoint[2]); - m2 = sqrt(vec.m_fPoint[0]*vec.m_fPoint[0]+vec.m_fPoint[1]*vec.m_fPoint[1]+vec.m_fPoint[2]*vec.m_fPoint[2]); - - return (float)(RTOD * acos(d / (m1*m2))); -} - -float Vector::Dot(const Vector& vec) -{ - return m_fPoint[0]*vec.m_fPoint[0]+m_fPoint[1]*vec.m_fPoint[1]+m_fPoint[2]*vec.m_fPoint[2]; -} - -void Vector::ToFloat(float *point) -{ - point[0] = m_fPoint[0]; - point[1] = m_fPoint[1]; - point[2] = m_fPoint[2]; -} diff --git a/common/vector.h b/common/vector.h deleted file mode 100644 index 47f96651..00000000 --- a/common/vector.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef _VECTOR_H_ -#define _VECTOR_H_ - -class Vector -{ -public: - Vector(); - Vector(float x, float y, float z); - Vector(const float *point); - Vector(const float *p1, const float *p2); - ~Vector() { }; - - float Dot(const Vector& vec); - float Angle(const Vector& vec); - Vector& Cross(const Vector& v1, const Vector& v2); - Vector& operator+=(const Vector& add); - Vector& operator-=(const Vector& sub); - Vector& operator*=(float scalar); - - operator const float*() const - { return m_fPoint; } - void ToFloat(float *point); - float Length(); - void Normalize(); - -protected: - float m_fPoint[3]; -}; - -#endif // _VECTOR_H_ diff --git a/win/Terrwnd.cpp b/win/Terrwnd.cpp index 3e8c80d2..61ddca9a 100644 --- a/win/Terrwnd.cpp +++ b/win/Terrwnd.cpp @@ -6,7 +6,6 @@ #include "camera.h" #include "Tools.h" #include "Matrix.h" -#include "Vector.h" #ifdef _DEBUG #define new DEBUG_NEW diff --git a/win/leocad.vcxproj b/win/leocad.vcxproj index 8451413f..4fccb6de 100644 --- a/win/leocad.vcxproj +++ b/win/leocad.vcxproj @@ -200,7 +200,6 @@ - @@ -315,6 +314,7 @@ + @@ -396,7 +396,6 @@ - diff --git a/win/leocad.vcxproj.filters b/win/leocad.vcxproj.filters index b795c5a2..672c92a8 100644 --- a/win/leocad.vcxproj.filters +++ b/win/leocad.vcxproj.filters @@ -266,9 +266,6 @@ Common Source Files - - Common Source Files - Common Source Files @@ -810,9 +807,6 @@ Common Header Files - - Common Header Files - Common Header Files @@ -834,6 +828,9 @@ Common Header Files + + Common Header Files +