Blending question

Started by
5 comments, last by Raduprv 18 years, 3 months ago
I didn't use OpenGl in more than 2 years, so now I became a n00b at it :( I have a simple question: Say I have a surface that was previously draw. I have a memory buffer of the size x * y (unsigned char). That memory buffer represents an alpha map, and has values ranging from 0 to 255. I am putting that buffer in a texture like this:

	glGenTextures(1, &texture);
	glBindTexture(GL_TEXTURE_2D, texture);	//failsafe
	bind_texture_id(texture);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);


	//glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,NIGHT_MAP_X_LEN, NIGHT_MAP_Y_LEN,0,GL_ALPHA8,GL_UNSIGNED_BYTE,night_map_memory);
	glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,NIGHT_MAP_X_LEN, NIGHT_MAP_Y_LEN,0,GL_LUMINANCE8,GL_UNSIGNED_BYTE,night_map_memory);
(I tried both GL_ALPHA8 and GL_LUMINANCE8) Then later on I have this code:

	//attempt to draw the night
	get_and_set_texture_id(night_map_text);
	glColor4f(0.0f,0.0f,0.0f,0.5f);
    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE,GL_SRC_ALPHA);
    glBegin(GL_QUADS);
    draw_2d_thing(0,1,1,0,0,0,window_width,window_height-(window_height-512));
	glEnd();
	glDisable(GL_BLEND);

Now, this doesn't work, I tried various blending functions and stuff. Can anyone point out where my mistake is, and how I can get it to work? [Edited by - Raduprv on January 4, 2006 9:22:05 PM]
Advertisement
GL_LUMINANCE only for the third last parameter to glTexImage. GL_ALPHA is not valid, and you don't include size, since that's specified in the second last parameter. GL_LUMINANCE/GL_UNSIGNED_BYTE means there is a single liminance component in the source image, and each component is an unsigned char.
Quote:Original post by Brother Bob
GL_LUMINANCE only for the third last parameter to glTexImage. GL_ALPHA is not valid, and you don't include size, since that's specified in the second last parameter.

That's not entirely correct. GL_ALPHA is valid as a format parameter, and means that the source data only contains a single alpha channel. GL_LUMINANCE means that there is only a single luminance channel, without any alpha information.

With an internal format of GL_RGBA (as he is currently using), GL_LUMINANCE will only set the RGB part of the data, and the alpha will always be set to one. GL_LUMINANCE will only affect alpha, if the internal format is set to GL_INTENSITY(8).

For this specific case, I would suggest one of these two possibilities:

1) Set format to GL_LUMINANCE, and internal format to GL_INTENSITY8, or

2) Set format to GL_ALPHA, and internal format to GL_ALPHA8.
I remember once (long time ago) having problem with using GL_ALPHA as the format parameter, so I looked it up and was surprised to notice it wasn't a valid parameter, so I just worked around it somehow. Since then, I haven't bothered looking it up as I've had no need for it, but you are of course correct that it is valid. Must have misread whatever I was reading or something...


edit: Never mind, I'm confused. It's GL_INTENSITY that isn't a valid format parameter and what I was having problems with in the past. Ignore what I've said about GL_ALPHA [razz]

[Edited by - Brother Bob on January 4, 2006 6:44:50 AM]
No, that didn't seem to help at all..
I was wondering, do I need to use glTexEnvi()?
You may want to change the line: glBlendFunc(GL_ONE,GL_SRC_ALPHA);
to
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
or
glBlendFunc(GL_SRC_ALPHA, GL_ONE);

The latter doesn't really produce a true alpha blended images, but it should be sufficient.
Oh, never mind, it doesn't work.

[Edited by - Skeleton_V@T on January 4, 2006 10:41:05 PM]
--> The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones <--
Nevermind, turns out I was an idiot, I was binding the texture using the texture cache ID when I should have bind it using it's direct texture ID.

Thank Yann and Skeleton_V@T

I am using
glTexImage2D(GL_TEXTURE_2D,0, GL_INTENSITY8,NIGHT_MAP_X_LEN, NIGHT_MAP_Y_LEN,0,GL_LUMINANCE,GL_UNSIGNED_BYTE,night_map_memory);
in conjunction with
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

This topic is closed to new replies.

Advertisement