Skip to content

Commit

Permalink
#5750: Texture Tool is now using the same grid size setting as the ma…
Browse files Browse the repository at this point in the history
…in scene (to re-use all existing shortcuts) but shifted by several powers towards smaller size. Max grid size is 2^0, smallest is 2^-7.
  • Loading branch information
codereader committed Sep 25, 2021
1 parent bd22004 commit 5c5d2c9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
49 changes: 37 additions & 12 deletions radiant/textool/TexTool.cpp
Expand Up @@ -8,6 +8,7 @@
#include "imainframe.h"
#include "igl.h"
#include "iundo.h"
#include "igrid.h"
#include "ibrush.h"
#include "iradiant.h"
#include "ipatch.h"
Expand Down Expand Up @@ -550,12 +551,45 @@ void TexTool::selectRelatedItems()
draw();
}

float TexTool::getGridSize()
{
// The Texture Tool is using the same grid "size" as the main orthographic views
// but since grid spacings >1 barely make sense when editing UV coordinates
// we offset the grid size such that GRID_1 refers to a UV spacing of 2^(-7) instead of 2^0.
int exponent = GlobalGrid().getGridPower() - 7;

if (exponent < -7)
{
exponent = -7; // not smaller than 2^-7
}
else if (exponent > 0)
{
exponent = 0; // not greater than 1.0
}

return pow(2.0f, exponent);
}

void TexTool::drawGrid()
{
const float MAX_NUMBER_OF_GRID_LINES = 1024;
auto gridSpacing = getGridSize();

const auto& texSpaceAABB = getVisibleTexSpace();
auto uPerPixel = texSpaceAABB.extents.x() / _windowDims.x();
auto vPerPixel = texSpaceAABB.extents.y() / _windowDims.y();

auto smallestUvPerPixel = uPerPixel > vPerPixel ? vPerPixel : uPerPixel;

auto gridSpacingInPixels = gridSpacing / smallestUvPerPixel;

// Ensure that the grid spacing is at least 10 pixels wide
while (gridSpacingInPixels < 12)
{
gridSpacing *= 2.0f;
gridSpacingInPixels = gridSpacing / smallestUvPerPixel;
}

const float MAX_NUMBER_OF_GRID_LINES = 256;
Vector3 topLeft = texSpaceAABB.origin - texSpaceAABB.extents;
Vector3 bottomRight = texSpaceAABB.origin + texSpaceAABB.extents;

Expand Down Expand Up @@ -615,22 +649,13 @@ void TexTool::drawGrid()
// Draw the manipulation grid
glColor4f(0.2f, 0.2f, 0.2f, 0.4f);

float grid = _grid;

// scale up the grid interval such that only a maximum number of lines are drawn
while (abs(endX - startX) / grid > MAX_NUMBER_OF_GRID_LINES ||
abs(endY - startY) / grid > MAX_NUMBER_OF_GRID_LINES)
{
grid *= 2;
}

for (float y = startY; y <= endY; y += grid)
for (float y = startY; y <= endY; y += gridSpacing)
{
glVertex2f(startX, y);
glVertex2f(endX, y);
}

for (float x = startX; x <= endX; x += grid)
for (float x = startX; x <= endX; x += gridSpacing)
{
glVertex2f(x, startY);
glVertex2f(x, endY);
Expand Down
2 changes: 2 additions & 0 deletions radiant/textool/TexTool.h
Expand Up @@ -250,6 +250,8 @@ class TexTool :
TextureToolMouseEvent createMouseEvent(const Vector2& point, const Vector2& delta = Vector2(0, 0));

void handleGLCapturedMouseMotion(const MouseToolPtr& tool, int x, int y, unsigned int mouseState);

float getGridSize();
};

} // namespace ui

0 comments on commit 5c5d2c9

Please sign in to comment.