refactor: Use gp_Ax2 to define and draw SketchGrid planes
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
+55
-50
@@ -13,6 +13,7 @@
|
||||
#include <QOpenGLShaderProgram>
|
||||
#include <QPainter>
|
||||
#include <QVector>
|
||||
#include <gp_Ax2.hxx>
|
||||
|
||||
namespace {
|
||||
struct GridParams {
|
||||
@@ -62,7 +63,7 @@ void SketchGrid::initializeGL()
|
||||
m_vbo.release();
|
||||
}
|
||||
|
||||
void SketchGrid::paintGL(SketchPlane plane, QOpenGLShaderProgram* shaderProgram, int colorLoc)
|
||||
void SketchGrid::paintGL(const gp_Ax2& plane, QOpenGLShaderProgram* shaderProgram, int colorLoc)
|
||||
{
|
||||
GLint previous_vao = 0;
|
||||
glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &previous_vao);
|
||||
@@ -77,12 +78,19 @@ void SketchGrid::paintGL(SketchPlane plane, QOpenGLShaderProgram* shaderProgram,
|
||||
QOpenGLContext::currentContext()->extraFunctions()->glBindVertexArray(previous_vao);
|
||||
}
|
||||
|
||||
void SketchGrid::drawGridLines(SketchPlane plane, QOpenGLShaderProgram* shaderProgram, int colorLoc)
|
||||
void SketchGrid::drawGridLines(const gp_Ax2& plane, QOpenGLShaderProgram* shaderProgram, int colorLoc)
|
||||
{
|
||||
auto params = getGridParams(-m_viewport->camera()->uiCameraDistance());
|
||||
const float minorIncrement = params.minorIncrement;
|
||||
const int gridSize = params.gridSize;
|
||||
|
||||
const auto& origin = plane.Location();
|
||||
const auto& xDir = plane.XDirection();
|
||||
const auto& yDir = plane.YDirection();
|
||||
QVector3D originVec(origin.X(), origin.Y(), origin.Z());
|
||||
QVector3D xDirVec(xDir.X(), xDir.Y(), xDir.Z());
|
||||
QVector3D yDirVec(yDir.X(), yDir.Y(), yDir.Z());
|
||||
|
||||
QVector<GLfloat> minorLines;
|
||||
QVector<GLfloat> majorLines;
|
||||
|
||||
@@ -93,16 +101,14 @@ void SketchGrid::drawGridLines(SketchPlane plane, QOpenGLShaderProgram* shaderPr
|
||||
|
||||
float pos = i * minorIncrement;
|
||||
QVector<GLfloat>& current_vector = (i % 5 == 0) ? majorLines : minorLines;
|
||||
if (plane == XY) {
|
||||
current_vector << pos << -gridSize << 0 << pos << gridSize << 0;
|
||||
current_vector << -gridSize << pos << 0 << gridSize << pos << 0;
|
||||
} else if (plane == XZ) {
|
||||
current_vector << pos << 0 << -gridSize << pos << 0 << gridSize;
|
||||
current_vector << -gridSize << 0 << pos << gridSize << 0 << pos;
|
||||
} else { // YZ
|
||||
current_vector << 0 << pos << -gridSize << 0 << pos << gridSize;
|
||||
current_vector << 0 << -gridSize << pos << 0 << gridSize << pos;
|
||||
}
|
||||
|
||||
QVector3D p1 = originVec + pos * xDirVec - gridSize * yDirVec;
|
||||
QVector3D p2 = originVec + pos * xDirVec + gridSize * yDirVec;
|
||||
current_vector << p1.x() << p1.y() << p1.z() << p2.x() << p2.y() << p2.z();
|
||||
|
||||
QVector3D p3 = originVec - gridSize * xDirVec + pos * yDirVec;
|
||||
QVector3D p4 = originVec + gridSize * xDirVec + pos * yDirVec;
|
||||
current_vector << p3.x() << p3.y() << p3.z() << p4.x() << p4.y() << p4.z();
|
||||
}
|
||||
|
||||
m_vbo.bind();
|
||||
@@ -120,50 +126,51 @@ void SketchGrid::drawGridLines(SketchPlane plane, QOpenGLShaderProgram* shaderPr
|
||||
glDrawArrays(GL_LINES, 0, majorLines.size() / 3);
|
||||
}
|
||||
|
||||
void SketchGrid::drawAxes(SketchPlane plane, QOpenGLShaderProgram* shaderProgram, int colorLoc)
|
||||
void SketchGrid::drawAxes(const gp_Ax2& plane, QOpenGLShaderProgram* shaderProgram, int colorLoc)
|
||||
{
|
||||
auto params = getGridParams(-m_viewport->camera()->uiCameraDistance());
|
||||
const int axisLength = params.gridSize;
|
||||
|
||||
const auto& origin = plane.Location();
|
||||
const auto& xDir = plane.XDirection();
|
||||
const auto& yDir = plane.YDirection();
|
||||
QVector3D originVec(origin.X(), origin.Y(), origin.Z());
|
||||
QVector3D xDirVec(xDir.X(), xDir.Y(), xDir.Z());
|
||||
QVector3D yDirVec(yDir.X(), yDir.Y(), yDir.Z());
|
||||
|
||||
QVector<GLfloat> vertices;
|
||||
|
||||
glLineWidth(2.0f);
|
||||
m_vbo.bind();
|
||||
|
||||
// X Axis (Red)
|
||||
if (plane == XY || plane == XZ) {
|
||||
vertices.clear();
|
||||
vertices << -axisLength << 0 << 0 << axisLength << 0 << 0;
|
||||
shaderProgram->setUniformValue(colorLoc, QVector4D(1.0f, 0.0f, 0.0f, 1.0f));
|
||||
m_vbo.allocate(vertices.constData(), vertices.size() * sizeof(GLfloat));
|
||||
glDrawArrays(GL_LINES, 0, 2);
|
||||
}
|
||||
vertices.clear();
|
||||
QVector3D p1 = originVec - axisLength * xDirVec;
|
||||
QVector3D p2 = originVec + axisLength * xDirVec;
|
||||
vertices << p1.x() << p1.y() << p1.z() << p2.x() << p2.y() << p2.z();
|
||||
shaderProgram->setUniformValue(colorLoc, QVector4D(1.0f, 0.0f, 0.0f, 1.0f));
|
||||
m_vbo.allocate(vertices.constData(), vertices.size() * sizeof(GLfloat));
|
||||
glDrawArrays(GL_LINES, 0, 2);
|
||||
|
||||
// Y Axis (Green)
|
||||
if (plane == XY || plane == YZ) {
|
||||
vertices.clear();
|
||||
vertices << 0 << -axisLength << 0 << 0 << axisLength << 0;
|
||||
shaderProgram->setUniformValue(colorLoc, QVector4D(0.0f, 1.0f, 0.0f, 1.0f));
|
||||
m_vbo.allocate(vertices.constData(), vertices.size() * sizeof(GLfloat));
|
||||
glDrawArrays(GL_LINES, 0, 2);
|
||||
}
|
||||
// Z Axis (Blue)
|
||||
if (plane == XZ || plane == YZ) {
|
||||
vertices.clear();
|
||||
vertices << 0 << 0 << -axisLength << 0 << 0 << axisLength;
|
||||
shaderProgram->setUniformValue(colorLoc, QVector4D(0.0f, 0.0f, 1.0f, 1.0f));
|
||||
m_vbo.allocate(vertices.constData(), vertices.size() * sizeof(GLfloat));
|
||||
glDrawArrays(GL_LINES, 0, 2);
|
||||
}
|
||||
vertices.clear();
|
||||
QVector3D p3 = originVec - axisLength * yDirVec;
|
||||
QVector3D p4 = originVec + axisLength * yDirVec;
|
||||
vertices << p3.x() << p3.y() << p3.z() << p4.x() << p4.y() << p4.z();
|
||||
shaderProgram->setUniformValue(colorLoc, QVector4D(0.0f, 1.0f, 0.0f, 1.0f));
|
||||
m_vbo.allocate(vertices.constData(), vertices.size() * sizeof(GLfloat));
|
||||
glDrawArrays(GL_LINES, 0, 2);
|
||||
|
||||
// Origin dot
|
||||
glPointSize(5.0f);
|
||||
vertices.clear();
|
||||
vertices << 0.0f << 0.0f << 0.0f;
|
||||
vertices << originVec.x() << originVec.y() << originVec.z();
|
||||
shaderProgram->setUniformValue(colorLoc, QVector4D(1.0f, 1.0f, 1.0f, 1.0f)); // White
|
||||
m_vbo.allocate(vertices.constData(), vertices.size() * sizeof(GLfloat));
|
||||
glDrawArrays(GL_POINTS, 0, 1);
|
||||
}
|
||||
|
||||
void SketchGrid::paintAxisLabels(QPainter& painter, SketchGrid::SketchPlane plane, const QMatrix4x4& modelView, const QMatrix4x4& projection)
|
||||
void SketchGrid::paintAxisLabels(QPainter& painter, const gp_Ax2& plane, const QMatrix4x4& modelView, const QMatrix4x4& projection)
|
||||
{
|
||||
painter.setPen(Qt::white);
|
||||
painter.setFont(QFont("Arial", 10));
|
||||
@@ -172,15 +179,21 @@ void SketchGrid::paintAxisLabels(QPainter& painter, SketchGrid::SketchPlane plan
|
||||
const float majorIncrement = params.majorIncrement;
|
||||
const int range = params.gridSize;
|
||||
|
||||
auto drawLabelsForAxis = [&](int axis_idx) {
|
||||
const auto& origin = plane.Location();
|
||||
const auto& xDir = plane.XDirection();
|
||||
const auto& yDir = plane.YDirection();
|
||||
QVector3D originVec(origin.X(), origin.Y(), origin.Z());
|
||||
QVector3D xDirVec(xDir.X(), xDir.Y(), xDir.Z());
|
||||
QVector3D yDirVec(yDir.X(), yDir.Y(), yDir.Z());
|
||||
|
||||
auto drawLabelsForAxis = [&](const QVector3D& axisDir) {
|
||||
int numLabels = range / majorIncrement;
|
||||
for (int i = -numLabels; i <= numLabels; ++i) {
|
||||
if (i == 0)
|
||||
continue;
|
||||
|
||||
float val = i * majorIncrement;
|
||||
QVector3D worldCoord;
|
||||
worldCoord[axis_idx] = val;
|
||||
QVector3D worldCoord = originVec + val * axisDir;
|
||||
|
||||
QVector3D screenPos = m_viewport->project(worldCoord, modelView, projection, m_viewport->rect());
|
||||
if (screenPos.z() < 1.0f) { // Not clipped
|
||||
@@ -189,14 +202,6 @@ void SketchGrid::paintAxisLabels(QPainter& painter, SketchGrid::SketchPlane plan
|
||||
}
|
||||
};
|
||||
|
||||
if (plane == SketchGrid::XY) {
|
||||
drawLabelsForAxis(0); // X
|
||||
drawLabelsForAxis(1); // Y
|
||||
} else if (plane == SketchGrid::XZ) {
|
||||
drawLabelsForAxis(0); // X
|
||||
drawLabelsForAxis(2); // Z
|
||||
} else if (plane == SketchGrid::YZ) {
|
||||
drawLabelsForAxis(1); // Y
|
||||
drawLabelsForAxis(2); // Z
|
||||
}
|
||||
drawLabelsForAxis(xDirVec);
|
||||
drawLabelsForAxis(yDirVec);
|
||||
}
|
||||
|
||||
+5
-10
@@ -12,6 +12,7 @@
|
||||
#include <QMatrix4x4>
|
||||
#include <QOpenGLVertexArrayObject>
|
||||
#include <QOpenGLBuffer>
|
||||
#include <gp_Ax2.hxx>
|
||||
|
||||
class QOpenGLShaderProgram;
|
||||
class QPainter;
|
||||
@@ -20,22 +21,16 @@ class ViewportWidget;
|
||||
class SketchGrid : protected QOpenGLFunctions
|
||||
{
|
||||
public:
|
||||
enum SketchPlane {
|
||||
XY = 1,
|
||||
XZ = 2,
|
||||
YZ = 3
|
||||
};
|
||||
|
||||
explicit SketchGrid(ViewportWidget* viewport);
|
||||
~SketchGrid();
|
||||
|
||||
void initializeGL();
|
||||
void paintGL(SketchPlane plane, QOpenGLShaderProgram* shaderProgram, int colorLoc);
|
||||
void paintAxisLabels(QPainter& painter, SketchPlane plane, const QMatrix4x4& modelView, const QMatrix4x4& projection);
|
||||
void paintGL(const gp_Ax2& plane, QOpenGLShaderProgram* shaderProgram, int colorLoc);
|
||||
void paintAxisLabels(QPainter& painter, const gp_Ax2& plane, const QMatrix4x4& modelView, const QMatrix4x4& projection);
|
||||
|
||||
private:
|
||||
void drawGridLines(SketchPlane plane, QOpenGLShaderProgram* shaderProgram, int colorLoc);
|
||||
void drawAxes(SketchPlane plane, QOpenGLShaderProgram* shaderProgram, int colorLoc);
|
||||
void drawGridLines(const gp_Ax2& plane, QOpenGLShaderProgram* shaderProgram, int colorLoc);
|
||||
void drawAxes(const gp_Ax2& plane, QOpenGLShaderProgram* shaderProgram, int colorLoc);
|
||||
|
||||
QOpenGLVertexArrayObject m_vao;
|
||||
QOpenGLBuffer m_vbo;
|
||||
|
||||
Reference in New Issue
Block a user