working with mesh...

This commit is contained in:
Selim Mustafaev 2018-01-29 20:23:28 +03:00
parent 69bb6c8177
commit de87219bae
3 changed files with 62 additions and 101 deletions

View File

@ -72,7 +72,7 @@ void GLObject::draw() {
} }
void GLObject::update() { void GLObject::update() {
generateVertices(); updateVertices();
glBindBuffer(GL_ARRAY_BUFFER, _vertexVbo); glBindBuffer(GL_ARRAY_BUFFER, _vertexVbo);
//glBufferData(GL_ARRAY_BUFFER, _vertexCount*sizeof(Vertex), _vertices.get(), GL_STATIC_DRAW); //glBufferData(GL_ARRAY_BUFFER, _vertexCount*sizeof(Vertex), _vertices.get(), GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, _vertexCount*sizeof(Vertex), _vertices.get()); glBufferSubData(GL_ARRAY_BUFFER, 0, _vertexCount*sizeof(Vertex), _vertices.get());

View File

@ -3,8 +3,8 @@
#include <glm/vec3.hpp> #include <glm/vec3.hpp>
#include <iostream> #include <iostream>
Mesh::Mesh(ShaderProgram* shader, std::size_t size) : GLObject(shader), _size(size), _xs(size), _ys(size) { Mesh::Mesh(ShaderProgram* shader, std::size_t size) : GLObject(shader), _size(size), _xs(size), _ys(size), _normals(2*size*size) {
_vertexCount = 2*3*(_size + 1)*(_size + 1); _vertexCount = (_size + 1)*(_size + 1);
_indexCount = _vertexCount; _indexCount = _vertexCount;
_vertices = std::make_unique<Vertex[]>(_vertexCount); _vertices = std::make_unique<Vertex[]>(_vertexCount);
_indices = std::make_unique<GLuint[]>(_indexCount); _indices = std::make_unique<GLuint[]>(_indexCount);
@ -15,116 +15,74 @@ Mesh::~Mesh() {
} }
void Mesh::generateVertices() const { void Mesh::generateVertices() const {
const float step = 1.0f/_size; for(std::size_t j = 0; j < _size; ++j) {
// const float expStep = (exp(4) - 1)/(_size - 2); for (std::size_t i = 0; i <= _size; ++i) {
size_t idx = j*(_size - 1) + i;
for(std::size_t j = 0; j < _size; ++j) _vertices[idx].x = (float)i/_size - 0.5f;
for(std::size_t i = 0; i <= _size; ++i) { _vertices[idx].y = (float)j/_size - 0.5f;
float x = (float)i/_size - 0.5f; _vertices[idx].z = 0.f;
float xStep = step; _vertices[idx].nx = 0.f;
float y = (float)j/_size - 0.5f; _vertices[idx].ny = 0.f;
//logf(j*expStep + 1)/4.0f - 0.5f; _vertices[idx].nz = 0.f;
}
// 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::size_t idx = (j*(_size - 1) + i)*2*3;
_vertices[idx].x = p1.x;
_vertices[idx].y = p1.y;
_vertices[idx].z = p1.z;
_vertices[idx].nx = norm1.x;
_vertices[idx].ny = norm1.y;
_vertices[idx].nz = norm1.z;
idx += 1;
_vertices[idx].x = p2.x;
_vertices[idx].y = p2.y;
_vertices[idx].z = p2.z;
_vertices[idx].nx = norm1.x;
_vertices[idx].ny = norm1.y;
_vertices[idx].nz = norm1.z;
idx += 1;
_vertices[idx].x = p3.x;
_vertices[idx].y = p3.y;
_vertices[idx].z = p3.z;
_vertices[idx].nx = norm1.x;
_vertices[idx].ny = norm1.y;
_vertices[idx].nz = norm1.z;
idx += 1;
_vertices[idx].x = p3.x;
_vertices[idx].y = p3.y;
_vertices[idx].z = p3.z;
_vertices[idx].nx = norm2.x;
_vertices[idx].ny = norm2.y;
_vertices[idx].nz = norm2.z;
idx += 1;
_vertices[idx].x = p2.x;
_vertices[idx].y = p2.y;
_vertices[idx].z = p2.z;
_vertices[idx].nx = norm2.x;
_vertices[idx].ny = norm2.y;
_vertices[idx].nz = norm2.z;
idx += 1;
_vertices[idx].x = p4.x;
_vertices[idx].y = p4.y;
_vertices[idx].z = p4.z;
_vertices[idx].nx = norm2.x;
_vertices[idx].ny = norm2.y;
_vertices[idx].nz = norm2.z;
}
} }
void Mesh::updateVertices() const { void Mesh::updateVertices() const {
for(std::size_t j = 0; j < _size; ++j) { for(std::size_t j = 0; j < _size; ++j) {
for(std::size_t i = 0; i <= _size; ++i) { for(std::size_t i = 0; i <= _size; ++i) {
std::size_t idx = (j*(_size - 1) + i)*2*3; std::size_t idx = j*(_size - 1) + i;
_vertices[idx].z = heightMap(i, j); _vertices[idx].z = heightMap(i, j);
} }
} }
for(std::size_t j = 0; j < _size; ++j) { // 1. Calc normals for all triangles
for(std::size_t i = 0; i <= _size; ++i) { // _normals.clear();
std::size_t idx = (j*(_size - 1) + i)*2*3; // for(std::size_t j = 0; j < _size; ++j) {
float x = _xs[i], x2 = _xs[i + 1]; // for(std::size_t i = 0; i <= _size; ++i) {
float y = _ys[j], y2 = _ys[j + 1]; // float x = _xs[i], x2 = _xs[i + 1];
float z = _vertices[idx] // 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);
// }
// }
glm::vec3 p1(x, y, 0.f); // 2. Calc normals for all vertices based on precalculated normals for triangles
glm::vec3 p2(x2, y, 0.f); // Normal of vertex is an average of normals of adjacent triangles
glm::vec3 p3(p1.x, p1.y + step, 0.f); // for(std::size_t j = 0; j < _size; ++j) {
glm::vec3 p4(p1.x + xStep, p1.y + step, 0.f); // for (std::size_t i = 0; i <= _size; ++i) {
glm::vec3 norm1 = glm::cross(p2 - p1, p3 - p1); //
glm::vec3 norm2 = glm::cross(p3 - p4, p2 - p4); // }
} // }
}
} }
void Mesh::generateIndices() const { void Mesh::generateIndices() const {
for(std::size_t i = 0; i < _size - 1; ++i) GLuint u = (GLuint)_size, v = (GLuint)_size;
for(std::size_t j = 0; j < _size - 1; ++j) { for (unsigned i = 0; i < (u - 1); i++)
std::size_t idx = (i*(_size - 1) + j)*6; for (unsigned j = 0; j < (v - 1); j++)
_indices[idx] = (GLuint)idx; {
_indices[idx + 1] = (GLuint)idx + 1; GLuint indexa = j*(u - 1) + i;
_indices[idx + 2] = (GLuint)idx + 2; GLuint indexb = j*u + i;
_indices[idx + 3] = (GLuint)idx + 3;
_indices[idx + 4] = (GLuint)idx + 4; _indices[indexa*6 + 0] = indexb;
_indices[idx + 5] = (GLuint)idx + 5; _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;
} }
} }

View File

@ -9,6 +9,7 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <vector> #include <vector>
#include <array>
class Mesh: public GLObject { class Mesh: public GLObject {
@ -16,6 +17,8 @@ protected:
size_t _size; size_t _size;
std::vector<float> _xs; std::vector<float> _xs;
std::vector<float> _ys; std::vector<float> _ys;
mutable std::vector<glm::vec3> _normals;
mutable std::array<glm::vec3, 6> _adjacentTriangles;
public: public:
Mesh(ShaderProgram* shader, std::size_t size); Mesh(ShaderProgram* shader, std::size_t size);