OpenGL中的宽高比拉伸 [英] Aspect Ratio Stretching in OpenGL

查看:634
本文介绍了OpenGL中的宽高比拉伸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在全屏模式下遇到了一些麻烦.我可以将窗口设置为800x600,但是当我全屏显示该分辨率时,它就会拉伸.我认为这是由于宽高比的变化.我该如何解决?

I am having some trouble with full screen mode. I can set my window to be 800x600, but when I full screen that resolution, it stretches. I assume this is because of a change in aspect ratio. How can I fix this?

编辑#1

这是我所看到的事情的屏幕截图.

Here's a screen shot of what I see happening.

左:800x600

右:1366x768

Right: 1366x768

编辑#2

每次调整窗口大小(WM_SIZE)时都会调用initGraphics函数.

My initGraphics function gets called every time I re-size the window (WM_SIZE).

void initGraphics(int width, int height) {
    float aspect = (float)width / (float)height;
    glViewport(0, 0, width, height);
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND); //Enable alpha blending
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, width, height * aspect, 0.0);
    glMatrixMode(GL_MODELVIEW);
}

推荐答案

解决方案: 真正的问题最终是您滥用了gluOrtho2D函数.而不是使用这个:

SOLUTION: The real problem ended up being that you were misusing the gluOrtho2D function. Instead of using this:

gluOrtho2D(0.0, width, height * aspect, 0.0);

您需要将其切换为正确的格式:

You needed to switch it to the correct form this:

gluOrtho2D(0.0, width, 0.0, height);

后者会创建2D正交投影,该投影会填满视口的整个宽度和高度,因此不会发生拉伸.

The latter creates a 2D orthographic projection, that fills the entire width and height of your viewport, so no stretching occurs.

原始答案:

您需要修改投影以考虑新的宽高比.

You need to modify your projection in order to account for the new aspect ratio.

确保首先将glViewport设置为新窗口大小.设置视口后,您需要通过调用glMatrixMode将矩阵模式切换为投影,然后最终使用width / height计算新的宽高比并将新的宽高比传递给gluPerspective.您也可以使用直接的glFrustum而不是gluPerspective,您可以找到gluPerspective的源,以实现与glFrustum相同的效果.

Make sure you first of all set glViewport to the new window size. After the viewport is set you will need to switch your matrix mode to projection with a call to glMatrixMode and then finally calculate your new aspect ratio with width / height and pass the new aspect ratio to gluPerspective. You can also use straight glFrustum instead of gluPerspective you can find source to gluPerspective to achieve that same effect with glFrustum.

类似这样的东西:

    float aspectRatio = width / height;
    glMatrixMode(GL_PROJECTION_MATRIX);
    glLoadIdentity();
    gluPerspective(fov, aspectRatio, near, far); 

这篇关于OpenGL中的宽高比拉伸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