Skip to content

Commit

Permalink
add mousewheel zoom/pitch to navigation. #40
Browse files Browse the repository at this point in the history
* Navigation now receives GLFWScrollEvents.
* pitch with mouse wheel
* zoom with mouse wheel while holding SHIFT
  • Loading branch information
lanice committed Dec 21, 2013
1 parent 41bbd4f commit f7a05b3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ EventHandler * Game::eventHandler()
return & m_eventHandler;
}

Navigation * Game::navigation()
{
return & m_navigation;
}

glowutils::Camera * Game::camera()
{
return & m_camera;
Expand Down
1 change: 1 addition & 0 deletions src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Game{
void end();

EventHandler * eventHandler();
Navigation * navigation();
glowutils::Camera * camera();

protected:
Expand Down
18 changes: 18 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,27 @@ static void keyCallback(GLFWwindow* /*window*/, int key, int scancode, int actio
game->eventHandler()->handleKeyEvent(key, scancode, action, mods);
}

static void scrollCallback(GLFWwindow* /*window*/, double xoffset, double yoffset)
{
// As soon as we have a HUD interface in which we scroll,
// e.g. to choose an element, add a condition here,
// as we don't want the navigation to scroll if we are "navigating" in our HUD.

// if (HUD_active)
// {
// game->hud()->handleScrollEvent(xoffset, yoffset);
// return;
// }

// or something like this...

game->navigation()->handleScrollEvent(xoffset, yoffset);
}

void setCallbacks(GLFWwindow * window)
{
glfwSetKeyCallback(window, keyCallback);
glfwSetScrollCallback(window, scrollCallback);
}


Expand Down
31 changes: 30 additions & 1 deletion src/navigation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,30 @@ void Navigation::setTransformation(const glm::vec3 & eye, const glm::vec3 & cent
apply();
}

void Navigation::handleScrollEvent(const double & /*xoffset*/, const double & yoffset)
{
if (glfwGetKey(&m_window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
{
if (yoffset > 0)
{
if (m_distanceEyeCenter <= 2.) return;
m_distanceEyeCenter -= 0.5;
} else {
m_distanceEyeCenter += 0.5;
}
} else {
glm::vec3 eye = m_camera->eye();
if (yoffset < 0)
{
if (eye.y <= (m_distanceEyeCenter/c_distanceEyeCenterDefault)) return;
pitch(2.);
} else {
if ((eye - m_center).y >= m_distanceEyeCenter - (m_distanceEyeCenter/c_distanceEyeCenterDefault)) return;
pitch(-2.);
}
}
}

void Navigation::update()
{
if (glfwGetWindowAttrib(&m_window, GLFW_FOCUSED))
Expand All @@ -43,7 +67,7 @@ void Navigation::update()
float boost = 1.f;

if (glfwGetKey(&m_window, GLFW_KEY_SPACE) == GLFW_PRESS)
setTransformation(glm::vec3(0, 2, 2), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0));
{ setTransformation(glm::vec3(0, 2, 2), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0)); m_distanceEyeCenter = c_distanceEyeCenterDefault; }
if (glfwGetKey(&m_window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
boost = 5.f;

Expand Down Expand Up @@ -90,6 +114,11 @@ void Navigation::rotate(const float & angle)
m_rotation = glm::angleAxis(angle, glm::vec3(0, 1, 0)) * m_rotation;
}

void Navigation::pitch(const float & angle)
{
m_rotation = glm::angleAxis(angle, m_rotation * glm::vec3(1, 0, 0)) * m_rotation;
}

const glowutils::Camera * Navigation::camera() const
{
return m_camera;
Expand Down
3 changes: 3 additions & 0 deletions src/navigation.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ class Navigation

void setTransformation(const glm::vec3 & eye, const glm::vec3 & center, const glm::vec3 & up);

void handleScrollEvent(const double & xoffset, const double & yoffset);

void update();
void apply();

void move(glm::vec3 & position, const glm::vec3 & direction);
void rotate(const float & degree);
void pitch(const float & degree);

const glowutils::Camera * camera() const;

Expand Down

0 comments on commit f7a05b3

Please sign in to comment.