Skip to main content
GameDev.net gamedev.net
🔒 Locked

OpenGL blending problem

Started by clearly Apr 29, 2006 at 4:52 PM 29 replies 4.6k views
Original Post
clearly
clearly
the problem is that the "tank" is seening under the "background" instade of standing on it.. how can i change it ? //Sorry for the bad english heres the code: initliazion:

	LoadGLTextures();
		glEnable(GL_TEXTURE_2D);				
	glShadeModel(GL_SMOOTH);				
	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);			
	glClearDepth(100.0f);					
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	
	glBlendFunc(GL_SRC_ALPHA, GL_ONE);			
	glEnable(GL_BLEND);	  

drawing:

 	glLoadIdentity(); // the tank
	glBindTexture(GL_TEXTURE_2D, texture[4]);
	glColor3f(1.0f,1.0f,1.5f);
	glTranslatef(x2rot,y2rot,-49.0f); //-39
	glBegin(GL_QUADS);
	glTexCoord2f(0.0f, 0.0f);glVertex3f(-7.0f, -7.0f,  5.0f);
	glTexCoord2f(1.0f, 0.0f);glVertex3f( 7.0f, -7.0f,  5.0f);
	glTexCoord2f(1.0f, 1.0f);glVertex3f( 7.0f,  7.0f,  5.0f);
	glTexCoord2f(0.0f, 1.0f); glVertex3f(-7.0f,  7.0f,  5.0f);
	glEnd();


		glLoadIdentity(); // background 
	glBindTexture(GL_TEXTURE_2D, texture[7]);
	glColor3f(1.0f,1.0f,1.5f);
	glTranslatef(0.0f,0.0f,-98.0f);
	glBegin(GL_QUADS);
	glTexCoord2f(0.0f, 0.0f);glVertex3f(-50.0f, -50.0f,  -2.0f);
	glTexCoord2f(1.0f, 0.0f);glVertex3f( 50.0f, -50.0f,  -2.0f);
	glTexCoord2f(1.0f, 1.0f);glVertex3f( 50.0f,  50.0f,  -2.0f);
	glTexCoord2f(0.0f, 1.0f); glVertex3f(-50.0f,  50.0f, -2.0f);
	glEnd();

textures: texture[4] = http://members.lycos.co.uk/clearly/RS_T.bmp texture[7] = http://members.lycos.co.uk/clearly/BG.bmp please help me :/
Ezbez
Ezbez
glEnable(GL_DEPTH_TEST), prehaps? That makes OpenGL sort drawings based on the z-buffer.

And if you already have it enabled, try turning it off. One or the other might help.
clearly
clearly
Quote:
Original post by Ezbez
glEnable(GL_DEPTH_TEST), prehaps? That makes OpenGL sort drawings based on the z-buffer.

And if you already have it enabled, try turning it off. One or the other might help.


i tryed enableing it and disableing it
it just making it worst ;/
i mean
when i do it
u can see the black pixels of the tank picture.. and i dont want to see them
what can i do about it?
dpadam450
dpadam450
Im assuming your doing 2d? Well either way if you have a texture with black parts that u dont want to draw, use the blend function GL_ONE, GL_ONE instead of what you have. The left parameter is what is already in the color buffer, the second is the color from the texture so if you have (255,0,0) already in the color buffer and draw a pixel from your texture (0,0,0) well your color is ( (255*1)+(0*1), 0*1 + 0*1, 0*1 + 0*1). :)
NBA2K, Madden, Maneater, Killing Floor, Sims 
dpadam450
dpadam450
Just to reiterate you are using GL_SRC_ALPHA, and its dealing with the alpha component currenty in the color buffer. Change to GL_ONE and let us know if it works.
NBA2K, Madden, Maneater, Killing Floor, Sims 
clearly
clearly
yeah its 2d
and i changed to GL_ONE
and still.. nothing changed..
still seeing the black pixels
dpadam450
dpadam450
Dont know if this is why, but enable blending before giving a glblend function. Also try , in clear color: take out the .5 alpha value.
NBA2K, Madden, Maneater, Killing Floor, Sims 
clearly
clearly
aha..
but its not the problem too..
i changed it now and still the same ;/
dpadam450
dpadam450
Did you try clearing to a color other than black? Maybe...
NBA2K, Madden, Maneater, Killing Floor, Sims 
clearly
clearly
i just tryed and when i did it red,green it was evrything was blended red or green
wierd//
when i did it white it didnt showed anything but white..
Jackrhino
Jackrhino
You do realise that when u are blending, u have to draw in depth order? That is the background must be drawn first and then the tank, so that the tank's transparent part blend with the background, because the blend function kind of work like CurrentColor = (1-alpha)CurrentColor + alpha*BackColor
dpadam450
dpadam450
No idea then. Maybe load up a pic of a render and the texture. Nevermind on that.

*edit again. Uhm you are drawing your background after you draw the tank. I just wasted all my time. Draw the background first, or enable depth testing. peace.

*edit again again. Damn you JackRhino :)
NBA2K, Madden, Maneater, Killing Floor, Sims 
clearly
clearly
well if i put the background drawing first
its doing the same thing that did on the start
the tanks is under the background..
Jackrhino
Jackrhino
haha, enable depth testing also won't work, u must draw in depth order, because even if depth testing is enabled, for example u draw a window in front, ok it blends with an empty background. Now u draw a tree behind the window. The window is not going to be redrawn and reblend with the tree behind, so the window will have 'incorrect' blending. Thou it is correct in the sense it has blend correctly with the previous background. This will result in the tree not being shown through the window
Jackrhino
Jackrhino
This depends on your viewpoint, are u sure the the -negative z axis is pointing towards u? If u have enable depth testing and u got the axis direction wrong, even if u draw the background first, but the background is drawn in front of the tank, u will continue to have the problem as the background will cover the tank.
clearly
clearly
so what do i need to do ?
dpadam450
dpadam450
Yea wasnt thinking Rhino man. What you need to do is switch your drawing operations...didn't u understand from what we said, your drawing a tank first, then placing a background over it......reverse the order.
NBA2K, Madden, Maneater, Killing Floor, Sims 
clearly
clearly
dude i already TOLD U
that i did IT
but its not changeing anything!
the background is covering the tank anyway!
LOOK:
http://members.lycos.co.uk/clearly/1.JPG
aleks_1661
aleks_1661
Turn off blending when drawing the background, then turn on blending and draw the tank.

If you wanted to get rid of just the black area around the tank you will have either use a second texture as a mask or use a texture format with an alpha channel.

If you are using the mask texture method you need to create a copy of the tank texture and colour all the pixels of the tank you want visible with black and all the transparent areas with white. (any greys will apear semi-transparent)

To render with the mask you need to do


disable blending
draw background

glBlendFunc(GL_DST_COLOR,GL_ZERO);

enable blending
draw tank mask image

glBlendFunc(GL_SRC_ALPHA, GL_ONE);

draw tank image

Hope it helps.
Twitter: [twitter]CaffinePwrdAl[/twitter] Website: (Closed for maintainance and god knows what else)
clearly
clearly
what do u mean when u say mask ?
can u show me an example functions ?
or something like it?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.