-1

I'm using opengl to draw the graphics for simple game like space invaders. So far I have it rendering moving meteorites and a gif file quite nicely. I get the basics. But I just cant get the framebuffer working properly which I indent to render bitmap font to.

The first function will be called inside the render function only when the score changes, This will produce a texture containing the score characters. The second function will draw the texture containing the bitmap font characters to the screen every time the render function is called. I thought this would be a more efficient way to draw the score. Right now I'm just trying to get it drawing a square using the frameBuffer, but it seems that the coordinates range from -1 to 0. I thought the coordinates for a texture went from 0 to 1? I commented which vertex effects which corner of the square and it seems to be wrong.

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);


    }

Any ideas where I'm going wrong?

genpfault
  • 51,148
  • 11
  • 85
  • 139

1 Answers1

1

You have not set the glViewport, this may give you problems.

Another possibility is that you have the matrix set to something other than identity. Ensure that you have reset the model-view and projection matrices to identity (or what you want them to be) before glBegin(GL_POLYGON) in UpdateScoreTexture() (You may wish to push the matrices to the stack before you make changes):

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

Then put them back at the end of the function:

glViewport(0,0, width, height)
glMatrixMode(GL_PROJECTION)
glPopMatrix()
glMatrixMode(GL_MODELVIEW)
glPopMatrix()
OFE
  • 366
  • 3
  • 5