Skip to content

Commit

Permalink
update overview generate
Browse files Browse the repository at this point in the history
  • Loading branch information
UnrealKaraulov committed Nov 19, 2024
1 parent 0567709 commit e82b5bc
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 72 deletions.
2 changes: 1 addition & 1 deletion src/bsp/bsplimits.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "bsplimits.h"


float FLT_MAX_COORD = 32767.f;
float FLT_MAX_COORD = 8192.f;
int LIGHTMAP_ATLAS_SIZE = 512;


Expand Down
90 changes: 75 additions & 15 deletions src/editor/Gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6900,7 +6900,7 @@ void Gui::drawOverviewWidget()
static Bsp* lastMap = NULL;
static bool orthoMode = true;
static bool updateFarNear = false;

static std::string imgFormat = ".tga";
if (updateFarNear)
{
updateFarNear = false;
Expand Down Expand Up @@ -6928,20 +6928,20 @@ void Gui::drawOverviewWidget()
return;
}

ImGui::SeparatorText("Custom Window Size");
ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.3f);
ImGui::DragFloat("Width", &ortho_custom_w, 1.0f, 256.0f, 2048.0f, "%.0f");
ImGui::SameLine();
ImGui::DragFloat("Height", &ortho_custom_h, 1.0f, 256.0f, 2048.0f, "%.0f");
ImGui::PopItemWidth();

/* ImGui::SeparatorText("Custom Window Size");
ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.3f);
ImGui::DragFloat("Width", &ortho_custom_w, 1.0f, 256.0f, 2048.0f, "%.0f");
ImGui::SameLine();
ImGui::DragFloat("Height", &ortho_custom_h, 1.0f, 256.0f, 2048.0f, "%.0f");
ImGui::PopItemWidth();
*/
ImGui::SeparatorText("Overview Settings");
ImGui::Checkbox("Show Overview", &orthoMode);
ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.3f);
ImGui::DragFloat("Aspect Ratio", &ortho_custom_aspect, 0.01f, 0.5f, 2.0f, "%.2f");
ImGui::DragFloat("Ortho FOV", &ortho_fov, 0.1f, 10.0f, 100.0f, "%.2f");
ImGui::DragFloat("Ortho FOV", &ortho_fov, 0.1f, 0.01f, 200.0f, "%.2f");
ImGui::DragFloat("Ortho Near", &ortho_near, 1.0f, -1.0f, 8192.0f, "%.2f");
ImGui::DragFloat("Ortho Far", &ortho_far, 1.0f, -1.0f, FLT_MAX_COORD, "%.2f");
ImGui::DragFloat("Ortho Far", &ortho_far, 1.0f, -1.0f, FLT_MAX, "%.2f");
ImGui::PopItemWidth();

ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.7f);
Expand All @@ -6955,7 +6955,7 @@ void Gui::drawOverviewWidget()
map->get_bounding_box(ortho_mins, ortho_maxs);
}
if (ImGui::Button("Fill from Verts")) {
map->get_model_vertex_bounds(0, ortho_mins, ortho_maxs,true);
map->get_model_vertex_bounds(0, ortho_mins, ortho_maxs, true);
}
if (ImGui::Button("Fill from Leaves")) {
ortho_mins = vec3(FLT_MAX, FLT_MAX, FLT_MAX);
Expand All @@ -6976,29 +6976,89 @@ void Gui::drawOverviewWidget()

ImGui::SeparatorText("Save to TGA");
ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.3f);
ImGui::DragInt("Width###2", &ortho_tga_w, 1.0f, 256, 2048);
ImGui::DragInt("Width###2", &ortho_tga_w, 1.0f, 256, 4096);
ImGui::SameLine();
ImGui::DragInt("Height###3", &ortho_tga_h, 1.0f, 256, 2048);
ImGui::DragInt("Height###3", &ortho_tga_h, 1.0f, 256, 4096);
ImGui::PopItemWidth();

if (ImGui::Button("Save .tga")) {
ortho_save_tga = true;
imgFormat = ".tga";
}
ImGui::SameLine();
if (ImGui::Button("Save .bmp"))
{
ortho_save_bmp = true;
imgFormat = ".bmp";
}
ImGui::SameLine();


float x_size = ortho_maxs.x - ortho_mins.x;
float y_size = ortho_maxs.y - ortho_mins.y;
float zoomFriction = ((float)ortho_tga_w / (float)ortho_tga_h);
float xScale, yScale;
bool rotated = false;

if (x_size < y_size) {
xScale = 8192.0f / (x_size * zoomFriction);
yScale = 8192.0f / y_size;
}
else {
rotated = true;
xScale = 8192.0f / x_size;
yScale = 8192.0f / (y_size * zoomFriction);
}

