Store the view matrix for each camera.

This commit is contained in:
leo 2012-05-28 23:33:22 +00:00
parent 8cd715b528
commit e9615a24e7
10 changed files with 630 additions and 443 deletions

View file

@ -283,6 +283,8 @@ public:
: m_Value(_v) { } : m_Value(_v) { }
inline explicit Vector3(const float _x, const float _y, const float _z) inline explicit Vector3(const float _x, const float _y, const float _z)
: m_Value(_x, _y, _z) { } : m_Value(_x, _y, _z) { }
inline explicit Vector3(const float* xyz)
: m_Value(xyz[0], xyz[1], xyz[2]) { }
inline operator const float*() const { return (const float*)this; } inline operator const float*() const { return (const float*)this; }
inline operator float*() { return (float*)this; } inline operator float*() { return (float*)this; }

View file

@ -8,7 +8,6 @@
#include <math.h> #include <math.h>
#include "opengl.h" #include "opengl.h"
#include "globals.h" #include "globals.h"
#include "matrix.h"
#include "lc_file.h" #include "lc_file.h"
#include "camera.h" #include "camera.h"
#include "tr.h" #include "tr.h"
@ -218,7 +217,7 @@ void Camera::Initialize()
for (unsigned char i = 0 ; i < sizeof(m_strName) ; i++ ) for (unsigned char i = 0 ; i < sizeof(m_strName) ; i++ )
m_strName[i] = 0; m_strName[i] = 0;
float *values[] = { m_fEye, m_fTarget, m_fUp }; float *values[] = { mPosition, mTargetPosition, mUpVector };
RegisterKeys (values, camera_key_info, LC_CK_COUNT); RegisterKeys (values, camera_key_info, LC_CK_COUNT);
m_pTarget = new CameraTarget (this); m_pTarget = new CameraTarget (this);
@ -414,47 +413,38 @@ void Camera::FileSave(lcFile& file) const
void Camera::Move(unsigned short nTime, bool bAnimation, bool bAddKey, float dx, float dy, float dz) void Camera::Move(unsigned short nTime, bool bAnimation, bool bAddKey, float dx, float dy, float dz)
{ {
lcVector3 Move(dx, dy, dz);
if (IsSide()) if (IsSide())
{ {
m_fEye[0] += dx; mPosition += Move;
m_fEye[1] += dy; mTargetPosition += Move;
m_fEye[2] += dz;
m_fTarget[0] += dx;
m_fTarget[1] += dy;
m_fTarget[2] += dz;
ChangeKey(nTime, bAnimation, bAddKey, m_fEye, LC_CK_EYE); ChangeKey(nTime, bAnimation, bAddKey, mPosition, LC_CK_EYE);
ChangeKey(nTime, bAnimation, bAddKey, m_fTarget, LC_CK_TARGET); ChangeKey(nTime, bAnimation, bAddKey, mTargetPosition, LC_CK_TARGET);
} }
else else
{ {
if (IsEyeSelected()) if (IsEyeSelected())
{ {
m_fEye[0] += dx; mPosition += Move;
m_fEye[1] += dy;
m_fEye[2] += dz;
ChangeKey(nTime, bAnimation, bAddKey, m_fEye, LC_CK_EYE); ChangeKey(nTime, bAnimation, bAddKey, mPosition, LC_CK_EYE);
} }
if (IsTargetSelected()) if (IsTargetSelected())
{ {
m_fTarget[0] += dx; mTargetPosition += Move;
m_fTarget[1] += dy;
m_fTarget[2] += dz;
ChangeKey(nTime, bAnimation, bAddKey, m_fTarget, LC_CK_TARGET); ChangeKey(nTime, bAnimation, bAddKey, mTargetPosition, LC_CK_TARGET);
} }
// Fix the up vector // Fix the up vector
lcVector3 UpVector(m_fUp[0], m_fUp[1], m_fUp[2]), SideVector; lcVector3 FrontVector(mTargetPosition - mPosition);
lcVector3 FrontVector(m_fTarget[0] - m_fEye[0], m_fTarget[1] - m_fEye[1], m_fTarget[2] - m_fEye[2]); lcVector3 SideVector = lcCross(FrontVector, mUpVector);
SideVector = lcCross(FrontVector, UpVector); mUpVector = lcNormalize(lcCross(SideVector, FrontVector));
UpVector = lcCross(SideVector, FrontVector);
UpVector.Normalize();
memcpy(m_fUp, UpVector, sizeof(m_fUp));
ChangeKey(nTime, bAnimation, bAddKey, m_fUp, LC_CK_UP); ChangeKey(nTime, bAnimation, bAddKey, mUpVector, LC_CK_UP);
} }
} }
@ -514,31 +504,28 @@ void Camera::UpdatePosition(unsigned short nTime, bool bAnimation)
{ {
CalculateKeys(nTime, bAnimation); CalculateKeys(nTime, bAnimation);
lcVector3 FrontVector(mPosition - mTargetPosition);
lcVector3 SideVector = lcCross(FrontVector, mUpVector);
mUpVector = lcNormalize(lcCross(SideVector, FrontVector));
mWorldView = lcMatrix44LookAt(mPosition, mTargetPosition, mUpVector);
UpdateBoundingBox(); UpdateBoundingBox();
} }
void Camera::UpdateBoundingBox() void Camera::UpdateBoundingBox()
{ {
// Fix the up vector // Fix the up vector
lcVector3 FrontVector(m_fEye[0] - m_fTarget[0], m_fEye[1] - m_fTarget[1], m_fEye[2] - m_fTarget[2]); lcVector3 FrontVector(mPosition - mTargetPosition);
lcVector3 UpVector(m_fUp[0], m_fUp[1], m_fUp[2]), SideVector;
SideVector = lcCross(FrontVector, UpVector);
UpVector = lcCross(SideVector, FrontVector);
UpVector.Normalize();
memcpy(m_fUp, UpVector, sizeof(m_fUp));
float len = FrontVector.Length(); float len = FrontVector.Length();
Matrix mat; lcMatrix44 Mat = lcMatrix44AffineInverse(mWorldView);
mat.CreateLookat (m_fEye, m_fTarget, m_fUp);
mat.Invert ();
mat.SetTranslation (m_fEye[0], m_fEye[1], m_fEye[2]); Mat.SetTranslation(mPosition);
BoundingBoxCalculate (&mat); BoundingBoxCalculate((Matrix*)&Mat);
mat.SetTranslation (m_fTarget[0], m_fTarget[1], m_fTarget[2]); Mat.SetTranslation(mTargetPosition);
m_pTarget->BoundingBoxCalculate (&mat); m_pTarget->BoundingBoxCalculate((Matrix*)&Mat);
mat.SetTranslation (0, 0, 0); Mat.SetTranslation(lcVector3(0, 0, 0));
if (!m_nList) if (!m_nList)
return; return;
@ -546,11 +533,12 @@ void Camera::UpdateBoundingBox()
glNewList(m_nList, GL_COMPILE); glNewList(m_nList, GL_COMPILE);
glPushMatrix(); glPushMatrix();
glTranslatef(m_fEye[0], m_fEye[1], m_fEye[2]); glTranslatef(mPosition[0], mPosition[1], mPosition[2]);
glMultMatrixf(mat.m); glMultMatrixf(Mat);
glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_VERTEX_ARRAY);
float verts[34][3] = { float verts[34][3] =
{
{ 0.3f, 0.3f, 0.3f }, { -0.3f, 0.3f, 0.3f }, { 0.3f, 0.3f, 0.3f }, { -0.3f, 0.3f, 0.3f },
{ -0.3f, 0.3f, 0.3f }, { -0.3f, -0.3f, 0.3f }, { -0.3f, 0.3f, 0.3f }, { -0.3f, -0.3f, 0.3f },
{ -0.3f, -0.3f, 0.3f }, { 0.3f, -0.3f, 0.3f }, { -0.3f, -0.3f, 0.3f }, { 0.3f, -0.3f, 0.3f },
@ -567,7 +555,9 @@ void Camera::UpdateBoundingBox()
{ 0.0f, 0.0f, -0.3f }, { -0.3f, -0.3f, -0.6f }, { 0.0f, 0.0f, -0.3f }, { -0.3f, -0.3f, -0.6f },
{ 0.3f, -0.3f, -0.6f }, { 0.0f, 0.0f, -0.3f }, { 0.3f, -0.3f, -0.6f }, { 0.0f, 0.0f, -0.3f },
{ 0.3f, 0.3f, -0.6f }, { 0.3f, -0.3f, -0.6f }, { 0.3f, 0.3f, -0.6f }, { 0.3f, -0.3f, -0.6f },
{ 0.3f, 0.3f, -0.6f }, { -0.3f, 0.3f, -0.6f } }; { 0.3f, 0.3f, -0.6f }, { -0.3f, 0.3f, -0.6f }
};
glVertexPointer (3, GL_FLOAT, 0, verts); glVertexPointer (3, GL_FLOAT, 0, verts);
glDrawArrays(GL_LINES, 0, 24); glDrawArrays(GL_LINES, 0, 24);
glDrawArrays(GL_LINE_STRIP, 24, 10); glDrawArrays(GL_LINE_STRIP, 24, 10);
@ -587,7 +577,9 @@ void Camera::UpdateBoundingBox()
glNewList (m_nTargetList, GL_COMPILE); glNewList (m_nTargetList, GL_COMPILE);
glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_VERTEX_ARRAY);
float box[24][3] = {
float box[24][3] =
{
{ 0.2f, 0.2f, 0.2f }, { -0.2f, 0.2f, 0.2f }, { 0.2f, 0.2f, 0.2f }, { -0.2f, 0.2f, 0.2f },
{ -0.2f, 0.2f, 0.2f }, { -0.2f, -0.2f, 0.2f }, { -0.2f, 0.2f, 0.2f }, { -0.2f, -0.2f, 0.2f },
{ -0.2f, -0.2f, 0.2f }, { 0.2f, -0.2f, 0.2f }, { -0.2f, -0.2f, 0.2f }, { 0.2f, -0.2f, 0.2f },
@ -599,7 +591,9 @@ void Camera::UpdateBoundingBox()
{ 0.2f, 0.2f, 0.2f }, { 0.2f, 0.2f, -0.2f }, { 0.2f, 0.2f, 0.2f }, { 0.2f, 0.2f, -0.2f },
{ -0.2f, 0.2f, 0.2f }, { -0.2f, 0.2f, -0.2f }, { -0.2f, 0.2f, 0.2f }, { -0.2f, 0.2f, -0.2f },
{ -0.2f, -0.2f, 0.2f }, { -0.2f, -0.2f, -0.2f }, { -0.2f, -0.2f, 0.2f }, { -0.2f, -0.2f, -0.2f },
{ 0.2f, -0.2f, 0.2f }, { 0.2f, -0.2f, -0.2f } }; { 0.2f, -0.2f, 0.2f }, { 0.2f, -0.2f, -0.2f }
};
glVertexPointer (3, GL_FLOAT, 0, box); glVertexPointer (3, GL_FLOAT, 0, box);
glDrawArrays(GL_LINES, 0, 24); glDrawArrays(GL_LINES, 0, 24);
glPopMatrix(); glPopMatrix();
@ -650,25 +644,22 @@ void Camera::Render(float fLineWidth)
lcSetColorCamera(); lcSetColorCamera();
glBegin(GL_LINES); glBegin(GL_LINES);
glVertex3fv(m_fEye); glVertex3fv(mPosition);
glVertex3fv(m_fTarget); glVertex3fv(mTargetPosition);
glEnd(); glEnd();
if (IsSelected()) if (IsSelected())
{ {
Matrix projection, modelview; lcVector3 FrontVector(mTargetPosition - mPosition);
lcVector3 FrontVector(m_fTarget[0] - m_fEye[0], m_fTarget[1] - m_fEye[1], m_fTarget[2] - m_fEye[2]);
float len = FrontVector.Length(); float len = FrontVector.Length();
glPushMatrix (); glPushMatrix ();
modelview.CreateLookat (m_fEye, m_fTarget, m_fUp); lcMatrix44 ViewWorld = lcMatrix44AffineInverse(mWorldView);
modelview.Invert (); glMultMatrixf(ViewWorld);
glMultMatrixf (modelview.m);
projection.CreatePerspective (m_fovy, 1.33f, 0.01f, len); lcMatrix44 InvProjection = lcMatrix44Inverse(lcMatrix44Perspective(m_fovy, 1.33f, 0.01f, len));
projection.Invert (); glMultMatrixf(InvProjection);
glMultMatrixf (projection.m);
// draw the viewing frustum // draw the viewing frustum
glBegin(GL_LINE_LOOP); glBegin(GL_LINE_LOOP);
@ -731,89 +722,66 @@ void Camera::LoadProjection(float fAspect)
} }
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); glLoadMatrixf(mWorldView);
gluLookAt(m_fEye[0], m_fEye[1], m_fEye[2], m_fTarget[0], m_fTarget[1], m_fTarget[2], m_fUp[0], m_fUp[1], m_fUp[2]);
} }
void Camera::DoZoom(int dy, int mouse, unsigned short nTime, bool bAnimation, bool bAddKey) void Camera::DoZoom(int dy, int mouse, unsigned short nTime, bool bAnimation, bool bAddKey)
{ {
lcVector3 FrontVector(m_fEye[0] - m_fTarget[0], m_fEye[1] - m_fTarget[1], m_fEye[2] - m_fTarget[2]); lcVector3 FrontVector(mPosition - mTargetPosition);
FrontVector.Normalize(); FrontVector.Normalize();
FrontVector *= 2.0f * dy / (21 - mouse); FrontVector *= 2.0f * dy / (21 - mouse);
// TODO: option to move eye, target or both // TODO: option to move eye, target or both
m_fEye[0] += FrontVector[0]; mPosition += FrontVector;
m_fEye[1] += FrontVector[1]; mTargetPosition += FrontVector;
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, mPosition, LC_CK_EYE);
ChangeKey(nTime, bAnimation, bAddKey, m_fTarget, LC_CK_TARGET); ChangeKey(nTime, bAnimation, bAddKey, mTargetPosition, LC_CK_TARGET);
UpdatePosition(nTime, bAnimation); UpdatePosition(nTime, bAnimation);
} }
void Camera::DoPan(int dx, int dy, int mouse, unsigned short nTime, bool bAnimation, bool bAddKey) void Camera::DoPan(int dx, int dy, int mouse, unsigned short nTime, bool bAnimation, bool bAddKey)
{ {
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; lcVector3 FrontVector(mPosition - mTargetPosition);
SideVector = lcCross(FrontVector, UpVector); lcVector3 SideVector = lcNormalize(lcCross(FrontVector, mUpVector));
SideVector.Normalize();
SideVector *= 2.0f*dx/(21-mouse);
UpVector.Normalize();
UpVector *= -2.0f*dy/(21-mouse);
m_fEye[0] += UpVector[0] + SideVector[0]; lcVector3 Move = (SideVector * (2.0f * dx / (21 - mouse))) + (mUpVector * (-2.0f * dy / (21 - mouse)));
m_fEye[1] += UpVector[1] + SideVector[1]; mPosition += Move;
m_fEye[2] += UpVector[2] + SideVector[2]; mTargetPosition += Move;
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, mPosition, LC_CK_EYE);
ChangeKey(nTime, bAnimation, bAddKey, m_fTarget, LC_CK_TARGET); ChangeKey(nTime, bAnimation, bAddKey, mTargetPosition, LC_CK_TARGET);
UpdatePosition(nTime, bAnimation); UpdatePosition(nTime, bAnimation);
} }
void Camera::DoRotate(int dx, int dy, int mouse, unsigned short nTime, bool bAnimation, bool bAddKey, float* /*center*/) void Camera::DoRotate(int dx, int dy, int mouse, unsigned short nTime, bool bAnimation, bool bAddKey, float* /*center*/)
{ {
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; lcVector3 FrontVector(mPosition - mTargetPosition);
SideVector = lcCross(FrontVector, UpVector); lcVector3 SideVector = lcNormalize(lcCross(FrontVector, mUpVector));
SideVector.Normalize();
SideVector *= 2.0f*dx/(21-mouse);
UpVector.Normalize();
UpVector *= -2.0f*dy/(21-mouse);
// TODO: option to move eye or target // TODO: option to move eye or target
float len = FrontVector.Length(); float len = FrontVector.Length();
FrontVector += lcVector3(UpVector[0] + SideVector[0], UpVector[1] + SideVector[1], UpVector[2] + SideVector[2]); FrontVector += (SideVector * (2.0f * dx / (21 - mouse))) + (mUpVector * (-2.0f * dy / (21 - mouse)));
FrontVector.Normalize(); FrontVector.Normalize();
FrontVector *= len; mPosition = (FrontVector * len) + mTargetPosition;
FrontVector += lcVector3(m_fTarget[0], m_fTarget[1], m_fTarget[2]);
memcpy(m_fEye, FrontVector, sizeof(m_fEye));
// Calculate new up // Calculate new up
UpVector = lcVector3(m_fUp[0], m_fUp[1], m_fUp[2]); FrontVector = mPosition - mTargetPosition;
FrontVector = lcVector3(m_fEye[0] - m_fTarget[0], m_fEye[1] - m_fTarget[1], m_fEye[2] - m_fTarget[2]); SideVector = lcCross(FrontVector, mUpVector);
SideVector = lcCross(FrontVector, UpVector); mUpVector = lcNormalize(lcCross(SideVector, FrontVector));
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, mPosition, LC_CK_EYE);
ChangeKey(nTime, bAnimation, bAddKey, m_fUp, LC_CK_UP); ChangeKey(nTime, bAnimation, bAddKey, mUpVector, LC_CK_UP);
UpdatePosition(nTime, bAnimation); UpdatePosition(nTime, bAnimation);
} }
void Camera::DoRoll(int dx, int mouse, unsigned short nTime, bool bAnimation, bool bAddKey) void Camera::DoRoll(int dx, int mouse, unsigned short nTime, bool bAnimation, bool bAddKey)
{ {
Matrix mat; lcVector3 FrontVector(mPosition - mTargetPosition);
float front[3] = { m_fEye[0]-m_fTarget[0], m_fEye[1]-m_fTarget[1], m_fEye[2]-m_fTarget[2] }; lcMatrix44 Rotation = lcMatrix44FromAxisAngle(FrontVector, 2.0f * dx / (21 - mouse) * LC_DTOR);
mat.FromAxisAngle(front, 2.0f*dx/(21-mouse)); mUpVector = lcMul30(mUpVector, Rotation);
mat.TransformPoints(m_fUp, 1); ChangeKey(nTime, bAnimation, bAddKey, mUpVector, LC_CK_UP);
ChangeKey(nTime, bAnimation, bAddKey, m_fUp, LC_CK_UP);
UpdatePosition(nTime, bAnimation); UpdatePosition(nTime, bAnimation);
} }

