【问题标题】:Objects become stretched in full screen对象全屏拉伸
【发布时间】:2018-12-30 12:24:23
【问题描述】:

我遇到的问题是,当我使用全屏时,我的对象会被拉伸。我希望它适合全屏坐标,因此它看起来与图像 A 完全相同。我知道glViewport 确定 OpenGL 绘制到的窗口部分,它可以帮助将我的对象设置为整个窗口.但是,我没有使用glViewport,而是使用gluOrtho2D

Click here to see the full code

图像 A(屏幕尺寸:700、600)

图片 B(全屏尺寸)

gluOrtho2D 代码

// this is the initialisation function, called once only
void init() {
    glClearColor(0.0, 0.0, 0.0, 0.0); // set what colour you want the background to be
    glMatrixMode(GL_PROJECTION); // set the matrix mode
    gluOrtho2D(0.0, winWidth, 0.0, winHeight); // set the projection window size in x and y.
}

我原来用的是gluOrtho2D,是用来设置一个二维的正交可视区域的。

【问题讨论】:

  • 假设您想要固定比率,您必须执行以下操作:float ratioXY = 700.0f/600.0f; ... gluOrtho2D(0.0, ratioXY*winHeight, 0.0, winHeight);。将ratioXY 设置为适合您的任何内容。请记住,如果全屏尺寸与比例不匹配,这将裁剪您的图像。

标签: c++ opengl glut


【解决方案1】:

glViewport 定义渲染到的(默认)帧缓冲区的区域。

如果你不想渲染到全屏,那么你可以缩小渲染到的区域,glViewport。你可以在边框上放一些黑色条纹。

您的应用程序的纵横比是 winWidth : winHeight

使用glutGet,分别使用参数GLUT_WINDOW_WIDTHGLUT_WINDOW_HEIGHT可以得到当前窗口的大小,可以计算出当前的纵横比:

int currWidth = glutGet( GLUT_WINDOW_WIDTH );
int currHeight = glutGet( GLUT_WINDOW_HEIGHT );

float window_aspcet = (float)currWidth / (float)currHeight;

有了这些信息,视图可以完美地居中于视口:

void display() {

    float app_aspcet = (float)winWidth / (float)winHeight;

    int currWidth = glutGet( GLUT_WINDOW_WIDTH );
    int currHeight = glutGet( GLUT_WINDOW_HEIGHT );

    float window_aspcet = (float)currWidth / (float)currHeight;

    if ( window_aspcet > app_aspcet )
    {
        int width = (int)((float)currWidth * app_aspcet / window_aspcet + 0.5f);
        glViewport((currWidth - width) / 2, 0, width, currHeight);
    }
    else
    {
        int height = (int)((float)currHeight * window_aspcet / app_aspcet + 0.5f);
        glViewport(0, (currHeight - height) / 2, currWidth, height);
    }

    // [...]

}

或者你可以熟练使用正投影的纵横比和中心

void display() {

    float app_aspcet = (float)winWidth / (float)winHeight;

    int currWidth = glutGet( GLUT_WINDOW_WIDTH );
    int currHeight = glutGet( GLUT_WINDOW_HEIGHT );

    float window_aspcet = (float)currWidth / (float)currHeight;

    glViewport(0, 0, currWidth, currHeight);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if ( window_aspcet > app_aspcet )
    {
        float delta_width = (float)currWidth * (float)winHeight / (float)currHeight - (float)winWidth;
        gluOrtho2D(-delta_width/2.0f, (float)winWidth + delta_width/2.0f, 0.0, (float)winHeight);    
    }
    else
    {
        float delta_height = (float)currHeight * (float)winWidth / (float)currWidth - (float)winHeight;
        gluOrtho2D(0.0, (float)winWidth, -delta_height/2.0f, (float)winHeight + delta_height/2.0f);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-20
    • 2019-12-03
    • 2013-01-26
    相关资源
    最近更新 更多