Jump to content
  • Advertisement
Sign in to follow this  
HellRiZZer

OpenGL FreeImage Problem

This topic is 4815 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

I'm having difficulties with using FreeImage to load OpenGL textures. I've successfuly used it to load textures in other projects using EXACTLY the same technique, but somehow when I try to texture plain quad, it doesn't display anything at all. It displays untextured white quad, but not textured, and that drives me nuts. I've used a Nehe way of loading bitmaps, and it does work when I use it outside my major code, but not inside. I was wondering that if I am supposed to call wglMakeCurrent before loading textures, although not sure about that. Here's the example code that works in other projects, but not in this one:
[source=Texture.cpp]
int CTexture::LoadImage(CString Filename)
{
	FREE_IMAGE_FORMAT FileType;

	FileType = FreeImage_GetFileType(Filename.GetBuffer());// Get filetype
	if(FileType == FIF_UNKNOWN)
		return 0;

	m_pBitmap = FreeImage_Load(FileType, Filename.GetBuffer());// LoadXML image
	if(m_pBitmap !=NULL)
	{
		m_strFilename = Filename;
		return 1;
	}
	return 0;
}

[source=OpenGLTexture.cpp]
UINT COpenGLTexture::Create()
{
	UINT ImageWidth = 0, ImageHeight = 0, ImageDepth = 0;
	BYTE* ImageData = NULL;

	ImageWidth = FreeImage_GetWidth(m_pBitmap);// Get width
	ImageHeight = FreeImage_GetHeight(m_pBitmap);// Get height
	ImageDepth = FreeImage_GetBPP(m_pBitmap);	// get depth
	
	if(!IsMasked())
		FreeImage_ConvertTo24Bits(m_pBitmap);// Convert to 3 bytes
	else
		FreeImage_ConvertTo32Bits(m_pBitmap);// Convert to 4 bytes

	ImageData = FreeImage_GetBits(m_pBitmap);// Get image data

	// OpenGL Stuff:
	//glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	glGenTextures(1, &m_uiGLTextureID);// Get next available texture handle
	glBindTexture(GL_TEXTURE_2D, m_uiGLTextureID);// Bind 2D texture to handle

	// If you want your own texture parameters, remove next two lines
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	BYTE *MaskedTexture = NULL;
	UINT Index = 0;

	if(IsMasked())
	{
		MaskedTexture = new BYTE[ImageWidth * ImageHeight * 4];

		UINT Index = 0;
		for (UINT i=0; i<ImageWidth * ImageHeight * (ImageDepth / 8); i+=(ImageDepth / 8))
		{
			MaskedTexture[Index] = ImageData;
			MaskedTexture[Index+1] = ImageData[i+1];
			MaskedTexture[Index+2] = ImageData[i+2];

			if((ImageData == (BYTE)(m_Mask.B)) && (ImageData[i+1] == (BYTE)(m_Mask.G)) && (ImageData[i+2] == (BYTE)(m_Mask.R)))
				MaskedTexture[Index + 3] = 0;
			else
				MaskedTexture[Index + 3] = 255;

			Index+=4;
		}

		glTexImage2D(GL_TEXTURE_2D, 0, 4, ImageWidth, ImageHeight, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, MaskedTexture);
		delete[] MaskedTexture;
	}
	else
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, ImageWidth, ImageHeight, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, ImageData);


	if( (!CheckSize(GetWidth())) && (!CheckSize(GetHeight()))) // TEXTURE ISNT OF POWER OF 2
	{
		// CREATE MIPMAPS
		gluBuild2DMipmaps(GL_TEXTURE_2D, 
			GetDepth(), 
			GetWidth(), 
			GetHeight(),
			GL_BGR_EXT, 
			GL_UNSIGNED_BYTE, 
			m_pBitmap);
	}

	return 1;
}

void COpenGLTexture::Bind()
{
	if(m_uiGLTextureID == 0)
	{
		glDisable(GL_TEXTURE_2D);
		return;
	}
	else
	{
		if(glIsTexture(m_uiGLTextureID))
		{
			glEnable(GL_TEXTURE_2D);
			glBindTexture(GL_TEXTURE_2D, m_uiGLTextureID);
		}
		else
			glDisable(GL_TEXTURE_2D);
	}
}

As I said before, it all worked before, I do initialization of OpenGL in my CView derived class (is it ok?) and drawing:
void CDerivedView::OnPaint()
{
	// Device context for painting
	CPaintDC dc(this); 

	// Model is stored in Document
	CGUIEditDoc *pDoc = (CGUIEditDoc *)GetDocument();
	//ASSERT_VALID(pDoc);

	// Useful in multidoc templates
	HWND hWnd = GetSafeHwnd();
	HDC hDC = ::GetDC(hWnd);
	wglMakeCurrent(hDC, m_hGLContext);

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();

	int viewport[4];
	glGetIntegerv(GL_VIEWPORT, viewport);

	glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);	// Really Nice Perspective Calculations
	glHint(GL_POINT_SMOOTH_HINT,GL_NICEST);				// Really Nice Point Smoothing

	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
	glPushMatrix();										// Store The Projection Matrix
	glLoadIdentity();									// Reset The Projection Matrix
	glOrtho(0, viewport[2], 0, viewport[3], 0, 100);					// Set Up An Ortho Screen
	glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
	glPushMatrix();										// Store The Modelview Matrix
	glLoadIdentity();
	// Position / translation / scale

	if(GetActiveChild() !=NULL)
	{
		if(GetActiveChild()->GetGUI() !=NULL)
			GetActiveChild()->GetGUI()->Draw();
	}
	
	EndOrtho();

	// Double buffer
	SwapBuffers(dc.m_ps.hdc);
	glFlush();

	// Release
	::ReleaseDC(hWnd,hDC);
}

As I said before, it all works in other projects, but not in this one. Probably its a tiny mistake, but I can't figure out what it is, been trying to do that for last 2 days! Thanks for your help.

Share this post


Link to post
Share on other sites
Advertisement
Nevermind all, I figured it all out. The problem was in here:

if(m_pTexture == NULL)
{
glDisable(GL_TEXTURE_2D);

if(m_pMaterial !=NULL)
{
// !!!!!! OVER HERE !!!!!!!
glDisable(GL_LIGHTING);
glColor4fv(m_pMaterial->GetAmbient().RGBA);
// m_pMaterial->Bind();
// !!!!!! OVER HERE !!!!!!!
}
else
{
glDisable(GL_LIGHTING);
glColor3d(255, 255, 255);
}

glBegin(GL_QUADS);
glVertex3d(left, bottom, 0);
glVertex3d(right, bottom, 0);
glVertex3d(right, top, 0);
glVertex3d(left, top, 0);
glEnd();
}



It seems that the glColor4fv OR glDisable(GL_LIGHTING) was corrupting the whole output.

Thanks for... reading this post.. :)

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!