Skip to content

Commit dfa1a64

Browse files
committed
Add new dialog to set camera position.
1 parent 0501925 commit dfa1a64

File tree

10 files changed

+261
-21
lines changed

10 files changed

+261
-21
lines changed

Sources/Modeler/Camera.cpp

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ static const double MinZoomDistance = 1.0f;
99
Camera::Camera (const glm::dvec3& eye,
1010
const glm::dvec3& center,
1111
const glm::dvec3& up,
12-
double fieldOfView,
12+
double fieldOfViewY,
1313
double nearPlane,
1414
double farPlane) :
1515
eye (eye),
1616
center (center),
1717
up (up),
18-
fieldOfViewY (fieldOfView),
18+
fieldOfViewY (fieldOfViewY),
1919
nearPlane (nearPlane),
2020
farPlane (farPlane)
2121
{
@@ -37,6 +37,21 @@ const glm::dvec3& Camera::GetUp () const
3737
return up;
3838
}
3939

40+
double Camera::GetFieldOfViewY () const
41+
{
42+
return fieldOfViewY;
43+
}
44+
45+
double Camera::GetNearPlane () const
46+
{
47+
return nearPlane;
48+
}
49+
50+
double Camera::GetFarPlane () const
51+
{
52+
return farPlane;
53+
}
54+
4055
glm::dmat4 Camera::GetViewMatrix () const
4156
{
4257
return glm::lookAt (eye, center, up);
@@ -125,4 +140,38 @@ void Camera::ZoomToSphere (const glm::dvec3& sphereCenter, double sphereRadius,
125140
// TODO: different center
126141
}
127142

143+
bool IsValidCamera (const glm::dvec3& eye,
144+
const glm::dvec3& center,
145+
const glm::dvec3& up,
146+
double fieldOfViewY,
147+
double nearPlane,
148+
double farPlane)
149+
{
150+
if (Geometry::IsZero (glm::distance (eye, center))) {
151+
return false;
152+
}
153+
154+
if (Geometry::IsZero (glm::length (up))) {
155+
return false;
156+
}
157+
158+
if (Geometry::IsEqual (glm::dot (glm::normalize (center - eye), glm::normalize (up)), -1.0)) {
159+
return false;
160+
}
161+
162+
if (Geometry::IsEqual (glm::dot (glm::normalize (center - eye), glm::normalize (up)), 1.0)) {
163+
return false;
164+
}
165+
166+
if (Geometry::IsZero (fieldOfViewY)) {
167+
return false;
168+
}
169+
170+
if (!Geometry::IsLower (nearPlane, farPlane)) {
171+
return false;
172+
}
173+
174+
return true;
175+
}
176+
128177
}

Sources/Modeler/Camera.hpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ class Camera
1212
Camera (const glm::dvec3& eye,
1313
const glm::dvec3& center,
1414
const glm::dvec3& up,
15-
double fieldOfView,
15+
double fieldOfViewY,
1616
double nearPlane,
1717
double farPlane);
1818

1919
const glm::dvec3& GetEye () const;
2020
const glm::dvec3& GetCenter () const;
2121
const glm::dvec3& GetUp () const;
22+
double GetFieldOfViewY () const;
23+
double GetNearPlane () const;
24+
double GetFarPlane () const;
2225

2326
glm::dmat4 GetViewMatrix () const;
2427
glm::dmat4 GetProjectionMatrix (double width, double height) const;
@@ -29,14 +32,21 @@ class Camera
2932
void ZoomToSphere (const glm::dvec3& sphereCenter, double sphereRadius, int width, int height);
3033

3134
private:
32-
glm::dvec3 eye;
33-
glm::dvec3 center;
34-
glm::dvec3 up;
35-
double fieldOfViewY;
36-
double nearPlane;
37-
double farPlane;
35+
glm::dvec3 eye;
36+
glm::dvec3 center;
37+
glm::dvec3 up;
38+
double fieldOfViewY;
39+
double nearPlane;
40+
double farPlane;
3841
};
3942

