Heightmap probing ... done?

This commit is contained in:
2023-05-09 14:30:39 +03:00
parent 368e5621d7
commit abf1b26ba2
9 changed files with 322 additions and 42 deletions
+24 -26
View File
@@ -145,6 +145,8 @@ static void add_triangle(std::vector<float>& buffer_data, glm::vec3 p1, glm::vec
void grbl::program_renderer::render(glm::mat4 model, glm::mat4 view, glm::mat4 projection, glm::mat3 normal_mat, glm::vec2 viewport_size) {
if (shader == nullptr || heightmap_shader == nullptr) return;
// draw heightmap
heightmap_shader->bind();
heightmap_shader->set_mat4(glm::value_ptr(model), "mmtx");
@@ -422,43 +424,39 @@ GLsizei grbl::program_renderer::update_model_vbo(const grbl::program& pgm) {
}
void grbl::program_renderer::update_grid(float from_x, float from_y, float to_x, float to_y, int resolution) {
int x_segments = ceil((to_x - from_x) / resolution);
int y_segments = ceil((to_y - from_y) / resolution);
void grbl::program_renderer::update_grid(const grbl::heightmap& grid) {
glm::vec4 color = {0.5, 0.3, 0, 1};
std::vector<glm::vec3> vertices;
float y_pos = from_y;
for (int y = 0; y < (y_segments + 1); y++) {
float x_pos = from_x;
for (int x = 0; x < (x_segments + 1); x++) {
vertices.emplace_back(x_pos, y_pos, 0);
x_pos += resolution;
}
y_pos += resolution;
}
std::vector<float> buffer_data;
for (int y = 0; y < y_segments; y++) {
for (int x = 0; x < x_segments; x++) {
int current = x + y * (x_segments + 1);
for (int y = 0; y < grid.y_segments; y++) {
for (int x = 0; x < grid.x_segments; x++) {
int current = x + y * (grid.x_segments + 1);
int next = current + 1;
int bottom = next + x_segments;
int bottom = next + grid.x_segments;
int bottom_next = bottom + 1;
auto p1 = vertices[current];
auto p2 = vertices[bottom];
auto p3 = vertices[next];
auto p1 = grid.vertices[current];
auto p2 = grid.vertices[bottom];
auto p3 = grid.vertices[next];
// exaggerate Z
auto exaggeration_factor = 1000.0f;
p1.z *= exaggeration_factor;
p2.z *= exaggeration_factor;
p3.z *= exaggeration_factor;
glm::vec3 normal = glm::normalize(glm::cross(p1 - p2, p3 - p1));
add_triangle(buffer_data, p1, p2, p3, normal, color);
p1 = vertices[next];
p2 = vertices[bottom];
p3 = vertices[bottom_next];
p1 = grid.vertices[next];
p2 = grid.vertices[bottom];
p3 = grid.vertices[bottom_next];
p1.z *= exaggeration_factor;
p2.z *= exaggeration_factor;
p3.z *= exaggeration_factor;
normal = glm::normalize(glm::cross(p1 - p2, p3 - p1));
add_triangle(buffer_data, p1, p2, p3, normal, color);
}