I am having some major issues with OpenGL textures. I have the following code to load a glTexture (typedef ed as a GLuint):

glTexture LoadBMP(const char *fname)
{
	HBITMAP hBMP;
	BITMAP	BMP;
	glTexture texid;
	glGenTextures(1, &texid);
	hBMP=(HBITMAP)LoadImage(GetModuleHandle(NULL), fname, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE );
	if (!hBMP)
		return NULL;
	GetObject(hBMP, sizeof(BMP), &BMP);
	glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
	glBindTexture(GL_TEXTURE_2D, texid);								// Bind To The Texture ID
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);	// Linear Min Filter
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);	// Linear Mag Filter
	glTexImage2D(GL_TEXTURE_2D, 0, 3, BMP.bmWidth, BMP.bmHeight, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, BMP.bmBits);
	DeleteObject(hBMP);													// Delete The Object
	return texid;														// Loading Was Successful
}

It works but for some reason it causes all of my drawings to get dark.
Here are 2 links to my program running, both with and without a texture:
Without a texture: http://i1221.photobucket.com/albums/dd464/lambdabeta/goodgl.jpg
With a texture: http://i1221.photobucket.com/albums/dd464/lambdabeta/badgl.jpg

Does anybody know what is causing the colour mix up?

Recommended Answers

All 6 Replies

>>Does anybody know what is causing the colour mix up?

The pixel transfer/storage. Clearly, the image is correctly loaded from the file (since you can see an undistorted NeHe icon). The problem must be with the format of the BITMAP image. I would guess that your format "GL_BGR_EXT" and "GL_UNSIGNED_BYTE" is not correct (and usually bitmap images are either GL_COLOR_INDEX or GL_RGB). Make sure that this format info matches that of the BITMAP information structure (bmBitsPixel, bmPlanes, etc.).

I will have to check that. Where can I find the documentation for BITMAP. I have never used them before and have no clue what fields they have.

I don't know much about BITMAP, but the info is on msdn. I usually use other more transparent and cross-platform libraries (like FreeImage for simple image IO).

I looked at that, it didn't help. I really need this to work. It seems that I can get either the texture to load or the colours to draw correctly but not both. Does this mean that I cannot combine drawing with colours and drawing with textures?

I have continued through NeHe's tutorials and it seems that I should be able to use colour and textures. Please tell me why loading textures is messing with my colours and if possible how to fix it?

>>Please tell me why loading textures is messing with my colours and if possible how to fix it?

The colors and texture pixels get blended together, that's just how it works. But normally, setting the color to white should leave the texture intact (e.g. setting the color to red should give a red tint to the texture, and so on).

Your problem is not with OpenGL, it is with the loading of the texture from the file and with the transferring of the image to the graphics card (with glTexImage2D). Try using another method for loading the texture, like following the NeHe's method of loading the texture, exactly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.