textures vanishing in opengl

1.2k views Asked by At

i am seeing this problem where the textures disappear after the application has been used for a minutes or two. why would the textures be disappearing? the 3d cube remains on the screen at all times. the place which the textures were appear as white boxes when the textures disappear.

my DrawGLScene method looks like this:

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
            glLoadIdentity();   // Reset The Current Modelview Matrix

            glTranslatef(0.0f, 0.0f, -7.0f);    // Translate Into The Screen 7.0 Units

//rotquad is a value that is updated as the user interacts with the ui by +/-9 to rotate the cube
        glRotatef(rotquad, 0.0f, 1.0f, 0.0f);

        //cube code here

            RECT desktop;
            const HWND hDesktop = GetDesktopWindow();
            GetWindowRect(hDesktop, &desktop);
            long horizontal = desktop.right;
            long vertical = desktop.bottom;

            glMatrixMode(GL_PROJECTION);
            glPushMatrix();
            glLoadIdentity();
            glOrtho(-5.0, 3, 3, -5.0, -1.0, 10.0);
            glMatrixMode(GL_MODELVIEW);
            glLoadIdentity();
            glDisable(GL_CULL_FACE);
            glEnable(GL_TEXTURE_2D);

            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

            glClear(GL_DEPTH_BUFFER_BIT);

            glColor4f(255.0f, 255.0f, 255.0f, 0.0f);
            if (hoverRight) {
                imageLoaderOut(outImage);
                imageLoaderIn(inImage);
                imageLoaderUp(upImage);
                imageLoaderLeft(leftHover);
                imageLoaderDown(upImage);
                imageLoaderRight(rightImage);
            }
    // code for hover left, up and down are the same as hover right code above 

        glDisable(GL_TEXTURE_2D);
        return TRUE;    // Keep Going
    }

this method is one of the imageLoad methods (others being called are almost identical, except for location/position..

        void imageLoaderOut(const char* value)
        {
            FIBITMAP* bitmap60 = FreeImage_Load(
                FreeImage_GetFileType(value, 0),
                value, PNG_DEFAULT);
            FIBITMAP *pImage60 = FreeImage_ConvertTo32Bits(bitmap60);
            int nWidth60 = FreeImage_GetWidth(pImage60);
            int nHeight60 = FreeImage_GetHeight(pImage60);
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, nWidth60, nHeight60, 0, GL_RGBA, GL_UNSIGNED_BYTE, (void*)FreeImage_GetBits(pImage60));
            FreeImage_Unload(pImage60);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            glBegin(GL_QUADS);
            glTexCoord2f(0.0f, 0.0f); glVertex2f(2.8f, -1.1f); // moves BOTTOM EDGE UP or DOWN - stretches length of image
            glTexCoord2f(0.0f, 1.0f); glVertex2f(2.8f, -1.9f);
            glTexCoord2f(1.0f, 1.0f); glVertex2f(2.1f, -1.9f);
            glTexCoord2f(1.0f, 0.0f); glVertex2f(2.1f, -1.1f); // moves BOTTOM EDGE UP or DOWN - stretches length of image
            glEnd();
        }
2

There are 2 answers

3
Ivan Aksamentov - Drop On BEST ANSWER

It's just a guess, but you have a severe design issue in your code, combined with memory leak, that can lead to such undefined results as you've described.

First, in imageLoaderOut() you are reading all the textures each frame from HDD, converting it to 32 bpp and sending data to OpenGL. You call it from DrawGLScene, which means you do it each frame. It's really invalid way to do things. You don't need to load resources each frame. Do it once and for all in some kind if Initialize() function, and just use GL resource on drawing.

Then, I think here you have memory leak, because you never unloading bitmap60. As you make loading each frame, possibly thousands times per second, this unreleased memory accumulating. So, after some time, something goes really bad and FreeImage refuses to load textures.

So, possible solution is to:

  • move resource loading to initialization phase of your application
  • free leaked resources: FreeImage_Unload(bitmap60) in each loading function

Hope it helps.

2
Amadeus On

The problem seems to be in glTexImage2D. The manual can be found here: http://www.opengl.org/sdk/docs/man/xhtml/glTexImage2D.xml

In particular, they said that:

glTexImage2D specifies the two-dimensional texture for the current texture unit, specified with glActiveTexture.

Once you are calling glTexImage2D multiple times, it seems that your are overwriting the same location multiples times.