Some fixes for spectral mesh

This commit is contained in:
selim mustafaev 2018-04-16 23:03:07 +03:00
parent 4487c9ac04
commit d33e2573a9
8 changed files with 36 additions and 36 deletions

View File

@ -40,7 +40,7 @@ vec3 hsv_to_rgb(float h, float s, float v)
void main () {
vec3 materialColor = hsv_to_rgb(zPos, 1, 1);
float diffuse = clamp(dot(normalize(norm), -normalize(vec3(0,0,1))), 0, 0.6);
float diffuse = clamp(dot(normalize(norm), -normalize(vec3(0,0,1))), 0.2, 0.6);
vec3 color = materialColor*diffuse;
frag_colour = vec4(color, 1.0);

View File

@ -18,7 +18,6 @@ protected:
std::vector<float> _xs;
std::vector<float> _ys;
mutable std::vector<glm::vec3> _normals;
mutable std::array<glm::vec3, 6> _adjacentTriangles;
public:
Mesh(ShaderProgram* shader, std::size_t size);
@ -34,7 +33,6 @@ protected:
protected:
virtual float heightMap(std::size_t nx, std::size_t ny) const = 0;
virtual float heightMap(float fx, float fy) const = 0;
};

View File

@ -6,6 +6,13 @@ SpectralMesh::SpectralMesh(ShaderProgram *shader, std::size_t size): Mesh(shader
_data = new float[size*size];
_spectrLine = new float[2*size];
_spectrLineComplex = (fftwf_complex*)fftwf_malloc((_size + 1)*sizeof(fftwf_complex));
float step = 1.0f/size;
for(std::size_t i = 0; i < size; ++i) {
_xs[i] = log10f(step*i + 1) - 0.5f;
_ys[i] = step*i - 0.5f;
std::cout << "[" << _xs[i] << ", " << _ys[i] << "]" << std::endl;
}
}
SpectralMesh::~SpectralMesh() {
@ -18,10 +25,6 @@ float SpectralMesh::heightMap(std::size_t nx, std::size_t ny) const {
return *(_data + _size*ny + nx);
}
float SpectralMesh::heightMap(float, float) const {
return 0.f;
}
void SpectralMesh::addLine(float *line, std::size_t depth) {
fftwf_plan plan = fftwf_plan_dft_r2c_1d(2*_size, line, _spectrLineComplex, FFTW_ESTIMATE);
fftwf_execute(plan);

View File

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

View File

@ -7,12 +7,6 @@ float WaveMesh::heightMap(std::size_t nx, std::size_t ny) const {
return cosf(d - _shift)*expf(-d/10.0f)/4;
}
[[deprecated]]
float WaveMesh::heightMap(float fx, float fy) const {
float d = 50*sqrtf(fx*fx + fy*fy);
return cosf(d - _shift)*expf(-d/10.0f)/2;
}
void WaveMesh::setShift(float shift) {
_shift = shift;
update();

View File

@ -15,7 +15,6 @@ public:
private:
float heightMap(std::size_t nx, std::size_t ny) const override final;
float heightMap(float fx, float fy) const override final;
};

View File

@ -1,6 +1,7 @@
#include "OGL.h"
#include "Camera.h"
#include "utils.h"
#include "GLObjects/WaveMesh.h"
#include <iostream>
#include <stdexcept>
@ -149,7 +150,13 @@ void OGL::run() {
updateFpsCounter();
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// double time = glfwGetTime();
// float shift = (float)fmod(time, 2*3.1415926);
for(IGLObject* obj: _glObjects) {
// WaveMesh* mesh = dynamic_cast<WaveMesh*>(obj);
// mesh->setShift(shift*2);
//obj->scale(glm::vec3(1.0f, 1.0f, 1.0f));
obj->update();
obj->draw();

View File

@ -20,32 +20,32 @@ int main(int argc, char** argv) {
ShaderProgram program("../shaders/vertex.glsl", "../shaders/fragment.glsl");
OGL::instance()->setCurShaderProgram(&program);
// SpectralMesh mesh(&program, 512);
WaveMesh mesh(&program, 128);
SpectralMesh mesh(&program, 512);
// WaveMesh mesh(&program, 128);
mesh.create();
OGL::instance()->addObject(&mesh);
Camera::instance()->init();
// AudioPlayer player(musicFile);
// float* line = new float[2048];
// memset(line, 0, 2048);
// player.setStreamListener([&mesh, line](float *data, std::size_t framesCount, std::size_t depth, std::size_t channels) {
// memmove(line, line + 1024, 1024);
// if(channels > 1) { // if we have more than one channel, use the first
// for(std::size_t i = 0, j = 0; i < framesCount; i += channels, ++j) {
// line[1024 + j] = data[i];
// }
// } else {
// memcpy(line + 1024, data, 1024);
// }
//
// mesh.addLine(line, depth);
// mesh.addLine(line + 256, depth);
// mesh.addLine(line + 512, depth);
// mesh.addLine(line + 768, depth);
// });
// player.play();
AudioPlayer player(musicFile);
float* line = new float[2048];
memset(line, 0, 2048);
player.setStreamListener([&mesh, line](float *data, std::size_t framesCount, std::size_t depth, std::size_t channels) {
memmove(line, line + 1024, 1024);
if(channels > 1) { // if we have more than one channel, use the first
for(std::size_t i = 0, j = 0; i < framesCount; i += channels, ++j) {
line[1024 + j] = data[i];
}
} else {
memcpy(line + 1024, data, 1024);
}
mesh.addLine(line, depth);
mesh.addLine(line + 256, depth);
mesh.addLine(line + 512, depth);
mesh.addLine(line + 768, depth);
});
player.play();
OGL::instance()->run();
// delete[] line;