Skip to content

Commit

Permalink
misc fixes
Browse files Browse the repository at this point in the history
- fix downscale not working with some non-POT dimensions
- fix disabled Solid Entities render flag still rendering ent clipnodes
- fix downscale crash
- don't select all faces when worldspawn is selected before switching to face picking mode (laggy)
- fix debug widget crash
  • Loading branch information
wootguy committed Apr 26, 2024
1 parent c7c5faf commit e018ece
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 28 deletions.
41 changes: 26 additions & 15 deletions src/bsp/Bsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2743,7 +2743,8 @@ bool Bsp::downscale_texture(int textureId, int newWidth, int newHeight) {
}
byte* newPalette = (byte*)(textures + texOffset + newOffset[3] + newWidths[3] * newHeights[3]);

float srcScale = (float)oldWidth / tex.nWidth;
float srcScaleX = (float)oldWidth / tex.nWidth;
float srcScaleY = (float)oldHeight / tex.nHeight;

for (int i = 0; i < 4; i++) {
byte* srcData = (byte*)(textures + texOffset + tex.nOffsets[i]);
Expand All @@ -2752,10 +2753,10 @@ bool Bsp::downscale_texture(int textureId, int newWidth, int newHeight) {
int dstWidth = newWidths[i];

for (int y = 0; y < newHeights[i]; y++) {
int srcY = (int)(srcScale * y + 0.5f);
int srcY = (int)(srcScaleY * y + 0.5f);

for (int x = 0; x < newWidths[i]; x++) {
int srcX = (int)(srcScale * x + 0.5f);
int srcX = (int)(srcScaleX * x + 0.5f);

dstData[y * dstWidth + x] = srcData[srcY * srcWidth + srcX];
}
Expand All @@ -2769,7 +2770,8 @@ bool Bsp::downscale_texture(int textureId, int newWidth, int newHeight) {
}

// scale up face texture coordinates
float scale = tex.nWidth / (float)oldWidth;
float scaleX = tex.nWidth / (float)oldWidth;
float scaleY = tex.nHeight / (float)oldHeight;

for (int i = 0; i < faceCount; i++) {
BSPFACE& face = faces[i];
Expand All @@ -2792,8 +2794,8 @@ bool Bsp::downscale_texture(int textureId, int newWidth, int newHeight) {

vec3 oldvs = info->vS;
vec3 oldvt = info->vT;
info->vS *= scale;
info->vT *= scale;
info->vS *= scaleX;
info->vT *= scaleY;

// get before/after uv coordinates
float oldu = (dotProduct(oldvs, vert) + info->shiftS) * (1.0f / (float)oldWidth);
Expand Down Expand Up @@ -2840,18 +2842,27 @@ bool Bsp::downscale_texture(int textureId, int maxDim) {
int newWidth = tex.nWidth;
int newHeight = tex.nHeight;

float ratio = oldHeight / (float)oldWidth;

while (newWidth > 0 && (newWidth > maxDim || newHeight > maxDim || (newHeight % 16) != 0)) {
newWidth -= 16;
newHeight = newWidth * ratio;
if (tex.nWidth > maxDim && tex.nWidth > tex.nHeight) {
float ratio = oldHeight / (float)oldWidth;
newWidth = maxDim;
newHeight = (int)(((newWidth * ratio) + 8) / 16) * 16;
if (newHeight > oldHeight) {
newHeight = (int)((newWidth * ratio) / 16) * 16;
}
}

if (oldWidth == newWidth) {
return false;
else if (tex.nHeight > maxDim) {
float ratio = oldWidth / (float)oldHeight;
newHeight = maxDim;
newWidth = (int)(((newHeight * ratio) + 8) / 16) * 16;
if (newWidth > oldWidth) {
newWidth = (int)((newHeight * ratio) / 16) * 16;
}
}
else {
return false; // no need to downscale
}

if (tex.nWidth == 0) {
if (oldWidth == newWidth && oldHeight == newHeight) {
logf("Failed to downscale texture %s %dx%d to max dim %d\n", tex.szName, oldWidth, oldHeight, maxDim);
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/editor/BspRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1502,7 +1502,7 @@ void BspRenderer::render(int highlightEnt, bool highlightAlwaysOnTop, int clipno
drawModelClipnodes(0, false, clipnodeHull);
}

if (g_render_flags & RENDER_ENT_CLIPNODES) {
if ((g_render_flags & RENDER_ENTS) && (g_render_flags & RENDER_ENT_CLIPNODES)) {
for (int i = 0, sz = map->ents.size(); i < sz; i++) {
if (renderEnts[i].modelIdx >= 0 && renderEnts[i].modelIdx < map->modelCount) {
if (clipnodeHull == -1 && renderModels[renderEnts[i].modelIdx].groupCount > 0) {
Expand Down
30 changes: 19 additions & 11 deletions src/editor/Gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ void Gui::draw3dContextMenus() {
set<int> downscaled;

for (int i = 0; i < app->selectedFaces.size(); i++) {
BSPFACE& face = map->faces[app->pickInfo.faceIdx];
BSPFACE& face = map->faces[app->selectedFaces[i]];
BSPTEXTUREINFO& info = map->texinfos[face.iTextureInfo];

if (downscaled.count(info.iMiptex))
Expand All @@ -541,8 +541,8 @@ void Gui::draw3dContextMenus() {
else if (maxDim > 32) { nextBestDim = 32; }
else if (maxDim > 32) { nextBestDim = 32; }

map->downscale_texture(info.iMiptex, nextBestDim);
downscaled.insert(info.iMiptex);
map->downscale_texture(info.iMiptex, nextBestDim);
}

app->deselectFaces();
Expand Down Expand Up @@ -958,8 +958,12 @@ void Gui::drawMenuBar() {
tooltip(g, "Render point-sized entities which either have no model or reference MDL/SPR files.");

if (ImGui::MenuItem("Solid Entities", 0, g_render_flags & RENDER_ENTS)) {
g_render_flags ^= RENDER_ENTS;
g_render_flags |= RENDER_SPECIAL_ENTS;
if (g_render_flags & RENDER_ENTS) {
g_render_flags &= ~(RENDER_ENTS | RENDER_SPECIAL_ENTS);
}
else {
g_render_flags |= RENDER_ENTS | RENDER_SPECIAL_ENTS;
}
}
tooltip(g, "Render entities that reference BSP models.");

Expand Down Expand Up @@ -1312,7 +1316,7 @@ void Gui::drawMenuBar() {
}
tooltip(g, "Subdivides faces until they have valid extents. The drawback to this method is reduced in-game performace from higher poly counts.");

ImGui::MenuItem("", "WIP");
ImGui::MenuItem("##", "WIP");
tooltip(g, "Anything you choose here will break lightmaps. "
"Run the map through a RAD compiler to fix, and pray that the mapper didn't "
"customize compile settings much.");
Expand Down Expand Up @@ -1496,11 +1500,15 @@ void Gui::drawToolbar() {
if (app->pickInfo.valid && app->pickInfo.modelIdx >= 0) {
Bsp* map = app->pickInfo.map;
BspRenderer* mapRenderer = app->mapRenderers[app->pickInfo.mapIdx];
BSPMODEL& model = map->models[app->pickInfo.modelIdx];
for (int i = 0; i < model.nFaces; i++) {
int faceIdx = model.iFirstFace + i;
mapRenderer->highlightFace(faceIdx, true);
app->selectedFaces.push_back(faceIdx);

// don't select all worldspawn faces because it lags the program
if (app->pickInfo.modelIdx != 0) {
BSPMODEL& model = map->models[app->pickInfo.modelIdx];
for (int i = 0; i < model.nFaces; i++) {
int faceIdx = model.iFirstFace + i;
mapRenderer->highlightFace(faceIdx, true);
app->selectedFaces.push_back(faceIdx);
}
}
}
app->selectMapIdx = app->pickInfo.mapIdx;
Expand Down Expand Up @@ -1688,7 +1696,7 @@ void Gui::drawDebugWidget() {

}

if (app->pickInfo.valid) {
if (app->pickInfo.valid && app->pickInfo.map) {
Bsp* map = app->pickInfo.map;

if (ImGui::CollapsingHeader("Map", ImGuiTreeNodeFlags_DefaultOpen))
Expand Down
2 changes: 1 addition & 1 deletion src/editor/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1682,7 +1682,7 @@ void Renderer::drawPlane(BSPPLANE& plane, COLOR4 color) {
vec3 right = crossProduct(plane.vNormal, crossDir);
vec3 up = crossProduct(right, plane.vNormal);

float s = 100.0f;
float s = 32768.0f;

vec3 topLeft = vec3(ori + right * -s + up * s).flip();
vec3 topRight = vec3(ori + right * s + up * s).flip();
Expand Down

0 comments on commit e018ece

Please sign in to comment.