From c29707ef6c4260e347e371abb7039f48c56e0e72 Mon Sep 17 00:00:00 2001 From: Bobby Battista Date: Wed, 17 May 2023 01:38:06 -0300 Subject: [PATCH] rename more variables --- src/game/client/baseheightmap.cpp | 95 ++++++++++++++++--------------- 1 file changed, 48 insertions(+), 47 deletions(-) diff --git a/src/game/client/baseheightmap.cpp b/src/game/client/baseheightmap.cpp index 6f97e2c96..00b79874f 100644 --- a/src/game/client/baseheightmap.cpp +++ b/src/game/client/baseheightmap.cpp @@ -966,7 +966,7 @@ void BaseHeightMapRenderObjClass::Do_The_Light(VertexFormatXYZDUV2 *vb, alpha); } -float BaseHeightMapRenderObjClass::Get_Height_Map_Height(float x, float y, Coord3D *pos) const +float BaseHeightMapRenderObjClass::Get_Height_Map_Height(float input_x, float input_y, Coord3D *pos) const { WorldHeightMap *height_map; @@ -978,69 +978,70 @@ float BaseHeightMapRenderObjClass::Get_Height_Map_Height(float x, float y, Coord } if (height_map != nullptr) { - float scaled_x = x * 0.1f; - float scaled_y = y * 0.1f; + float scaled_x = input_x * 0.1f; + float scaled_y = input_y * 0.1f; float floor_x = GameMath::Fast_Float_Floor(scaled_x); float floor_y = GameMath::Fast_Float_Floor(scaled_y); float fract_x = scaled_x - floor_x; float fract_y = scaled_y - floor_y; int index_x = height_map->Border_Size() + GameMath::Lrintf(floor_x); int index_y = height_map->Border_Size() + GameMath::Lrintf(floor_y); - int x_extent = height_map->Get_X_Extent(); + int map_width = height_map->Get_X_Extent(); // Check if the indices are within valid bounds - if (index_x <= x_extent - 3 && index_y <= height_map->Get_Y_Extent() - 3 && index_y >= 1 && index_x >= 1) { - unsigned char *data = height_map->Get_Data_Ptr(); - int index_1 = x_extent * index_y + index_x; - float height_1 = data[index_1]; - float height_2 = data[x_extent + 1 + index_1]; + if (index_x <= map_width - 3 && index_y <= height_map->Get_Y_Extent() - 3 && index_y >= 1 && index_x >= 1) { + unsigned char *height_data = height_map->Get_Data_Ptr(); + int current_point_index = map_width * index_y + index_x; + float top_left_height = height_data[current_point_index]; + float bottom_right_height = height_data[map_width + 1 + current_point_index]; float interpolated_height; // Calculate the height based on interpolation + // If fractional y is less than or equal to fractional x, our point is in the top right triangle of the cell if (fract_y <= fract_x) { - float height_3 = data[index_1 + 1]; - interpolated_height = ((height_2 - height_3) * fract_y + height_3 + (1.0f - fract_x) * (height_1 - height_3)) * HEIGHTMAP_SCALE; + float top_right_height = height_data[current_point_index + 1]; + interpolated_height = ((bottom_right_height - top_right_height) * fract_y + top_right_height + (1.0f - fract_x) * (top_left_height - top_right_height)) * HEIGHTMAP_SCALE; } else { - float height_3 = data[x_extent + index_1]; - interpolated_height = ((1.0f - fract_y) * (height_1 - height_3) + height_3 + (height_2 - height_3) * fract_x) * HEIGHTMAP_SCALE; + float bottom_left_height = height_data[map_width + current_point_index]; + interpolated_height = ((1.0f - fract_y) * (top_left_height - bottom_left_height) + bottom_left_height + (bottom_right_height - bottom_left_height) * fract_x) * HEIGHTMAP_SCALE; } // Calculate the normal vector if pos is provided if (pos != nullptr) { - int index_2 = x_extent * (index_y - 1) + index_x; - int index_3 = x_extent * index_y + index_x; - int index_4 = x_extent + index_3; - int index_5 = x_extent * (index_y + 2) + index_x; - unsigned char value_1 = data[index_3]; - unsigned char value_2 = data[index_3 + 1]; - unsigned char value_3 = data[x_extent + 1 + index_3]; - unsigned char value_4 = data[x_extent + index_3]; - unsigned char value_5 = data[index_2]; - unsigned char value_6 = data[index_2 + 1]; - unsigned char value_7 = data[index_5 + 1]; - unsigned char value_8 = data[index_5]; - float diff_1 = value_2 - data[index_3 - 1]; - float diff_2 = data[index_3 + 2] - value_1; - float diff_3 = data[x_extent + 2 + index_3] - value_4; - float diff_4 = value_4 - value_5; - float diff_5 = value_3 - value_6; - float diff_6 = value_7 - value_2; - float diff_7 = value_8 - value_1; - float diff_8 = (1.0f - fract_x) * diff_1 + fract_x * diff_2; - float diff_9 = (1.0f - fract_x) * diff_2 + fract_x * diff_3; - float diff_10 = diff_8 * (1.0f - fract_y) + fract_y * diff_9; - float diff_11 = (1.0f - fract_x) * diff_4 + fract_x * diff_7; - float diff_12 = (1.0f - fract_x) * diff_5 + fract_x * diff_6; - float diff_13 = diff_11 * (1.0f - fract_y) + fract_y * diff_12; - Vector3 vector_1; - Vector3 vector_2; - Vector3 vector_3; - vector_1.Set(32.0f, 0.0f, diff_10); - vector_2.Set(0.0f, 32.0f, diff_13); - Vector3::Normalized_Cross_Product(vector_1, vector_2, &vector_3); - pos->x = vector_3.X; - pos->y = vector_3.Y; - pos->z = vector_3.Z; + int prev_row_cell_index = map_width * (index_y - 1) + index_x; + int current_cell_index = map_width * index_y + index_x; + int next_row_cell_index = map_width + current_cell_index; + int next_next_row_cell_index = map_width * (index_y + 2) + index_x; + unsigned char current_cell_height_data = height_data[current_cell_index]; + unsigned char right_neighbor_height_data = height_data[current_cell_index + 1]; + unsigned char bottom_right_neighbor_height_data = data[map_width + 1 + current_cell_index]; + unsigned char bottom_neighbor_height_data = data[map_width + current_cell_index]; + unsigned char top_neighbor_height_data = data[prev_row_cell_index]; + unsigned char top_right_neighbor_height_data = data[prev_row_cell_index + 1]; + unsigned char down_two_right_neighbor_height_data = data[next_next_row_cell_index + 1]; + unsigned char down_two_neighbor_height_data = data[next_next_row_cell_index]; + float slope_1 = right_neighbor_height_data - data[current_cell_index - 1]; + float slope_2 = data[current_cell_index + 2] - current_cell_height_data; + float slope_3 = data[map_width + 2 + current_cell_index] - bottom_neighbor_height_data; + float slope_4 = bottom_neighbor_height_data - top_neighbor_height_data; + float slope_5 = bottom_right_neighbor_height_data - top_right_neighbor_height_data; + float slope_6 = down_two_right_neighbor_height_data - right_neighbor_height_data; + float slope_7 = down_two_neighbor_height_data - current_cell_height_data; + float interpolated_slope_x_difference = (1.0f - fract_x) * slope_1 + fract_x * slope_2; + float interpolated_slope_y_difference = (1.0f - fract_x) * slope_2 + fract_x * slope_3; + float interpolated_normal_x = interpolated_slope_x_difference * (1.0f - fract_y) + fract_y * interpolated_slope_y_difference; + float interpolated_height_x_difference = (1.0f - fract_x) * slope_4 + fract_x * slope_7; + float interpolated_height_y_difference = (1.0f - fract_x) * slope_5 + fract_x * slope_6; + float interpolated_normal_y = interpolated_height_x_difference * (1.0f - fract_y) + fract_y * interpolated_height_y_difference; + Vector3 v_x_axis; + Vector3 v_y_axis; + Vector3 normal_vector; + v_x_axis.Set(32.0f, 0.0f, interpolated_normal_x); + v_y_axis.Set(0.0f, 32.0f, interpolated_normal_y); + Vector3::Normalized_Cross_Product(v_x_axis, v_y_axis, &normal_vector); + pos->x = normal_vector.X; + pos->y = normal_vector.Y; + pos->z = normal_vector.Z; } return interpolated_height;