Skip to content

Commit

Permalink
Merge pull request #295 from ABRG-Models/dev/panning_camera
Browse files Browse the repository at this point in the history
Adds a method to rotate the scene about an axis
  • Loading branch information
sebjameswml authored Dec 2, 2024
2 parents 8e02da3 + c17e25a commit 6a443c6
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
6 changes: 6 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,12 @@ if(USE_GLEW)
target_link_libraries(rod GLEW::GLEW)
endif()

add_executable(rod_pan rod_pan.cpp)
target_link_libraries(rod_pan OpenGL::GL glfw Freetype::Freetype)
if(USE_GLEW)
target_link_libraries(rod_pan GLEW::GLEW)
endif()

# This example uses constexpr code
if(NOT CMAKE_CXX_COMPILER_ID MATCHES Clang)
add_executable(grating grating.cpp)
Expand Down
65 changes: 65 additions & 0 deletions examples/rod_pan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Visualize a Rod
*/
#include <morph/Visual.h>
#include <morph/ColourMap.h>
#include <morph/RodVisual.h>
#include <morph/vec.h>
#include <iostream>
#include <fstream>
#include <cmath>
#include <array>
#include <stdexcept>
#include <string>

int main()
{
int rtn = -1;

// Demonstrates use of offset (left at 0,0,0), lengths (3,2,1) and the 'thickness'
// scaling factor (0.5) for the coordinate arrows
morph::Visual v(1024, 768, "Visualization", {0,0}, {.5,.5,.5}, 1.0f, 0.05f);
v.zNear = 0.001;
v.showCoordArrows = true;
v.coordArrowsInScene = true;
// For a white background:
v.backgroundWhite();
// Switch on a mix of diffuse/ambient lighting
v.lightingEffects(true);

try {
morph::vec<float, 3> offset = { 0.0, 0.0, 0.0 };

morph::vec<float, 3> start = { 0, 0, 0 };
morph::vec<float, 3> end = { 0.25, 0, 0 };

morph::vec<float, 3> colour1 = { 1.0, 0.0, 0.0 };
morph::vec<float, 3> colour2 = { 0.0, 0.9, 0.4 };

std::unique_ptr<morph::VisualModel<>> rvm = std::make_unique<morph::RodVisual<>> (offset, start, end, 0.1f, colour1, colour2);
v.bindmodel (rvm);
rvm->finalize();
v.addVisualModel (rvm);

morph::vec<float, 3> start2 = { -0.1, 0.2, 0.6 };
morph::vec<float, 3> end2 = { 0.2, 0.4, 0.6 };
// You can reuse the unique_ptr rvm, once you've transferred ownership with v.addVisualModel (rvm)
rvm = std::make_unique<morph::RodVisual<>>(offset, start2, end2, 0.05f, colour2);
v.bindmodel (rvm);
rvm->finalize();
v.addVisualModel (rvm);

morph::vec<float> axis = {0,1,0}; // y
while (v.readyToFinish == false) {
v.waitevents (0.001);
v.rotate_scene (axis, morph::mathconst<float>::two_pi / (9*360));
v.render();
}

} catch (const std::exception& e) {
std::cerr << "Caught exception: " << e.what() << std::endl;
rtn = -1;
}

return rtn;
}
8 changes: 8 additions & 0 deletions morph/Visual.h
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,14 @@ namespace morph {
return needs_render;
}

//! Rotate the scene about axis by angle (angle in radians)
void rotate_scene (const morph::vec<float>& axis, const float angle)
{
this->rotationAxis = axis;
morph::quaternion<float> rotnQuat (this->rotationAxis, -angle);
this->rotation.postmultiply (rotnQuat);
}

virtual bool cursor_position_callback (double x, double y)
{
this->cursorpos[0] = static_cast<float>(x);
Expand Down

0 comments on commit 6a443c6

Please sign in to comment.