basic mouse support

This commit is contained in:
Selim Mustafaev 2015-08-04 21:29:20 +03:00
parent 92e0d3a0de
commit 4d0cb4cefe
5 changed files with 41 additions and 3 deletions

View File

@ -9,7 +9,7 @@ Camera::Camera() {
_at = glm::vec3(0.0f, 0.0f, 0.0f);
_up = glm::vec3(0.0f, 1.0f, 0.0f);
_view = glm::lookAt(_eye, _at, _up);
_projection = glm::perspective(45.0f, 1.0f, 2.0f, -2.0f);
_projection = glm::perspective(45.0f, 1.0f, 1.0f, 7.0f);
}
void Camera::init() {
@ -88,3 +88,12 @@ void Camera::moveForward(float distance) {
void Camera::moveBackward(float distance) {
}
void Camera::mouseMoveAroundCenter(float dx, float dy) {
glm::quat q = glm::angleAxis(-dx, _up);
glm::vec3 axis = glm::normalize(glm::cross(_up, _eye));
glm::quat q2 = glm::angleAxis(-dy, axis);
_eye = q*q2*_eye;
_up = glm::normalize(glm::cross(_eye, axis));
updateView();
}

View File

@ -28,6 +28,7 @@ public:
void rotateCCW(float angle);
void moveToCenter(float distance);
void moveFromCenter(float distance);
void mouseMoveAroundCenter(float dx, float dy);
void moveForward(float distance);
void moveBackward(float distance);

View File

@ -1,6 +1,6 @@
#include "SpectralMesh.h"
#include <iostream>
#include <cfloat>
#include <cstring>
SpectralMesh::SpectralMesh(ShaderProgram *shader, std::size_t size): Mesh(shader, size) {
_data = new float[size*size];

View File

@ -5,6 +5,8 @@
#include <stdexcept>
bool wiredFlag = false;
double lastX = 0.0, lastY = 0.0;
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
const float turnStep = 0.1f; // in radians
@ -45,6 +47,30 @@ void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods
}
}
void cursorPositionCallback(GLFWwindow* window, double xpos, double ypos) {
if(lastX != 0 || lastY != 0) {
float dx = (float)(xpos - lastX);
float dy = (float)(ypos - lastY);
static const float coeff = 0.01f;
if(glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS) {
Camera::instance()->mouseMoveAroundCenter(coeff*dx, coeff*dy);
}
}
lastX = xpos;
lastY = ypos;
}
void scrollCallback(GLFWwindow* window, double xoffset, double yoffset) {
if(yoffset > 0) {
Camera::instance()->moveToCenter(0.1f);
} else {
Camera::instance()->moveFromCenter(0.1f);
}
}
OGL::OGL() {
}
@ -86,6 +112,8 @@ void OGL::init() {
_window = glfwCreateWindow(1024, 1024, "OpenGL", nullptr, nullptr); // Windowed
glfwMakeContextCurrent(_window);
glfwSetKeyCallback(_window, keyCallback);
glfwSetCursorPosCallback(_window, cursorPositionCallback);
glfwSetScrollCallback(_window, scrollCallback);
glewExperimental = GL_TRUE;
GLenum err = glewInit();

View File

@ -19,7 +19,7 @@ int main() {
Camera::instance()->init();
AudioPlayer player("/Users/selim/Dropbox/euphoria.wav" /*"/home/selim/dl/euphoria.wav"*/);
AudioPlayer player(/*"/Users/selim/Dropbox/euphoria.wav"*/ "/home/selim/dl/euphoria.wav");
player.setStreamListener([&mesh](float *data, std::size_t framesCount) {
mesh.addLine(data);
});