Skip to content
Snippets Groups Projects
Commit 05134d0e authored by Pascal Engeler's avatar Pascal Engeler
Browse files

Calculate EBO index array, upload data to GPU

parent 3c4dbe77
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,8 @@
#include <fstream>
#include <iostream>
#include <sstream>
#include <utility>
#include <algorithm>
Heightmap::Heightmap(std::string filename): _filename(filename) {
std::string file_string = "";
......@@ -54,8 +56,8 @@ Heightmap::Heightmap(std::string filename): _filename(filename) {
_maxZ = max(_data, 2, 3);
_zRangeUm = (_maxZ - _minZ) / 10.; //in um
/*Throw if _zRange is less than 1 A (i.e. it's probably 0)*/
if (_zRange < 1e-4) {
throw Nt1100_exception("Heightmap: z range is too close to zero (" + std::to_string(_zRange) + ")");
if (_zRangeUm < 1e-4) {
throw Nt1100_exception("Heightmap::Heightmap(std::string): z range is too close to zero (" + std::to_string(_zRangeUm) + ")");
}
/*TODO: Renormalize model coordinates sensible here*/
//For now, I just renormalize the z range to [0,1]
......@@ -69,6 +71,50 @@ Heightmap::Heightmap(std::string filename): _filename(filename) {
glGenBuffers(1, &_vbo);
glGenBuffers(1, &_ebo);
glGenVertexArrays(1, &_vao);
/*Calculate EBO indexing array*/
std::vector<unsigned> ebo_array;
ebo_array.reserve(2 * _data.size()); //rough upper bound
//Calculate Ny assuming that _data is crossed column by column
_Ny = 0;
while (3 * _Ny < _data.size() && _data[3 * _Ny] == _data[0]) {
++_Ny;
}
//Sanity check _Ny
if (_Ny < 5 || 3 * _Ny + 10 > _data.size()) {
throw Nt1100_exception("Heightmap::Heightmap(std::string): Probably failed to deduce _Ny. Found _Ny=" + std::to_string(_Ny) + ", for _data of size " + std::to_string(_data.size()));
}
//Deduce _Nx
_Nx = _data.size() / 3 / _Ny;
if (3 * _Nx * _Ny != _data.size()) {
throw Nt1100_exception("Heightmap::Heightmap(std::string): Value of _Nx=" + std::to_string(_Nx) + " or _Ny=" + std::to_string(_Ny) + " is incorrect for _data of size " + std::to_string(_data.size()));
}
for (unsigned i = 0; i < _Nx - 1; ++i) {
for (unsigned j = 0; j < _Ny - 1; ++j) {
std::array<std::pair<unsigned, unsigned>, 6> ijs = { { { i, j }, {i, j + 1}, {i + 1, j}, {i + 1, j}, {i + 1, j + 1}, {i, j + 1} } };
std::array<unsigned, 6> index_array;
std::transform(ijs.begin(), ijs.end(), index_array.begin(), [&](const std::pair<unsigned, unsigned>& p) {return ij2index(p.first, p.second, _Ny); });
ebo_array.insert(ebo_array.end(), index_array.begin(), index_array.end());
}
}
/*Upload EBO data*/
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned) * ebo_array.size(), ebo_array.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
/*Upload VBO data*/
//Temporarily cast to float
std::vector<float> data_float;
data_float.reserve(_data.size());
data_float.insert(data_float.begin(), _data.begin(), _data.end());
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * data_float.size(), data_float.data(), GL_STATIC_DRAW);
/*Setup VAO*/
glBindVertexArray(_vao);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
/*TODO: Generate shader, I may want to use a Factory here*/
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment