Jump to content
  • Advertisement
Sign in to follow this  

OleLoadPicture Distoring Textures

This topic is 4300 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I modified Nehe's IPicture JPEG loader function to load a picture from a resource. Well, it works fine, loads the picture, but it distorts it too. This is what it does to a resource. What The Texture Is Supposed To Look Like How My Function Loads It The code for the function is:

int LoadTexturesFromResource(void)				// Load Image And Convert To A Texture
	IPicture	*ipic;
	HDC			hDCtemp;
	GLint		glMaxTexDim;
	long		pWidth;
	long		pHeight;
	long		pPixWidth;
	long		pPixHeight;
	HBITMAP		bmpTemp;

	byte	Texture[]={IDR_GRASS,IDR_FRONT};

	for(int l=0;l<sizeof(Texture);l++)
		HMODULE hModule = GetModuleHandle(NULL);
		HRSRC resource = FindResource(hModule, MAKEINTRESOURCE(Texture[l]), "JPG");
		HGLOBAL r=LoadResource(hModule,resource);
		LPVOID lpData = (unsigned char*)LockResource(r);
		int size = SizeofResource(hModule, resource);
		if(size==0)return FALSE;

		HGLOBAL global=GlobalAlloc(GMEM_MOVEABLE,size);
		LPVOID gdata=GlobalLock(global);
		LPSTREAM stream;
		HRESULT hr = CreateStreamOnHGlobal(global, TRUE, &stream);
		if(FAILED(hr))return FALSE;
		if(FAILED(hr))return FALSE;

			return FALSE;

		glGetIntegerv(GL_MAX_TEXTURE_SIZE, &glMaxTexDim);


		// Resize Image To Closest Power Of Two
		if (pPixWidth <= glMaxTexDim)					// Is Image Width Less Than Or Equal To Cards Limit
			pPixWidth = 1 << (int)floor((log((double)pPixWidth)/log(2.0f)) + 0.5f); 
		else									// Otherwise  Set Width To "Max Power Of Two" That The Card Can Handle
			pPixWidth = glMaxTexDim;
		if (pPixHeight <= glMaxTexDim)				// Is Image Height Greater Than Cards Limit
			pPixHeight = 1 << (int)floor((log((double)pPixHeight)/log(2.0f)) + 0.5f);
		else									// Otherwise  Set Height To "Max Power Of Two" That The Card Can Handle
			pPixHeight = glMaxTexDim;

		BITMAPINFO bi={0};
		DWORD *pBits=0;


		bmpTemp = CreateDIBSection(hDCtemp, &bi, DIB_RGB_COLORS, (void**)&pBits, 0, 0);
			return FALSE;


		for(long i = 0; i < pPixWidth * pPixHeight; i++)			// Loop Through All Of The Pixels
			BYTE* pPixel	= (BYTE*)(&pBits);				// Grab The Current Pixel
			BYTE  temp	= pPixel[0];					// Store 1st Color In Temp Variable (Blue)
			pPixel[0]	= pPixel[2];					// Move Red Value To Correct Position (1st)
			pPixel[2]	= temp;						// Move Temp Value To Correct Blue Position (3rd)
			pPixel[3]	= 255;						// Set The Alpha Value To 255

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

		// Typical Texture Generation Using Data From The Bitmap
		glBindTexture(GL_TEXTURE_2D, texture[l]);					// Bind To The Texture ID
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);		// (Modify This For The Type Of Filtering You Want)
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);		// (Modify This For The Type Of Filtering You Want)

		// (Modify This If You Want Mipmaps)	
		glTexImage2D(GL_TEXTURE_2D, 0, 3, pPixWidth, pPixHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, pBits);

		DeleteObject(bmpTemp);							// Delete The Object
		DeleteDC(hDCtemp);							// Delete The Device Context

		ipic->Release();							// Decrements IPicture Reference Count				

	return TRUE;

I really don't see what could be wrong? It only loads it like that sometimes too. Usually It loads it wrong the first time, and then right. But now, it won't load any right. Thanks for any help. Levi [Edited by - levjs on March 7, 2007 9:06:05 AM]

Share this post

Link to post
Share on other sites
Ok, So I've been messing around, and I've figured some things out. It seems it's not my load function that's messing up the image, it's loading it into Visual Studio as a resource that does it.

When I set the images as read-only, it loads them fine, but when I don't, it loads them distorted, and also saves the distorted image over the old image. I have no idea why it's doing this. Whether it's my load funtion or whether it's Visual Studio? Can anyone give me some tips? Until then, I'll just set them as read-only I guess.



Share this post

Link to post
Share on other sites
I don't know the answer to the problem with loading it as a resource, but the image distortion looks to be the dropping of the color depth to something like 8 bit or 256 colors. That might be a line of investigation. I will check my VS docs in the mean time.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!