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

Finished constructor, implemented other methods

parent a0efe750
No related branches found
No related tags found
No related merge requests found
......@@ -7,6 +7,7 @@
#include <sstream>
#include <utility>
#include <algorithm>
#include <resource_manager.h>
Heightmap::Heightmap(std::string filename): _filename(filename) {
std::string file_string = "";
......@@ -20,6 +21,7 @@ Heightmap::Heightmap(std::string filename): _filename(filename) {
}
catch(const std::exception e){
std::cerr << "An error occurred loading file " << filename << ": " << e.what() << std::endl;
file_string = "";
}
if (file_string != "") {
......@@ -62,9 +64,14 @@ Heightmap::Heightmap(std::string filename): _filename(filename) {
/*TODO: Renormalize model coordinates sensible here*/
//For now, I just renormalize the z range to [0,1]
double zRangeInverse = 1. / (_maxZ - _minZ);
for (auto it = _data.begin()+2; it < _data.end(); it += 3) {
*it -= _minZ;
*it *= zRangeInverse;
for (size_t i = 2; i < _data.size(); i += 3) {
if (_data[i] >= _minZ) {
_data[i] -= _minZ;
_data[i] *= zRangeInverse;
}
else {
_data[i] = 0.;
}
}
/*Initialize Renderer Infrastructure*/
......@@ -72,6 +79,14 @@ Heightmap::Heightmap(std::string filename): _filename(filename) {
glGenBuffers(1, &_ebo);
glGenVertexArrays(1, &_vao);
/*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);
/*Calculate EBO indexing array*/
std::vector<unsigned> ebo_array;
ebo_array.reserve(2 * _data.size()); //rough upper bound
......@@ -97,29 +112,53 @@ Heightmap::Heightmap(std::string filename): _filename(filename) {
ebo_array.insert(ebo_array.end(), index_array.begin(), index_array.end());
}
}
_numElements = ebo_array.size();
/*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);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
/*TODO: Generate shader, I may want to use a Factory here*/
/*TODO: This should be made prettier*/
std::string vertFile = std::string(SHADER_LOC) + std::string("heightmap0.vert");
std::string fragFile = std::string(SHADER_LOC) + std::string("heightmap0.frag");
_shader_ptr = ResourceManager::fetch_shader(vertFile, fragFile);
}
else {
throw Nt1100_exception("Heightmap::Heightmap(std::string): File " + filename + " contents are empty.");
}
}
void Heightmap::draw(glm::mat4 projection, glm::mat4 view, glm::mat4 model){
_shader_ptr->use();
_shader_ptr->setMat4("projection", projection);
_shader_ptr->setMat4("view", view);
_shader_ptr->setMat4("model", model);
glBindVertexArray(_vao);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo);
glDrawElements(GL_TRIANGLES, _numElements, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
_shader_ptr->unuse();
}
void Heightmap::upload() {
return;
}
void Heightmap::unload() {
return;
}
bool Heightmap::is(const std::string filename) const {
return _filename == filename;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment