OpenGL视口失真 [英] OpenGL viewport distortion

查看:215
本文介绍了OpenGL视口失真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还是OpenGL的初学者。我试图使用着色器在屏幕尺寸1280×720上绘制一个完美的正方形。

I'm still a beginner in OpenGL. I'm trying to draw a perfect square on the screen size 1280 by 720 using shaders.

我使用OpenGL核心配置文件,版本3.3。
当我尝试在1280年由720绘制一个正方形时,我被困住了

I'm using OpenGL core profile, version 3.3. I was stuck with this when I tried to draw a square in 1280 by 720

经过一段时间的搜索,我意识到尺寸被视口大小扭曲,将视口尺寸更改为720×720后,我得到了这个。

After some time of searching, I realized that the size is distort by the viewport size, after changing the viewport size to 720 by 720, I got this.

在旧版OpenGL中, a href =http://stackoverflow.com/questions/10891977/aspect-ratio-stretching-in-opengl>解决方案来修复此问题,但现在已在Core配置文件中弃用。

In the legacy OpenGL, they have a solution to fix this, but now it's deprecated in the Core profile.

问题:如何在1280 x 720屏幕尺寸上绘制一个完美的正方形,仅使用核心配置文件OpenGL 3.3?

Issue: How can I draw a perfect square in 1280 x 720 screensize, using only core profile, OpenGL 3.3?

推荐答案

应用投影变换是解决这个问题的最标准的方法。你可以在几乎任何教程中找到如何使用OpenGL和着色器的详细信息。总之,通常在顶点着色器中有一个投影矩阵作为一个统一变量,并将其应用到输入位置:

Applying a projection transformation is the most standard way of solving this. You can find details on how to do that in almost any tutorial that shows how to use OpenGL with shaders. In summary, you typically have a projection matrix as a uniform variable in your vertex shader, and apply it to the input positions:

uniform mat4 ProjMat;
in vec4 InPosition;
...
    gl_Position = ProjMat * InPosition;

在您的C ++代码中,计算矩阵应用必要的缩放, c $ c> glUniformMatrix4fv()。

In your C++ code, you calculate the matrix to apply the necessary scaling, and set it using glUniformMatrix4fv().

另一个选择是保留视口正方形。虽然视口经常设置为与窗口大小匹配,但这不会具有。例如,视口可以扩展到窗口的大小之外。

Another option is that you keep the viewport square. While the viewport is often set to match the size of the window, this does not have to be the case. For example, the viewport can extend beyond the size of the window.

要使用这种方法,你应该在代码中设置视口响应到窗口(重新)大小事件。使用 w h 窗口的宽度和高度:

To use this approach, you would have something like this in the code where you set the viewport in response to window (re-)size events. With w and h the width and height of the window:

if (w > h) {
    glViewport(0, (h - w) / 2, w, w);
} else {
    glViewport((w - h) / 2, 0, h, h);
}

这需要两个窗口尺寸中较大的一个作为视口大小,在其他维度的窗口之外的视口产生一个正方形视口。

This takes the larger of the two window dimensions as the viewport size, and extends the viewport beyond the window in the other dimension to produce a square viewport.

这篇关于OpenGL视口失真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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