在framebuffer中绘制纹理然后将纹理渲染到屏幕时坐标错误?

时间:2015-05-20 10:01:55

标签: opengl coordinates framebuffer

我使用opengl为像太空入侵者这样的简单游戏绘制图形。到目前为止,我让它很好地渲染移动陨石和gif文件。我得到了基础知识。但我不能让帧缓冲工作正常,我缩进以渲染位图字体。

只有当分数改变时,才会在渲染函数内调用第一个函数。这将产生一个包含分数字符的纹理。每次调用渲染函数时,第二个函数都会将包含位图字体字符的纹理绘制到屏幕上。我认为这将是一种更有效的方式来绘制分数。现在我只是试图让它使用frameBuffer绘制一个正方形,但看起来坐标的范围从-1到0.我认为纹理的坐标从0到1?我评论了哪个顶点效果广场的哪个角落似乎是错误的。

void Score::UpdateScoreTexture(int* success)
    {
        int length = 8;
        char* chars = LongToNumberDigits(count, &length, 0);

        glDeleteTextures(1, &textureScore);//last texture containing previous score deleted to make room for new score
        glGenTextures(1, &textureScore);
        GLuint frameBufferScore;
        glGenTextures(1, &textureScore);
        glBindTexture(GL_TEXTURE_2D, textureScore);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
        glGenFramebuffers(1, &frameBufferScore);
        glBindFramebuffer(GL_FRAMEBUFFER, frameBufferScore);
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureScore, 0);
        GLenum status;
        status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
        std::cout << "status is: ";
        std::cout << "\n";
switch (status)
        {
        case         GL_FRAMEBUFFER_COMPLETE:
            std::cout << "good";
            break;
        default:
            PrintGLStatus(status);
            while (1 == 1);
        }
        glBindFramebuffer(GL_FRAMEBUFFER, frameBufferScore);

        glBegin(GL_POLYGON);
        glVertex3f(-1, -1, 0.0);//appears to be the bottom left, 
        glVertex3f(0, -1, 0.0);//appears to be the bottom right
        glVertex3f(0, 0, 0.0);//appears to be the top right
        glVertex3f(-1, 0, 0.0);//appears to be the top left
        glEnd();
        glDisable(GL_TEXTURE_2D);
        glBindFramebuffer(GL_FRAMEBUFFER, 0);
        glBindTexture(GL_TEXTURE_2D,0);
        glDeleteFramebuffers(1, &frameBufferScore);














        //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, chars2);

    }
    void Score::DrawScore(void)
    {
        glEnable(GL_TEXTURE_2D);
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
        glBindTexture(GL_TEXTURE_2D, textureScore);
        glBegin(GL_QUADS);
        glTexCoord2f(0.0, 0.0); glVertex3f(0.7, 0.925, 0.0);
        glTexCoord2f(0.0, 1.0); glVertex3f(0.7, 0.975, 0.0);
        glTexCoord2f(1.0, 1.0); glVertex3f(0.975, 0.975, 0.0);
        glTexCoord2f(1.0, 0.0); glVertex3f(0.975, 0.925, 0.0);
        glEnd();
        glFlush();
        glDisable(GL_TEXTURE_2D);


    }

我出错的任何想法?

1 个答案:

答案 0 :(得分:1)

你还没有设置glViewport,这可能会给你带来麻烦。

另一种可能性是你将矩阵设置为身份以外的东西。 确保在UpdateScoreTexture()中的glBegin(GL_POLYGON)之前将模型视图和投影矩阵重置为标识(或者您希望它们是什么)(您可能希望在进行更改之前将矩阵推入堆栈):< BR />

glViewport(0,0, framebufferWidth, framebufferHeight)
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glLoadIdentity()
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glLoadIdentity()

然后将它们放回函数的末尾:

glViewport(0,0, width, height)
glMatrixMode(GL_PROJECTION)
glPopMatrix()
glMatrixMode(GL_MODELVIEW)
glPopMatrix()