View file

@ -3,7 +3,7 @@
#include "opengl.h" #include "opengl.h"
#include "object.h" #include "object.h"
#include "algebra.h" #include "lc_math.h"
#define LC_CAMERA_HIDDEN 0x01 #define LC_CAMERA_HIDDEN 0x01
#define LC_CAMERA_SELECTED 0x02 #define LC_CAMERA_SELECTED 0x02
@ -69,13 +69,6 @@ public:
virtual ~Camera (); virtual ~Camera ();
// Query functions. // Query functions.
inline Vector3 GetEyePosition() const
{ return Vector3(m_fEye[0], m_fEye[1], m_fEye[2]); };
inline Vector3 GetTargetPosition() const
{ return Vector3(m_fTarget[0], m_fTarget[1], m_fTarget[2]); };
inline Vector3 GetUpVector() const
{ return Vector3(m_fUp[0], m_fUp[1], m_fUp[2]); };
const char* GetName() const const char* GetName() const
{ return m_strName; }; { return m_strName; };
@ -83,22 +76,6 @@ public:
{ return m_pTarget; } { return m_pTarget; }
// Deprecated functions:
const float* GetEyePos () const
{ return m_fEye; };
void GetEyePos (float* eye) const
{ memcpy(eye, m_fEye, sizeof(m_fEye)); };
const float* GetTargetPos () const
{ return m_fTarget; };
void GetTargetPos (float* target) const
{ memcpy(target, m_fTarget, sizeof(m_fTarget)); };
const float* GetUpVec () const
{ return m_fUp; };
void GetUpVec (float* up) const
{ memcpy(up, m_fUp, sizeof(m_fUp)); };
@ -169,6 +146,11 @@ public:
float m_zNear; float m_zNear;
float m_zFar; float m_zFar;
lcMatrix44 mWorldView;
lcVector3 mPosition;
lcVector3 mTargetPosition;
lcVector3 mUpVector;
protected: protected:
void Initialize(); void Initialize();
void UpdateBoundingBox(); void UpdateBoundingBox();
@ -183,11 +165,6 @@ protected:
GLuint m_nList; GLuint m_nList;
static GLuint m_nTargetList; static GLuint m_nTargetList;
// Current position and orientation.
float m_fEye[3];
float m_fTarget[3];
float m_fUp[3];
TiledRender* m_pTR; TiledRender* m_pTR;
}; };

View file

