【问题标题】:Tying to create a background in OpenGL在 OpenGL 中创建背景
【发布时间】:2020-07-04 11:04:58
【问题描述】:

我正在尝试在 OpenGL 游戏中创建背景。这是基于 freeglut 和 OpenGL 即时模式(由于要求)。我的问题是我无法正确显示背景图像,它显示为与我的相机一起旋转的细条。这让我相信这是一个定位问题,但我已尝试更改对象的所有 Z 位置。下面是我要绘制背景的渲染函数,然后是播放器“mSpaceShip”

对于某些上下文 屏幕宽度 = 1200 屏幕高度 = 1200

void GameInstance::Render()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    gluOrtho2D(-SCREEN_WIDTH / 2, SCREEN_WIDTH / 2, -SCREEN_HEIGHT / 2, SCREEN_HEIGHT / 2);
    glMatrixMode(GL_MODELVIEW);

    glPushMatrix();
    glTranslatef(0.0f, 0.0f, 100.0f);
    glBegin(GL_QUADS);
    glVertex2i(0, 0);
    glVertex2i(SCREEN_WIDTH, 0);
    glVertex2i(SCREEN_WIDTH, SCREEN_HEIGHT);
    glVertex2i(0, SCREEN_HEIGHT);
    glEnd();
    glPopMatrix();

    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);



    mSpaceShip->Render();

    //for (int i = 0; i < 500; i++) {
    //  cubeField[i]->Draw();
    //}

    glFlush();
    glutSwapBuffers();
}

【问题讨论】:

    标签: c++ opengl freeglut opengl-compat


    【解决方案1】:

    尽量不要让自己的事情过于复杂。

    void GameInstance::Render()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        // the default matricies set the origin to be at the center of the screen, with
        // (-1, -1) being at the bottom-left corner, and
        // ( 1,  1) being at the top-right.
    
        glMatrixMode(GL_PROJECTION);
        glPushMatrix();
        glLoadIdentity();
        glMatrixMode(GL_MODELVIEW);
        glPushMatrix();
        glLoadIdentity();
    
        // render a full-screen quad without writing to the depth buffer
    
        glDisable(GL_DEPTH_TEST);
        glBegin(GL_QUADS);
            glVertex2i(-1, -1);
            glVertex2i( 1, -1);
            glVertex2i( 1,  1);
            glVertex2i(-1,  1);
        glEnd();
        glEnable(GL_DEPTH_TEST);
    
        glMatrixMode(GL_PROJECTION);
        glPopMatrix();
        glMatrixMode(GL_MODELVIEW);
        glPopMatrix();
    
        mSpaceShip->Render();
    
        glFlush();
        glutSwapBuffers();
    }
    

    在您对gluOrtho2D 的调用中,您将原点设置在屏幕中心,但仅在右上象限内渲染四边形(但当您在外部对GL_MODELVIEW 进行更改时Render(),这只是猜测)。

    使用gluOrtho2D 相当于调用glOrtho 并将z 裁剪范围设置为[-1 .. 1],所以我怀疑你调用glTranslatef 需要更小的z 值,例如0.5f 而不是100.0f.

    【讨论】:

      猜你喜欢
      • 2011-03-21
      • 1970-01-01
      • 1970-01-01
      • 2012-03-07
      • 1970-01-01
      • 1970-01-01
      • 2022-12-26
      • 2011-06-27
      • 2014-01-10
      相关资源
      最近更新 更多