add camera movements (on sphere around zero point)

This commit is contained in:
selim mustafaev 2015-08-02 20:32:14 +03:00
parent d8c8365690
commit 92e0d3a0de
8 changed files with 110 additions and 36 deletions

View File

@ -88,7 +88,7 @@ int AudioPlayer::streamCallback(const void* input,
_sndSource->readData(framesPerBuffer, output);
if(_streamListener) {
_streamListener(output, framesPerBuffer);
_streamListener((float*)output, framesPerBuffer);
}
return paContinue;
@ -110,7 +110,7 @@ void AudioPlayer::stop() {
PA_CHECK_ERROR(err);
}
void AudioPlayer::setStreamListener(std::function<void(void*,std::size_t)> callback) {
void AudioPlayer::setStreamListener(std::function<void(float*,std::size_t)> callback) {
_streamListener = callback;
}

View File

@ -10,7 +10,7 @@ class AudioPlayer {
private:
ISndSource* _sndSource;
PaStream* _stream;
std::function<void(void*,std::size_t)> _streamListener;
std::function<void(float*,std::size_t)> _streamListener;
std::function<void()> _streamFinishedLitener;
public:
@ -18,7 +18,7 @@ public:
~AudioPlayer();
void play();
void stop();
void setStreamListener(std::function<void(void*,std::size_t)> callback);
void setStreamListener(std::function<void(float*,std::size_t)> callback);
void setStreamFinishedListener(std::function<void()> callback);
int streamCallback(const void* input,
void* output,

View File

@ -1,6 +1,7 @@
#include "Camera.h"
#include "OGL.h"
#include <glm/gtx/rotate_vector.hpp>
#include <glm/gtx/quaternion.hpp>
Camera::Camera() {
_viewDistance = 4.0f;
@ -8,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, 2.0f, -2.0f);
}
void Camera::init() {
@ -32,25 +33,58 @@ void Camera::updateView() {
}
void Camera::turnLeft(float angle) {
_up = glm::rotateY(_up, angle);
_eye = glm::rotateY(_eye, angle);
glm::quat q = glm::angleAxis(angle, _up);
_eye = glm::rotate(q, _eye);
updateView();
}
void Camera::turnRight(float angle) {
_up = glm::rotateY(_up, -angle);
_eye = glm::rotateY(_eye, -angle);
glm::quat q = glm::angleAxis(-angle, _up);
_eye = glm::rotate(q, _eye);
updateView();
}
void Camera::turnUp(float angle) {
_up = glm::rotateX(_up, angle);
_eye = glm::rotateX(_eye, angle);
glm::vec3 axis = glm::normalize(glm::cross(_up, _eye));
glm::quat q = glm::angleAxis(angle, axis);
_eye = glm::rotate(q, _eye);
_up = glm::normalize(glm::cross(_eye, axis));
updateView();
}
void Camera::turnDown(float angle) {
_up = glm::rotateX(_up, -angle);
_eye = glm::rotateX(_eye, -angle);
glm::vec3 axis = glm::normalize(glm::cross(_up, _eye));
glm::quat q = glm::angleAxis(-angle, axis);
_eye = glm::rotate(q, _eye);
_up = glm::normalize(glm::cross(_eye, axis));
updateView();
}
void Camera::rotateCW(float angle) {
glm::quat q = glm::angleAxis(-angle, glm::normalize(_eye));
_up = glm::rotate(q, _up);
updateView();
}
void Camera::rotateCCW(float angle) {
glm::quat q = glm::angleAxis(angle, glm::normalize(_eye));
_up = glm::rotate(q, _up);
updateView();
}
void Camera::moveToCenter(float distance) {
_eye -= distance*glm::normalize(_eye);
updateView();
}
void Camera::moveFromCenter(float distance) {
_eye += distance*glm::normalize(_eye);
updateView();
}
void Camera::moveForward(float distance) {
}
void Camera::moveBackward(float distance) {
}

View File

@ -24,6 +24,13 @@ public:
void turnRight(float angle);
void turnUp(float angle);
void turnDown(float angle);
void rotateCW(float angle);
void rotateCCW(float angle);
void moveToCenter(float distance);
void moveFromCenter(float distance);
void moveForward(float distance);
void moveBackward(float distance);
};

View File

@ -1,7 +1,6 @@
#include "SpectralMesh.h"
#include <string.h>
#include <iostream>
#include <cmath>
#include <cfloat>
SpectralMesh::SpectralMesh(ShaderProgram *shader, std::size_t size): Mesh(shader, size) {
_data = new float[size*size];
@ -30,7 +29,7 @@ void SpectralMesh::addLine(float *line) {
for(std::size_t i = 0; i < _size; ++i) {
float real = _spectrLineComplex[i][0], imag = _spectrLineComplex[i][1];
_spectrLine[i] = 0.3*log10f(sqrtf(real*real + imag*imag)) + 0.1f;
_spectrLine[i] = 0.3f*log10f(sqrtf(real*real + imag*imag)) + 0.1f;
if(_spectrLine[i] < 0) {
_spectrLine[i] = 0;
}
@ -41,3 +40,22 @@ void SpectralMesh::addLine(float *line) {
fftwf_destroy_plan(plan);
}
void SpectralMesh::normalizeArray(float *array, std::size_t size) {
float min = FLT_MAX, max = 0.0f;
// calc min and max elements
for(std::size_t i = 0; i < size; ++i) {
float cur = array[i];
if(cur < min)
min = cur;
if(cur > max)
max = cur;
}
// actual normalization
for(std::size_t i = 0; i < size; ++i) {
array[i] = (array[i] - min)/(max - min);
}
}

View File

@ -18,6 +18,7 @@ public:
private:
float heightMapFunc(std::size_t nx, std::size_t ny, float fx, float fy) const override final;
void normalizeArray(float* array, std::size_t size);
};

View File

@ -8,6 +8,7 @@ bool wiredFlag = false;
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
const float turnStep = 0.1f; // in radians
const float moveStep = 0.1f;
if (key == GLFW_KEY_M && action == GLFW_PRESS) {
glPolygonMode(GL_FRONT_AND_BACK, wiredFlag ? GL_FILL : GL_LINE);
@ -28,6 +29,18 @@ void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods
case GLFW_KEY_DOWN:
Camera::instance()->turnDown(turnStep);
break;
case GLFW_KEY_PAGE_UP:
Camera::instance()->rotateCW(turnStep);
break;
case GLFW_KEY_PAGE_DOWN:
Camera::instance()->rotateCCW(turnStep);
break;
case GLFW_KEY_W:
Camera::instance()->moveToCenter(moveStep);
break;
case GLFW_KEY_S:
Camera::instance()->moveFromCenter(moveStep);
break;
}
}
}
@ -80,6 +93,10 @@ void OGL::init() {
std::string errMsg = std::string("GLEW init error: ") + (char*)glewGetErrorString(err);
throw new std::runtime_error(errMsg);
}
glEnable(GL_DEPTH_TEST);
glClearDepth(1.0);
glDepthFunc(GL_LEQUAL);
}
void OGL::run() {

View File

@ -1,35 +1,32 @@
#include "Shaders/ShaderProgram.h"
#include "GLObjects/WaveMesh.h"
#include "Audio/AudioPlayer.h"
#include "GLObjects/SpectralMesh.h"
#include "OGL.h"
#include "Camera.h"
#include <iostream>
#include <fstream>
#include <sstream>
int main() {
OGL::instance()->init();
try {
OGL::instance()->init();
ShaderProgram program("../shaders/vertex.glsl", "../shaders/fragment.glsl");
OGL::instance()->setCurShaderProgram(&program);
ShaderProgram program("../shaders/vertex.glsl", "../shaders/fragment.glsl");
OGL::instance()->setCurShaderProgram(&program);
SpectralMesh mesh(&program, 256);
mesh.create();
OGL::instance()->addObject(&mesh);
SpectralMesh mesh(&program, 256);
mesh.create();
OGL::instance()->addObject(&mesh);
Camera::instance()->init();
Camera::instance()->init();
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
AudioPlayer player("/Users/selim/Dropbox/euphoria.wav" /*"/home/selim/dl/euphoria.wav"*/);
player.setStreamListener([&mesh](float *data, std::size_t framesCount) {
mesh.addLine(data);
});
player.play();
AudioPlayer player("/home/selim/dl/euphoria.wav");
player.setStreamListener([&mesh](void* data, std::size_t framesCount){
mesh.addLine((float*)data);
});
player.play();
OGL::instance()->run();
OGL::instance()->run();
} catch (std::exception& ex) {
std::cout << "exception: " << ex.what() << std::endl;
}
}