@ -46,8 +46,8 @@ public:
} }
void Normalize(); void Normalize();
void Dot();
float Length() const; float Length() const;
float LengthSquared() const;
float x, y, z; float x, y, z;
}; };
@ -64,6 +64,11 @@ public:
{ {
} }
lcVector4(const lcVector3& _xyz, const float _w)
: x(_xyz.x), y(_xyz.y), z(_xyz.z), w(_w)
{
}
operator const float*() const operator const float*() const
{ {
return (const float*)this; return (const float*)this;
@ -233,6 +238,11 @@ inline float lcVector3::Length() const
return sqrtf(x * x + y * y + z * z); return sqrtf(x * x + y * y + z * z);
} }
inline float lcVector3::LengthSquared() const
{
return x * x + y * y + z * z;
}
inline lcVector3 lcNormalize(const lcVector3& a) inline lcVector3 lcNormalize(const lcVector3& a)
{ {
lcVector3 Ret(a); lcVector3 Ret(a);
@ -285,6 +295,26 @@ inline lcVector4 operator/(const lcVector4& a, const lcVector4& b)
return lcVector4(a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w); return lcVector4(a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w);
} }
inline lcVector4& operator*=(lcVector4& a, float b)
{
a.x *= b;
a.y *= b;
a.z *= b;
a.w *= b;
return a;
}
inline lcVector4& operator/=(lcVector4& a, float b)
{
a.x /= b;
a.y /= b;
a.z /= b;
a.w /= b;
return a;
}
inline lcVector3 lcMul31(const lcVector3& a, const lcMatrix44& b) inline lcVector3 lcMul31(const lcVector3& a, const lcMatrix44& b)
{ {
lcVector4 v = b.r[0] * a[0] + b.r[1] * a[1] + b.r[2] * a[2] + b.r[3]; lcVector4 v = b.r[0] * a[0] + b.r[1] * a[1] + b.r[2] * a[2] + b.r[3];
@ -394,6 +424,57 @@ inline lcMatrix44 lcMatrix44RotationZ(const float Radians)
return m; return m;
} }
inline lcMatrix44 lcMatrix44LookAt(const lcVector3& Eye, const lcVector3& Target, const lcVector3& Up)
{
lcVector3 x, y, z;
z = lcNormalize(Eye - Target);
x = lcNormalize(lcCross(Up, z));
y = lcNormalize(lcCross(z, x));
lcMatrix44 m;
m.r[0] = lcVector4(x[0], y[0], z[0], 0.0f);
m.r[1] = lcVector4(x[1], y[1], z[1], 0.0f);
m.r[2] = lcVector4(x[2], y[2], z[2], 0.0f);
m.r[3] = m.r[0] * -Eye[0] + m.r[1] * -Eye[1] + m.r[2] * -Eye[2];
m.r[3][3] = 1.0f;
return m;
}
inline lcMatrix44 lcMatrix44Perspective(float FoVy, float Aspect, float Near, float Far)
{
float Left, Right, Bottom, Top;
Top = Near * (float)tan(FoVy * LC_PI / 360.0f);
Bottom = -Top;
Left = Bottom * Aspect;
Right = Top * Aspect;
if ((Near <= 0.0f) || (Far <= 0.0f) || (Near == Far) || (Left == Right) || (Top == Bottom))
return lcMatrix44Identity();
float x, y, a, b, c, d;
x = (2.0f * Near) / (Right - Left);
y = (2.0f * Near) / (Top - Bottom);
a = (Right + Left) / (Right - Left);
b = (Top + Bottom) / (Top - Bottom);
c = -(Far + Near) / (Far - Near);
d = -(2.0f * Far * Near) / (Far - Near);
lcMatrix44 m;
m.r[0] = lcVector4(x, 0, 0, 0);
m.r[1] = lcVector4(0, y, 0, 0);
m.r[2] = lcVector4(a, b, c, -1);
m.r[3] = lcVector4(0, 0, d, 0);
return m;
}
inline lcMatrix44 lcMatrix44FromAxisAngle(const lcVector3& Axis, const float Radians) inline lcMatrix44 lcMatrix44FromAxisAngle(const lcVector3& Axis, const float Radians)
{ {
float s, c, mag, xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c; float s, c, mag, xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c;
@ -524,4 +605,193 @@ inline lcVector4 lcMatrix44ToAxisAngle(const lcMatrix44& m)
return rot; return rot;
} }
inline lcMatrix44 lcMatrix44AffineInverse(const lcMatrix44& m)
{
lcMatrix44 Inv;
Inv.r[0] = lcVector4(m.r[0][0], m.r[1][0], m.r[2][0], m.r[0][3]);
Inv.r[1] = lcVector4(m.r[0][1], m.r[1][1], m.r[2][1], m.r[1][3]);
Inv.r[2] = lcVector4(m.r[0][2], m.r[1][2], m.r[2][2], m.r[2][3]);
lcVector3 Trans = -lcMul30(lcVector3(Inv[3][0], Inv[3][1], Inv[3][2]), Inv);
Inv.r[3] = lcVector4(Trans[0], Trans[1], Trans[2], 1.0f);
return Inv;
}
// Inverse code from the GLU library.
inline lcMatrix44 lcMatrix44Inverse(const lcMatrix44& m)
{
#define SWAP_ROWS(a, b) { float *_tmp = a; (a)=(b); (b)=_tmp; }
#define MAT(m,col,row) m.r[row][col]
float wtmp[4][8];
float m0, m1, m2, m3, s;
float *r0, *r1, *r2, *r3;
r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];
r0[0] = MAT(m,0,0), r0[1] = MAT(m,0,1),
r0[2] = MAT(m,0,2), r0[3] = MAT(m,0,3),
r0[4] = 1.0, r0[5] = r0[6] = r0[7] = 0.0,
r1[0] = MAT(m,1,0), r1[1] = MAT(m,1,1),
r1[2] = MAT(m,1,2), r1[3] = MAT(m,1,3),
r1[5] = 1.0, r1[4] = r1[6] = r1[7] = 0.0,
r2[0] = MAT(m,2,0), r2[1] = MAT(m,2,1),
r2[2] = MAT(m,2,2), r2[3] = MAT(m,2,3),
r2[6] = 1.0, r2[4] = r2[5] = r2[7] = 0.0,
r3[0] = MAT(m,3,0), r3[1] = MAT(m,3,1),
r3[2] = MAT(m,3,2), r3[3] = MAT(m,3,3),
r3[7] = 1.0, r3[4] = r3[5] = r3[6] = 0.0;
// choose pivot - or die
if (fabs(r3[0])>fabs(r2[0])) SWAP_ROWS(r3, r2);
if (fabs(r2[0])>fabs(r1[0])) SWAP_ROWS(r2, r1);
if (fabs(r1[0])>fabs(r0[0])) SWAP_ROWS(r1, r0);
// if (0.0 == r0[0]) return GL_FALSE;
// eliminate first variable
m1 = r1[0]/r0[0]; m2 = r2[0]/r0[0]; m3 = r3[0]/r0[0];
s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
s = r0[4];
if (s != 0.0) { r1[4] -= m1 * s; r2[4] -= m2 * s; r3[4] -= m3 * s; }
s = r0[5];
if (s != 0.0) { r1[5] -= m1 * s; r2[5] -= m2 * s; r3[5] -= m3 * s; }
s = r0[6];
if (s != 0.0) { r1[6] -= m1 * s; r2[6] -= m2 * s; r3[6] -= m3 * s; }
s = r0[7];
if (s != 0.0) { r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s; }
// choose pivot - or die
if (fabs(r3[1])>fabs(r2[1])) SWAP_ROWS(r3, r2);
if (fabs(r2[1])>fabs(r1[1])) SWAP_ROWS(r2, r1);
// if (0.0 == r1[1]) return GL_FALSE;
// eliminate second variable
m2 = r2[1]/r1[1]; m3 = r3[1]/r1[1];
r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
s = r1[4]; if (0.0 != s) { r2[4] -= m2 * s; r3[4] -= m3 * s; }
s = r1[5]; if (0.0 != s) { r2[5] -= m2 * s; r3[5] -= m3 * s; }
s = r1[6]; if (0.0 != s) { r2[6] -= m2 * s; r3[6] -= m3 * s; }
s = r1[7]; if (0.0 != s) { r2[7] -= m2 * s; r3[7] -= m3 * s; }
// choose pivot - or die
if (fabs(r3[2])>fabs(r2[2])) SWAP_ROWS(r3, r2);
// if (0.0 == r2[2]) return GL_FALSE;
// eliminate third variable
m3 = r3[2]/r2[2];
r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4],
r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6],
r3[7] -= m3 * r2[7];
// last check
// if (0.0 == r3[3]) return GL_FALSE;
s = 1.0f/r3[3]; // now back substitute row 3
r3[4] *= s; r3[5] *= s; r3[6] *= s; r3[7] *= s;
m2 = r2[3]; // now back substitute row 2
s = 1.0f/r2[2];
r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2),
r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2);
m1 = r1[3];
r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1,
r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1;
m0 = r0[3];
r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0,
r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;
m1 = r1[2]; // now back substitute row 1
s = 1.0f/r1[1];
r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1),
r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1);
m0 = r0[2];
r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0,
r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;
m0 = r0[1]; // now back substitute row 0
s = 1.0f/r0[0];
r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0),
r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0);
lcVector4 Row0(r0[4], r1[4], r2[4], r3[4]);
lcVector4 Row1(r0[5], r1[5], r2[5], r3[5]);
lcVector4 Row2(r0[6], r1[6], r2[6], r3[6]);
lcVector4 Row3(r0[7], r1[7], r2[7], r3[7]);
lcMatrix44 out(Row0, Row1, Row2, Row3);
return out;
#undef MAT
#undef SWAP_ROWS
}
// Convert world coordinates to screen coordinates.
inline lcVector3 lcProjectPoint(const lcVector3& Point, const lcMatrix44& ModelView, const lcMatrix44& Projection, const int Viewport[4])
{
lcVector4 Tmp;
Tmp = lcMul4(lcVector4(Point[0], Point[1], Point[2], 1.0f), ModelView);
Tmp = lcMul4(Tmp, Projection);
// Normalize.
Tmp /= Tmp[3];
// Screen coordinates.
return lcVector3(Viewport[0] + (1 + Tmp[0]) * Viewport[2] / 2, Viewport[1] + (1 + Tmp[1]) * Viewport[3] / 2, (1 + Tmp[2]) / 2);
}
inline lcVector3 lcUnprojectPoint(const lcVector3& Point, const lcMatrix44& ModelView, const lcMatrix44& Projection, const int Viewport[4])
{
// Calculate the screen to model transform.
lcMatrix44 Transform = lcMatrix44Inverse(lcMul(ModelView, Projection));
lcVector4 Tmp;
// Convert the point to homogeneous coordinates.
Tmp[0] = (Point[0] - Viewport[0]) * 2.0f / Viewport[2] - 1.0f;
Tmp[1] = (Point[1] - Viewport[1]) * 2.0f / Viewport[3] - 1.0f;
Tmp[2] = Point[2] * 2.0f - 1.0f;
Tmp[3] = 1.0f;
Tmp = lcMul4(Tmp, Transform);
if (Tmp[3] != 0.0f)
Tmp /= Tmp[3];
return lcVector3(Tmp[0], Tmp[1], Tmp[2]);
}
inline void lcUnprojectPoints(lcVector3* Points, int NumPoints, const lcMatrix44& ModelView, const lcMatrix44& Projection, const int Viewport[4])
{
// Calculate the screen to model transform.
lcMatrix44 Transform = lcMatrix44Inverse(lcMul(ModelView, Projection));
for (int i = 0; i < NumPoints; i++)
{
lcVector4 Tmp;
// Convert the point to homogeneous coordinates.
Tmp[0] = (Points[i][0] - Viewport[0]) * 2.0f / Viewport[2] - 1.0f;
Tmp[1] = (Points[i][1] - Viewport[1]) * 2.0f / Viewport[3] - 1.0f;
Tmp[2] = Points[i][2] * 2.0f - 1.0f;
Tmp[3] = 1.0f;
Tmp = lcMul4(Tmp, Transform);
if (Tmp[3] != 0.0f)
Tmp /= Tmp[3];
Points[i] = lcVector3(Tmp[0], Tmp[1], Tmp[2]);
}
}
#endif // _LC_MATH_H_ #endif // _LC_MATH_H_

View file

