started changes in generating of vertices for mesh

This commit is contained in:
selim mustafaev 2018-01-29 11:46:08 +03:00
parent fd6adee1e2
commit 69bb6c8177
10 changed files with 76 additions and 65 deletions

2
.gitignore vendored
View File

@ -2,4 +2,4 @@ bin/
lib/
.DS_Store
.idea/
cmake-build-*

View File

@ -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/")

View File

@ -37,6 +37,7 @@ public:
protected:
virtual void generateVertices() const = 0;
virtual void updateVertices() const = 0;
virtual void generateIndices() const = 0;
};

View File

@ -3,7 +3,7 @@
#include <glm/vec3.hpp>
#include <iostream>
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<Vertex[]>(_vertexCount);
@ -14,49 +14,30 @@ Mesh::~Mesh() {
}
/*
VertexArray Mesh::generateVertices() const {
std::size_t u = _size, v = _size;
auto retVal = std::make_unique<Vertex[]>(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<GLuint[]>((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;
indices[indexa*6+3]=indexb;
indices[indexa*6+4]=indexb+u;
indices[indexa*6+5]=indexb+u+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);
}
}
return std::make_tuple(std::move(indices), (u-1)*(v-1)*6*sizeof(GLuint));
}*/
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);
}
}
}
void Mesh::generateIndices() const {
for(std::size_t i = 0; i < _size - 1; ++i)

View File

@ -8,11 +8,14 @@
#include <cstddef>
#include <GL/glew.h>
#include <glm/glm.hpp>
#include <vector>
class Mesh: public GLObject {
protected:
std::size_t _size;
size_t _size;
std::vector<float> _xs;
std::vector<float> _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;
};

View File

@ -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);

View File

@ -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);
};

View File

@ -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<float>(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() {

View File

@ -2,6 +2,7 @@
#define GLTEST_WAVEMESH_H
#include "Mesh.h"
#include <vector>
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;
};

View File

@ -3,6 +3,7 @@
#include "GLObjects/WaveMesh.h"
#include "OGL.h"
#include "Camera.h"
#include "GLObjects/SpectralMesh.h"
#include <iostream>
@ -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);