Replaced old Vector class.

This commit is contained in:
leo 2012-03-29 01:10:55 +00:00
parent c8cb684299
commit 3e5a0a447d
13 changed files with 365 additions and 314 deletions

View file

@ -1,13 +1,13 @@
// Camera object.
#include "lc_global.h"
#include "lc_math.h"
#include "lc_colors.h"
#include <stdlib.h>
#include <string.h>
#include <math.h>
#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);

View file

@ -2,6 +2,7 @@
//
#include "lc_global.h"
#include "lc_math.h"
#include "lc_colors.h"
#include <stdlib.h>
#include <math.h>
@ -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];

190
common/lc_math.h Normal file
View file

@ -0,0 +1,190 @@
#ifndef _LC_MATH_H_
#define _LC_MATH_H_
#include <math.h>
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_

View file

@ -1,6 +1,7 @@
// Light object.
#include "lc_global.h"
#include "lc_math.h"
#include "lc_colors.h"
#include <stdlib.h>
#include <string.h>
@ -8,7 +9,6 @@
#include <math.h>
#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);
}
}

View file

@ -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

View file

@ -2,6 +2,7 @@
//
#include "lc_global.h"
#include "lc_math.h"
#include <stdlib.h>
#include <float.h>
#include <math.h>
@ -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)
{

View file

@ -2,6 +2,7 @@
//
#include "lc_global.h"
#include "lc_math.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
@ -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.

View file

@ -2,6 +2,7 @@
//
#include "lc_global.h"
#include "lc_math.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@ -9,7 +10,6 @@
#include <math.h>
#include <locale.h>
#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)

View file

@ -1,105 +0,0 @@
#include "lc_global.h"
#include <math.h>
#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];
}

View file

@ -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_

View file

@ -6,7 +6,6 @@
#include "camera.h"
#include "Tools.h"
#include "Matrix.h"
#include "Vector.h"
#ifdef _DEBUG
#define new DEBUG_NEW

View file

@ -200,7 +200,6 @@
<ClCompile Include="..\common\texfont.cpp" />
<ClCompile Include="..\Common\texture.cpp" />
<ClCompile Include="..\Common\Tr.cpp" />
<ClCompile Include="..\Common\vector.cpp" />
<ClCompile Include="..\common\view.cpp" />
</ItemGroup>
<ItemGroup>
@ -315,6 +314,7 @@
<ClInclude Include="..\common\lc_colors.h" />
<ClInclude Include="..\common\lc_file.h" />
<ClInclude Include="..\common\lc_global.h" />
<ClInclude Include="..\common\lc_math.h" />
<ClInclude Include="AboutDlg.h" />
<ClInclude Include="ArrayDlg.h" />
<ClInclude Include="CADBar.h" />
@ -396,7 +396,6 @@
<ClInclude Include="..\Common\texture.h" />
<ClInclude Include="..\Common\Tr.h" />
<ClInclude Include="..\Common\typedefs.h" />
<ClInclude Include="..\Common\vector.h" />
<ClInclude Include="..\common\view.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

View file

@ -266,9 +266,6 @@
<ClCompile Include="..\Common\Tr.cpp">
<Filter>Common Source Files</Filter>
</ClCompile>
<ClCompile Include="..\Common\vector.cpp">
<Filter>Common Source Files</Filter>
</ClCompile>
<ClCompile Include="..\common\view.cpp">
<Filter>Common Source Files</Filter>
</ClCompile>
@ -810,9 +807,6 @@
<ClInclude Include="..\Common\typedefs.h">
<Filter>Common Header Files</Filter>
</ClInclude>
<ClInclude Include="..\Common\vector.h">
<Filter>Common Header Files</Filter>
</ClInclude>
<ClInclude Include="..\common\view.h">
<Filter>Common Header Files</Filter>
</ClInclude>
@ -834,6 +828,9 @@
<ClInclude Include="..\common\lc_colors.h">
<Filter>Common Header Files</Filter>
</ClInclude>
<ClInclude Include="..\common\lc_math.h">
<Filter>Common Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<CustomBuild Include="hlp\LeoCAD.hpj">