Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom camera controller #244

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions cpp/modmesh/view/R3DWidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,45 @@ class ROrbitCameraController
using Qt3DExtras::QOrbitCameraController::QOrbitCameraController;
}; /* end class ROrbitCameraController */

class RCustomCameraController
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though this is prototype code, we may use a descriptive name for the class. What name would be informative?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, I don't know, I think it depends on the behavior of the camera? like orbital or FPS

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you like the camera to behave (move)?

: public Qt3DExtras::QAbstractCameraController
{
Q_OBJECT
public:
explicit RCustomCameraController(Qt3DCore::QNode * parent = nullptr)
: QAbstractCameraController(parent)
{
}
~RCustomCameraController() {}

private:
void moveCamera(const QAbstractCameraController::InputState & state, float dt) override
{
Qt3DRender::QCamera * theCamera = camera();

if (theCamera == nullptr)
return;

theCamera->translate(QVector3D(state.txAxisValue * linearSpeed(),
state.tyAxisValue * linearSpeed(),
state.tzAxisValue * linearSpeed()) *
dt);
if (state.leftMouseButtonActive)
{
float theLookSpeed = lookSpeed();
if (state.shiftKeyActive)
{
theLookSpeed *= 0.2f;
}

const QVector3D upVector(0.0f, 1.0f, 0.0f);

theCamera->pan(state.rxAxisValue * theLookSpeed * dt, upVector);
theCamera->tilt(state.ryAxisValue * theLookSpeed * dt);
}
};
}; /* end class RCustomCameraController */

class RScene
: public Qt3DCore::QEntity
{
Expand All @@ -85,6 +124,8 @@ class RScene

void setFirstPersonCameraController() { setCameraController(new RFirstPersonCameraController(this)); }

void setCustomCameraController() { setCameraController(new RCustomCameraController(this)); }

private:

Qt3DExtras::QAbstractCameraController * m_controller = nullptr;
Expand Down
24 changes: 24 additions & 0 deletions cpp/modmesh/view/RManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,39 @@ void RManager::setUpMenu()
}
});

auto * use_custom_camera = new RAction(
QString("Use Custom Camera Controller"),
QString("Use Custom Camera Controller"),
[this]()
{
qDebug() << "Use Custom Camera Controller (menu demo)";
for (auto subwin : m_mdiArea->subWindowList())
{
try
{
R3DWidget * viewer = dynamic_cast<R3DWidget *>(subwin->widget());
viewer->scene()->setCustomCameraController();
viewer->scene()->controller()->setCamera(viewer->camera());
}
catch (std::bad_cast & e)
{
std::cerr << e.what() << std::endl;
}
}
});

auto * cameraGroup = new QActionGroup(m_mainWindow);
cameraGroup->addAction(use_orbit_camera);
cameraGroup->addAction(use_fps_camera);
cameraGroup->addAction(use_custom_camera);
use_orbit_camera->setCheckable(true);
use_fps_camera->setCheckable(true);
use_custom_camera->setCheckable(true);
use_orbit_camera->setChecked(true);

m_cameraMenu->addAction(use_orbit_camera);
m_cameraMenu->addAction(use_fps_camera);
m_cameraMenu->addAction(use_custom_camera);
}

{
Expand Down
Loading