add camera movements (on sphere around zero point)
This commit is contained in:
parent
d8c8365690
commit
92e0d3a0de
@ -88,7 +88,7 @@ int AudioPlayer::streamCallback(const void* input,
|
|||||||
_sndSource->readData(framesPerBuffer, output);
|
_sndSource->readData(framesPerBuffer, output);
|
||||||
|
|
||||||
if(_streamListener) {
|
if(_streamListener) {
|
||||||
_streamListener(output, framesPerBuffer);
|
_streamListener((float*)output, framesPerBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
return paContinue;
|
return paContinue;
|
||||||
@ -110,7 +110,7 @@ void AudioPlayer::stop() {
|
|||||||
PA_CHECK_ERROR(err);
|
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;
|
_streamListener = callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -10,7 +10,7 @@ class AudioPlayer {
|
|||||||
private:
|
private:
|
||||||
ISndSource* _sndSource;
|
ISndSource* _sndSource;
|
||||||
PaStream* _stream;
|
PaStream* _stream;
|
||||||
std::function<void(void*,std::size_t)> _streamListener;
|
std::function<void(float*,std::size_t)> _streamListener;
|
||||||
std::function<void()> _streamFinishedLitener;
|
std::function<void()> _streamFinishedLitener;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@ -18,7 +18,7 @@ public:
|
|||||||
~AudioPlayer();
|
~AudioPlayer();
|
||||||
void play();
|
void play();
|
||||||
void stop();
|
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);
|
void setStreamFinishedListener(std::function<void()> callback);
|
||||||
int streamCallback(const void* input,
|
int streamCallback(const void* input,
|
||||||
void* output,
|
void* output,
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
#include "Camera.h"
|
#include "Camera.h"
|
||||||
#include "OGL.h"
|
#include "OGL.h"
|
||||||
#include <glm/gtx/rotate_vector.hpp>
|
#include <glm/gtx/rotate_vector.hpp>
|
||||||
|
#include <glm/gtx/quaternion.hpp>
|
||||||
|
|
||||||
Camera::Camera() {
|
Camera::Camera() {
|
||||||
_viewDistance = 4.0f;
|
_viewDistance = 4.0f;
|
||||||
@ -8,7 +9,7 @@ Camera::Camera() {
|
|||||||
_at = glm::vec3(0.0f, 0.0f, 0.0f);
|
_at = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||||
_up = glm::vec3(0.0f, 1.0f, 0.0f);
|
_up = glm::vec3(0.0f, 1.0f, 0.0f);
|
||||||
_view = glm::lookAt(_eye, _at, _up);
|
_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() {
|
void Camera::init() {
|
||||||
@ -32,25 +33,58 @@ void Camera::updateView() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Camera::turnLeft(float angle) {
|
void Camera::turnLeft(float angle) {
|
||||||
_up = glm::rotateY(_up, angle);
|
glm::quat q = glm::angleAxis(angle, _up);
|
||||||
_eye = glm::rotateY(_eye, angle);
|
_eye = glm::rotate(q, _eye);
|
||||||
updateView();
|
updateView();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::turnRight(float angle) {
|
void Camera::turnRight(float angle) {
|
||||||
_up = glm::rotateY(_up, -angle);
|
glm::quat q = glm::angleAxis(-angle, _up);
|
||||||
_eye = glm::rotateY(_eye, -angle);
|
_eye = glm::rotate(q, _eye);
|
||||||
updateView();
|
updateView();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::turnUp(float angle) {
|
void Camera::turnUp(float angle) {
|
||||||
_up = glm::rotateX(_up, angle);
|
glm::vec3 axis = glm::normalize(glm::cross(_up, _eye));
|
||||||
_eye = glm::rotateX(_eye, angle);
|
glm::quat q = glm::angleAxis(angle, axis);
|
||||||
|
_eye = glm::rotate(q, _eye);
|
||||||
|
_up = glm::normalize(glm::cross(_eye, axis));
|
||||||
updateView();
|
updateView();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::turnDown(float angle) {
|
void Camera::turnDown(float angle) {
|
||||||
_up = glm::rotateX(_up, -angle);
|
glm::vec3 axis = glm::normalize(glm::cross(_up, _eye));
|
||||||
_eye = glm::rotateX(_eye, -angle);
|
glm::quat q = glm::angleAxis(-angle, axis);
|
||||||
|
_eye = glm::rotate(q, _eye);
|
||||||
|
_up = glm::normalize(glm::cross(_eye, axis));
|
||||||
updateView();
|
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) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@ -24,6 +24,13 @@ public:
|
|||||||
void turnRight(float angle);
|
void turnRight(float angle);
|
||||||
void turnUp(float angle);
|
void turnUp(float angle);
|
||||||
void turnDown(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);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
#include "SpectralMesh.h"
|
#include "SpectralMesh.h"
|
||||||
#include <string.h>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <cmath>
|
#include <cfloat>
|
||||||
|
|
||||||
SpectralMesh::SpectralMesh(ShaderProgram *shader, std::size_t size): Mesh(shader, size) {
|
SpectralMesh::SpectralMesh(ShaderProgram *shader, std::size_t size): Mesh(shader, size) {
|
||||||
_data = new float[size*size];
|
_data = new float[size*size];
|
||||||
@ -30,7 +29,7 @@ void SpectralMesh::addLine(float *line) {
|
|||||||
|
|
||||||
for(std::size_t i = 0; i < _size; ++i) {
|
for(std::size_t i = 0; i < _size; ++i) {
|
||||||
float real = _spectrLineComplex[i][0], imag = _spectrLineComplex[i][1];
|
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) {
|
if(_spectrLine[i] < 0) {
|
||||||
_spectrLine[i] = 0;
|
_spectrLine[i] = 0;
|
||||||
}
|
}
|
||||||
@ -41,3 +40,22 @@ void SpectralMesh::addLine(float *line) {
|
|||||||
|
|
||||||
fftwf_destroy_plan(plan);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -18,6 +18,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
float heightMapFunc(std::size_t nx, std::size_t ny, float fx, float fy) const override final;
|
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);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
17
src/OGL.cpp
17
src/OGL.cpp
@ -8,6 +8,7 @@ bool wiredFlag = false;
|
|||||||
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
|
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
|
||||||
{
|
{
|
||||||
const float turnStep = 0.1f; // in radians
|
const float turnStep = 0.1f; // in radians
|
||||||
|
const float moveStep = 0.1f;
|
||||||
|
|
||||||
if (key == GLFW_KEY_M && action == GLFW_PRESS) {
|
if (key == GLFW_KEY_M && action == GLFW_PRESS) {
|
||||||
glPolygonMode(GL_FRONT_AND_BACK, wiredFlag ? GL_FILL : GL_LINE);
|
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:
|
case GLFW_KEY_DOWN:
|
||||||
Camera::instance()->turnDown(turnStep);
|
Camera::instance()->turnDown(turnStep);
|
||||||
break;
|
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);
|
std::string errMsg = std::string("GLEW init error: ") + (char*)glewGetErrorString(err);
|
||||||
throw new std::runtime_error(errMsg);
|
throw new std::runtime_error(errMsg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
glClearDepth(1.0);
|
||||||
|
glDepthFunc(GL_LEQUAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OGL::run() {
|
void OGL::run() {
|
||||||
|
|||||||
17
src/main.cpp
17
src/main.cpp
@ -1,15 +1,13 @@
|
|||||||
#include "Shaders/ShaderProgram.h"
|
#include "Shaders/ShaderProgram.h"
|
||||||
#include "GLObjects/WaveMesh.h"
|
|
||||||
#include "Audio/AudioPlayer.h"
|
#include "Audio/AudioPlayer.h"
|
||||||
#include "GLObjects/SpectralMesh.h"
|
#include "GLObjects/SpectralMesh.h"
|
||||||
#include "OGL.h"
|
#include "OGL.h"
|
||||||
#include "Camera.h"
|
#include "Camera.h"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
try {
|
||||||
OGL::instance()->init();
|
OGL::instance()->init();
|
||||||
|
|
||||||
ShaderProgram program("../shaders/vertex.glsl", "../shaders/fragment.glsl");
|
ShaderProgram program("../shaders/vertex.glsl", "../shaders/fragment.glsl");
|
||||||
@ -21,15 +19,14 @@ int main() {
|
|||||||
|
|
||||||
Camera::instance()->init();
|
Camera::instance()->init();
|
||||||
|
|
||||||
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
AudioPlayer player("/Users/selim/Dropbox/euphoria.wav" /*"/home/selim/dl/euphoria.wav"*/);
|
||||||
glDepthFunc(GL_LEQUAL);
|
player.setStreamListener([&mesh](float *data, std::size_t framesCount) {
|
||||||
glEnable(GL_DEPTH_TEST);
|
mesh.addLine(data);
|
||||||
|
|
||||||
AudioPlayer player("/home/selim/dl/euphoria.wav");
|
|
||||||
player.setStreamListener([&mesh](void* data, std::size_t framesCount){
|
|
||||||
mesh.addLine((float*)data);
|
|
||||||
});
|
});
|
||||||
player.play();
|
player.play();
|
||||||
|
|
||||||
OGL::instance()->run();
|
OGL::instance()->run();
|
||||||
|
} catch (std::exception& ex) {
|
||||||
|
std::cout << "exception: " << ex.what() << std::endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user