gpt4 book ai didi

opengl - 在 OpenGL 中调整大小时保持纵横比

转载 作者:行者123 更新时间:2023-12-01 05:21:21 26 4
gpt4 key购买 nike

我实现了以下代码:

void TestGlPlot::resizeGL(int width, int height) 
{
setupViewport(width, height);
}

void TestGlPlot::setupViewport(int width, int height)
{
/* Prevent divide by zero --------------------------------------------------------------------*/
if (height == 0) height = 1;
/* Calculate aspect ratio --------------------------------------------------------------------*/
float aspectRatio = (float)width / (float)height;

/* Set viewport to cover the window ----------------------------------------------------------*/
glViewport(0, 0, width, height);

/* Set aspect ratio --------------------------------------------------------------------------*/
glMatrixMode(GL_PROJECTION); /* switch to projection matrix */
glLoadIdentity();
/*
if (width >= height)
{
gluOrtho2D(-0.5*aspectRatio, 0.5*aspectRatio, 0.0, 1.0);
}
else
{
gluOrtho2D(-0.5, 0.5, 0.0*aspectRatio, 1.0*aspectRatio);
}
glMatrixMode(GL_MODELVIEW);
*/
gluOrtho2D(-1, 1, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}

void TestGlPlot::paintEvent(QPaintEvent *event) {
makeCurrent();
setupViewport(width(), height());

glMatrixMode(GL_MODELVIEW);

/* Set white background ----------------------------------------------------------------------*/
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(255,255,255,0);

/* Paint OpenGL events -----------------------------------------------------------------------*/
glColor4f(1.0, 0.0, 0.0, 1.0);

/* light grey */
glColor4f(0.0, 0.0, 0.0, 0.3);
gluPartialDisk(plainQuad, 0.1, 1, 20, 4, -60, 120);

/* Paint QPainter events ---------------------------------------------------------------------*/
QPainter painter(this);

/* Draw grey border around plot --------------------------------------------------------------*/
painter.setPen(QColor("#808080"));
painter.drawRect(0, 0, width()-1, height()-1);

painter.setFont(font);

/* Translate coordinate system to (0,0) center -----------------------------------------------*/
QMatrix m;
m.translate(width()*0.5, height()*0.5);
painter.setMatrix(m);

/* Left side descriptions for radius ---------------------------------------------------------*/
painter.drawText(-0.17*width(), 0.38*height(), tr("100m"));
painter.drawText(-0.27*width(), 0.28*height(), tr("200m"));
painter.drawText(-0.37*width(), 0.18*height(), tr("300m"));
painter.drawText(-0.47*width(), 0.08*height(), tr("400m"));

painter.drawText(0.45*width(), -0.01*height(), tr("60°"));
painter.drawText(0.26*width(), -0.38*height(), tr("30°"));

painter.drawText(-0.47*width(), -0.01*height(), tr("300°"));
painter.drawText(-0.28*width(), -0.38*height(), tr("330°"));

painter.end();
}

我尝试了不同的方法来调整大小处理(保持 partialDisk 对象的形状而不拉伸(stretch)它),但每种方法都失败了。我还想保留单位圆的坐标处理(这样我就可以标准化我的测量并将它们绘制成极坐标图)。

最佳答案

为了保持纵横比,您通常有几种选择:

  • 始终缩放到一维。例如,您只需定义您希望始终看到水平范围 [-0.5,0.5]。在这种情况下,您必须按因子 (1/aspect_viewport) 校正垂直范围。
  • 使用一些等效的信箱。所以你定义了一个“感兴趣的区域”,你总是想完整地看到它,但是你可能会看到更多的宽度或高度,这取决于窗口的纵横比(这基本上相当于观看带有信箱的电影时的黑条)。有两种情况需要考虑:视口(viewport)的方面大于您所在区域的方面,因此它更宽。在这种情况下,您应该映射整个高度并将水平范围增加 (aspect_viewport/aspect_region) 的一个因子。否则,窗口的纵横比低于您所在区域的纵横比,因此您应该使用全宽并将垂直范围扩大 (aspect_region/aspect_viewport)。请注意,在这两种情况下,因子都 >= 1。

  • 在您的代码中,您几乎已经实现了方法 2,但是您的 aspectRatio < 1 case 错误,应该是
       if (width >= height)
    gluOrtho2D(-0.5f*aspectRatio, 0.5f*aspectRatio, 0.0f, 1.0f);
    else
    gluOrtho2D(-0.5f, 0.5f, 0.0, 1.0/aspectRatio);

    关于opengl - 在 OpenGL 中调整大小时保持纵横比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16498784/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com