From efdfdfe6a57c2225e2c314bd138bf13159d706a9 Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 23 Feb 2006 18:05:46 +0000 Subject: Moved all LeoCAD units conversion code to a single function, Fixed a bug moving the camera with the modify dialog. git-svn-id: http://svn.leocad.org/trunk@489 c7d43263-9d01-0410-8a33-9dba5d9f93d6 --- common/algebra.h | 3 ++ common/piece.h | 2 ++ common/project.cpp | 84 +++++++++++++++++++++++------------------------- common/project.h | 8 ++--- common/typedefs.h | 16 ++++++---- linux/toolbar.cpp | 8 +++-- win/Mainfrm.cpp | 5 +-- win/Moddlg.cpp | 94 +++++++++++++++--------------------------------------- win/System.cpp | 6 ++-- 9 files changed, 94 insertions(+), 132 deletions(-) diff --git a/common/algebra.h b/common/algebra.h index 826fe7a..4a34b36 100644 --- a/common/algebra.h +++ b/common/algebra.h @@ -299,6 +299,9 @@ public: friend inline Vector3 operator*=(Vector3& a, float b) { a.m_Value = a.m_Value * b; return a; } + friend inline Vector3 operator/=(Vector3& a, float b) + { a.m_Value = a.m_Value / b; return a; } + // Other functions. inline float Length() const { return m_Value.Length3(); } diff --git a/common/piece.h b/common/piece.h index 91dfc74..6304681 100644 --- a/common/piece.h +++ b/common/piece.h @@ -100,6 +100,8 @@ public: { return m_nFrameHide; } const float* GetConstPosition() { return m_fPosition; } + inline Vector3 GetPosition() const + { return Vector3(m_fPosition[0], m_fPosition[1], m_fPosition[2]); } void GetPosition (float* position) { memcpy(position, m_fPosition, sizeof(m_fPosition)); } void GetRotation (float* rotation) diff --git a/common/project.cpp b/common/project.cpp index 36f109b..2426cd5 100644 --- a/common/project.cpp +++ b/common/project.cpp @@ -3496,18 +3496,18 @@ void Project::HandleNotify(LC_NOTIFY id, unsigned long param) LC_PIECE_MODIFY* mod = (LC_PIECE_MODIFY*)param; Piece* pPiece = (Piece*)mod->piece; - float pos[3], rot[4]; - pPiece->GetPosition(pos); + float rot[4]; + Vector3 Pos = pPiece->GetPosition(); pPiece->GetRotation(rot); - Matrix mat(rot, pos); + Matrix mat(rot, Pos); mat.ToEulerAngles(rot); - if (mod->pos[0] != pos[0] || mod->pos[1] != pos[1] || mod->pos[2] != pos[2]) - pPiece->ChangeKey(m_bAnimation ? m_nCurFrame : m_nCurStep, m_bAnimation, m_bAddKeys, mod->pos, LC_PK_POSITION); + if (Pos != mod->Position) + pPiece->ChangeKey(m_bAnimation ? m_nCurFrame : m_nCurStep, m_bAnimation, m_bAddKeys, mod->Position, LC_PK_POSITION); - if (mod->rot[0] != rot[0] || mod->rot[1] != rot[1] || mod->rot[2] != rot[2]) + if (mod->Rotation[0] != rot[0] || mod->Rotation[1] != rot[1] || mod->Rotation[2] != rot[2]) { - mat.FromEulerAngles (mod->rot[0], mod->rot[1], mod->rot[2]); + mat.FromEulerAngles(mod->Rotation[0], mod->Rotation[1], mod->Rotation[2]); mat.ToAxisAngle(rot); pPiece->ChangeKey(m_bAnimation ? m_nCurFrame : m_nCurStep, m_bAnimation, m_bAddKeys, rot, LC_PK_ROTATION); } @@ -3543,21 +3543,20 @@ void Project::HandleNotify(LC_NOTIFY id, unsigned long param) { LC_CAMERA_MODIFY* mod = (LC_CAMERA_MODIFY*)param; Camera* pCamera = (Camera*)mod->camera; - float tmp[3]; if (mod->hidden) pCamera->Hide(); else pCamera->UnHide(); - pCamera->GetUpVec(tmp); + if (pCamera->GetEyePosition() != mod->Eye) + pCamera->ChangeKey(m_bAnimation ? m_nCurFrame : m_nCurStep, m_bAnimation, m_bAddKeys, mod->Eye, LC_CK_EYE); + + if (pCamera->GetTargetPosition() != mod->Target) + pCamera->ChangeKey(m_bAnimation ? m_nCurFrame : m_nCurStep, m_bAnimation, m_bAddKeys, mod->Target, LC_CK_TARGET); - if (tmp[0] != mod->eye[0] || tmp[1] != mod->eye[1] || tmp[2] != mod->eye[2]) - pCamera->ChangeKey(m_bAnimation ? m_nCurFrame : m_nCurStep, m_bAnimation, m_bAddKeys, mod->eye, LC_CK_EYE); - if (tmp[0] != mod->target[0] || tmp[1] != mod->target[1] || tmp[2] != mod->target[2]) - pCamera->ChangeKey(m_bAnimation ? m_nCurFrame : m_nCurStep, m_bAnimation, m_bAddKeys, mod->target, LC_CK_TARGET); - if (tmp[0] != mod->up[0] || tmp[1] != mod->up[1] || tmp[2] != mod->up[2]) - pCamera->ChangeKey(m_bAnimation ? m_nCurFrame : m_nCurStep, m_bAnimation, m_bAddKeys, mod->up, LC_CK_UP); + if (pCamera->GetUpVector() != mod->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_zNear = mod->znear; @@ -6513,54 +6512,51 @@ void Project::GetActiveViewportMatrices(Matrix44& ModelView, Matrix44& Projectio Projection.CreatePerspective(Cam->m_fovy, Aspect, Cam->m_zNear, Cam->m_zFar); } -void Project::GetFocusPosition(float* pos) +void Project::ConvertToUserUnits(Vector3& Value) const +{ + if ((m_nSnap & LC_DRAW_CM_UNITS) == 0) + Value /= 0.08f; +} + +void Project::ConvertFromUserUnits(Vector3& Value) const +{ + if ((m_nSnap & LC_DRAW_CM_UNITS) == 0) + Value *= 0.08f; +} + +bool Project::GetFocusPosition(Vector3& Position) const { Piece* pPiece; Camera* pCamera; + float* pos = &Position[0]; for (pPiece = m_pPieces; pPiece; pPiece = pPiece->m_pNext) if (pPiece->IsFocused()) { pPiece->GetPosition(pos); - if ((m_nSnap & LC_DRAW_CM_UNITS) == 0) - { - pos[0] /= 0.08f; - pos[1] /= 0.08f; - pos[2] /= 0.08f; - } - return; + return true; } for (pCamera = m_pCameras; pCamera; pCamera = pCamera->m_pNext) { if (pCamera->IsEyeFocused()) { - pCamera->GetEyePos (pos); - if ((m_nSnap & LC_DRAW_CM_UNITS) == 0) - { - pos[0] /= 0.08f; - pos[1] /= 0.08f; - pos[2] /= 0.08f; - } - return; + pCamera->GetEyePos(pos); + return true; } if (pCamera->IsTargetFocused()) { - pCamera->GetTargetPos (pos); - if ((m_nSnap & LC_DRAW_CM_UNITS) == 0) - { - pos[0] /= 0.08f; - pos[1] /= 0.08f; - pos[2] /= 0.08f; - } - return; + pCamera->GetTargetPos(pos); + return true; } } // TODO: light pos[0] = pos[1] = pos[2] = 0.0f; + + return false; } // Returns the object that currently has focus. @@ -8897,16 +8893,16 @@ void Project::MouseUpdateOverlays(int x, int y) } } - for (int i = 1; i < 4; i++) + int i, Mode = -1; + Vector3 Pt((float)x, (float)y, 0); + + for (i = 1; i < 4; i++) Points[i] += Points[0]; ProjectPoints(Points, 4, ModelView, Projection, Viewport); - int Mode = -1; - Vector3 Pt((float)x, (float)y, 0); - // Check if the mouse is over an arrow. - for (int i = 1; i < 4; i++) + for (i = 1; i < 4; i++) { Vector3 Line = Points[i] - Points[0]; Vector3 Vec = Pt - Points[0]; diff --git a/common/project.h b/common/project.h index 870bfce..dd495c6 100644 --- a/common/project.h +++ b/common/project.h @@ -73,8 +73,6 @@ public: { m_bAnimation = Anim; } // only to be called from lcApplication::Initialize() unsigned short GetCurrentTime () { return m_bAnimation ? m_nCurFrame : m_nCurStep; } - unsigned long GetSnapFlags() const - { return m_nSnap; } void SetCurrentPiece(PieceInfo* pInfo) { m_pCurPiece = pInfo; } int GetCurrentColor () const @@ -96,6 +94,8 @@ public: unsigned short GetTotalFrames () const { return m_nTotalFrames; } + void ConvertToUserUnits(Vector3& Value) const; + void ConvertFromUserUnits(Vector3& Value) const; void GetArrays(Piece** ppPiece, Camera** ppCamera, Light** ppLight) { *ppPiece = m_pPieces; @@ -116,7 +116,8 @@ public: void Render(bool bToMemory); void SetViewSize(int cx, int cy); void CheckAutoSave(); - void GetFocusPosition(float* pos); + bool GetSelectionCenter(Vector3& Center) const; + bool GetFocusPosition(Vector3& Position) const; Object* GetFocusObject() const; Group* AddGroup (const char* name, Group* pParent, float x, float y, float z); @@ -166,7 +167,6 @@ protected: void FindObjectFromPoint(int x, int y, LC_CLICKLINE* pLine, bool PiecesOnly = false); void FindObjectsInBox(float x1, float y1, float x2, float y2, PtrArray& Objects); void SelectAndFocusNone(bool bFocusOnly); - bool GetSelectionCenter(Vector3& Center) const; void GetActiveViewportMatrices(Matrix44& ModelView, Matrix44& Projection, int Viewport[4]); void CalculateStep(); diff --git a/common/typedefs.h b/common/typedefs.h index a12882c..ce68b1f 100644 --- a/common/typedefs.h +++ b/common/typedefs.h @@ -7,9 +7,11 @@ class Group; class Piece; class PieceInfo; +class Camera; #include "defines.h" #include "str.h" +#include "algebra.h" typedef enum { @@ -225,10 +227,10 @@ typedef struct typedef struct { - void* piece; + Piece* piece; + Vector3 Position; + Vector3 Rotation; char name[81]; - float pos[3]; - float rot[3]; int from; int to; bool hidden; @@ -237,11 +239,11 @@ typedef struct typedef struct { - void* camera; + Camera* camera; + Vector3 Eye; + Vector3 Target; + Vector3 Up; char name[81]; - float eye[3]; - float target[3]; - float up[3]; float fovy; float znear; float zfar; diff --git a/linux/toolbar.cpp b/linux/toolbar.cpp index 59b33f6..3152fef 100644 --- a/linux/toolbar.cpp +++ b/linux/toolbar.cpp @@ -768,10 +768,12 @@ static void statusbar_listener (int message, void *data, void *user) if (message == LC_MSG_FOCUS_CHANGED) { char text[32]; - float pos[3]; - project->GetFocusPosition (pos); + Point3 pos; + + project->GetFocusPosition(pos); + project->ConvertToUserUnits(pos); + sprintf (text, "X: %.2f Y: %.2f Z: %.2f", pos[0], pos[1], pos[2]); - gtk_label_set (GTK_LABEL (label_position), text); } } diff --git a/win/Mainfrm.cpp b/win/Mainfrm.cpp index 0761390..4a97393 100644 --- a/win/Mainfrm.cpp +++ b/win/Mainfrm.cpp @@ -416,11 +416,12 @@ LONG CMainFrame::OnUpdateInfo(UINT lParam, LONG wParam) m_wndModifyDlg.UpdateInfo((Object*)lParam); char str[128]; - float pos[3]; + Vector3 pos; lcGetActiveProject()->GetFocusPosition(pos); - sprintf (str, "X: %.2f Y: %.2f Z: %.2f", pos[0], pos[1], pos[2]); + lcGetActiveProject()->ConvertToUserUnits(pos); + sprintf (str, "X: %.2f Y: %.2f Z: %.2f", pos[0], pos[1], pos[2]); SetStatusBarPane(ID_INDICATOR_POSITION, str); return TRUE; diff --git a/win/Moddlg.cpp b/win/Moddlg.cpp index b81ec37..11e85bd 100644 --- a/win/Moddlg.cpp +++ b/win/Moddlg.cpp @@ -165,23 +165,18 @@ void CModifyDialog::UpdateInfo(Object* pObject) { case LC_OBJECT_PIECE: { - float pos[3], rot[4]; + float rot[4]; Piece* pPiece = (Piece*)m_pObject; - pPiece->GetPosition(pos); + Vector3 Pos = pPiece->GetPosition(); pPiece->GetRotation(rot); - Matrix mat(rot, pos); + Matrix mat(rot, Pos); mat.ToEulerAngles(rot); - if ((lcGetActiveProject()->GetSnapFlags() & LC_DRAW_CM_UNITS) == 0) - { - pos[0] /= 0.08f; - pos[1] /= 0.08f; - pos[2] /= 0.08f; - } + lcGetActiveProject()->ConvertToUserUnits(Pos); - m_fPosX = pos[0]; - m_fPosY = pos[1]; - m_fPosZ = pos[2]; + m_fPosX = Pos[0]; + m_fPosY = Pos[1]; + m_fPosZ = Pos[2]; m_fRotX = rot[0]; m_fRotY = rot[1]; m_fRotZ = rot[2]; @@ -206,42 +201,27 @@ void CModifyDialog::UpdateInfo(Object* pObject) case LC_OBJECT_CAMERA: case LC_OBJECT_CAMERA_TARGET: { - float tmp[3]; + Vector3 tmp; Camera* pCamera; if (m_nType == LC_OBJECT_CAMERA) pCamera = (Camera*)m_pObject; else pCamera = ((CameraTarget*)m_pObject)->GetParent(); - pCamera->GetEyePos(tmp); - if ((lcGetActiveProject()->GetSnapFlags() & LC_DRAW_CM_UNITS) == 0) - { - tmp[0] /= 0.08f; - tmp[1] /= 0.08f; - tmp[2] /= 0.08f; - } + tmp = pCamera->GetEyePosition(); + lcGetActiveProject()->ConvertToUserUnits(tmp); m_fPosX = tmp[0]; m_fPosY = tmp[1]; m_fPosZ = tmp[2]; - pCamera->GetTargetPos(tmp); - if ((lcGetActiveProject()->GetSnapFlags() & LC_DRAW_CM_UNITS) == 0) - { - tmp[0] /= 0.08f; - tmp[1] /= 0.08f; - tmp[2] /= 0.08f; - } + tmp = pCamera->GetTargetPosition(); + lcGetActiveProject()->ConvertToUserUnits(tmp); m_fRotX = tmp[0]; m_fRotY = tmp[1]; m_fRotZ = tmp[2]; - pCamera->GetUpVec(tmp); - if ((lcGetActiveProject()->GetSnapFlags() & LC_DRAW_CM_UNITS) == 0) - { - tmp[0] /= 0.08f; - tmp[1] /= 0.08f; - tmp[2] /= 0.08f; - } + tmp = pCamera->GetUpVector(); + lcGetActiveProject()->ConvertToUserUnits(tmp); m_fUpX = tmp[0]; m_fUpY = tmp[1]; m_fUpZ = tmp[2]; @@ -369,19 +349,10 @@ void CModifyDialog::OnModdlgApply() { LC_PIECE_MODIFY mod; - mod.piece = m_pObject; - mod.pos[0] = m_fPosX; - mod.pos[1] = m_fPosY; - mod.pos[2] = m_fPosZ; - if ((lcGetActiveProject()->GetSnapFlags() & LC_DRAW_CM_UNITS) == 0) - { - mod.pos[0] *= 0.08f; - mod.pos[1] *= 0.08f; - mod.pos[2] *= 0.08f; - } - mod.rot[0] = m_fRotX; - mod.rot[1] = m_fRotY; - mod.rot[2] = m_fRotZ; + mod.piece = (Piece*)m_pObject; + mod.Position = Vector3(m_fPosX, m_fPosY, m_fPosZ); + lcGetActiveProject()->ConvertFromUserUnits(mod.Position); + mod.Rotation = Vector3(m_fRotX, m_fRotY, m_fRotZ); mod.from = m_nFrom; mod.to = m_nTo; mod.hidden = (m_bHidden != FALSE); @@ -395,29 +366,14 @@ void CModifyDialog::OnModdlgApply() { LC_CAMERA_MODIFY mod; - mod.camera = m_pObject; + mod.camera = (Camera*)m_pObject; mod.hidden = (m_bHidden != FALSE); - mod.eye[0] = m_fPosX; - mod.eye[1] = m_fPosY; - mod.eye[2] = m_fPosZ; - mod.target[0] = m_fRotX; - mod.target[1] = m_fRotY; - mod.target[2] = m_fRotZ; - mod.up[0] = m_fUpX; - mod.up[1] = m_fUpY; - mod.up[2] = m_fUpZ; - if ((lcGetActiveProject()->GetSnapFlags() & LC_DRAW_CM_UNITS) == 0) - { - mod.eye[0] *= 0.08f; - mod.eye[1] *= 0.08f; - mod.eye[2] *= 0.08f; - mod.target[0] *= 0.08f; - mod.target[1] *= 0.08f; - mod.target[2] *= 0.08f; - mod.up[0] *= 0.08f; - mod.up[1] *= 0.08f; - mod.up[2] *= 0.08f; - } + mod.Eye = Vector3(m_fPosX, m_fPosY, m_fPosZ); + mod.Target = Vector3(m_fRotX, m_fRotY, m_fRotZ); + mod.Up = Vector3(m_fUpX, m_fUpY, m_fUpZ); + lcGetActiveProject()->ConvertFromUserUnits(mod.Eye); + lcGetActiveProject()->ConvertFromUserUnits(mod.Target); + lcGetActiveProject()->ConvertFromUserUnits(mod.Up); mod.fovy = m_fFOV; mod.znear = m_fNear; mod.zfar = m_fFar; diff --git a/win/System.cpp b/win/System.cpp index 0e2cb92..89b57ca 100644 --- a/win/System.cpp +++ b/win/System.cpp @@ -451,7 +451,7 @@ void SystemUpdateViewport(int nNew, int nOld) CMenu* pMenu = GetMainMenu(2); if (!pMenu) return; - pMenu = pMenu->GetSubMenu(12); + pMenu = pMenu->GetSubMenu(13); pMenu->CheckMenuItem(nOld + ID_VIEWPORT01, MF_BYCOMMAND | MF_UNCHECKED); pMenu->CheckMenuItem(nNew + ID_VIEWPORT01, MF_BYCOMMAND | MF_CHECKED); } @@ -770,7 +770,7 @@ void SystemUpdateCurrentCamera(Camera* pOld, Camera* pNew, Camera* pCamera) CMenu* Menu = GetMainMenu(2); if (!Menu) return; - CBMPMenu* pMainMenu = (CBMPMenu*)Menu->GetSubMenu(13); + CBMPMenu* pMainMenu = (CBMPMenu*)Menu->GetSubMenu(14); CMenu* pPopupMenu = menuPopups.GetSubMenu(1)->GetSubMenu(3); int i; @@ -798,7 +798,7 @@ void SystemUpdateCameraMenu(Camera* pCamera) CMenu* Menu = GetMainMenu(2); if (!Menu) return; - CBMPMenu* pMainMenu = (CBMPMenu*)Menu->GetSubMenu(13); + CBMPMenu* pMainMenu = (CBMPMenu*)Menu->GetSubMenu(14); CMenu* pPopupMenu = menuPopups.GetSubMenu(1)->GetSubMenu(3); Camera* pFirst = pCamera; int i; -- cgit v1.2.3