0

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();
        }
genpfault
  • 51,148
  • 11
  • 85
  • 139
BigBug
  • 6,202
  • 23
  • 87
  • 138
  • 1
    Why are you loading texture image (unconditionally) in the drawing code? You should not do that. Load textures only once in a one-time initialization function or use a conditional in the drawing code if a texture needs updating. – datenwolf Dec 02 '13 at 17:53

2 Answers2

2

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.

Amadeus
  • 10,199
  • 3
  • 25
  • 31
  • 1
    In terms of API, updating texture data this way is perfectly valid even if you writing same data in same location each frame. Of course, writing data in this manner is redundant and is a totally waste of resources, but unlikely it can lead to "texture vanish after the application has been used for a minutes or two"/ So I think it is not a problem here. – Ivan Aksamentov - Drop Dec 03 '13 at 03:07
  • @Drop The problem is that he is updating differents images to the same context. If textures are from diferents sizes, it is probable that some portions of an images can vanish or be wrong. – Amadeus Dec 03 '13 at 16:06
2

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.

Ivan Aksamentov - Drop
  • 12,860
  • 3
  • 34
  • 61
  • thanks... i don't know much about opengl first time using it.. so it's a big confusing at first. – BigBug Dec 02 '13 at 23:12
  • @BlueMonster Its always OK to try and fail or try and succeed. Just curious, does my guess was right? Did you fixed textures vanishing? – Ivan Aksamentov - Drop Dec 03 '13 at 02:59
  • Yeah, you were completely right. I shouldn't have been reading the image in every single time. storing the images as resources using a GLUint solved the problem. thank you so much! – BigBug Dec 03 '13 at 21:50