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:
@@ -1,11 +1,35 @@
|
|||||||
#include "SketchGrid.h"
|
#include "SketchGrid.h"
|
||||||
#include "ViewportWidget.h"
|
#include "ViewportWidget.h"
|
||||||
|
#include "Camera.h"
|
||||||
#include <QOpenGLContext>
|
#include <QOpenGLContext>
|
||||||
#include <QOpenGLExtraFunctions>
|
#include <QOpenGLExtraFunctions>
|
||||||
#include <QOpenGLShaderProgram>
|
#include <QOpenGLShaderProgram>
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
#include <QVector>
|
#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)
|
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)
|
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> minorLines;
|
||||||
QVector<GLfloat> majorLines;
|
QVector<GLfloat> majorLines;
|
||||||
|
|
||||||
for (int i = -gridSize; i <= gridSize; ++i)
|
int numLines = gridSize / minorIncrement;
|
||||||
{
|
for (int i = -numLines; i <= numLines; ++i) {
|
||||||
if (i == 0) continue;
|
if (i == 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
float pos = i * minorIncrement;
|
||||||
QVector<GLfloat>& current_vector = (i % 5 == 0) ? majorLines : minorLines;
|
QVector<GLfloat>& current_vector = (i % 5 == 0) ? majorLines : minorLines;
|
||||||
if (plane == XY) {
|
if (plane == XY) {
|
||||||
current_vector << i << 0 << -gridSize << i << 0 << gridSize;
|
current_vector << pos << 0 << -gridSize << pos << 0 << gridSize;
|
||||||
current_vector << -gridSize << 0 << i << gridSize << 0 << i;
|
current_vector << -gridSize << 0 << pos << gridSize << 0 << pos;
|
||||||
} else if (plane == XZ) {
|
} else if (plane == XZ) {
|
||||||
current_vector << i << -gridSize << 0 << i << gridSize << 0;
|
current_vector << pos << -gridSize << 0 << pos << gridSize << 0;
|
||||||
current_vector << -gridSize << i << 0 << gridSize << i << 0;
|
current_vector << -gridSize << pos << 0 << gridSize << pos << 0;
|
||||||
} else { // YZ
|
} else { // YZ
|
||||||
current_vector << 0 << i << -gridSize << 0 << i << gridSize;
|
current_vector << 0 << pos << -gridSize << 0 << pos << gridSize;
|
||||||
current_vector << 0 << -gridSize << i << 0 << gridSize << i;
|
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.setPen(Qt::white);
|
||||||
painter.setFont(QFont("Arial", 10));
|
painter.setFont(QFont("Arial", 10));
|
||||||
|
|
||||||
const int range = 50;
|
auto params = getGridParams(m_viewport->camera()->zoom());
|
||||||
const int step = 5;
|
const float majorIncrement = params.majorIncrement;
|
||||||
|
const int range = params.gridSize;
|
||||||
|
|
||||||
auto drawLabelsForAxis = [&](int axis_idx) {
|
auto drawLabelsForAxis = [&](int axis_idx) {
|
||||||
for (int i = -range; i <= range; i += step) {
|
int numLabels = range / majorIncrement;
|
||||||
if (i == 0) continue;
|
for (int i = -numLabels; i <= numLabels; ++i) {
|
||||||
|
if (i == 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
float val = i * majorIncrement;
|
||||||
QVector3D worldCoord;
|
QVector3D worldCoord;
|
||||||
worldCoord[axis_idx] = i;
|
worldCoord[axis_idx] = val;
|
||||||
|
|
||||||
QVector3D screenPos = m_viewport->project(worldCoord, modelView, projection, m_viewport->rect());
|
QVector3D screenPos = m_viewport->project(worldCoord, modelView, projection, m_viewport->rect());
|
||||||
if (screenPos.z() < 1.0f) { // Not clipped
|
if (screenPos.z() < 1.0f) { // Not clipped
|
||||||
painter.drawText(screenPos.toPoint(), QString::number(i));
|
painter.drawText(screenPos.toPoint(), QString::number(val));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user