simple blending question

Started by
1 comment, last by ktw6675 19 years, 10 months ago
I'm trying to follow a Gamedev tutorial on soft 2D shadows--but I can't even get started. Right now I'm trying to acheive the effect visible at the bottom of this page: http://www.gamedev.net/reference/programming/features/2dsoftshadow/page2.asp Basically you start by rendering a "light," which is just a triangle fan centered around a point, with 1.0f for the alpha value in the middle and 0.0f at the edges.

void renderLightAlpha(float intensity, float centerx, float centery, float radius)
{
  
	assert (intensity > 0.0f && intensity <= 1.0f);
    	
	int numSubdivisions = 20;
	float depth = 0.0f;
    
	glBegin(GL_TRIANGLE_FAN);
	{
		
		glColor4f(0.0f, 0.0f, 0.0f,  intensity);
		glVertex3f(centerx, centery, depth);
      
		// Set edge colour for rest of shape
		glColor4f(0.0f, 0.0f, 0.0f, 0.0f);
      
		for (float angle=0; angle<=PI*2; angle+=((PI*2)/numSubdivisions) )
		{
			glVertex3f( radius*(float)cos(angle) + centerx,
			radius*(float)sin(angle) + centery, depth);  
		}
		glVertex3f(centerx+radius, centery, depth);
	}

	glEnd();
}
Then the tutorial indicates that you should change the way blending is handled using glBlendFunc(). Here's my main drawing loop, with two test lights:

glPushMatrix();			
glTranslatef(-cam.pos.x*cam.depth, -cam.pos.y*cam.depth,0.0f);
glScalef(cam.depth,cam.depth,1.0f);			
renderLightAlpha(.8,300,300,200);
renderLightAlpha(1,600,400,150);	
glBlendFunc(GL_DST_ALPHA, GL_ONE);
drawRoom();	// draw out tiles
			glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); // return to original blending mode
glPopMatrix();
drawText(fpsStr,0,0);	// draw FPS
The idea is that glBlendFunc(GL_DST_ALPHA,GL_ONE) tells OpenGL to render all the geometry with the alpha values existing in the buffer already in mind, so that you get a nice circular lighting effect like on the tutorial page above. For whatever reason, I'm not seeing ANY difference in lighting, and no amount of fiddling seems to change that...I'm just getting the flat tiles from drawRoom() like usual. Any ideas?
Advertisement
The usual problem involving blending with destination alpha is that you don't even have a destination alpha channel. Make sure you're really asking for an alpha channel in the frame buffer when creating the pixel format. If you don't explicitly say you want it, you don't usually get it. Check the documentation on how to request an alpha channel for your windowing system.

You can check whether you have a destination alpha channel or not with glGetInteger and GL_ALPHA_BITS.
You, sir, were right! Thank you for the quick answer! It looks great. Dark, though. I'll have to draw an "ambient" quad over the whole screen or something first.

This topic is closed to new replies.

Advertisement