diff --git a/.gitignore b/.gitignore index 81fe549..b6fcad9 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,4 @@ bin/ lib/ .DS_Store .idea/ - +cmake-build-* diff --git a/CMakeLists.txt b/CMakeLists.txt index ceb305e..9a971b9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.2) project(glTest) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Ofast -g -Wall") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Og -g -Wall") set(CMAKE_BUILD_TYPE DEBUG) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/") diff --git a/src/GLObjects/GLObject.h b/src/GLObjects/GLObject.h index bb6acad..0a63fe2 100644 --- a/src/GLObjects/GLObject.h +++ b/src/GLObjects/GLObject.h @@ -37,6 +37,7 @@ public: protected: virtual void generateVertices() const = 0; + virtual void updateVertices() const = 0; virtual void generateIndices() const = 0; }; diff --git a/src/GLObjects/Mesh.cpp b/src/GLObjects/Mesh.cpp index 3a0fb74..b3b46e8 100644 --- a/src/GLObjects/Mesh.cpp +++ b/src/GLObjects/Mesh.cpp @@ -3,7 +3,7 @@ #include #include -Mesh::Mesh(ShaderProgram* shader, std::size_t size) : GLObject(shader), _size(size) { +Mesh::Mesh(ShaderProgram* shader, std::size_t size) : GLObject(shader), _size(size), _xs(size), _ys(size) { _vertexCount = 2*3*(_size + 1)*(_size + 1); _indexCount = _vertexCount; _vertices = std::make_unique(_vertexCount); @@ -14,49 +14,30 @@ Mesh::~Mesh() { } -/* -VertexArray Mesh::generateVertices() const { - std::size_t u = _size, v = _size; - auto retVal = std::make_unique(u*v); - const float step = (exp(4) - 1)/_size; - - for (std::size_t i = 0; i < u; i++) - for (std::size_t j = 0; j < v; j++) - { -// Vertex vertex; - //float x = (float)i/(float)u - float x = logf(i*step + 1)/4.0f - 0.5f; - float y = (float)j/(float)v - 0.5f; - retVal[j*u+i].coords[0] = x*3; - retVal[j*u+i].coords[1] = y*3; - retVal[j*u+i].coords[2] = heightMapFunc(j, i, x, y); - retVal[j*u+i].normal[0] = 1.0f; - retVal[j*u+i].normal[1] = 1.0f; - retVal[j*u+i].normal[2] = 1.0f; - } - - return std::make_tuple(std::move(retVal), u*v*sizeof(Vertex)); -} -*/ - void Mesh::generateVertices() const { const float step = 1.0f/_size; - const float expStep = (exp(4) - 1)/(_size - 2); +// const float expStep = (exp(4) - 1)/(_size - 2); for(std::size_t j = 0; j < _size; ++j) for(std::size_t i = 0; i <= _size; ++i) { - float x = (float)i/(_size - 0) - 0.5f; + float x = (float)i/_size - 0.5f; float xStep = step; - float y = (float)j/(_size - 0) - 0.5f; + float y = (float)j/_size - 0.5f; //logf(j*expStep + 1)/4.0f - 0.5f; - glm::vec3 p1(x, y, heightMapFunc(i, j, x, y)); - glm::vec3 p2(p1.x + xStep, p1.y, heightMapFunc(i + 1, j, x + xStep, y)); - glm::vec3 p3(p1.x, p1.y + step, heightMapFunc(i, j + 1, x, y + step)); - glm::vec3 p4(p1.x + xStep, p1.y + step, heightMapFunc(i + 1, j + 1, x + xStep, y + step)); + +// float h1 = heightMapFunc(i, j, x, y); +// float h2 = heightMapFunc(i + 1, j, x + xStep, y); +// float h3 = heightMapFunc(i, j + 1, x, y + step); +// float h4 = heightMapFunc(i + 1, j + 1, x + xStep, y + step); + + glm::vec3 p1(x, y, 0.f); + glm::vec3 p2(p1.x + xStep, p1.y, 0.f); + glm::vec3 p3(p1.x, p1.y + step, 0.f); + glm::vec3 p4(p1.x + xStep, p1.y + step, 0.f); glm::vec3 norm1 = glm::cross(p2 - p1, p3 - p1); glm::vec3 norm2 = glm::cross(p3 - p4, p2 - p4); - std::cout << "p1 = [" << p1.x << "," << p1.y << "]" << std::endl; + //std::cout << "p1 = [" << p1.x << "," << p1.y << "]" << std::endl; std::size_t idx = (j*(_size - 1) + i)*2*3; @@ -109,26 +90,31 @@ void Mesh::generateVertices() const { } } -/* -IndexArray Mesh::generateIndices() const { - unsigned u = _size, v = _size; - auto indices = std::make_unique((u-1)*(v-1)*6); - for (unsigned i = 0; i < (u - 1); i++) - for (unsigned j = 0; j < (v - 1); j++) - { - unsigned int indexa=j*(u-1)+i; - unsigned int indexb=j*u+i; - indices[indexa*6+0]=indexb; - indices[indexa*6+1]=indexb+1+u; - indices[indexa*6+2]=indexb+1; +void Mesh::updateVertices() const { + for(std::size_t j = 0; j < _size; ++j) { + for(std::size_t i = 0; i <= _size; ++i) { + std::size_t idx = (j*(_size - 1) + i)*2*3; + _vertices[idx].z = heightMap(i, j); + } + } - indices[indexa*6+3]=indexb; - indices[indexa*6+4]=indexb+u; - indices[indexa*6+5]=indexb+u+1; - } + for(std::size_t j = 0; j < _size; ++j) { + for(std::size_t i = 0; i <= _size; ++i) { + std::size_t idx = (j*(_size - 1) + i)*2*3; + float x = _xs[i], x2 = _xs[i + 1]; + float y = _ys[j], y2 = _ys[j + 1]; + float z = _vertices[idx] + + glm::vec3 p1(x, y, 0.f); + glm::vec3 p2(x2, y, 0.f); + glm::vec3 p3(p1.x, p1.y + step, 0.f); + glm::vec3 p4(p1.x + xStep, p1.y + step, 0.f); + glm::vec3 norm1 = glm::cross(p2 - p1, p3 - p1); + glm::vec3 norm2 = glm::cross(p3 - p4, p2 - p4); + } + } +} - return std::make_tuple(std::move(indices), (u-1)*(v-1)*6*sizeof(GLuint)); -}*/ void Mesh::generateIndices() const { for(std::size_t i = 0; i < _size - 1; ++i) diff --git a/src/GLObjects/Mesh.h b/src/GLObjects/Mesh.h index 0b7f8af..5ae1475 100644 --- a/src/GLObjects/Mesh.h +++ b/src/GLObjects/Mesh.h @@ -8,11 +8,14 @@ #include #include #include +#include class Mesh: public GLObject { protected: - std::size_t _size; + size_t _size; + std::vector _xs; + std::vector _ys; public: Mesh(ShaderProgram* shader, std::size_t size); @@ -20,10 +23,12 @@ public: protected: void generateVertices() const override final; + void updateVertices() const override final; void generateIndices() const override final; protected: - virtual float heightMapFunc(std::size_t nx, std::size_t ny, float fx, float fy) const = 0; + virtual float heightMap(std::size_t nx, std::size_t ny) const = 0; + virtual float heightMap(float fx, float fy) const = 0; }; diff --git a/src/GLObjects/SpectralMesh.cpp b/src/GLObjects/SpectralMesh.cpp index fdb3c8a..e4bbf8a 100644 --- a/src/GLObjects/SpectralMesh.cpp +++ b/src/GLObjects/SpectralMesh.cpp @@ -14,10 +14,14 @@ SpectralMesh::~SpectralMesh() { fftwf_free(_spectrLineComplex); } -float SpectralMesh::heightMapFunc(std::size_t nx, std::size_t ny, float fx, float fy) const { +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); diff --git a/src/GLObjects/SpectralMesh.h b/src/GLObjects/SpectralMesh.h index b6d3bcd..8ade9f6 100644 --- a/src/GLObjects/SpectralMesh.h +++ b/src/GLObjects/SpectralMesh.h @@ -16,7 +16,8 @@ public: void addLine(float* line, std::size_t depth = 0); private: - float heightMapFunc(std::size_t nx, std::size_t ny, float fx, float fy) const override final; + 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); }; diff --git a/src/GLObjects/WaveMesh.cpp b/src/GLObjects/WaveMesh.cpp index 8d5bac4..3b4e127 100644 --- a/src/GLObjects/WaveMesh.cpp +++ b/src/GLObjects/WaveMesh.cpp @@ -1,8 +1,15 @@ #include "WaveMesh.h" -float WaveMesh::heightMapFunc(std::size_t nx, std::size_t ny, float fx, float fy) const { - double d = 50*sqrt(fx*fx + fy*fy); - return static_cast(cos(d - _shift)*exp(-d/10.0f)/2); +float WaveMesh::heightMap(std::size_t nx, std::size_t ny) const { + float x = _xs[nx]; + float y = _xs[ny]; + float d = 50*sqrtf(x*x + y*y); + return cosf(d - _shift)*expf(-d/10.0f)/2; +} + +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) { @@ -11,7 +18,11 @@ void WaveMesh::setShift(float shift) { } WaveMesh::WaveMesh(ShaderProgram *shader, std::size_t size): Mesh(shader, size), _shift(0) { - + float step = 1.0f/size; + for(std::size_t i = 0; i < size; ++i) { + _xs[i] = step*i - 0.5f; + _ys[i] = _xs[i]; + } } WaveMesh::~WaveMesh() { diff --git a/src/GLObjects/WaveMesh.h b/src/GLObjects/WaveMesh.h index fe68c5f..718d799 100644 --- a/src/GLObjects/WaveMesh.h +++ b/src/GLObjects/WaveMesh.h @@ -2,6 +2,7 @@ #define GLTEST_WAVEMESH_H #include "Mesh.h" +#include class WaveMesh: public Mesh { private: @@ -13,7 +14,8 @@ public: void setShift(float shift); private: - float heightMapFunc(std::size_t nx, std::size_t ny, float fx, float fy) const override final; + float heightMap(std::size_t nx, std::size_t ny) const override final; + float heightMap(float fx, float fy) const override final; }; diff --git a/src/main.cpp b/src/main.cpp index 52d936d..935ab59 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,7 @@ #include "GLObjects/WaveMesh.h" #include "OGL.h" #include "Camera.h" +#include "GLObjects/SpectralMesh.h" #include @@ -19,8 +20,8 @@ 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, 4); +// SpectralMesh mesh(&program, 512); + WaveMesh mesh(&program, 256); mesh.create(); OGL::instance()->addObject(&mesh);