Sign in to follow this  
FireViper

free(TextureImage[0]) function error

Recommended Posts

When I try to compile the following code, I always get an error saying "implicit declaration of function int free(...)" What am I doing wrong? if (TextureImage[0]) // If Texture Exists { if (TextureImage[0]->data) // If Texture Image Exists { free(TextureImage[0]->data); // Free The Texture Image Memory } free(TextureImage[0]); // Free The Image Structure }

Share this post


Link to post
Share on other sites
Did you write those lines in a function or out of everything? Some days I tried to call a function before entering the winMain function and I got nearly the same error.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
QUOTE{
TextureImage[0]
}ETOUQ

Arrays are evil....Just as evil as overloading the operator[]

Share this post


Link to post
Share on other sites
ok, so if I want to laod one texture I would use the following code,

if (TextureImage[0]=LoadBMP("Pic1.bmp"))
{
Status=TRUE; // Set The Status To TRUE

glGenTextures(2, &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]) // If Texture Exists
{
if (TextureImage[0]->data) {
free(TextureImage[0]->data); }

free(TextureImage[0]); }




But when I try to add a second texture,


if (TextureImage[0]=LoadBMP("Pic2.bmp"))
{
Status=TRUE; // Set The Status To TRUE

glGenTextures(2, &texture[1]);

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



if (TextureImage[1]) // If Texture Exists
{
if (TextureImage[1]->data) {
free(TextureImage[1]->data); }

free(TextureImage[1]); }




It dosen't work

Share this post


Link to post
Share on other sites
Two errors there (possibly three).
  1. TextureImage[0]=LoadBMP("Pic1.bmp")
    ...
    glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[1]->sizeX, TextureImage[1]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[1]->data);

    You load the texture into TextureImage[0] but access it from TextureImage[1]

  2. glBindTexture(GL_TEXTURE_2D, texture[2]);
    You bind texture[2] when you probably want texture[1]. It's not necessarily an error, but it's odd.

  3. Are your arrays big enough? Both TextureImage and texture need to be big enough for at least two elements the way you are doing things.


Enigma

Share this post


Link to post
Share on other sites
OK, I fixed the array errors like you told me to, and got the program to compile. But when I try to run it, I get a message saying, "this program encountered an error and needs to be closed". Is there still something wrong with the code?



GLuint texture[2];

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc

AUX_RGBImageRec *LoadBMP(char *Filename) // Loads A Bitmap Image
{
FILE *File=NULL; // File Handle

if (!Filename) // Make Sure A Filename Was Given
{
return NULL; // If Not Return NULL
}

File=fopen(Filename,"r"); // Check To See If The File Exists

if (File) // Does The File Exist?
{
fclose(File); // Close The Handle
return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer
}

return NULL; // If Load Failed Return NULL
}

int LoadGLTextures() // Load Bitmaps And Convert To Textures
{
int Status=FALSE; // Status Indicator

AUX_RGBImageRec *TextureImage[1]; // Create Storage Space For The Texture

memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL

// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
if (TextureImage[0]=LoadBMP("Pic1.bmp"))
{
Status=TRUE; // Set The Status To TRUE

glGenTextures(1, &texture[0]); // Create The Texture

// Typical Texture Generation Using Data From The Bitmap
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[1]=LoadBMP("Pic2.bmp"))
{
Status=TRUE; // Set The Status To TRUE

glGenTextures(2, &texture[1]); // Create The Texture

// Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[1]->sizeX, TextureImage[1]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[1]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
}



if (TextureImage[0]) // If Texture Exists
{
if (TextureImage[0]->data) // If Texture Image Exists
{
free(TextureImage[0]->data); // Free The Texture Image Memory
}

free(TextureImage[0]); // Free The Image Structure
}

if (TextureImage[1]) // If Texture Exists
{
if (TextureImage[1]->data) // If Texture Image Exists
{
free(TextureImage[1]->data); // Free The Texture Image Memory
}

free(TextureImage[1]); // Free The Image Structure
}

return Status; // Return The Status
}




[Edited by - FireViper on December 10, 2004 2:11:31 PM]

Share this post


Link to post
Share on other sites
Two errors, a useless line of code and a wrong comment:
  1. AUX_RGBImageRec *TextureImage[1];
    ...
    if (TextureImage[1]=LoadBMP("Pic2.bmp"))
    ...

    The TextureImage array is not big enough.

  2. int Status=FALSE;
    ...
    if (TextureImage[0]=LoadBMP("Pic1.bmp"))
    {
    Status=TRUE;
    ...
    }
    if (TextureImage[1]=LoadBMP("Pic2.bmp"))
    {
    Status=TRUE;
    ...
    }

    What is the value of Status if Pic2.bmp loads successfully but Pic1.bmp fails to load?

  3. memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL

    // Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
    if (TextureImage[0]=LoadBMP("Pic1.bmp"))
    ...

    You memset (half) the structure to zero and then immediately assign to it. The memset call is completely redundant.

  4. GLuint texture[2]; // Storage For One Texture ( NEW )
    The comment is not only completely pointless, it's also wrong. I realise this one's from the original NeHe code, but it shows why redundant comments should not generally be used. It doesn't take much to make a liar out of them.


Enigma

Share this post


Link to post
Share on other sites
It depends on whether you're changing the texture or not.
You have to call
glBindTexture(GL_TEXTURE_2D, texture[0]);
before drawing objects that use the first texture, and
glBindTexture(GL_TEXTURE_2D, texture[1]);
before those that use the second one.

Share this post


Link to post
Share on other sites
Well If I want to create a new figure with a texture it works, but If I create a figure and color it using the glColor3f() function, it dosen't work.

Share this post


Link to post
Share on other sites
Remember that OpenGL is a state machine. When you enable texturing it is not just enabled for the next object you draw, it is enabled until you explicitly disable it. So to draw a textured object, then an untextured coloured object, then a coloured textured object in a different texture you would need to do something like:
// enable texturing - texturing is enabled, texture is unknown, colour is white
glEnable(GL_TEXTURE_2D);

// bind texture - texturing is enabled, texture is firstTexture, colour is white
glBindTexture(GL_TEXTURE_2D, firstTexture);

// render object - texturing is enabled, texture is firstTexture, colour is white
texturedObject.render();

// disable texturing - texturing is disabled, texture is firstTexture, colour is white
glDisable(GL_TEXTURE_2D);

// set colour - texturing is disabled, texture is firstTexture, colour is objectColour
glColor3fv(objectColour);

// render object - texturing is disabled, texture is firstTexture, colour is objectColour
colouredObject.render();

// enable texturing - texturing is enabled, texture is firstTexture, colour is objectColour
glEnable(GL_TEXTURE_2D);

// bind texture - texturing is enabled, texture is secondTexture, colour is objectColour
glBindTexture(GL_TEXTURE_2D, secondTexture);

// render object - texturing is enabled, texture is secondTexture, colour is objectColour
colouredTexturedObject.render();

// disable texturing - texturing is disabled, texture is secondTexture, colour is objectColour
glDisable(GL_TEXTURE_2D);

// set colour - texturing is disabled, texture is secondTexture, colour is white
glColor3f(1, 1, 1);


Enigma

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this