Loading/using multiple textures -SOLVED

Started by
-1 comments, last by jamesbf 18 years, 5 months ago
EDIT: I just fixed the problem turns out you cant bind the textures inside glBegin/glEnd. Ill leave this here in case it helps someone else. -------------------------------------------------------------------- hey guys, im writing a simple particle engine at the moment, and all was going well until i tried to implement animated particles. Basically what im trying to do is load multiple textures like this

	glGenTextures(STAR_NUM_FRAMES, texture);
	
		glBindTexture(GL_TEXTURE_2D, texture[0]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		LoadBMP("star/star001.bmp");

		glBindTexture(GL_TEXTURE_2D, texture[1]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		LoadBMP("star/star002.bmp");

		glBindTexture(GL_TEXTURE_2D, texture[2]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		LoadBMP("star/star003.bmp");

		glBindTexture(GL_TEXTURE_2D, texture[3]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		LoadBMP("star/star004.bmp");



(here is the LoadBMP code i am using...i got it off the net so im pretty sure its ok

///////////////////////////////////////////////////////////////////////////////
// This function loads a 24-bit color Windows bitmap and sets it as the current
// texture. The bitmap must be 24-bit color, and the dimensions must be powers
// of 2 (no additional checks are performed).
BOOL LoadBMP(TCHAR* szFileName)
	{
	HANDLE hFileHandle;
	BITMAPINFO *pBitmapInfo = NULL;
	unsigned long lInfoSize = 0;
	unsigned long lBitSize = 0;
	int nTextureWidth;
	int nTextureHeight;
	BYTE *pBitmapData;


	// Open the Bitmap file
	hFileHandle = CreateFile(szFileName,GENERIC_READ,FILE_SHARE_READ,
		NULL,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,NULL);

	// Check for open failure (most likely file does not exist).
	if(hFileHandle == INVALID_HANDLE_VALUE)
		return FALSE;

	// File is Open. Read in bitmap header information
	BITMAPFILEHEADER	bitmapHeader;
	DWORD dwBytes;
	ReadFile(hFileHandle,&bitmapHeader,sizeof(BITMAPFILEHEADER),	
		&dwBytes,NULL);

	__try {
		if(dwBytes != sizeof(BITMAPFILEHEADER))
			return FALSE;

		// Check format of bitmap file
		if(bitmapHeader.bfType != 'MB')
			return FALSE;

		// Read in bitmap information structure
		lInfoSize = bitmapHeader.bfOffBits - sizeof(BITMAPFILEHEADER);
		pBitmapInfo = (BITMAPINFO *) new BYTE[lInfoSize];

		ReadFile(hFileHandle,pBitmapInfo,lInfoSize,&dwBytes,NULL);

		if(dwBytes != lInfoSize)
			return FALSE;


		nTextureWidth = pBitmapInfo->bmiHeader.biWidth;
		nTextureHeight = pBitmapInfo->bmiHeader.biHeight;
		lBitSize = pBitmapInfo->bmiHeader.biSizeImage;

		if(lBitSize == 0)
			lBitSize = (nTextureWidth *
               pBitmapInfo->bmiHeader.biBitCount + 7) / 8 *
  			  abs(nTextureHeight);
	
		// Allocate space for the actual bitmap
		pBitmapData = new BYTE[lBitSize];

		// Read in the bitmap bits
		ReadFile(hFileHandle,pBitmapData,lBitSize,&dwBytes,NULL);

		if(lBitSize != dwBytes)
			{
			if(pBitmapData)
				delete [] (BYTE *) pBitmapData;
			pBitmapData = NULL;
			
			return FALSE;
			}
		}
	__finally // Fail or success, close file and free working memory
		{
		CloseHandle(hFileHandle);

		if(pBitmapInfo != NULL)
			delete [] (BYTE *)pBitmapInfo;
		}

   
	// This is specific to the binary format of the data read in.
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);	
    glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
    glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
    glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);

    glTexImage2D(GL_TEXTURE_2D, 0, 3, nTextureWidth, nTextureHeight, 0,
                 GL_BGR_EXT, GL_UNSIGNED_BYTE, pBitmapData);

	if(pBitmapData)
		delete [] (BYTE *) pBitmapData;

	return TRUE;
	}



Now basically i have a frame counter for each particle that i increment depending on how much time has passed since the last time i rendered that particular particle. I use the frame counter (p->currentFrame) as the index in the texture array... as below...

	glPushMatrix();
	glEnable(GL_TEXTURE_2D);							// Enable Texture Mapping
	glShadeModel(GL_SMOOTH);							// Enable Smooth Shading
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	
	glBegin(GL_QUADS);
	
	while(p)
	{

		GLfloat size = p->size/2;
		CVector pos = p->position;

		glBindTexture(GL_TEXTURE_2D, texture[p->currentFrame]);
		
		glColor4fv(p->colour);		
			glTexCoord2f(0.0, 0.0); glVertex3fv((pos + (right + up) * -size).v);
			glTexCoord2f(1.0, 0.0); glVertex3fv((pos + (right - up) * size).v);
			glTexCoord2f(1.0, 1.0); glVertex3fv((pos + (right + up) * size).v);
			glTexCoord2f(0.0, 1.0); glVertex3fv((pos + (up - right) * size).v);
	
			p = p->next;
	}
	glEnd();
	glPopMatrix();
	glDisable(GL_BLEND);



ive looked it up on the net, and all the code i could find does it this way (or something similar). But for some reason, the texture shown on screen is always the last one loaded. What am i doing wrong? [Edited by - jamesbf on November 7, 2005 11:21:04 PM]

This topic is closed to new replies.

Advertisement