still working on WaveMesh...
This commit is contained in:
parent
9b65b32b55
commit
107c3c0557
@ -3,7 +3,11 @@
|
||||
#include <glm/vec3.hpp>
|
||||
#include <iostream>
|
||||
|
||||
Mesh::Mesh(ShaderProgram* shader, std::size_t size) : GLObject(shader), _size(size), _xs(size), _ys(size), _normals(2*size*size) {
|
||||
Mesh::Mesh(ShaderProgram* shader, std::size_t size) : GLObject(shader),
|
||||
_size(size),
|
||||
_xs(size),
|
||||
_ys(size),
|
||||
_normals(2*size*size) {
|
||||
_vertexCount = _size*_size;
|
||||
_indexCount = (_size - 1)*(_size - 1)*6;
|
||||
_vertices = std::make_unique<Vertex[]>(_vertexCount);
|
||||
@ -28,7 +32,7 @@ void Mesh::generateVertices() const {
|
||||
}
|
||||
}
|
||||
|
||||
inline size_t Mesh::normalIndex(size_t x, size_t y) {
|
||||
inline size_t Mesh::normalIndex(size_t x, size_t y) const {
|
||||
return 2*(x + y*_size);
|
||||
}
|
||||
|
||||
@ -56,8 +60,8 @@ void Mesh::updateVertices() const {
|
||||
glm::vec3 v2 = p2 - p3;
|
||||
glm::vec3 v3 = p4 - p3;
|
||||
|
||||
glm::vec3 norm1 = glm::cross(v2, v1);
|
||||
glm::vec3 norm2 = glm::cross(v3, v2);
|
||||
glm::vec3 norm1 = glm::cross(v1, v2);
|
||||
glm::vec3 norm2 = glm::cross(v2, v3);
|
||||
|
||||
_normals.emplace_back(norm1);
|
||||
_normals.emplace_back(norm2);
|
||||
@ -68,8 +72,26 @@ void Mesh::updateVertices() const {
|
||||
// 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) {
|
||||
glm::vec3 normal;
|
||||
|
||||
size_t idx1 = normalIndex(i - 1, j - 1);
|
||||
for(size_t k = idx1; k < idx1 + 3; ++k) {
|
||||
if(k >= 0) {
|
||||
normal += _normals[k];
|
||||
}
|
||||
}
|
||||
|
||||
size_t idx2 = normalIndex(i - 1, j);
|
||||
for(size_t k = idx2; k < idx2 + 3; ++k) {
|
||||
if(k >= 0) {
|
||||
normal += _normals[k];
|
||||
}
|
||||
}
|
||||
|
||||
size_t idx = j*(_size - 1) + i;
|
||||
_vertices[idx].nx = normal.x;
|
||||
_vertices[idx].ny = normal.y;
|
||||
_vertices[idx].nz = normal.z;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -25,7 +25,7 @@ public:
|
||||
virtual ~Mesh();
|
||||
|
||||
private:
|
||||
size_t normalIndex(size_t x, size_t y);
|
||||
size_t normalIndex(size_t x, size_t y) const;
|
||||
|
||||
protected:
|
||||
void generateVertices() const override final;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user