@ -1764,9 +1764,7 @@ void Project::RenderScenePieces(View* view)
PtrArray<Piece> OpaquePieces(512); PtrArray<Piece> OpaquePieces(512);
ObjArray<lcTranslucentRenderSection> TranslucentSections(512); ObjArray<lcTranslucentRenderSection> TranslucentSections(512);
Matrix44 ModelView; const lcMatrix44& WorldView = view->m_Camera->mWorldView;
Camera* Cam = view->m_Camera;
ModelView.CreateLookAt(Cam->GetEyePosition(), Cam->GetTargetPosition(), Cam->GetUpVector());
for (Piece* pPiece = m_pPieces; pPiece; pPiece = pPiece->m_pNext) for (Piece* pPiece = m_pPieces; pPiece; pPiece = pPiece->m_pNext)
{ {
@ -1781,7 +1779,7 @@ void Project::RenderScenePieces(View* view)
if ((Info->m_nFlags & LC_PIECE_HAS_TRANSLUCENT) || ((Info->m_nFlags & LC_PIECE_HAS_DEFAULT) && Translucent)) if ((Info->m_nFlags & LC_PIECE_HAS_TRANSLUCENT) || ((Info->m_nFlags & LC_PIECE_HAS_DEFAULT) && Translucent))
{ {
Vector3 Pos = Mul31(pPiece->GetPosition(), ModelView); lcVector3 Pos = lcMul31(pPiece->mPosition, WorldView);
lcTranslucentRenderSection RenderSection; lcTranslucentRenderSection RenderSection;
@ -2006,7 +2004,7 @@ void Project::RenderSceneObjects(View* view)
// glClear(GL_DEPTH_BUFFER_BIT); // glClear(GL_DEPTH_BUFFER_BIT);
Matrix Mats[3]; Matrix Mats[3];
Mats[0].CreateLookat(view->m_Camera->GetEyePos(), view->m_Camera->GetTargetPos(), view->m_Camera->GetUpVec()); Mats[0].CreateLookat(view->m_Camera->mPosition, view->m_Camera->mTargetPosition, view->m_Camera->mUpVector);
Mats[0].SetTranslation(0, 0, 0); Mats[0].SetTranslation(0, 0, 0);
float m1[] = { 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }; float m1[] = { 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
@ -2229,7 +2227,6 @@ void Project::RenderOverlays(View* view)
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);
Camera* Cam = view->m_Camera; Camera* Cam = view->m_Camera;
Matrix44 Mat;
int j; int j;
// Find the rotation from the focused piece if relative snap is enabled. // Find the rotation from the focused piece if relative snap is enabled.
@ -2332,8 +2329,7 @@ void Project::RenderOverlays(View* view)
} }
} }
Mat.CreateLookAt(Cam->GetEyePosition(), Cam->GetTargetPosition(), Cam->GetUpVector()); lcMatrix44 Mat = lcMatrix44AffineInverse(Cam->mWorldView);
Mat.Transpose3();
Mat.SetTranslation(m_OverlayCenter); Mat.SetTranslation(m_OverlayCenter);
// Draw the circles. // Draw the circles.
@ -2342,29 +2338,28 @@ void Project::RenderOverlays(View* view)
for (j = 0; j < 32; j++) for (j = 0; j < 32; j++)
{ {
Vector3 Pt; lcVector3 Pt;
Pt[0] = cosf(LC_2PI * j / 32) * OverlayRotateRadius * OverlayScale; Pt[0] = cosf(LC_2PI * j / 32) * OverlayRotateRadius * OverlayScale;
Pt[1] = sinf(LC_2PI * j / 32) * OverlayRotateRadius * OverlayScale; Pt[1] = sinf(LC_2PI * j / 32) * OverlayRotateRadius * OverlayScale;
Pt[2] = 0.0f; Pt[2] = 0.0f;
Pt = Mul31(Pt, Mat); Pt = lcMul31(Pt, Mat);
glVertex3f(Pt[0], Pt[1], Pt[2]); glVertex3f(Pt[0], Pt[1], Pt[2]);
} }
glEnd(); glEnd();
Vector3 ViewDir = Cam->GetTargetPosition() - Cam->GetEyePosition(); lcVector3 ViewDir = Cam->mTargetPosition - Cam->mPosition;
ViewDir.Normalize(); ViewDir.Normalize();
// Transform ViewDir to local space. // Transform ViewDir to local space.
if (Focus) if (Focus)
{ {
Matrix33 RotMat; lcMatrix44 RotMat = lcMatrix44FromAxisAngle(lcVector3(Rot[0], Rot[1], Rot[2]), -Rot[3] * LC_DTOR);
RotMat.CreateFromAxisAngle(Vector3(Rot[0], Rot[1], Rot[2]), -Rot[3] * LC_DTOR);
ViewDir = Mul(ViewDir, RotMat); ViewDir = lcMul30(ViewDir, RotMat);
} }
glTranslatef(m_OverlayCenter[0], m_OverlayCenter[1], m_OverlayCenter[2]); glTranslatef(m_OverlayCenter[0], m_OverlayCenter[1], m_OverlayCenter[2]);
@ -2399,30 +2394,30 @@ void Project::RenderOverlays(View* view)
for (int j = 0; j < 32; j++) for (int j = 0; j < 32; j++)
{ {
Vector3 v1, v2; lcVector3 v1, v2;
switch (i) switch (i)
{ {
case 0: case 0:
v1 = Vector3(0.0f, cosf(LC_2PI * j / 32), sinf(LC_2PI * j / 32)); v1 = lcVector3(0.0f, cosf(LC_2PI * j / 32), sinf(LC_2PI * j / 32));
v2 = Vector3(0.0f, cosf(LC_2PI * (j + 1) / 32), sinf(LC_2PI * (j + 1) / 32)); v2 = lcVector3(0.0f, cosf(LC_2PI * (j + 1) / 32), sinf(LC_2PI * (j + 1) / 32));
break; break;
case 1: case 1:
v1 = Vector3(cosf(LC_2PI * j / 32), 0.0f, sinf(LC_2PI * j / 32)); v1 = lcVector3(cosf(LC_2PI * j / 32), 0.0f, sinf(LC_2PI * j / 32));
v2 = Vector3(cosf(LC_2PI * (j + 1) / 32), 0.0f, sinf(LC_2PI * (j + 1) / 32)); v2 = lcVector3(cosf(LC_2PI * (j + 1) / 32), 0.0f, sinf(LC_2PI * (j + 1) / 32));
break; break;
case 2: case 2:
v1 = Vector3(cosf(LC_2PI * j / 32), sinf(LC_2PI * j / 32), 0.0f); v1 = lcVector3(cosf(LC_2PI * j / 32), sinf(LC_2PI * j / 32), 0.0f);
v2 = Vector3(cosf(LC_2PI * (j + 1) / 32), sinf(LC_2PI * (j + 1) / 32), 0.0f); v2 = lcVector3(cosf(LC_2PI * (j + 1) / 32), sinf(LC_2PI * (j + 1) / 32), 0.0f);
break; break;
} }
if (Dot3(ViewDir, v1+v2) <= 0.0f) if (lcDot(ViewDir, v1 + v2) <= 0.0f)
{ {
Vector3 Pt1 = v1 * OverlayRotateRadius * OverlayScale; lcVector3 Pt1 = v1 * (OverlayRotateRadius * OverlayScale);
Vector3 Pt2 = v2 * OverlayRotateRadius * OverlayScale; lcVector3 Pt2 = v2 * (OverlayRotateRadius * OverlayScale);
glVertex3f(Pt1[0], Pt1[1], Pt1[2]); glVertex3f(Pt1[0], Pt1[1], Pt1[2]);
glVertex3f(Pt2[0], Pt2[1], Pt2[2]); glVertex3f(Pt2[0], Pt2[1], Pt2[2]);
@ -2437,23 +2432,23 @@ void Project::RenderOverlays(View* view)
{ {
if ((m_OverlayMode == LC_OVERLAY_X) || (m_OverlayMode == LC_OVERLAY_Y) || (m_OverlayMode == LC_OVERLAY_Z)) if ((m_OverlayMode == LC_OVERLAY_X) || (m_OverlayMode == LC_OVERLAY_Y) || (m_OverlayMode == LC_OVERLAY_Z))
{ {
Vector3 Tangent, Normal = m_OverlayTrackStart - m_OverlayCenter; lcVector3 Normal = lcNormalize(m_OverlayTrackStart - m_OverlayCenter);
Normal.Normalize(); lcVector3 Tangent;
float Angle; float Angle;
switch (m_OverlayMode) switch (m_OverlayMode)
{ {
case LC_OVERLAY_X: case LC_OVERLAY_X:
Angle = m_MouseTotalDelta[0]; Angle = m_MouseTotalDelta[0];
Tangent = Vector3(0.0f, -Normal[2], Normal[1]); Tangent = lcVector3(0.0f, -Normal[2], Normal[1]);
break; break;
case LC_OVERLAY_Y: case LC_OVERLAY_Y:
Angle = m_MouseTotalDelta[1]; Angle = m_MouseTotalDelta[1];
Tangent = Vector3(Normal[2], 0.0f, -Normal[0]); Tangent = lcVector3(Normal[2], 0.0f, -Normal[0]);
break; break;
case LC_OVERLAY_Z: case LC_OVERLAY_Z:
Angle = m_MouseTotalDelta[2]; Angle = m_MouseTotalDelta[2];
Tangent = Vector3(-Normal[1], Normal[0], 0.0f); Tangent = lcVector3(-Normal[1], Normal[0], 0.0f);
break; break;
} }
@ -2469,10 +2464,10 @@ void Project::RenderOverlays(View* view)
const float OverlayRotateArrowSize = 1.5f; const float OverlayRotateArrowSize = 1.5f;
const float OverlayRotateArrowCapSize = 0.25f; const float OverlayRotateArrowCapSize = 0.25f;
Vector3 Pt = Normal * OverlayScale * OverlayRotateRadius; lcVector3 Pt = Normal * (OverlayScale * OverlayRotateRadius);
Vector3 Tip = Pt + Tangent * OverlayScale * OverlayRotateArrowSize; lcVector3 Tip = Pt + Tangent * (OverlayScale * OverlayRotateArrowSize);
Vector3 Arrow; lcVector3 Arrow;
Matrix33 Rot; lcMatrix44 Rot;
glBegin(GL_LINES); glBegin(GL_LINES);
glColor3f(0.8f, 0.8f, 0.0f); glColor3f(0.8f, 0.8f, 0.0f);
@ -2480,14 +2475,14 @@ void Project::RenderOverlays(View* view)
glVertex3f(Pt[0], Pt[1], Pt[2]); glVertex3f(Pt[0], Pt[1], Pt[2]);
glVertex3f(Tip[0], Tip[1], Tip[2]); glVertex3f(Tip[0], Tip[1], Tip[2]);
Rot.CreateFromAxisAngle(Normal, LC_PI * 0.15f); Rot = lcMatrix44FromAxisAngle(Normal, LC_PI * 0.15f);
Arrow = Mul(Tangent, Rot) * OverlayRotateArrowCapSize; Arrow = lcMul30(Tangent, Rot) * OverlayRotateArrowCapSize;
glVertex3f(Tip[0], Tip[1], Tip[2]); glVertex3f(Tip[0], Tip[1], Tip[2]);
glVertex3f(Tip[0] - Arrow[0], Tip[1] - Arrow[1], Tip[2] - Arrow[2]); glVertex3f(Tip[0] - Arrow[0], Tip[1] - Arrow[1], Tip[2] - Arrow[2]);
Rot.CreateFromAxisAngle(Normal, -LC_PI * 0.15f); Rot = lcMatrix44FromAxisAngle(Normal, -LC_PI * 0.15f);
Arrow = Mul(Tangent, Rot) * OverlayRotateArrowCapSize; Arrow = lcMul30(Tangent, Rot) * OverlayRotateArrowCapSize;
glVertex3f(Tip[0], Tip[1], Tip[2]); glVertex3f(Tip[0], Tip[1], Tip[2]);
glVertex3f(Tip[0] - Arrow[0], Tip[1] - Arrow[1], Tip[2] - Arrow[2]); glVertex3f(Tip[0] - Arrow[0], Tip[1] - Arrow[1], Tip[2] - Arrow[2]);
@ -2502,11 +2497,10 @@ void Project::RenderOverlays(View* view)
float Aspect = (float)Viewport[2]/(float)Viewport[3]; float Aspect = (float)Viewport[2]/(float)Viewport[3];
Camera* Cam = view->m_Camera; Camera* Cam = view->m_Camera;
Matrix44 ModelView, Projection; const lcMatrix44& ModelView = Cam->mWorldView;
ModelView.CreateLookAt(Cam->GetEyePosition(), Cam->GetTargetPosition(), Cam->GetUpVector()); lcMatrix44 Projection = lcMatrix44Perspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar);
Projection.CreatePerspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar);
Vector3 Screen = ProjectPoint(Vector3(0, 0, 0), ModelView, Projection, Viewport); lcVector3 Screen = lcProjectPoint(lcVector3(0, 0, 0), ModelView, Projection, Viewport);
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glPushMatrix(); glPushMatrix();
@ -3321,13 +3315,13 @@ void Project::HandleNotify(LC_NOTIFY id, unsigned long param)
else else
pCamera->UnHide(); pCamera->UnHide();
if (pCamera->GetEyePosition() != mod->Eye) if (pCamera->mPosition != mod->Eye)
pCamera->ChangeKey(m_bAnimation ? m_nCurFrame : m_nCurStep, m_bAnimation, m_bAddKeys, mod->Eye, LC_CK_EYE); pCamera->ChangeKey(m_bAnimation ? m_nCurFrame : m_nCurStep, m_bAnimation, m_bAddKeys, mod->Eye, LC_CK_EYE);
if (pCamera->GetTargetPosition() != mod->Target) if (pCamera->mTargetPosition != mod->Target)
pCamera->ChangeKey(m_bAnimation ? m_nCurFrame : m_nCurStep, m_bAnimation, m_bAddKeys, mod->Target, LC_CK_TARGET); pCamera->ChangeKey(m_bAnimation ? m_nCurFrame : m_nCurStep, m_bAnimation, m_bAddKeys, mod->Target, LC_CK_TARGET);
if (pCamera->GetUpVector() != mod->Up) if (pCamera->mUpVector != mod->Up)
pCamera->ChangeKey(m_bAnimation ? m_nCurFrame : m_nCurStep, m_bAnimation, m_bAddKeys, mod->Up, LC_CK_UP); pCamera->ChangeKey(m_bAnimation ? m_nCurFrame : m_nCurStep, m_bAnimation, m_bAddKeys, mod->Up, LC_CK_UP);
pCamera->m_fovy = mod->fovy; pCamera->m_fovy = mod->fovy;
@ -3990,13 +3984,12 @@ void Project::HandleCommand(LC_COMMANDS id, unsigned long nParam)
POVFile.WriteLine(Line); POVFile.WriteLine(Line);
} }
float eye[3], target[3], up[3]; const lcVector3& Position = m_ActiveView->m_Camera->mPosition;
m_ActiveView->m_Camera->GetEyePos(eye); const lcVector3& Target = m_ActiveView->m_Camera->mTargetPosition;
m_ActiveView->m_Camera->GetTargetPos(target); const lcVector3& Up = m_ActiveView->m_Camera->mUpVector;
m_ActiveView->m_Camera->GetUpVec(up);
sprintf(Line, "camera {\n sky<%1g,%1g,%1g>\n location <%1g, %1g, %1g>\n look_at <%1g, %1g, %1g>\n angle %.0f\n}\n\n", sprintf(Line, "camera {\n sky<%1g,%1g,%1g>\n location <%1g, %1g, %1g>\n look_at <%1g, %1g, %1g>\n angle %.0f\n}\n\n",
up[0], up[1], up[2], eye[1], eye[0], eye[2], target[1], target[0], target[2], m_ActiveView->m_Camera->m_fovy); Up[0], Up[1], Up[2], Position[1], Position[0], Position[2], Target[1], Target[0], Target[2], m_ActiveView->m_Camera->m_fovy);
POVFile.WriteLine(Line); POVFile.WriteLine(Line);
sprintf(Line, "background { color rgb <%1g, %1g, %1g> }\n\nlight_source { <0, 0, 20> White shadowless }\n\n", sprintf(Line, "background { color rgb <%1g, %1g, %1g> }\n\nlight_source { <0, 0, 20> White shadowless }\n\n",
m_fBackground[0], m_fBackground[1], m_fBackground[2]); m_fBackground[0], m_fBackground[1], m_fBackground[2]);
@ -5384,10 +5377,10 @@ void Project::HandleCommand(LC_COMMANDS id, unsigned long nParam)
float Aspect = (float)Viewport[2]/(float)Viewport[3]; float Aspect = (float)Viewport[2]/(float)Viewport[3];
Camera* Cam = view->m_Camera; Camera* Cam = view->m_Camera;
Vector3 Position(Cam->GetEyePosition() + Center - Cam->GetTargetPosition()); Vector3 Position(Vector3(Cam->mPosition) + Center - Vector3(Cam->mTargetPosition));
Matrix44 ModelView, Projection; Matrix44 ModelView, Projection;
ModelView.CreateLookAt(Position, Center, Cam->GetUpVector()); ModelView.CreateLookAt(Position, Center, Vector3(Cam->mUpVector));
Projection.CreatePerspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar); Projection.CreatePerspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar);
Position = ZoomExtents(Position, ModelView, Projection, &Points[0], Points.GetSize()); Position = ZoomExtents(Position, ModelView, Projection, &Points[0], Points.GetSize());
@ -6053,7 +6046,7 @@ void Project::SelectAndFocusNone(bool bFocusOnly)
// AfxGetMainWnd()->PostMessage(WM_LC_UPDATE_INFO, NULL, OT_PIECE); // AfxGetMainWnd()->PostMessage(WM_LC_UPDATE_INFO, NULL, OT_PIECE);
} }
bool Project::GetSelectionCenter(Vector3& Center) const bool Project::GetSelectionCenter(lcVector3& Center) const
{ {
float bs[6] = { 10000, 10000, 10000, -10000, -10000, -10000 }; float bs[6] = { 10000, 10000, 10000, -10000, -10000, -10000 };
bool Selected = false; bool Selected = false;
@ -6067,7 +6060,7 @@ bool Project::GetSelectionCenter(Vector3& Center) const
} }
} }
Center = Vector3((bs[0] + bs[3]) * 0.5f, (bs[1] + bs[4]) * 0.5f, (bs[2] + bs[5]) * 0.5f); Center = lcVector3((bs[0] + bs[3]) * 0.5f, (bs[1] + bs[4]) * 0.5f, (bs[2] + bs[5]) * 0.5f);
return Selected; return Selected;
} }
@ -6081,28 +6074,27 @@ Camera* Project::GetCamera(int i)
return pCamera; return pCamera;
} }
void Project::ConvertToUserUnits(Vector3& Value) const void Project::ConvertToUserUnits(lcVector3& Value) const
{ {
if ((m_nSnap & LC_DRAW_CM_UNITS) == 0) if ((m_nSnap & LC_DRAW_CM_UNITS) == 0)
Value /= 0.04f; Value /= 0.04f;
} }
void Project::ConvertFromUserUnits(Vector3& Value) const void Project::ConvertFromUserUnits(lcVector3& Value) const
{ {
if ((m_nSnap & LC_DRAW_CM_UNITS) == 0) if ((m_nSnap & LC_DRAW_CM_UNITS) == 0)
Value *= 0.04f; Value *= 0.04f;
} }
bool Project::GetFocusPosition(Vector3& Position) const bool Project::GetFocusPosition(lcVector3& Position) const
{ {
Piece* pPiece; Piece* pPiece;
Camera* pCamera; Camera* pCamera;
float* pos = &Position[0];
for (pPiece = m_pPieces; pPiece; pPiece = pPiece->m_pNext) for (pPiece = m_pPieces; pPiece; pPiece = pPiece->m_pNext)
if (pPiece->IsFocused()) if (pPiece->IsFocused())
{ {
pPiece->GetPosition(pos); Position = pPiece->mPosition;
return true; return true;
} }
@ -6110,20 +6102,20 @@ bool Project::GetFocusPosition(Vector3& Position) const
{ {
if (pCamera->IsEyeFocused()) if (pCamera->IsEyeFocused())
{ {
pCamera->GetEyePos(pos); Position = pCamera->mPosition;
return true; return true;
} }
if (pCamera->IsTargetFocused()) if (pCamera->IsTargetFocused())
{ {
pCamera->GetTargetPos(pos); Position = pCamera->mTargetPosition;
return true; return true;
} }
} }
// TODO: light // TODO: light
pos[0] = pos[1] = pos[2] = 0.0f; Position = lcVector3(0.0f, 0.0f, 0.0f);
return false; return false;
} }
@ -6184,16 +6176,14 @@ void Project::GetPieceInsertPosition(View* view, int MouseX, int MouseY, Vector3
float Aspect = (float)Viewport[2]/(float)Viewport[3]; float Aspect = (float)Viewport[2]/(float)Viewport[3];
Camera* Cam = view->m_Camera; Camera* Cam = view->m_Camera;
// Build the matrices. const lcMatrix44& ModelView = Cam->mWorldView;
Matrix44 ModelView, Projection; lcMatrix44 Projection = lcMatrix44Perspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar);
ModelView.CreateLookAt(Cam->GetEyePosition(), Cam->GetTargetPosition(), Cam->GetUpVector());
Projection.CreatePerspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar);
Vector3 ClickPoints[2] = { Vector3((float)m_nDownX, (float)m_nDownY, 0.0f), Vector3((float)m_nDownX, (float)m_nDownY, 1.0f) }; lcVector3 ClickPoints[2] = { lcVector3((float)m_nDownX, (float)m_nDownY, 0.0f), lcVector3((float)m_nDownX, (float)m_nDownY, 1.0f) };
UnprojectPoints(ClickPoints, 2, ModelView, Projection, Viewport); lcUnprojectPoints(ClickPoints, 2, ModelView, Projection, Viewport);
Vector3 Intersection; Vector3 Intersection;
if (LinePlaneIntersection(Intersection, ClickPoints[0], ClickPoints[1], Vector4(0, 0, 1, 0))) if (LinePlaneIntersection(Intersection, Vector3(ClickPoints[0]), Vector3(ClickPoints[1]), Vector4(0, 0, 1, 0)))
{ {
SnapVector(Intersection); SnapVector(Intersection);
Position = Intersection; Position = Intersection;
@ -6202,7 +6192,7 @@ void Project::GetPieceInsertPosition(View* view, int MouseX, int MouseY, Vector3
} }
// Couldn't find a good position, so just place the piece somewhere near the camera. // Couldn't find a good position, so just place the piece somewhere near the camera.
Position = UnprojectPoint(Vector3((float)m_nDownX, (float)m_nDownY, 0.9f), ModelView, Projection, Viewport); Position = Vector3(lcUnprojectPoint(lcVector3((float)m_nDownX, (float)m_nDownY, 0.9f), ModelView, Projection, Viewport));
Rotation = Vector4(0, 0, 1, 0); Rotation = Vector4(0, 0, 1, 0);
} }
@ -6212,12 +6202,11 @@ Object* Project::FindObjectFromPoint(View* view, int x, int y, bool PiecesOnly)
float Aspect = (float)Viewport[2]/(float)Viewport[3]; float Aspect = (float)Viewport[2]/(float)Viewport[3];
Camera* Cam = view->m_Camera; Camera* Cam = view->m_Camera;
Matrix44 ModelView, Projection; const lcMatrix44& ModelView = Cam->mWorldView;
ModelView.CreateLookAt(Cam->GetEyePosition(), Cam->GetTargetPosition(), Cam->GetUpVector()); lcMatrix44 Projection = lcMatrix44Perspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar);
Projection.CreatePerspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar);
Vector3 Start = UnprojectPoint(Vector3((float)x, (float)y, 0.0f), ModelView, Projection, Viewport); lcVector3 Start = lcUnprojectPoint(lcVector3((float)x, (float)y, 0.0f), ModelView, Projection, Viewport);
Vector3 End = UnprojectPoint(Vector3((float)x, (float)y, 1.0f), ModelView, Projection, Viewport); lcVector3 End = lcUnprojectPoint(lcVector3((float)x, (float)y, 1.0f), ModelView, Projection, Viewport);
LC_CLICKLINE ClickLine; LC_CLICKLINE ClickLine;
@ -6253,10 +6242,8 @@ void Project::FindObjectsInBox(float x1, float y1, float x2, float y2, PtrArray<
float Aspect = (float)Viewport[2]/(float)Viewport[3]; float Aspect = (float)Viewport[2]/(float)Viewport[3];
Camera* Cam = m_ActiveView->m_Camera; Camera* Cam = m_ActiveView->m_Camera;
// Build the matrices. const lcMatrix44& ModelView = Cam->mWorldView;
Matrix44 ModelView, Projection; lcMatrix44 Projection = lcMatrix44Perspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar);
ModelView.CreateLookAt(Cam->GetEyePosition(), Cam->GetTargetPosition(), Cam->GetUpVector());
Projection.CreatePerspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar);
// Find out the top-left and bottom-right corners in screen coordinates. // Find out the top-left and bottom-right corners in screen coordinates.
float Left, Top, Bottom, Right; float Left, Top, Bottom, Right;
@ -6284,37 +6271,38 @@ void Project::FindObjectsInBox(float x1, float y1, float x2, float y2, PtrArray<
} }
// Unproject 6 points to world space. // Unproject 6 points to world space.
Vector3 Corners[6] = lcVector3 Corners[6] =
{ {
Vector3(Left, Top, 0), Vector3(Left, Bottom, 0), Vector3(Right, Bottom, 0), lcVector3(Left, Top, 0), lcVector3(Left, Bottom, 0), lcVector3(Right, Bottom, 0),
Vector3(Right, Top, 0), Vector3(Left, Top, 1), Vector3(Right, Bottom, 1) lcVector3(Right, Top, 0), lcVector3(Left, Top, 1), lcVector3(Right, Bottom, 1)
}; };
UnprojectPoints(Corners, 6, ModelView, Projection, Viewport); lcUnprojectPoints(Corners, 6, ModelView, Projection, Viewport);
// Build the box planes. // Build the box planes.
Vector4 Planes[6]; lcVector3 PlaneNormals[6];
Planes[0] = Cross3(Corners[4] - Corners[0], Corners[1] - Corners[0]).Normalize(); // Left PlaneNormals[0] = lcNormalize(lcCross(Corners[4] - Corners[0], Corners[1] - Corners[0])); // Left
Planes[1] = Cross3(Corners[5] - Corners[2], Corners[3] - Corners[2]).Normalize(); // Right PlaneNormals[1] = lcNormalize(lcCross(Corners[5] - Corners[2], Corners[3] - Corners[2])); // Right
Planes[2] = Cross3(Corners[3] - Corners[0], Corners[4] - Corners[0]).Normalize(); // Top PlaneNormals[2] = lcNormalize(lcCross(Corners[3] - Corners[0], Corners[4] - Corners[0])); // Top
Planes[3] = Cross3(Corners[1] - Corners[2], Corners[5] - Corners[2]).Normalize(); // Bottom PlaneNormals[3] = lcNormalize(lcCross(Corners[1] - Corners[2], Corners[5] - Corners[2])); // Bottom
Planes[4] = Cross3(Corners[1] - Corners[0], Corners[3] - Corners[0]).Normalize(); // Front PlaneNormals[4] = lcNormalize(lcCross(Corners[1] - Corners[0], Corners[3] - Corners[0])); // Front
Planes[5] = Cross3(Corners[1] - Corners[2], Corners[3] - Corners[2]).Normalize(); // Back PlaneNormals[5] = lcNormalize(lcCross(Corners[1] - Corners[2], Corners[3] - Corners[2])); // Back
Planes[0][3] = -Dot3(Planes[0], Corners[0]); lcVector4 Planes[6];
Planes[1][3] = -Dot3(Planes[1], Corners[5]); Planes[0] = lcVector4(PlaneNormals[0], -lcDot(PlaneNormals[0], Corners[0]));
Planes[2][3] = -Dot3(Planes[2], Corners[0]); Planes[1] = lcVector4(PlaneNormals[1], -lcDot(PlaneNormals[1], Corners[5]));
Planes[3][3] = -Dot3(Planes[3], Corners[5]); Planes[2] = lcVector4(PlaneNormals[2], -lcDot(PlaneNormals[2], Corners[0]));
Planes[4][3] = -Dot3(Planes[4], Corners[0]); Planes[3] = lcVector4(PlaneNormals[3], -lcDot(PlaneNormals[3], Corners[5]));
Planes[5][3] = -Dot3(Planes[5], Corners[5]); Planes[4] = lcVector4(PlaneNormals[4], -lcDot(PlaneNormals[4], Corners[0]));
Planes[5] = lcVector4(PlaneNormals[5], -lcDot(PlaneNormals[5], Corners[5]));
// Check if any objects are inside the volume. // Check if any objects are inside the volume.
for (Piece* piece = m_pPieces; piece != NULL; piece = piece->m_pNext) for (Piece* piece = m_pPieces; piece != NULL; piece = piece->m_pNext)
{ {
if (piece->IsVisible(m_bAnimation ? m_nCurFrame : m_nCurStep, m_bAnimation)) if (piece->IsVisible(m_bAnimation ? m_nCurFrame : m_nCurStep, m_bAnimation))
{ {
if (piece->IntersectsVolume(Planes, 6)) if (piece->IntersectsVolume((Vector4*)Planes, 6))
Objects.Add(piece); Objects.Add(piece);
} }
} }
@ -6448,10 +6436,8 @@ bool Project::StopTracking(bool bAccept)
float Aspect = (float)Viewport[2]/(float)Viewport[3]; float Aspect = (float)Viewport[2]/(float)Viewport[3];
Camera* Cam = m_ActiveView->m_Camera; Camera* Cam = m_ActiveView->m_Camera;
// Build the matrices. const lcMatrix44& ModelView = Cam->mWorldView;
Matrix44 ModelView, Projection; lcMatrix44 Projection = lcMatrix44Perspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar);
ModelView.CreateLookAt(Cam->GetEyePosition(), Cam->GetTargetPosition(), Cam->GetUpVector());
Projection.CreatePerspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar);
// Find out the top-left and bottom-right corners in screen coordinates. // Find out the top-left and bottom-right corners in screen coordinates.
float Left, Top, Bottom, Right; float Left, Top, Bottom, Right;
@ -6479,20 +6465,20 @@ bool Project::StopTracking(bool bAccept)
} }
// Unproject screen points to world space. // Unproject screen points to world space.
Vector3 Points[3] = lcVector3 Points[3] =
{ {
Vector3((Left + Right) / 2, (Top + Bottom) / 2, 0.9f), lcVector3((Left + Right) / 2, (Top + Bottom) / 2, 0.9f),
Vector3((float)Viewport[2] / 2.0f, (float)Viewport[3] / 2.0f, 0.9f), lcVector3((float)Viewport[2] / 2.0f, (float)Viewport[3] / 2.0f, 0.9f),
Vector3((float)Viewport[2] / 2.0f, (float)Viewport[3] / 2.0f, 0.1f), lcVector3((float)Viewport[2] / 2.0f, (float)Viewport[3] / 2.0f, 0.1f),
}; };
UnprojectPoints(Points, 3, ModelView, Projection, Viewport); lcUnprojectPoints(Points, 3, ModelView, Projection, Viewport);
// Center camera. // Center camera.
Vector3 Eye = Cam->GetEyePosition(); lcVector3 Eye = Cam->mPosition;
Eye = Eye + (Points[0] - Points[1]); Eye = Eye + (Points[0] - Points[1]);
Vector3 Target = Cam->GetTargetPosition(); lcVector3 Target = Cam->mTargetPosition;
Target = Target + (Points[0] - Points[1]); Target = Target + (Points[0] - Points[1]);
// Zoom in/out. // Zoom in/out.
@ -6500,7 +6486,7 @@ bool Project::StopTracking(bool bAccept)
float RatioY = (Top - Bottom) / Viewport[3]; float RatioY = (Top - Bottom) / Viewport[3];
float ZoomFactor = -max(RatioX, RatioY) + 0.75f; float ZoomFactor = -max(RatioX, RatioY) + 0.75f;
Vector3 Dir = Points[1] - Points[2]; lcVector3 Dir = Points[1] - Points[2];
Eye = Eye + Dir * ZoomFactor; Eye = Eye + Dir * ZoomFactor;
Target = Target + Dir * ZoomFactor; Target = Target + Dir * ZoomFactor;
@ -7180,7 +7166,7 @@ bool Project::OnKeyDown(char nKey, bool bControl, bool bShift)
{ {
Matrix mat; Matrix mat;
mat.CreateLookat(camera->GetEyePos (), camera->GetTargetPos (), camera->GetUpVec ()); mat.CreateLookat(camera->mPosition, camera->mTargetPosition, camera->mUpVector);
mat.SetTranslation(0, 0, 0); mat.SetTranslation(0, 0, 0);
mat.Invert(); mat.Invert();
@ -7244,12 +7230,11 @@ bool Project::OnKeyDown(char nKey, bool bControl, bool bShift)
float Aspect = (float)Viewport[2]/(float)Viewport[3]; float Aspect = (float)Viewport[2]/(float)Viewport[3];
Camera* Cam = m_ActiveView->m_Camera; Camera* Cam = m_ActiveView->m_Camera;
Matrix44 ModelView, Projection; const lcMatrix44& ModelView = Cam->mWorldView;
ModelView.CreateLookAt(Cam->GetEyePosition(), Cam->GetTargetPosition(), Cam->GetUpVector()); lcMatrix44 Projection = lcMatrix44Perspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar);
Projection.CreatePerspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar);
Vector3 Pts[3] = { Vector3(5.0f, 5.0f, 0.1f), Vector3(10.0f, 5.0f, 0.1f), Vector3(5.0f, 10.0f, 0.1f) }; lcVector3 Pts[3] = { lcVector3(5.0f, 5.0f, 0.1f), lcVector3(10.0f, 5.0f, 0.1f), lcVector3(5.0f, 10.0f, 0.1f) };
UnprojectPoints(Pts, 3, ModelView, Projection, Viewport); lcUnprojectPoints(Pts, 3, ModelView, Projection, Viewport);
float ax, ay; float ax, ay;
Vector3 vx((Pts[1][0] - Pts[0][0]), (Pts[1][1] - Pts[0][1]), 0);//Pts[1][2] - Pts[0][2] }; Vector3 vx((Pts[1][0] - Pts[0][0]), (Pts[1][1] - Pts[0][1]), 0);//Pts[1][2] - Pts[0][2] };
@ -7348,11 +7333,10 @@ void Project::OnLeftButtonDown(View* view, int x, int y, bool bControl, bool bSh
float Aspect = (float)Viewport[2]/(float)Viewport[3]; float Aspect = (float)Viewport[2]/(float)Viewport[3];
Camera* Cam = view->m_Camera; Camera* Cam = view->m_Camera;
Matrix44 ModelView, Projection; const lcMatrix44& ModelView = Cam->mWorldView;
ModelView.CreateLookAt(Cam->GetEyePosition(), Cam->GetTargetPosition(), Cam->GetUpVector()); lcMatrix44 Projection = lcMatrix44Perspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar);
Projection.CreatePerspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar);
Vector3 point = UnprojectPoint(Vector3((float)x, (float)y, 0.9f), ModelView, Projection, Viewport); lcVector3 point = lcUnprojectPoint(lcVector3((float)x, (float)y, 0.9f), ModelView, Projection, Viewport);
m_fTrack[0] = point[0]; m_fTrack[1] = point[1]; m_fTrack[2] = point[2]; m_fTrack[0] = point[0]; m_fTrack[1] = point[1]; m_fTrack[2] = point[2];
if (Sys_KeyDown(KEY_ALT)) if (Sys_KeyDown(KEY_ALT))
@ -7563,7 +7547,7 @@ void Project::OnLeftButtonDown(View* view, int x, int y, bool bControl, bool bSh
if (count == max) if (count == max)
break; break;
Vector3 tmp = UnprojectPoint(Vector3(x+1.0f, y-1.0f, 0.9f), ModelView, Projection, Viewport); lcVector3 tmp = lcUnprojectPoint(lcVector3(x+1.0f, y-1.0f, 0.9f), ModelView, Projection, Viewport);
SelectAndFocusNone(false); SelectAndFocusNone(false);
StartTracking(LC_TRACK_START_LEFT); StartTracking(LC_TRACK_START_LEFT);
pLight = new Light (m_fTrack[0], m_fTrack[1], m_fTrack[2], tmp[0], tmp[1], tmp[2]); pLight = new Light (m_fTrack[0], m_fTrack[1], m_fTrack[2], tmp[0], tmp[1], tmp[2]);
@ -7577,7 +7561,7 @@ void Project::OnLeftButtonDown(View* view, int x, int y, bool bControl, bool bSh
case LC_ACTION_CAMERA: case LC_ACTION_CAMERA:
{ {
Vector3 tmp = UnprojectPoint(Vector3(x+1.0f, y-1.0f, 0.9f), ModelView, Projection, Viewport); lcVector3 tmp = lcUnprojectPoint(lcVector3(x+1.0f, y-1.0f, 0.9f), ModelView, Projection, Viewport);
SelectAndFocusNone(false); SelectAndFocusNone(false);
StartTracking(LC_TRACK_START_LEFT); StartTracking(LC_TRACK_START_LEFT);
Camera* pCamera = new Camera(m_fTrack[0], m_fTrack[1], m_fTrack[2], tmp[0], tmp[1], tmp[2], m_pCameras); Camera* pCamera = new Camera(m_fTrack[0], m_fTrack[1], m_fTrack[2], tmp[0], tmp[1], tmp[2], m_pCameras);
@ -7627,7 +7611,7 @@ void Project::OnLeftButtonDown(View* view, int x, int y, bool bControl, bool bSh
if (sel) if (sel)
{ {
StartTracking(LC_TRACK_START_LEFT); StartTracking(LC_TRACK_START_LEFT);
m_OverlayDelta = Vector3(0.0f, 0.0f, 0.0f); m_OverlayDelta = lcVector3(0.0f, 0.0f, 0.0f);
m_MouseSnapLeftover = Vector3(0.0f, 0.0f, 0.0f); m_MouseSnapLeftover = Vector3(0.0f, 0.0f, 0.0f);
} }
} break; } break;
@ -7641,7 +7625,7 @@ void Project::OnLeftButtonDown(View* view, int x, int y, bool bControl, bool bSh
if (pPiece->IsSelected()) if (pPiece->IsSelected())
{ {
StartTracking(LC_TRACK_START_LEFT); StartTracking(LC_TRACK_START_LEFT);
m_OverlayDelta = Vector3(0.0f, 0.0f, 0.0f); m_OverlayDelta = lcVector3(0.0f, 0.0f, 0.0f);
break; break;
} }
} }
@ -7674,11 +7658,10 @@ void Project::OnLeftButtonDoubleClick(View* view, int x, int y, bool bControl, b
float Aspect = (float)Viewport[2]/(float)Viewport[3]; float Aspect = (float)Viewport[2]/(float)Viewport[3];
Camera* Cam = view->m_Camera; Camera* Cam = view->m_Camera;
Matrix44 ModelView, Projection; const lcMatrix44& ModelView = Cam->mWorldView;
ModelView.CreateLookAt(Cam->GetEyePosition(), Cam->GetTargetPosition(), Cam->GetUpVector()); lcMatrix44 Projection = lcMatrix44Perspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar);
Projection.CreatePerspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar);
Vector3 point = UnprojectPoint(Vector3((float)x, (float)y, 0.9f), ModelView, Projection, Viewport); lcVector3 point = lcUnprojectPoint(lcVector3((float)x, (float)y, 0.9f), ModelView, Projection, Viewport);
m_fTrack[0] = point[0]; m_fTrack[1] = point[1]; m_fTrack[2] = point[2]; m_fTrack[0] = point[0]; m_fTrack[1] = point[1]; m_fTrack[2] = point[2];
Object* Closest = FindObjectFromPoint(view, x, y); Object* Closest = FindObjectFromPoint(view, x, y);
@ -7779,11 +7762,10 @@ void Project::OnMiddleButtonDown(View* view, int x, int y, bool bControl, bool b
float Aspect = (float)Viewport[2]/(float)Viewport[3]; float Aspect = (float)Viewport[2]/(float)Viewport[3];
Camera* Cam = view->m_Camera; Camera* Cam = view->m_Camera;
Matrix44 ModelView, Projection; const lcMatrix44& ModelView = Cam->mWorldView;
ModelView.CreateLookAt(Cam->GetEyePosition(), Cam->GetTargetPosition(), Cam->GetUpVector()); lcMatrix44 Projection = lcMatrix44Perspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar);
Projection.CreatePerspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar);
Vector3 point = UnprojectPoint(Vector3((float)x, (float)y, 0.9f), ModelView, Projection, Viewport); lcVector3 point = lcUnprojectPoint(lcVector3((float)x, (float)y, 0.9f), ModelView, Projection, Viewport);
m_fTrack[0] = point[0]; m_fTrack[1] = point[1]; m_fTrack[2] = point[2]; m_fTrack[0] = point[0]; m_fTrack[1] = point[1]; m_fTrack[2] = point[2];
if (Sys_KeyDown(KEY_ALT)) if (Sys_KeyDown(KEY_ALT))
@ -7822,11 +7804,10 @@ void Project::OnRightButtonDown(View* view, int x, int y, bool bControl, bool bS
float Aspect = (float)Viewport[2]/(float)Viewport[3]; float Aspect = (float)Viewport[2]/(float)Viewport[3];
Camera* Cam = view->m_Camera; Camera* Cam = view->m_Camera;
Matrix44 ModelView, Projection; const lcMatrix44& ModelView = Cam->mWorldView;
ModelView.CreateLookAt(Cam->GetEyePosition(), Cam->GetTargetPosition(), Cam->GetUpVector()); lcMatrix44 Projection = lcMatrix44Perspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar);
Projection.CreatePerspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar);
Vector3 point = UnprojectPoint(Vector3((float)x, (float)y, 0.9f), ModelView, Projection, Viewport); lcVector3 point = lcUnprojectPoint(lcVector3((float)x, (float)y, 0.9f), ModelView, Projection, Viewport);
m_fTrack[0] = point[0]; m_fTrack[1] = point[1]; m_fTrack[2] = point[2]; m_fTrack[0] = point[0]; m_fTrack[1] = point[1]; m_fTrack[2] = point[2];
if (Sys_KeyDown(KEY_ALT)) if (Sys_KeyDown(KEY_ALT))
@ -7920,11 +7901,10 @@ void Project::OnMouseMove(View* view, int x, int y, bool bControl, bool bShift)
float Aspect = (float)Viewport[2]/(float)Viewport[3]; float Aspect = (float)Viewport[2]/(float)Viewport[3];
Camera* Cam = view->m_Camera; Camera* Cam = view->m_Camera;
Matrix44 ModelView, Projection; const lcMatrix44& ModelView = Cam->mWorldView;
ModelView.CreateLookAt(Cam->GetEyePosition(), Cam->GetTargetPosition(), Cam->GetUpVector()); lcMatrix44 Projection = lcMatrix44Perspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar);
Projection.CreatePerspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar);
Vector3 tmp = UnprojectPoint(Vector3((float)x, (float)y, 0.9f), ModelView, Projection, Viewport); lcVector3 tmp = lcUnprojectPoint(lcVector3((float)x, (float)y, 0.9f), ModelView, Projection, Viewport);
ptx = tmp[0]; pty = tmp[1]; ptz = tmp[2]; ptx = tmp[0]; pty = tmp[1]; ptz = tmp[2];
switch (m_nCurAction) switch (m_nCurAction)
@ -8011,8 +7991,8 @@ void Project::OnMouseMove(View* view, int x, int y, bool bControl, bool bShift)
if ((m_OverlayActive && (m_OverlayMode != LC_OVERLAY_XYZ)) || (!Camera->IsSide())) if ((m_OverlayActive && (m_OverlayMode != LC_OVERLAY_XYZ)) || (!Camera->IsSide()))
{ {
Vector3 ScreenX = Cross3(Camera->GetTargetPosition() - Camera->GetEyePosition(), Camera->GetUpVector()); Vector3 ScreenX = Cross3(Vector3(Camera->mTargetPosition) - Vector3(Camera->mPosition), Vector3(Camera->mUpVector));
Vector3 ScreenY = Camera->GetUpVector(); Vector3 ScreenY = Vector3(Camera->mUpVector);
ScreenX.Normalize(); ScreenX.Normalize();
Vector3 Dir1, Dir2; Vector3 Dir1, Dir2;
bool SingleDir = true; bool SingleDir = true;
@ -8148,9 +8128,9 @@ void Project::OnMouseMove(View* view, int x, int y, bool bControl, bool bShift)
else else
{ {
// 3D movement. // 3D movement.
Vector3 ScreenZ = (Camera->GetTargetPosition() - Camera->GetEyePosition()).Normalize(); Vector3 ScreenZ = (Vector3(Camera->mTargetPosition) - Vector3(Camera->mPosition)).Normalize();
Vector3 ScreenX = Cross3(ScreenZ, Camera->GetUpVector()); Vector3 ScreenX = Cross3(ScreenZ, Vector3(Camera->mUpVector));
Vector3 ScreenY = Camera->GetUpVector(); Vector3 ScreenY = Vector3(Camera->mUpVector);
Vector3 TotalMove; Vector3 TotalMove;
@ -8190,8 +8170,8 @@ void Project::OnMouseMove(View* view, int x, int y, bool bControl, bool bShift)
if ((m_OverlayActive && (m_OverlayMode != LC_OVERLAY_XYZ)) || (!Camera->IsSide())) if ((m_OverlayActive && (m_OverlayMode != LC_OVERLAY_XYZ)) || (!Camera->IsSide()))
{ {
Vector3 ScreenX = Cross3(Camera->GetTargetPosition() - Camera->GetEyePosition(), Camera->GetUpVector()); Vector3 ScreenX = Cross3(Vector3(Camera->mTargetPosition) - Vector3(Camera->mPosition), Vector3(Camera->mUpVector));
Vector3 ScreenY = Camera->GetUpVector(); Vector3 ScreenY = Vector3(Camera->mUpVector);
ScreenX.Normalize(); ScreenX.Normalize();
Vector3 Dir1, Dir2; Vector3 Dir1, Dir2;
bool SingleDir = true; bool SingleDir = true;
@ -8306,9 +8286,9 @@ void Project::OnMouseMove(View* view, int x, int y, bool bControl, bool bShift)
else else
{ {
// 3D movement. // 3D movement.
Vector3 ScreenZ = (Camera->GetTargetPosition() - Camera->GetEyePosition()).Normalize(); Vector3 ScreenZ = (Vector3(Camera->mTargetPosition) - Vector3(Camera->mPosition)).Normalize();
Vector3 ScreenX = Cross3(ScreenZ, Camera->GetUpVector()); Vector3 ScreenX = Cross3(ScreenZ, Vector3(Camera->mUpVector));
Vector3 ScreenY = Camera->GetUpVector(); Vector3 ScreenY = Vector3(Camera->mUpVector);
Vector3 Delta; Vector3 Delta;
@ -8383,11 +8363,10 @@ void Project::OnMouseMove(View* view, int x, int y, bool bControl, bool bShift)
// We can't rotate the side cameras. // We can't rotate the side cameras.
if (m_ActiveView->m_Camera->IsSide()) if (m_ActiveView->m_Camera->IsSide())
{ {
float eye[3], target[3], up[3]; const lcVector3& Position = m_ActiveView->m_Camera->mPosition;
m_ActiveView->m_Camera->GetEyePos(eye); const lcVector3& Target = m_ActiveView->m_Camera->mTargetPosition;
m_ActiveView->m_Camera->GetTargetPos(target); const lcVector3& Up = m_ActiveView->m_Camera->mUpVector;
m_ActiveView->m_Camera->GetUpVec(up); Camera* pCamera = new Camera(Position, Target, Up, m_pCameras);
Camera* pCamera = new Camera(eye, target, up, m_pCameras);
m_ActiveView->m_Camera = pCamera; m_ActiveView->m_Camera = pCamera;
SystemUpdateCameraMenu(m_pCameras); SystemUpdateCameraMenu(m_pCameras);
@ -8482,17 +8461,16 @@ void Project::MouseUpdateOverlays(View* view, int x, int y)
float Aspect = (float)Viewport[2]/(float)Viewport[3]; float Aspect = (float)Viewport[2]/(float)Viewport[3];
Camera* Cam = view->m_Camera; Camera* Cam = view->m_Camera;
Matrix44 ModelView, Projection; const lcMatrix44& ModelView = Cam->mWorldView;
ModelView.CreateLookAt(Cam->GetEyePosition(), Cam->GetTargetPosition(), Cam->GetUpVector()); lcMatrix44 Projection = lcMatrix44Perspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar);
Projection.CreatePerspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar);
// Array of points for the arrow edges. // Array of points for the arrow edges.
Vector3 Points[4] = lcVector3 Points[4] =
{ {
Vector3(m_OverlayCenter[0], m_OverlayCenter[1], m_OverlayCenter[2]), lcVector3(m_OverlayCenter[0], m_OverlayCenter[1], m_OverlayCenter[2]),
Vector3(OverlayMoveArrowSize * OverlayScale, 0, 0), lcVector3(OverlayMoveArrowSize * OverlayScale, 0, 0),
Vector3(0, OverlayMoveArrowSize * OverlayScale, 0), lcVector3(0, OverlayMoveArrowSize * OverlayScale, 0),
Vector3(0, 0, OverlayMoveArrowSize * OverlayScale), lcVector3(0, 0, OverlayMoveArrowSize * OverlayScale),
}; };
// Find the rotation from the focused piece if relative snap is enabled. // Find the rotation from the focused piece if relative snap is enabled.
@ -8502,39 +8480,36 @@ void Project::MouseUpdateOverlays(View* view, int x, int y)
if ((Focus != NULL) && Focus->IsPiece()) if ((Focus != NULL) && Focus->IsPiece())
{ {
float Rot[4]; const lcMatrix44& RotMat = ((Piece*)Focus)->mModelWorld;
((Piece*)Focus)->GetRotation(Rot);
Matrix33 RotMat;
RotMat.CreateFromAxisAngle(Vector3(Rot[0], Rot[1], Rot[2]), Rot[3] * LC_DTOR);
for (int i = 1; i < 4; i++) for (int i = 1; i < 4; i++)
Points[i] = Mul(Points[i], RotMat); Points[i] = lcMul30(Points[i], RotMat);
} }
} }
int i, Mode = -1; int i, Mode = -1;
Vector3 Pt((float)x, (float)y, 0); lcVector3 Pt((float)x, (float)y, 0);
for (i = 1; i < 4; i++) for (i = 1; i < 4; i++)
{
Points[i] += Points[0]; Points[i] += Points[0];
Points[i] = lcProjectPoint(Points[i], ModelView, Projection, Viewport);
ProjectPoints(Points, 4, ModelView, Projection, Viewport); }
// Check if the mouse is over an arrow. // Check if the mouse is over an arrow.
for (i = 1; i < 4; i++) for (i = 1; i < 4; i++)
{ {
Vector3 Line = Points[i] - Points[0]; lcVector3 Line = Points[i] - Points[0];
Vector3 Vec = Pt - Points[0]; lcVector3 Vec = Pt - Points[0];
float u = Dot3(Vec, Line) / Line.LengthSquared(); float u = lcDot(Vec, Line) / Line.LengthSquared();
// Point is outside the line segment. // Point is outside the line segment.
if (u < 0.0f || u > 1.0f) if (u < 0.0f || u > 1.0f)
continue; continue;
// Closest point in the line segment to the mouse. // Closest point in the line segment to the mouse.
Vector3 Closest = Points[0] + u * Line; lcVector3 Closest = Points[0] + Line * u;
if ((Closest - Pt).LengthSquared() < 100.0f) if ((Closest - Pt).LengthSquared() < 100.0f)
{ {
@ -8586,23 +8561,22 @@ void Project::MouseUpdateOverlays(View* view, int x, int y)
float Aspect = (float)Viewport[2]/(float)Viewport[3]; float Aspect = (float)Viewport[2]/(float)Viewport[3];
Camera* Cam = view->m_Camera; Camera* Cam = view->m_Camera;
Matrix44 ModelView, Projection; const lcMatrix44& ModelView = Cam->mWorldView;
ModelView.CreateLookAt(Cam->GetEyePosition(), Cam->GetTargetPosition(), Cam->GetUpVector()); lcMatrix44 Projection = lcMatrix44Perspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar);
Projection.CreatePerspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar);
// Unproject the mouse point against both the front and the back clipping planes. // Unproject the mouse point against both the front and the back clipping planes.
Vector3 SegStart = UnprojectPoint(Vector3((float)x, (float)y, 0.0f), ModelView, Projection, Viewport); lcVector3 SegStart = lcUnprojectPoint(lcVector3((float)x, (float)y, 0.0f), ModelView, Projection, Viewport);
Vector3 SegEnd = UnprojectPoint(Vector3((float)x, (float)y, 1.0f), ModelView, Projection, Viewport); lcVector3 SegEnd = lcUnprojectPoint(lcVector3((float)x, (float)y, 1.0f), ModelView, Projection, Viewport);
Vector3 Center(m_OverlayCenter[0], m_OverlayCenter[1], m_OverlayCenter[2]); lcVector3 Center(m_OverlayCenter);
Vector3 Line = SegEnd - SegStart; lcVector3 Line = SegEnd - SegStart;
Vector3 Vec = Center - SegStart; lcVector3 Vec = Center - SegStart;
float u = Dot3(Vec, Line) / Line.LengthSquared(); float u = lcDot(Vec, Line) / Line.LengthSquared();
// Closest point in the line to the mouse. // Closest point in the line to the mouse.
Vector3 Closest = SegStart + u * Line; lcVector3 Closest = SegStart + Line * u;
int Mode = -1; int Mode = -1;
float Distance = (Closest - Center).Length(); float Distance = (Closest - Center).Length();
@ -8643,22 +8617,22 @@ void Project::MouseUpdateOverlays(View* view, int x, int y)
if (f >= 0.0f) if (f >= 0.0f)
{ {
Camera* Cam = view->m_Camera; Camera* Cam = view->m_Camera;
Vector3 ViewDir = Cam->GetTargetPosition() - Cam->GetEyePosition(); lcVector3 ViewDir(Cam->mTargetPosition - Cam->mPosition);
float u1 = (-b + sqrtf(f)) / (2*a); float u1 = (-b + sqrtf(f)) / (2*a);
float u2 = (-b - sqrtf(f)) / (2*a); float u2 = (-b - sqrtf(f)) / (2*a);
Vector3 Intersections[2] = lcVector3 Intersections[2] =
{ {
Vector3(x1 + u1*(x2-x1), y1 + u1*(y2-y1), z1 + u1*(z2-z1)), lcVector3(x1 + u1*(x2-x1), y1 + u1*(y2-y1), z1 + u1*(z2-z1)),
Vector3(x1 + u2*(x2-x1), y1 + u2*(y2-y1), z1 + u2*(z2-z1)) lcVector3(x1 + u2*(x2-x1), y1 + u2*(y2-y1), z1 + u2*(z2-z1))
}; };
for (int i = 0; i < 2; i++) for (int i = 0; i < 2; i++)
{ {
Vector3 Dist = Intersections[i] - Center; lcVector3 Dist = Intersections[i] - Center;
if (Dot3(ViewDir, Dist) > 0.0f) if (lcDot(ViewDir, Dist) > 0.0f)
continue; continue;
// Find the rotation from the focused piece if relative snap is enabled. // Find the rotation from the focused piece if relative snap is enabled.
@ -8668,13 +8642,11 @@ void Project::MouseUpdateOverlays(View* view, int x, int y)
if ((Focus != NULL) && Focus->IsPiece()) if ((Focus != NULL) && Focus->IsPiece())
{ {
float Rot[4]; const lcVector4& Rot = ((Piece*)Focus)->mRotation;
((Piece*)Focus)->GetRotation(Rot);
Matrix33 RotMat; lcMatrix44 RotMat = lcMatrix44FromAxisAngle(lcVector3(Rot[0], Rot[1], Rot[2]), -Rot[3] * LC_DTOR);
RotMat.CreateFromAxisAngle(Vector3(Rot[0], Rot[1], Rot[2]), -Rot[3] * LC_DTOR);
Dist = Mul(Dist, RotMat); Dist = lcMul30(Dist, RotMat);
} }
} }
@ -8814,15 +8786,14 @@ void Project::UpdateOverlayScale()
float Aspect = (float)Viewport[2]/(float)Viewport[3]; float Aspect = (float)Viewport[2]/(float)Viewport[3];
Camera* Cam = m_ActiveView->m_Camera; Camera* Cam = m_ActiveView->m_Camera;
Matrix44 ModelView, Projection; const lcMatrix44& ModelView = Cam->mWorldView;
ModelView.CreateLookAt(Cam->GetEyePosition(), Cam->GetTargetPosition(), Cam->GetUpVector()); lcMatrix44 Projection = lcMatrix44Perspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar);
Projection.CreatePerspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar);
Vector3 Screen = ProjectPoint(m_OverlayCenter, ModelView, Projection, Viewport); lcVector3 Screen = lcProjectPoint(m_OverlayCenter, ModelView, Projection, Viewport);
Screen[0] += 10.0f; Screen[0] += 10.0f;
Vector3 Point = UnprojectPoint(Screen, ModelView, Projection, Viewport); lcVector3 Point = lcUnprojectPoint(Screen, ModelView, Projection, Viewport);
Vector3 Dist(Point - m_OverlayCenter); lcVector3 Dist(Point - m_OverlayCenter);
m_ActiveView->m_OverlayScale = Dist.Length() * 5.0f; m_ActiveView->m_OverlayScale = Dist.Length() * 5.0f;
} }
} }

View file

@ -6,6 +6,7 @@
#include "opengl.h" #include "opengl.h"
#include "array.h" #include "array.h"
#include "algebra.h" #include "algebra.h"
#include "lc_math.h"
typedef enum typedef enum
{ {
@ -105,8 +106,8 @@ public:
unsigned short GetTotalFrames () const unsigned short GetTotalFrames () const
{ return m_nTotalFrames; } { return m_nTotalFrames; }
void ConvertToUserUnits(Vector3& Value) const; void ConvertToUserUnits(lcVector3& Value) const;
void ConvertFromUserUnits(Vector3& Value) const; void ConvertFromUserUnits(lcVector3& Value) const;
void GetArrays(Piece** ppPiece, Camera** ppCamera, Light** ppLight) void GetArrays(Piece** ppPiece, Camera** ppCamera, Light** ppLight)
{ {
*ppPiece = m_pPieces; *ppPiece = m_pPieces;
@ -128,8 +129,8 @@ public:
void CreateImages(Image* images, int width, int height, unsigned short from, unsigned short to, bool hilite); void CreateImages(Image* images, int width, int height, unsigned short from, unsigned short to, bool hilite);
void Render(View* view, bool bToMemory); void Render(View* view, bool bToMemory);
void CheckAutoSave(); void CheckAutoSave();
bool GetSelectionCenter(Vector3& Center) const; bool GetSelectionCenter(lcVector3& Center) const;
bool GetFocusPosition(Vector3& Position) const; bool GetFocusPosition(lcVector3& Position) const;
Object* GetFocusObject() const; Object* GetFocusObject() const;
Group* AddGroup (const char* name, Group* pParent, float x, float y, float z); Group* AddGroup (const char* name, Group* pParent, float x, float y, float z);
@ -219,9 +220,9 @@ protected:
int m_OverlayMode; int m_OverlayMode;
bool m_OverlayActive; bool m_OverlayActive;
Vector3 m_OverlayCenter; lcVector3 m_OverlayCenter;
Vector3 m_OverlayTrackStart; lcVector3 m_OverlayTrackStart;
Vector3 m_OverlayDelta; lcVector3 m_OverlayDelta;
void MouseUpdateOverlays(View* view, int x, int y); void MouseUpdateOverlays(View* view, int x, int y);
void ActivateOverlay(); void ActivateOverlay();
void UpdateOverlayScale(); void UpdateOverlayScale();

View file

@ -575,8 +575,7 @@ void Terrain::Render(Camera* pCam, float aspect)
{ {
if (m_nOptions & LC_TERRAIN_FLAT) if (m_nOptions & LC_TERRAIN_FLAT)
{ {
float eye[3]; const lcVector3& eye = pCam->mPosition;
pCam->GetEyePos(eye);
glPushMatrix(); glPushMatrix();
glTranslatef(eye[0], eye[1], 0); glTranslatef(eye[0], eye[1], 0);
glScalef(pCam->m_zFar, pCam->m_zFar, 1); glScalef(pCam->m_zFar, pCam->m_zFar, 1);
@ -669,8 +668,7 @@ ty = (th*eye[1])/(2*pCam->m_zFar);
void Terrain::FindVisiblePatches(Camera* pCam, float aspect) void Terrain::FindVisiblePatches(Camera* pCam, float aspect)
{ {
// Get camera position information. // Get camera position information.
float eye[3]; const lcVector3& eye = pCam->mPosition;
pCam->GetEyePos(eye);
// Get perspective information. // Get perspective information.
float alpha = pCam->m_fovy / 2.0f; float alpha = pCam->m_fovy / 2.0f;
@ -681,9 +679,8 @@ void Terrain::FindVisiblePatches(Camera* pCam, float aspect)
float beta = 2.0f * halfFovX; float beta = 2.0f * halfFovX;
// Get vector stuff from the position. // Get vector stuff from the position.
float nonOrthoTop[3], target[3]; const lcVector3& nonOrthoTop = pCam->mUpVector;
pCam->GetUpVec(nonOrthoTop); const lcVector3& target = pCam->mTargetPosition;
pCam->GetTargetPos(target);
float front[3] = { target[0] - eye[0], target[1] - eye[1], target[2] - eye[2]}; float front[3] = { target[0] - eye[0], target[1] - eye[1], target[2] - eye[2]};
float side[3]; float side[3];
side[0] = nonOrthoTop[1]*front[2] - nonOrthoTop[2]*front[1]; side[0] = nonOrthoTop[1]*front[2] - nonOrthoTop[2]*front[1];

View file

@ -11,6 +11,7 @@ class Camera;
#include "str.h" #include "str.h"
#include "algebra.h" #include "algebra.h"
#include "lc_math.h"
typedef enum typedef enum
{ {
@ -200,8 +201,8 @@ struct LC_SEL_DATA
struct LC_PIECE_MODIFY struct LC_PIECE_MODIFY
{ {
Piece* piece; Piece* piece;
Vector3 Position; lcVector3 Position;
Vector3 Rotation; lcVector3 Rotation;
char name[81]; char name[81];
int from; int from;
int to; int to;
@ -212,9 +213,9 @@ struct LC_PIECE_MODIFY
struct LC_CAMERA_MODIFY struct LC_CAMERA_MODIFY
{ {
Camera* camera; Camera* camera;
Vector3 Eye; lcVector3 Eye;
Vector3 Target; lcVector3 Target;
Vector3 Up; lcVector3 Up;
char name[81]; char name[81];
float fovy; float fovy;
float znear; float znear;

View file

@ -432,7 +432,7 @@ LONG CMainFrame::OnUpdateInfo(UINT lParam, LONG wParam)
m_wndProperties.Update(Focus); m_wndProperties.Update(Focus);
char str[128]; char str[128];
Vector3 pos; lcVector3 pos;
lcGetActiveProject()->GetFocusPosition(pos); lcGetActiveProject()->GetFocusPosition(pos);
lcGetActiveProject()->ConvertToUserUnits(pos); lcGetActiveProject()->ConvertToUserUnits(pos);

View file

@ -205,7 +205,7 @@ void CPropertiesPane::SetPiece(Object* Focus)
float Rot[4]; float Rot[4];
Piece* pPiece = (Piece*)Focus; Piece* pPiece = (Piece*)Focus;
Vector3 Pos = pPiece->GetPosition(); lcVector3 Pos = pPiece->mPosition;
pPiece->GetRotation(Rot); pPiece->GetRotation(Rot);
Matrix mat(Rot, Pos); Matrix mat(Rot, Pos);
mat.ToEulerAngles(Rot); mat.ToEulerAngles(Rot);
@ -249,11 +249,11 @@ void CPropertiesPane::ModifyPiece()
Modify.piece = (Piece*)mObject; Modify.piece = (Piece*)mObject;
CMFCPropertyGridProperty* Position = m_wndPropList.GetProperty(0); CMFCPropertyGridProperty* Position = m_wndPropList.GetProperty(0);
Modify.Position = Vector3(Position->GetSubItem(0)->GetValue().fltVal, Position->GetSubItem(1)->GetValue().fltVal, Position->GetSubItem(2)->GetValue().fltVal); Modify.Position = lcVector3(Position->GetSubItem(0)->GetValue().fltVal, Position->GetSubItem(1)->GetValue().fltVal, Position->GetSubItem(2)->GetValue().fltVal);
lcGetActiveProject()->ConvertFromUserUnits(Modify.Position); lcGetActiveProject()->ConvertFromUserUnits(Modify.Position);
CMFCPropertyGridProperty* Rotation = m_wndPropList.GetProperty(1); CMFCPropertyGridProperty* Rotation = m_wndPropList.GetProperty(1);
Modify.Rotation = Vector3(Rotation->GetSubItem(0)->GetValue().fltVal, Rotation->GetSubItem(1)->GetValue().fltVal, Rotation->GetSubItem(2)->GetValue().fltVal); Modify.Rotation = lcVector3(Rotation->GetSubItem(0)->GetValue().fltVal, Rotation->GetSubItem(1)->GetValue().fltVal, Rotation->GetSubItem(2)->GetValue().fltVal);
CMFCPropertyGridProperty* Appearence = m_wndPropList.GetProperty(2); CMFCPropertyGridProperty* Appearence = m_wndPropList.GetProperty(2);
Modify.from = Appearence->GetSubItem(0)->GetValue().ulVal; Modify.from = Appearence->GetSubItem(0)->GetValue().ulVal;
@ -330,7 +330,7 @@ void CPropertiesPane::SetCamera(Object* Focus)
Camera* pCamera = (Camera*)Focus; Camera* pCamera = (Camera*)Focus;
Vector3 Pos = pCamera->GetEyePosition(); lcVector3 Pos = pCamera->mPosition;
lcGetActiveProject()->ConvertToUserUnits(Pos); lcGetActiveProject()->ConvertToUserUnits(Pos);
CMFCPropertyGridProperty* Position = m_wndPropList.GetProperty(0); CMFCPropertyGridProperty* Position = m_wndPropList.GetProperty(0);
@ -338,7 +338,7 @@ void CPropertiesPane::SetCamera(Object* Focus)
UpdateProperty(Position->GetSubItem(1), Pos[1]); UpdateProperty(Position->GetSubItem(1), Pos[1]);
UpdateProperty(Position->GetSubItem(2), Pos[2]); UpdateProperty(Position->GetSubItem(2), Pos[2]);
Vector3 Target = pCamera->GetTargetPosition(); lcVector3 Target = pCamera->mTargetPosition;
lcGetActiveProject()->ConvertToUserUnits(Target); lcGetActiveProject()->ConvertToUserUnits(Target);
CMFCPropertyGridProperty* TargetProp = m_wndPropList.GetProperty(1); CMFCPropertyGridProperty* TargetProp = m_wndPropList.GetProperty(1);
@ -346,7 +346,7 @@ void CPropertiesPane::SetCamera(Object* Focus)
UpdateProperty(TargetProp->GetSubItem(1), Target[1]); UpdateProperty(TargetProp->GetSubItem(1), Target[1]);
UpdateProperty(TargetProp->GetSubItem(2), Target[2]); UpdateProperty(TargetProp->GetSubItem(2), Target[2]);
Vector3 Up = pCamera->GetUpVector(); lcVector3 Up = pCamera->mUpVector;
CMFCPropertyGridProperty* UpProp = m_wndPropList.GetProperty(2); CMFCPropertyGridProperty* UpProp = m_wndPropList.GetProperty(2);
UpdateProperty(UpProp->GetSubItem(0), Up[0]); UpdateProperty(UpProp->GetSubItem(0), Up[0]);
@ -369,15 +369,15 @@ void CPropertiesPane::ModifyCamera()
Modify.camera = (Camera*)mObject; Modify.camera = (Camera*)mObject;
CMFCPropertyGridProperty* PositionProp = m_wndPropList.GetProperty(0); CMFCPropertyGridProperty* PositionProp = m_wndPropList.GetProperty(0);
Modify.Eye = Vector3(PositionProp->GetSubItem(0)->GetValue().fltVal, PositionProp->GetSubItem(1)->GetValue().fltVal, PositionProp->GetSubItem(2)->GetValue().fltVal); Modify.Eye = lcVector3(PositionProp->GetSubItem(0)->GetValue().fltVal, PositionProp->GetSubItem(1)->GetValue().fltVal, PositionProp->GetSubItem(2)->GetValue().fltVal);
lcGetActiveProject()->ConvertFromUserUnits(Modify.Eye); lcGetActiveProject()->ConvertFromUserUnits(Modify.Eye);
CMFCPropertyGridProperty* TargetProp = m_wndPropList.GetProperty(1); CMFCPropertyGridProperty* TargetProp = m_wndPropList.GetProperty(1);
Modify.Target = Vector3(TargetProp->GetSubItem(0)->GetValue().fltVal, TargetProp->GetSubItem(1)->GetValue().fltVal, TargetProp->GetSubItem(2)->GetValue().fltVal); Modify.Target = lcVector3(TargetProp->GetSubItem(0)->GetValue().fltVal, TargetProp->GetSubItem(1)->GetValue().fltVal, TargetProp->GetSubItem(2)->GetValue().fltVal);
lcGetActiveProject()->ConvertFromUserUnits(Modify.Target); lcGetActiveProject()->ConvertFromUserUnits(Modify.Target);
CMFCPropertyGridProperty* UpProp = m_wndPropList.GetProperty(2); CMFCPropertyGridProperty* UpProp = m_wndPropList.GetProperty(2);
Modify.Up = Vector3(UpProp->GetSubItem(0)->GetValue().fltVal, UpProp->GetSubItem(1)->GetValue().fltVal, UpProp->GetSubItem(2)->GetValue().fltVal); Modify.Up = lcVector3(UpProp->GetSubItem(0)->GetValue().fltVal, UpProp->GetSubItem(1)->GetValue().fltVal, UpProp->GetSubItem(2)->GetValue().fltVal);
CMFCPropertyGridProperty* SettingsProp = m_wndPropList.GetProperty(3); CMFCPropertyGridProperty* SettingsProp = m_wndPropList.GetProperty(3);
Modify.fovy = SettingsProp->GetSubItem(0)->GetValue().fltVal; Modify.fovy = SettingsProp->GetSubItem(0)->GetValue().fltVal;