working on WaveMesh...

This commit is contained in:
selim 2018-01-30 19:47:36 +03:00
parent de87219bae
commit 9b65b32b55
3 changed files with 42 additions and 34 deletions

View File

@ -25,7 +25,7 @@ void GLObject::create() {
glBindBuffer(GL_ARRAY_BUFFER, _vertexVbo);
glBufferData(GL_ARRAY_BUFFER, _vertexCount*sizeof(Vertex), nullptr, GL_STREAM_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexVbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, _indexCount*sizeof(GLuint), _indices.get(), GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, _indexCount*sizeof(GLuint), _indices.get(), GL_STREAM_DRAW);
_trianglesCount = _indexCount*sizeof(GLuint)/sizeof(GLuint);

View File

@ -4,8 +4,8 @@
#include <iostream>
Mesh::Mesh(ShaderProgram* shader, std::size_t size) : GLObject(shader), _size(size), _xs(size), _ys(size), _normals(2*size*size) {
_vertexCount = (_size + 1)*(_size + 1);
_indexCount = _vertexCount;
_vertexCount = _size*_size;
_indexCount = (_size - 1)*(_size - 1)*6;
_vertices = std::make_unique<Vertex[]>(_vertexCount);
_indices = std::make_unique<GLuint[]>(_indexCount);
}
@ -16,8 +16,8 @@ Mesh::~Mesh() {
void Mesh::generateVertices() const {
for(std::size_t j = 0; j < _size; ++j) {
for (std::size_t i = 0; i <= _size; ++i) {
size_t idx = j*(_size - 1) + i;
for (std::size_t i = 0; i < _size; ++i) {
size_t idx = j*_size + i;
_vertices[idx].x = (float)i/_size - 0.5f;
_vertices[idx].y = (float)j/_size - 0.5f;
_vertices[idx].z = 0.f;
@ -28,45 +28,50 @@ void Mesh::generateVertices() const {
}
}
inline size_t Mesh::normalIndex(size_t x, size_t y) {
return 2*(x + y*_size);
}
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;
for(std::size_t i = 0; i < _size; ++i) {
std::size_t idx = j*_size + i;
_vertices[idx].z = heightMap(i, j);
}
}
// 1. Calc normals for all triangles
// _normals.clear();
// for(std::size_t j = 0; j < _size; ++j) {
// for(std::size_t i = 0; i <= _size; ++i) {
// float x = _xs[i], x2 = _xs[i + 1];
// float y = _ys[j], y2 = _ys[j + 1];
//
// glm::vec3 p1(x, y, _vertices[j*(_size - 1) + i].z);
// glm::vec3 p2(x2, y, _vertices[j*(_size - 1) + i + 1].z);
// glm::vec3 p3(x, y2, _vertices[(j + 1)*(_size - 1) + i].z);
// glm::vec3 p4(x2, y2, _vertices[(j + 1)*(_size - 1) + i + 1].z);
//
// glm::vec3 v1 = p1 - p3;
// glm::vec3 v2 = p2 - p3;
// glm::vec3 v3 = p4 - p3;
//
// glm::vec3 norm1 = glm::cross(v2, v1);
// glm::vec3 norm2 = glm::cross(v3, v2);
//
// _normals.emplace_back(norm1);
// _normals.emplace_back(norm2);
// }
// }
_normals.clear();
for(std::size_t j = 0; j < _size; ++j) {
for(std::size_t i = 0; i < _size; ++i) {
float x = _xs[i], x2 = _xs[i + 1];
float y = _ys[j], y2 = _ys[j + 1];
glm::vec3 p1(x, y, _vertices[j*(_size - 1) + i].z);
glm::vec3 p2(x2, y, _vertices[j*(_size - 1) + i + 1].z);
glm::vec3 p3(x, y2, _vertices[(j + 1)*(_size - 1) + i].z);
glm::vec3 p4(x2, y2, _vertices[(j + 1)*(_size - 1) + i + 1].z);
glm::vec3 v1 = p1 - p3;
glm::vec3 v2 = p2 - p3;
glm::vec3 v3 = p4 - p3;
glm::vec3 norm1 = glm::cross(v2, v1);
glm::vec3 norm2 = glm::cross(v3, v2);
_normals.emplace_back(norm1);
_normals.emplace_back(norm2);
}
}
// 2. Calc normals for all vertices based on precalculated normals for triangles
// Normal of vertex is an average of normals of adjacent triangles
// for(std::size_t j = 0; j < _size; ++j) {
// for (std::size_t i = 0; i <= _size; ++i) {
//
// }
// }
for(std::size_t j = 0; j < _size; ++j) {
for (std::size_t i = 0; i < _size; ++i) {
size_t idx1 = normalIndex(i - 1, j - 1);
size_t idx2 = normalIndex(i - 1, j);
}
}
}
void Mesh::generateIndices() const {

View File

@ -24,6 +24,9 @@ public:
Mesh(ShaderProgram* shader, std::size_t size);
virtual ~Mesh();
private:
size_t normalIndex(size_t x, size_t y);
protected:
void generateVertices() const override final;
void updateVertices() const override final;