if (ImGui::IsMouseReleased(ImGuiMouseButton_Left))
float zoom = (xScale < yScale) ? xScale : yScale;

vec3 origin = vec3((ortho_mins.x + ortho_maxs.x) / 2.0f + ortho_offset.x,
(ortho_mins.y + ortho_maxs.y) / 2.0f + ortho_offset.y,
(ortho_mins.z + ortho_maxs.z) / 2.0f + ortho_offset.z);

if (ImGui::Button("Save .txt"))
{
FILE* overfile = NULL;
fopen_s(&overfile, (g_working_dir + map->bsp_name + ".txt").c_str(), "wb");
if (overfile)
{
fprintf(overfile, "// overview description file for %s\n\n", map->bsp_name.c_str());
fprintf(overfile, "global \n{\n");
fprintf(overfile, "\tZOOM\t%.2f\n", zoom);
fprintf(overfile, "\tORIGIN\t%.2f\t%.2f\t%.2f\n", origin.x, origin.y, origin.z);
fprintf(overfile, "\tROTATED\t%i\n}\n\n", rotated ? 1 : 0);
fprintf(overfile, "layer \n{\n");
fprintf(overfile, "\tIMAGE\t\"overviews/%s%s\"\n", map->bsp_name.c_str(),imgFormat.c_str());
fprintf(overfile, "\tHEIGHT\t%.2f\n}\n", origin.z);
fclose(overfile);
print_log("Saved to {}\n", g_working_dir + map->bsp_name + ".txt");
}
}
ImGui::SeparatorText("DEV INFO");


ImGui::Text("Overview: Zoom %.2f", zoom);

ImGui::Text("Map Origin: (%.2f, %.2f, %.2f)",
(ortho_mins.x + ortho_maxs.x) / 2.0f + ortho_offset.x,
(ortho_mins.y + ortho_maxs.y) / 2.0f + ortho_offset.y,
(ortho_mins.z + ortho_maxs.z) / 2.0f + ortho_offset.z);

ImGui::Text("Z Min: %.2f, Z Max: %.2f", ortho_near, ortho_far);

ImGui::Text("Rotated: %s", rotated ? "true" : "false");

ImGui::Text("X Scale: %.2f, Y Scale: %.2f", xScale, yScale);

/*if (ImGui::IsMouseReleased(ImGuiMouseButton_Left))
{
if (std::fabs(ortho_custom_w) > EPSILON && ortho_custom_w < 256.0f)
ortho_custom_w = 256.0f;

if (std::fabs(ortho_custom_h) > EPSILON && ortho_custom_h < 256.0f)
ortho_custom_h = 256.0f;
}

*/
}
ImGui::End();
}
Expand Down
93 changes: 70 additions & 23 deletions src/editor/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"

#include "quantizer.h"

Renderer* g_app = NULL;
std::vector<BspRenderer*> mapRenderers{};

