简体   繁体   English

OpenGL-如何在调整大小时适合窗口

[英]OpenGL - how to fit a window on resize

I have a shape that is centered on the window screen. 我的形状位于窗口屏幕的中央。 For now on window resize it remains the same (same width and height). 现在,在窗口调整大小上,它保持不变(宽度和高度相同)。 What is the best way to fit it to the window on resize, and also keep it's aspect ratio? 在调整大小时使其适应窗口并保持其纵横比的最佳方法是什么?

const int WIDTH = 490;
const int HEIGHT = 600;

/**
* render scene
*/
void renderScene(void)
{
    //clear all data on scene
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    //set white bg color
    glClearColor(1, 1, 1, 1);

    glColor3ub(153, 153, 255);
    glBegin(GL_POLYGON);
        glVertex2f(60, 310);
        glVertex2f(245, 50);
        glVertex2f(430, 310);
        glVertex2f(245, 530);
    glEnd();

    glutSwapBuffers();
}

/**
* reshape scene
*/
void reshapeScene(GLint width, GLint height)
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glViewport(0, 0, width, height);
    gluOrtho2D(0, width, height, 0);
    glMatrixMode(GL_MODELVIEW);

    glutPostRedisplay();
}

/**
* entry point
*/
int main(int argc, char **argv)
{
    //set window properties
    glutInit(&argc, argv);  

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
    glutInitWindowSize(WIDTH, HEIGHT);
    glutInitWindowPosition((glutGet(GLUT_SCREEN_WIDTH)-WIDTH)/2, (glutGet(GLUT_SCREEN_HEIGHT)-HEIGHT)/2);
    glutCreateWindow("Test Window");

    //paste opengl version in console
    printf((const char*)glGetString(GL_VERSION));

    //register callbacks
    glutDisplayFunc(renderScene);
    glutReshapeFunc(reshapeScene);

    //start animation
    glutMainLoop();

    return 0;
}

Thanks 谢谢

At the top: 在顶部:

const int WIDTH = 490;
const int HEIGHT = 600;
const float ASPECT = float(WIDTH)/HEIGHT;   // desired aspect ratio

Then reshapeScene : 然后reshapeScene

void reshapeScene(GLint width, GLint height)
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    int w = height * ASPECT;           // w is width adjusted for aspect ratio
    int left = (width - w) / 2;
    glViewport(left, 0, w, height);       // fix up the viewport to maintain aspect ratio
    gluOrtho2D(0, WIDTH, HEIGHT, 0);   // only the window is changing, not the camera
    glMatrixMode(GL_MODELVIEW);

    glutPostRedisplay();
}

Because you are not constraining the window resize to a particular aspect ratio, you need to pick one of the two dimensions (height, here) to drive the viewport reshape and calculate an adjusted width based on that height and the desired aspect ratio. 因为您没有将窗口调整大小限制为特定的宽高比,所以需要选择两个尺寸之一(此处为高度)来驱动视口调整形状,并根据该高度和所需的宽高比计算调整后的宽度。 See adjusted glViewport call above. 请参阅上面的已调整glViewport调用。

Also, gluOrtho2D is your camera, essentially. 同样, gluOrtho2D就是您的相机。 Since you aren't moving the camera or the object so much, you don't need to change that (so it just uses the initial WIDTH and HEIGHT ). 由于您不需要移动太多的相机或对象,因此不需要更改它(因此它只使用初始的WIDTHHEIGHT )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM