feat: Implement dynamic sketch grid based on camera zoom

Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
2026-02-17 12:32:02 -07:00
parent b056ccbfec
commit 1779725d53

View File

@@ -1,11 +1,35 @@
#include "SketchGrid.h"
#include "ViewportWidget.h"
#include "Camera.h"
#include <QOpenGLContext>
#include <QOpenGLExtraFunctions>
#include <QOpenGLShaderProgram>
#include <QPainter>
#include <QVector>
namespace {
struct GridParams {
float minorIncrement;
float majorIncrement;
int gridSize;
};
GridParams getGridParams(float zoom)
{
if (zoom > 8.0f) {
return { 0.2f, 1.0f, 10 };
} else if (zoom > 2.0f) {
return { 1.0f, 5.0f, 50 };
} else if (zoom > 0.8f) {
return { 5.0f, 25.0f, 250 };
} else if (zoom > 0.4f) {
return { 10.0f, 50.0f, 500 };
} else {
return { 20.0f, 100.0f, 1000 };
}
}
} // namespace
SketchGrid::SketchGrid(ViewportWidget* viewport) : m_viewport(viewport)
{
}
@@ -48,23 +72,29 @@ void SketchGrid::paintGL(SketchPlane plane, QOpenGLShaderProgram* shaderProgram,
void SketchGrid::drawGridLines(SketchPlane plane, QOpenGLShaderProgram* shaderProgram, int colorLoc)
{
const int gridSize = 50;
auto params = getGridParams(m_viewport->camera()->zoom());
const float minorIncrement = params.minorIncrement;
const int gridSize = params.gridSize;
QVector<GLfloat> minorLines;
QVector<GLfloat> majorLines;
for (int i = -gridSize; i <= gridSize; ++i)
{
if (i == 0) continue;
int numLines = gridSize / minorIncrement;
for (int i = -numLines; i <= numLines; ++i) {
if (i == 0)
continue;
float pos = i * minorIncrement;
QVector<GLfloat>& current_vector = (i % 5 == 0) ? majorLines : minorLines;
if (plane == XY) {
current_vector << i << 0 << -gridSize << i << 0 << gridSize;
current_vector << -gridSize << 0 << i << gridSize << 0 << i;
current_vector << pos << 0 << -gridSize << pos << 0 << gridSize;
current_vector << -gridSize << 0 << pos << gridSize << 0 << pos;
} else if (plane == XZ) {
current_vector << i << -gridSize << 0 << i << gridSize << 0;
current_vector << -gridSize << i << 0 << gridSize << i << 0;
current_vector << pos << -gridSize << 0 << pos << gridSize << 0;
current_vector << -gridSize << pos << 0 << gridSize << pos << 0;
} else { // YZ
current_vector << 0 << i << -gridSize << 0 << i << gridSize;
current_vector << 0 << -gridSize << i << 0 << gridSize << i;
current_vector << 0 << pos << -gridSize << 0 << pos << gridSize;
current_vector << 0 << -gridSize << pos << 0 << gridSize << pos;
}
}
@@ -130,18 +160,23 @@ void SketchGrid::paintAxisLabels(QPainter& painter, SketchGrid::SketchPlane plan
painter.setPen(Qt::white);
painter.setFont(QFont("Arial", 10));
const int range = 50;
const int step = 5;
auto params = getGridParams(m_viewport->camera()->zoom());
const float majorIncrement = params.majorIncrement;
const int range = params.gridSize;
auto drawLabelsForAxis = [&](int axis_idx) {
for (int i = -range; i <= range; i += step) {
if (i == 0) continue;
int numLabels = range / majorIncrement;
for (int i = -numLabels; i <= numLabels; ++i) {
if (i == 0)
continue;
float val = i * majorIncrement;
QVector3D worldCoord;
worldCoord[axis_idx] = i;
worldCoord[axis_idx] = val;
QVector3D screenPos = m_viewport->project(worldCoord, modelView, projection, m_viewport->rect());
if (screenPos.z() < 1.0f) { // Not clipped
painter.drawText(screenPos.toPoint(), QString::number(i));
painter.drawText(screenPos.toPoint(), QString::number(val));
}
}
};