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

Updated vsgcameras example to use mouse buttons 4/5 #249

Merged
merged 2 commits into from
Aug 21, 2023
Merged
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
73 changes: 52 additions & 21 deletions examples/app/vsgcameras/vsgcameras.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,29 @@ class CameraSelector : public vsg::Inherit<vsg::Visitor, CameraSelector>
vsg::ref_ptr<vsg::Camera> main_camera;
vsg::ref_ptr<vsg::LookAt> original_viewMatrix;
Cameras cameras;
int currentCameraIndex = 0;

void apply(vsg::ButtonPressEvent& buttonPress) override
{
if (cameras.empty()) return;

if (buttonPress.button == 4)
{
// next camera
currentCameraIndex++;
currentCameraIndex %= cameras.size();

selectCameraByIndex(currentCameraIndex);
}
else if (buttonPress.button == 5)
{
// previous camera
currentCameraIndex--;
currentCameraIndex %= cameras.size();

selectCameraByIndex(currentCameraIndex);
}
}

void apply(vsg::KeyPressEvent& keyPress) override
{
Expand All @@ -40,31 +63,39 @@ class CameraSelector : public vsg::Inherit<vsg::Visitor, CameraSelector>
}
else if ((keyPress.keyBase >= '1') && (keyPress.keyBase <= '9'))
{
uint16_t keyForCamera{'1'};
for(auto& [nodePath, camera] : cameras)
currentCameraIndex = keyPress.keyBase - '1';

selectCameraByIndex(currentCameraIndex);
}
}

private:
void selectCameraByIndex(int index)
{
int cameraIndex = 0;
for(auto& [nodePath, camera] : cameras)
{
if (cameraIndex == index)
{
if (keyForCamera == keyPress.keyBase)
auto begin = nodePath.begin();
auto end = nodePath.end();
if (begin != end) --end;

// auto matrix = vsg::computeTransform(nodePath);
auto matrix = vsg::visit<vsg::ComputeTransform>(begin, end).matrix;

std::cout<<"Matched: "<<camera->name<<" "<<matrix<<std::endl;

auto selected_lookAt = camera->viewMatrix.cast<vsg::LookAt>();
auto main_lookAt = main_camera->viewMatrix.cast<vsg::LookAt>();
if (main_lookAt)
{
auto begin = nodePath.begin();
auto end = nodePath.end();
if (begin != end) --end;

// auto matrix = vsg::computeTransform(nodePath);
auto matrix = vsg::visit<vsg::ComputeTransform>(begin, end).matrix;

std::cout<<"Matched: "<<camera->name<<" "<<matrix<<std::endl;

auto selected_lookAt = camera->viewMatrix.cast<vsg::LookAt>();
auto main_lookAt = main_camera->viewMatrix.cast<vsg::LookAt>();
if (main_lookAt)
{
*main_lookAt = *selected_lookAt;
main_lookAt->transform(matrix);
}
break;
*main_lookAt = *selected_lookAt;
main_lookAt->transform(matrix);
}
++keyForCamera;
break;
}
++cameraIndex;
}
}
};
Expand Down