Expand All @@ -29,7 +31,7 @@ vec3 ortho_mins(-FLT_MAX_COORD,-FLT_MAX_COORD, -FLT_MAX_COORD), ortho_maxs(FLT_M
vec3 ortho_offset = {};
float ortho_near = 1.0f;
float ortho_far = 262144.0f;
float ortho_fov = 45.0f;
float ortho_fov = 10.0f;
float ortho_custom_aspect = 0.0f;
float ortho_custom_w = 0.0f;
float ortho_custom_h = 0.0f;
Expand Down Expand Up @@ -549,6 +551,24 @@ void Renderer::renderLoop()

GLuint fbo = NULL, texture, rbo;


if (ortho_overview)
{
if (ortho_save_tga || ortho_save_bmp)
{
setupFakeOrthoView(ortho_tga_w, ortho_tga_h, ortho_mins, ortho_maxs);
}
else
{
setupFakeOrthoView(0, 0, ortho_mins, ortho_maxs);
}
}
else
{
setupView();
}


if (ortho_save_tga || ortho_save_bmp)
{
glGenFramebuffers(1, &fbo);
Expand Down Expand Up @@ -673,24 +693,6 @@ void Renderer::renderLoop()
gui->shouldUpdateUi = true;
}

if (getSelectedMap() && ortho_overview)
{
/*static vec3 bmins = vec3(), bmaxs = vec3();
if (bmins == vec3() && bmaxs == vec3())
{
for (int v = 0; v < getSelectedMap()->vertCount; v++)
{
expandBoundingBox(getSelectedMap()->verts[v], bmins, bmaxs);
}
}*/
setupFakeOrthoView(0, 0, ortho_mins, ortho_maxs);
}
else
{
setupView();
}

drawEntConnections();

isLoading = reloading;
Expand Down Expand Up @@ -1180,12 +1182,55 @@ void Renderer::renderLoop()

if (ortho_save_tga)
{
stbi_write_tga((g_working_dir + "overview.tga").c_str(), ortho_tga_w, ortho_tga_h, 3, pixels.data());
stbi_write_tga((g_working_dir + (SelectedMap ? (SelectedMap->bsp_name + ".tga") : "overview.tga")).c_str(), ortho_tga_w, ortho_tga_h, 3, pixels.data());
print_log("Saved to {} file!\n", (g_working_dir + "overview.tga").c_str());
}
else
{
stbi_write_bmp((g_working_dir + "overview.bmp").c_str(), ortho_tga_w, ortho_tga_h, 3, pixels.data());
Quantizer* tmpCQuantizer = new Quantizer(256, 8);
tmpCQuantizer->ApplyColorTable((COLOR3*)pixels.data(), ortho_tga_w * ortho_tga_h);
delete tmpCQuantizer;

int colors = 0;
COLOR3 palette[256];
std::vector<unsigned char> indexedPixels(ortho_tga_w* ortho_tga_h);

for (int y = 0; y < ortho_tga_h; y++)
{
for (int x = 0; x < ortho_tga_w; x++)
{
int paletteIdx = -1;
for (int k = 0; k < colors; k++)
{
if (pixels[(y * ortho_tga_w + x) * 3] == palette[k].r &&
pixels[(y * ortho_tga_w + x) * 3 + 1] == palette[k].g &&
pixels[(y * ortho_tga_w + x) * 3 + 2] == palette[k].b)
{
paletteIdx = k;
break;
}
}
if (paletteIdx == -1)
{
if (colors >= 256)
{
print_log(PRINT_RED | PRINT_INTENSITY, get_localized_string(LANG_0167));
delete tmpCQuantizer;
return;
}
palette[colors].r = pixels[(y * ortho_tga_w + x) * 3];
palette[colors].g = pixels[(y * ortho_tga_w + x) * 3 + 1];
palette[colors].b = pixels[(y * ortho_tga_w + x) * 3 + 2];
paletteIdx = colors;
colors++;
}
indexedPixels[y * ortho_tga_w + x] = (unsigned char)paletteIdx;
}
}


WriteBMP_PAL((g_working_dir + (SelectedMap ? (SelectedMap->bsp_name + ".bmp") : "overview.bmp")).c_str(), indexedPixels.data(), ortho_tga_w, ortho_tga_h, palette);

print_log("Saved to {} file!\n", (g_working_dir + "overview.bmp").c_str());
}

Expand Down Expand Up @@ -2813,8 +2858,10 @@ void Renderer::setupFakeOrthoView(int forceW, int forceH, vec3 bboxMin, vec3 bbo
float bboxWidth = bboxMax.x - bboxMin.x;
float bboxHeight = bboxMax.y - bboxMin.y;

projection.perspective(ortho_fov, aspect, std::fabs(ortho_near) > EPSILON ? ortho_near : zNear,
std::fabs(ortho_far) > EPSILON ? ortho_far : zFar);
float newZNear = ortho_near / (2.0f * tan(ortho_fov * HL_PI / 360.0f));
float newZFar = ortho_far / (2.0f * tan(ortho_fov * HL_PI / 360.0f));

projection.perspective(ortho_fov, aspect, newZNear, newZFar);

vec3 center = getCenter(bboxMin, bboxMax) + ortho_offset;

Expand Down
3 changes: 0 additions & 3 deletions src/res/Wad.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ int calcMipsSize(int w, int h);

#pragma pack(push, 1)

COLOR3 operator*(COLOR3 v, float f);
bool operator==(COLOR3 c1, COLOR3 c2);

COLOR4 operator*(COLOR4 v, float f);
bool operator==(COLOR4 c1, COLOR4 c2);

Expand Down
7 changes: 7 additions & 0 deletions src/util/mat4x4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ void mat4x4::perspective(float fov, float aspect, float near, float far)
glhFrustumf2(m, -xmax, xmax, -ymax, ymax, near, far);
}

void mat4x4::perspective(float left, float right, float bottom, float top,
float znear, float zfar)
{
loadIdentity();
glhFrustumf2(m, left, right, bottom, top, znear, zfar);
}


void mat4x4::ortho(float left, float right, float bottom, float top, float near, float far)
{
Expand Down
3 changes: 3 additions & 0 deletions src/util/mat4x4.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ struct mat4x4

void perspective(float fov, float aspect, float near, float far);

void perspective(float left, float right, float bottom, float top,
float znear, float zfar);

// Set up an orthographic projection matrix
void ortho(float left, float right, float bottom, float top, float near, float far);

Expand Down
Loading

0 comments on commit e82b5bc

Please sign in to comment.