Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL Texture Mapping Error

Tags:

c++

opengl

Here's a BIG problem with my project:

I love the tutorials on the NeHe website, and Windows XP ran the programs perfectly. However, when I reformatted my computer, changed the OS to Windows Vista and reinstalled my Dev-C++ compiler, and then I tried to open any C++ program that used textures, the program crashed.

I realised my glaux.h went missing. I found the file on the Internet and recompiled my project, but it still crashed. Everything went well when I excluded the texture functions.

Where does the problem lie and what can I do to solve it?

I was thinking one of these is the culprit: Windows Vista OS, my graphics card, glaux.h and libraries (I know it's bugged), OpenGL itself.

.

Update: I determined the source of the problem.

This chunk of code caused my program to crash:

if (TextureImage[0]) {
    if (TextureImage[0]->data) {    
    free(TextureImage[0]->data);
    }
    free(TextureImage[0]);
}

For some reason, my program always crashes whenever I order it to free memory. When I commented this section off, my program works fine, except that all the colours went dark (I'm thinking it's because of the colours of my bitmap file). Any tips?

.

Reply to: Matias Valdenegro

Well, this was derived from NeHe lesson6 which worked fine when I still used Windows XP. Absolutely nothing was changed when I switched to Windows Vista.

Just so you know, here's the entire function:

#define NoOfTextures 3
GLuint  texture[NoOfTextures];

int LoadGLTextures()
{
    int Status=FALSE;

    AUX_RGBImageRec *TextureImage[NoOfTextures];

    memset(TextureImage,0,sizeof(void *)*1);

    if (TextureImage[0]=LoadBMP("Data/Bitmaps/texture.bmp"))
    {
        Status=TRUE;

        glGenTextures(1, &texture[0]);

        glBindTexture(GL_TEXTURE_2D, texture[0]);
        glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

        if (TextureImage[0] != NULL) {  
            if (TextureImage[0]->data != NULL) {    
                free(TextureImage[0]->data);
            }
            free(TextureImage[0]);  
        }
    }

    return Status;                                      
}

.

Additional Information:

I rebuild my project often and LoadBMP() is part of the same header file. This is the LoadBMP() function:

AUX_RGBImageRec *LoadBMP(char *Filename)
{
    FILE *File=NULL;

    if (!Filename)
    {
        return NULL;
    }

    File=fopen(Filename,"r");

    if (File)
    {
        fclose(File);
        return auxDIBImageLoad(Filename);
    }

    return NULL;
}

This seems pretty clear cut to me.

like image 822
EoS Avatar asked Mar 09 '26 18:03

EoS


1 Answers

As it's crashing in a free call, check

  • That you're not double freeing pointers.
  • That you have valid pointers anywhere. This means free pointers allocated using malloc, you can't free a pointer obtained with new. Also, you must free the original pointer value you got from the allocation function, not the pointer + 1 or some arithmetic on it.
like image 110
Dr. Snoopy Avatar answered Mar 12 '26 06:03

Dr. Snoopy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!