Sign in to follow this  
levjs

OleLoadPicture Distoring Textures

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;
		FreeResource(resource);

		HGLOBAL global=GlobalAlloc(GMEM_MOVEABLE,size);
		LPVOID gdata=GlobalLock(global);
		memcpy(gdata,lpData,size);
		GlobalUnlock(global);
		LPSTREAM stream;
		HRESULT hr = CreateStreamOnHGlobal(global, TRUE, &stream);
		if(FAILED(hr))return FALSE;
		hr=OleLoadPicture(stream,0,TRUE,IID_IPicture,(void**)&ipic);
		if(FAILED(hr))return FALSE;

		hDCtemp=CreateCompatibleDC(GetDC(0));
		if(hDCtemp==NULL)
		{
			ipic->Release();
			return FALSE;
		}

		glGetIntegerv(GL_MAX_TEXTURE_SIZE, &glMaxTexDim);

		ipic->get_Width(&pWidth);
		pPixWidth=MulDiv(pWidth,GetDeviceCaps(hDCtemp,LOGPIXELSX),2540);
		ipic->get_Height(&pHeight);
		pPixHeight=MulDiv(pHeight,GetDeviceCaps(hDCtemp,LOGPIXELSY),2540);

		// 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;

		bi.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
		bi.bmiHeader.biBitCount=32;
		bi.bmiHeader.biPlanes=1;
		bi.bmiHeader.biWidth=pPixWidth;
		bi.bmiHeader.biHeight=pPixHeight;
		bi.bmiHeader.biCompression=BI_RGB;

		bmpTemp = CreateDIBSection(hDCtemp, &bi, DIB_RGB_COLORS, (void**)&pBits, 0, 0);
		if(!bmpTemp)
		{
			DeleteDC(hDCtemp);
			ipic->Release();
			return FALSE;
		}
		SelectObject(hDCtemp,bmpTemp);

		ipic->Render(hDCtemp,0,0,pPixWidth,pPixHeight,0,pHeight,pWidth,-pHeight,0);

		for(long i = 0; i < pPixWidth * pPixHeight; i++)			// Loop Through All Of The Pixels
		{
			BYTE* pPixel	= (BYTE*)(&pBits[i]);				// 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.

Thanks,

Levi

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

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