Multiple textures in OpenGL...

This is a discussion on Multiple textures in OpenGL... within the Game Programming forums, part of the General Programming Boards category; I'm trying to adjust NeHe's OpenGL texturing code to easily enable multiple textures within my program but nothing seems to ...

  1. #1
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204

    Thumbs up Multiple textures in OpenGL...

    I'm trying to adjust NeHe's OpenGL texturing code to easily enable multiple textures within my program but nothing seems to work. This is my attempt:

    Code:
    GLuint	texture[2]; //adjust the number to the amount of textures
    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)
    	{
    		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
        
        int texsiz=sizeof(texture)/sizeof(texture[0]);
    	AUX_RGBImageRec *TextureImage[texsiz];
    
    	memset(TextureImage,0,sizeof(void *)*texsiz);
    
    	// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
    	if (  (TextureImage[0]=LoadBMP("test1.bmp")) //add directories here
    	    &&(TextureImage[1]=LoadBMP("test2.bmp"))
            ){
    		Status=TRUE;									// Set The Status To TRUE
    
    		glGenTextures(texsiz, &texture[0]);					// Create The Texture
    
    		for (int loop=0; loop<texsiz; loop++) // Loop Through All The Textures
            {
                glBindTexture(GL_TEXTURE_2D, texture[loop]);
                glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
                glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
                glTexImage2D(GL_TEXTURE_2D,0,3,TextureImage[loop]->sizeX,TextureImage[loop]->sizeY,0,GL_RGB,GL_UNSIGNED_BYTE,TextureImage[loop]->data);
            }
    	}
    
    	for (int loop=0; loop<texsiz; loop++)
                   {
            if (TextureImage[loop]) // If Texture Exists
            {
                if (TextureImage[loop]->data) // If Texture Image Exists
                    free(TextureImage[loop]->data); // Free The Texture Image Memory
                free(TextureImage[loop]); // Free The Image Structure
            }
        }
    	return Status;					
    }
    Thanks.

  2. #2
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    glGenTextures:


    PARAMETERS
    n Specifies the number of texture names to be
    generated.

    textures Specifies an array in which the generated texture
    names are stored.

    glGenTextures(texsiz, &texture[0]); // Create The Texture
    Don't you mean, create the Textures

    Looks like you're passing the address of a specific texture ID in the texture ID array...(which still is technically an array, and why it compiles)...

    Try passing &texture instead.

    EDIT: Course I am working blindly, so that might not be it.

    Here is my loadgltexture function that I modified in the past,
    I'm really perplexed as to how this works myself.
    If a gluint is nothing but a dword, what good is returning it?
    Code:
    GLuint LoadGLTexture( const char *filename )			// Load Bitmaps And Convert To Textures
    {
    	AUX_RGBImageRec *pImage;							// Create Storage Space For The Texture
    	GLuint texture = 0;									// Texture ID
    
    	pImage = LoadBMP( filename );						// Loads The Bitmap Specified By filename
    
    	// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
    	if ( pImage != NULL && pImage->data != NULL )		// If Texture Image Exists
    	{
    		glGenTextures(1, &texture);						// Create The Texture
    
    		// Typical Texture Generation Using Data From The Bitmap
    		glBindTexture(GL_TEXTURE_2D, texture);
    		glTexImage2D(GL_TEXTURE_2D, 0, 3, pImage->sizeX, pImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, pImage->data);
    		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    
    		free(pImage->data);								// Free The Texture Image Memory
    		free(pImage);									// Free The Image Structure
    	}
    
    	return texture;										// Return The Status
    }

    This stuff is weird.
    Code:
    glGenTextures(1, &texture);						// Create The Texture


    Parameter 2 is supposed to be a pointer or reference to pointer of an array in which the generated names are stored]

    Whats odd here, is that it's allowing me to pass a gluint in replacement of this array of texture id's. A gluint can be a texture ID I suppose but it's not a texture ID array. My LoadGLTextures function only does 1 texture at a time, thats why I use a GLuint as the parameter for the pointer to the container of texture id's.

    So it sounds like to me an array of gluint's should suffice as a paramter, you're still passing it a single gluint though here: (which is why you're only loading 1 texture)
    Code:
    glGenTextures(/*parameter for number of textures*/, /*parameter for array of tex id's*/); 	// Create The Textures
    First off, parameter 1 is a number, simply that; the number of textures you are loading.
    For the first parameter, try manually entering 2 as the number of textures.
    Last edited by Shamino; 02-12-2008 at 11:37 AM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. openGL text in multiple windows
    By hannibar in forum Windows Programming
    Replies: 1
    Last Post: 03-02-2006, 01:17 PM
  2. OpenGL Textures and TGAs
    By ay_okay in forum Game Programming
    Replies: 3
    Last Post: 06-13-2005, 09:06 PM
  3. OpenGL, loading BMP Textures?
    By Zeusbwr in forum Game Programming
    Replies: 12
    Last Post: 12-09-2004, 04:16 PM
  4. OpenGL / Moveing textures?
    By Oluf in forum Game Programming
    Replies: 7
    Last Post: 06-08-2004, 01:26 PM
  5. OpenGL Dual Textures
    By drdroid in forum Game Programming
    Replies: 5
    Last Post: 01-02-2003, 05:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21