43+
bool IsValidCamera (const glm::dvec3& eye,
44+
const glm::dvec3& center,
45+
const glm::dvec3& up,
46+
double fieldOfViewY,
47+
double nearPlane,
48+
double farPlane);
49+
4050
}
4151

4252
#endif
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include "CameraDialog.hpp"
2+
#include "Geometry.hpp"
3+
4+
static const wxSize nameMinSize (100, -1);
5+
static const wxSize smallControlMinSize (100, -1);
6+
static const wxSize controlMinSize (300, -1);
7+
static const double MinCameraValue = -10000.0;
8+
static const double MaxCameraValue = 10000.0;
9+
10+
CameraDialog::CameraDialog (wxWindow* parent, const Modeler::Camera& camera) :
11+
wxDialog (parent, wxID_ANY, L"Camera Settings", wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE),
12+
origCamera (camera),
13+
saveButton (new wxButton (this, DialogIds::SaveButtonId, L"Save")),
14+
boxSizer (new wxBoxSizer (wxVERTICAL)),
15+
eyeXSpin (new wxSpinCtrlDouble (this, DialogIds::EyeXSpinId, L"", wxDefaultPosition, smallControlMinSize, wxSP_ARROW_KEYS, MinCameraValue, MaxCameraValue, 0.0, 0.1)),
16+
eyeYSpin (new wxSpinCtrlDouble (this, DialogIds::EyeYSpinId, L"", wxDefaultPosition, smallControlMinSize, wxSP_ARROW_KEYS, MinCameraValue, MaxCameraValue, 0.0, 0.1)),
17+
eyeZSpin (new wxSpinCtrlDouble (this, DialogIds::EyeZSpinId, L"", wxDefaultPosition, smallControlMinSize, wxSP_ARROW_KEYS, MinCameraValue, MaxCameraValue, 0.0, 0.1)),
18+
centerXSpin (new wxSpinCtrlDouble (this, DialogIds::CenterXSpinId, L"", wxDefaultPosition, smallControlMinSize, wxSP_ARROW_KEYS, MinCameraValue, MaxCameraValue, 0.0, 0.1)),
19+
centerYSpin (new wxSpinCtrlDouble (this, DialogIds::CenterYSpinId, L"", wxDefaultPosition, smallControlMinSize, wxSP_ARROW_KEYS, MinCameraValue, MaxCameraValue, 0.0, 0.1)),
20+
centerZSpin (new wxSpinCtrlDouble (this, DialogIds::CenterZSpinId, L"", wxDefaultPosition, smallControlMinSize, wxSP_ARROW_KEYS, MinCameraValue, MaxCameraValue, 0.0, 0.1)),
21+
upXSpin (new wxSpinCtrlDouble (this, DialogIds::UpXSpinId, L"", wxDefaultPosition, smallControlMinSize, wxSP_ARROW_KEYS, MinCameraValue, MaxCameraValue, 0.0, 0.1)),
22+
upYSpin (new wxSpinCtrlDouble (this, DialogIds::UpYSpinId, L"", wxDefaultPosition, smallControlMinSize, wxSP_ARROW_KEYS, MinCameraValue, MaxCameraValue, 0.0, 0.1)),
23+
upZSpin (new wxSpinCtrlDouble (this, DialogIds::UpZSpinId, L"", wxDefaultPosition, smallControlMinSize, wxSP_ARROW_KEYS, MinCameraValue, MaxCameraValue, 0.0, 0.1))
24+
{
25+
{
26+
wxBoxSizer* horizontalSizer = new wxBoxSizer (wxHORIZONTAL);
27+
horizontalSizer->Add (new wxStaticText (this, wxID_ANY, L"Eye Position", wxDefaultPosition, nameMinSize), 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
28+
wxBoxSizer* vectorHorizontalSizer = new wxBoxSizer (wxHORIZONTAL);
29+
vectorHorizontalSizer->Add (eyeXSpin, 1, wxEXPAND | wxALL, 5);
30+
vectorHorizontalSizer->Add (eyeYSpin, 1, wxEXPAND | wxALL, 5);
31+
vectorHorizontalSizer->Add (eyeZSpin, 1, wxEXPAND | wxALL, 5);
32+
horizontalSizer->Add (vectorHorizontalSizer, 1, wxEXPAND | wxALL, 0);
33+
boxSizer->Add (horizontalSizer, 0, wxEXPAND);
34+
}
35+
36+
{
37+
wxBoxSizer* horizontalSizer = new wxBoxSizer (wxHORIZONTAL);
38+
horizontalSizer->Add (new wxStaticText (this, wxID_ANY, L"Center Position", wxDefaultPosition, nameMinSize), 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
39+
wxBoxSizer* vectorHorizontalSizer = new wxBoxSizer (wxHORIZONTAL);
40+
vectorHorizontalSizer->Add (centerXSpin, 1, wxEXPAND | wxALL, 5);
41+
vectorHorizontalSizer->Add (centerYSpin, 1, wxEXPAND | wxALL, 5);
42+
vectorHorizontalSizer->Add (centerZSpin, 1, wxEXPAND | wxALL, 5);
43+
horizontalSizer->Add (vectorHorizontalSizer, 1, wxEXPAND | wxALL, 0);
44+
boxSizer->Add (horizontalSizer, 0, wxEXPAND);
45+
}
46+
47+
{
48+
wxBoxSizer* horizontalSizer = new wxBoxSizer (wxHORIZONTAL);
49+
horizontalSizer->Add (new wxStaticText (this, wxID_ANY, L"Up Vector", wxDefaultPosition, nameMinSize), 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
50+
wxBoxSizer* vectorHorizontalSizer = new wxBoxSizer (wxHORIZONTAL);
51+
vectorHorizontalSizer->Add (upXSpin, 1, wxEXPAND | wxALL, 5);
52+
vectorHorizontalSizer->Add (upYSpin, 1, wxEXPAND | wxALL, 5);
53+
vectorHorizontalSizer->Add (upZSpin, 1, wxEXPAND | wxALL, 5);
54+
horizontalSizer->Add (vectorHorizontalSizer, 1, wxEXPAND | wxALL, 0);
55+
boxSizer->Add (horizontalSizer, 0, wxEXPAND);
56+
}
57+
58+
eyeXSpin->SetValue (camera.GetEye ().x);
59+
eyeYSpin->SetValue (camera.GetEye ().y);
60+
eyeZSpin->SetValue (camera.GetEye ().z);
61+
62+
centerXSpin->SetValue (camera.GetCenter ().x);
63+
centerYSpin->SetValue (camera.GetCenter ().y);
64+
centerZSpin->SetValue (camera.GetCenter ().z);
65+
66+
upXSpin->SetValue (camera.GetUp ().x);
67+
upYSpin->SetValue (camera.GetUp ().y);
68+
upZSpin->SetValue (camera.GetUp ().z);
69+
70+
boxSizer->Add (saveButton, 0, wxEXPAND | wxDOWN | wxRIGHT | wxLEFT, 5);
71+
SetSizerAndFit (boxSizer);
72+
SetEscapeId (wxID_CANCEL);
73+
}
74+
75+
Modeler::Camera CameraDialog::GetCamera () const
76+
{
77+
glm::dvec3 eye (eyeXSpin->GetValue (), eyeYSpin->GetValue (), eyeZSpin->GetValue ());
78+
glm::dvec3 center (centerXSpin->GetValue (), centerYSpin->GetValue (), centerZSpin->GetValue ());
79+
glm::dvec3 up (upXSpin->GetValue (), upYSpin->GetValue (), upZSpin->GetValue ());
80+
Modeler::Camera camera (eye, center, glm::normalize (up), origCamera.GetFieldOfViewY (), origCamera.GetNearPlane (), origCamera.GetFarPlane ());
81+
return camera;
82+
}
83+
84+
void CameraDialog::OnButtonClick (wxCommandEvent& evt)
85+
{
86+
if (evt.GetId () == DialogIds::SaveButtonId) {
87+
glm::dvec3 eye (eyeXSpin->GetValue (), eyeYSpin->GetValue (), eyeZSpin->GetValue ());
88+
glm::dvec3 center (centerXSpin->GetValue (), centerYSpin->GetValue (), centerZSpin->GetValue ());
89+
glm::dvec3 up (upXSpin->GetValue (), upYSpin->GetValue (), upZSpin->GetValue ());
90+
if (Modeler::IsValidCamera (eye, center, up, origCamera.GetFieldOfViewY (), origCamera.GetNearPlane (), origCamera.GetFarPlane ())) {
91+
EndModal (wxID_OK);
92+
}
93+
}
94+
}
95+
96+
BEGIN_EVENT_TABLE (CameraDialog, wxDialog)
97+
EVT_BUTTON (wxID_ANY, CameraDialog::OnButtonClick)
98+
END_EVENT_TABLE ()
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#ifndef CAMERADIALOG_HPP
2+
#define CAMERADIALOG_HPP
3+
4+
#include "Camera.hpp"
5+
6+
#include <wx/wx.h>
7+
#include <wx/spinctrl.h>
8+
9+
class CameraDialog : public wxDialog
10+
{
11+
public:
12+
enum DialogIds
13+
{
14+
EyeXSpinId = 1001,
15+
EyeYSpinId = 1002,
16+
EyeZSpinId = 1003,
17+
CenterXSpinId = 1004,
18+
CenterYSpinId = 1005,
19+
CenterZSpinId = 1006,
20+
UpXSpinId = 1007,
21+
UpYSpinId = 1008,
22+
UpZSpinId = 1009,
23+
SaveButtonId = 1100
24+
};
25+
26+
CameraDialog (wxWindow* parent, const Modeler::Camera& camera);
27+
28+
Modeler::Camera GetCamera () const;
29+
30+
private:
31+
void OnButtonClick (wxCommandEvent& evt);
32+
33+
Modeler::Camera origCamera;
34+
35+
wxButton* saveButton;
36+
wxBoxSizer* boxSizer;
37+
wxSpinCtrlDouble* eyeXSpin;
38+
wxSpinCtrlDouble* eyeYSpin;
39+
wxSpinCtrlDouble* eyeZSpin;
40+
wxSpinCtrlDouble* centerXSpin;
41+
wxSpinCtrlDouble* centerYSpin;
42+
wxSpinCtrlDouble* centerZSpin;
43+
wxSpinCtrlDouble* upXSpin;
44+
wxSpinCtrlDouble* upYSpin;
45+
wxSpinCtrlDouble* upZSpin;
46+
47+
DECLARE_EVENT_TABLE ();
48+
};
49+
50+
#endif

Sources/VisualScriptCAD/Application/MainWindow.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "SettingsDialog.hpp"
88
#include "InfoDialog.hpp"
99
#include "ExportDialog.hpp"
10+
#include "CameraDialog.hpp"
1011
#include "IconStore.hpp"
1112
#include "Version.hpp"
1213
#include "VersionInfo.hpp"
@@ -158,6 +159,8 @@ MenuBar::MenuBar () :
158159
viewMenu->AppendSeparator ();
159160
viewMenu->Append (CommandId::View_Model_FitToWindow, "Fit Model to Window");
160161
viewMenu->Append (CommandId::View_Model_ResetView, "Reset Model View");
162+
viewMenu->AppendSeparator ();
163+
viewMenu->Append (CommandId::View_Model_CameraSettings, "Camera Settings");
161164
Append (viewMenu, L"&View");
162165

163166
wxMenu* toolsMenu = new wxMenu ();
@@ -453,6 +456,14 @@ void MainWindow::ProcessCommand (CommandId commandId)
453456
modelControl->ResetView ();
454457
}
455458
break;
459+
case View_Model_CameraSettings:
460+
{
461+
CameraDialog cameraDialog (this, modelControl->GetCamera ());
462+
if (cameraDialog.ShowModal () == wxID_OK) {
463+
modelControl->SetCamera (cameraDialog.GetCamera ());
464+
}
465+
}
466+
break;
456467
case Model_Info:
457468
{
458469
const Modeler::Model& model = evaluationData->GetModel ();

Sources/VisualScriptCAD/Application/MainWindow.hpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,19 @@ enum CommandId
2020
View_Editor_FitToWindow = 8,
2121
View_Model_FitToWindow = 9,
2222
View_Model_ResetView = 10,
23-
Tools_Options = 11,
24-
Model_Info = 12,
25-
Model_Export = 13,
26-
About_GitHub = 14,
27-
Tool_Undo = 15,
28-
Tool_Redo = 16,
29-
Tool_View_Editor = 17,
30-
Tool_View_Model = 18,
31-
Tool_View_Split = 19,
32-
Tool_Mode_Automatic = 20,
33-
Tool_Mode_Manual = 21,
34-
Tool_Mode_Update = 22,
23+
View_Model_CameraSettings = 11,
24+
Tools_Options = 12,
25+
Model_Info = 13,
26+
Model_Export = 14,
27+
About_GitHub = 15,
28+
Tool_Undo = 16,
29+
Tool_Redo = 17,
30+
Tool_View_Editor = 18,
31+
Tool_View_Model = 19,
32+
Tool_View_Split = 20,
33+
Tool_Mode_Automatic = 21,
34+
Tool_Mode_Manual = 22,
35+
Tool_Mode_Update = 23,
3536
File_OpenExample_First = 100,
3637
File_OpenRecent_First = 200
3738
};

Sources/VisualScriptCAD/Renderer/ModelControl.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,17 @@ void ModelControl::SetRenderSettings (const RenderSettings& newSettings)
163163
Refresh ();
164164
}
165165

166+
const Modeler::Camera& ModelControl::GetCamera () const
167+
{
168+
return renderScene.GetCamera ();
169+
}
170+
171+
void ModelControl::SetCamera (const Modeler::Camera& newCamera)
172+
{
173+
renderScene.SetCamera (newCamera);
174+
Refresh ();
175+
}
176+
166177
const RenderScene& ModelControl::GetRenderScene () const
167178
{
168179
return renderScene;

Sources/VisualScriptCAD/Renderer/ModelControl.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ class ModelControl : public wxGLCanvas
4444
const RenderSettings& GetRenderSettings () const;
4545
void SetRenderSettings (const RenderSettings& newSettings);
4646

47+
const Modeler::Camera& GetCamera () const;
48+
void SetCamera (const Modeler::Camera& newCamera);
49+
4750
const RenderScene& GetRenderScene () const;
4851

4952
private:

Sources/VisualScriptCAD/Renderer/RenderScene.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,11 @@ const Modeler::Camera& RenderScene::GetCamera () const
576576
return camera;
577577
}
578578

579+
void RenderScene::SetCamera (const Modeler::Camera& newCamera)
580+
{
581+
camera = newCamera;
582+
}
583+
579584
RenderModel& RenderScene::GetModel ()
580585
{
581586
return model;

Sources/VisualScriptCAD/Renderer/RenderScene.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ class RenderScene
197197
void Draw (int width, int height, const RenderSettings& settings) const;
198198
void DrawOffscreen (const RenderSettings& settings, RenderPixels& pixels) const;
199199
const Modeler::Camera& GetCamera () const;
200+
void SetCamera (const Modeler::Camera& newCamera);
201+
200202
RenderModel& GetModel ();
201203

202204
void OnMouseMove (MouseButton mouseButton, int diffX, int diffY);

0 commit comments

Comments
 (0)