Advertisement

free(TextureImage[0]) function error

Started by December 02, 2004 03:51 PM
14 comments, last by Enigma 19 years, 11 months ago
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 WndProcAUX_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]
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
Advertisement
The progarm is working, thanks Enigma. But now every thing in the screen has that same tetxure. is that suppost to happen?
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.
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.
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 whiteglEnable(GL_TEXTURE_2D);// bind texture - texturing is enabled, texture is firstTexture, colour is whiteglBindTexture(GL_TEXTURE_2D, firstTexture);// render object - texturing is enabled, texture is firstTexture, colour is whitetexturedObject.render();// disable texturing - texturing is disabled, texture is firstTexture, colour is whiteglDisable(GL_TEXTURE_2D);// set colour - texturing is disabled, texture is firstTexture, colour is objectColourglColor3fv(objectColour);// render object - texturing is disabled, texture is firstTexture, colour is objectColourcolouredObject.render();// enable texturing - texturing is enabled, texture is firstTexture, colour is objectColourglEnable(GL_TEXTURE_2D);// bind texture - texturing is enabled, texture is secondTexture, colour is objectColourglBindTexture(GL_TEXTURE_2D, secondTexture);// render object - texturing is enabled, texture is secondTexture, colour is objectColourcolouredTexturedObject.render();// disable texturing - texturing is disabled, texture is secondTexture, colour is objectColourglDisable(GL_TEXTURE_2D);// set colour - texturing is disabled, texture is secondTexture, colour is whiteglColor3f(1, 1, 1);


Enigma

This topic is closed to new replies.

Advertisement