【问题标题】:OpenGL - Maximizing / minimizing the output window resizes object inside. How can I avoid this?OpenGL - 最大化/最小化输出窗口调整内部对象的大小。我怎样才能避免这种情况?
【发布时间】:2020-05-26 00:16:50
【问题描述】:

如何确保输出窗口是固定的,或者每次在 OpenGL 中最大化/最小化窗口时,窗口中的圆形输出不会改变形状。我在 Visual Studio 2019 上使用 c++(意味着已经设置了 glew 和 glut 依赖项。)我正在尝试使用中点绘图算法(半径 5cm,起点坐标(0,3))创建一个圆并确保它旋转 90 度之后。

请注意,屏幕截图显示最小化窗口后圆形变为椭圆形。Screenshot link...

代码:

    #include <Windows.h>
#include <GL\glew.h>
#include <GL\freeglut.h>
#include <iostream>

using namespace std;

int xForRotation = 000;
int yForRotation = 120;
int rFlag;
double theta = 0.0;

void myinit(void)
{
    glClearColor(1, 1, 1, 0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glutInitWindowSize(1000,1000);
    gluOrtho2D(-1*640.0, 640.0, -1*640.0, 640.0);
    glMatrixMode(GL_MODELVIEW);
}
void circleMidPoint()
{
    int xCenter = 000;
    int yCenter = 240; //3cm
    int radius = 400; //5cm

    int x = 0;
    int y = radius;
    int p = 1 - radius;//5/4 is rounded to 1 for integer radius

    while (x < y) {// iterates to draw the first sector
        x++;
        if (p < 0)// the mid point is inside the circle
            p += 2 * x + 1;
        else {// the mid point is outside or at the circle
            y--;
            p += 2 * (x - y) + 1;
        }
        glBegin(GL_POINTS);
        glVertex2i(xCenter + x, yCenter + y);
        glVertex2i(xCenter - x, yCenter + y);
        glVertex2i(xCenter + x, yCenter - y);
        glVertex2i(xCenter - x, yCenter - y);
        glVertex2i(xCenter + y, yCenter + x);
        glVertex2i(xCenter - y, yCenter + x);
        glVertex2i(xCenter + y, yCenter - x);
        glVertex2i(xCenter - y, yCenter - x);
        glEnd();
    }
    // OPTIONAL:-> center of the circle
    glBegin(GL_POINTS);
    glVertex2i(xCenter, yCenter);
    glEnd();
}
void display()
{

    glClear(GL_COLOR_BUFFER_BIT);     // clear the screen  
    glColor3f(1.0, 0.0, 0.0);          // red foreground
    glPointSize(5.0);// size of points to be drawin (in pixel)

    //establish a coordinate system for the image

    circleMidPoint();
    glFlush(); // send all output to the display

    glLoadIdentity();

        if (rFlag == 1) //Rotate the circle around fixed point
            {
                yForRotation;
                xForRotation;
                theta = 0.25;
            }
        glTranslatef(xForRotation, yForRotation, 0.0);
        glRotatef(theta, 0.0, 0.0, 1.0);
        glTranslatef(-xForRotation, -yForRotation, 0.0);
        glutPostRedisplay();
        glutSwapBuffers();


}

void rotateMenu(int option){
    if (option == 1)
        rFlag = 1;
}

int main(int argc, char** argv){
    glutInit(&argc, argv);
    glutInitWindowSize(640, 480); // set the size of the window
    glutInitWindowPosition(10, 10); // the position of the top-left of window
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutCreateWindow("Midpoint Circle Drawing Algorithm. *Right CLick to Rotate");
    myinit();
    glutDisplayFunc(display); // set the gl display callback function
    glutCreateMenu(rotateMenu);
    glutAddMenuEntry("Rotate Circle", 1);
    glutAttachMenu(GLUT_RIGHT_BUTTON);
    glutMainLoop(); // enter the GL event loop
}

【问题讨论】:

  • 调整大小后是否更新视口 (glViewport) 和投影?
  • 不,我没有。它是如何使用的?

标签: c++ visual-studio opengl graphics


【解决方案1】:

反映窗口的新大小,你必须添加一个glutReshapeFunc 回调。在回调中,您必须将视口矩形调整为glViewport 的新大小。 此外,您还必须使正交投影适应新的窗口大小。

必须在创建窗口之前设置窗口大小。以后调用glutInitWindowSize 无效。

由于正射投影设置在reshape,所以不需要设置在myinit

void myinit(void)
{
    glClearColor(1, 1, 1, 0);
}

void reshape(int width, int height) {
    glViewport(0, 0, width, height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-width, width, -height, height);
    glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv) {
    // [...]

    glutInitWindowSize(1000, 1000);
    glutCreateWindow("Midpoint Circle Drawing Algorithm. *Right CLick to Rotate");

    // [...]

    glutReshapeFunc(reshape);

    // [...]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多