# How can I implement “cut outs” for lighting in OpenGL?

So, I'm working with OpenGL (I'm not exactly sure of the version), and I want to do an old-style lighting setup by essentially drawing a black rectangle over the screen, and drawing white circles over the areas that should be lit. I already have the rectangle and the circles, but I can't figure out how to make it so that the white circles are transparent when applied to the black areas. Any ideas? I tried to use blending modes, but it still evades me...

EDIT: Here's the code that I'm using (Python).

view_buf = Buffer(GL_INT, 4)
glGetIntegerv(GL_VIEWPORT, view_buf)
view = view_buf.to_list() if hasattr(view_buf, "to_list") else view_buf.list

glDisable(GL_DEPTH_TEST)

glDisable(GL_LIGHTING)

glEnable(GL_BLEND)

if logic.lights['smooth']:
glEnable(GL_LINE_SMOOTH)

# Setup the matrices

glMatrixMode(GL_PROJECTION)
glPushMatrix()
gluOrtho2D(0, view[2], 0, view[3])
glMatrixMode(GL_MODELVIEW)
glPushMatrix()

#glAlphaFunc(GL_GREATER, 0.5)

glColor4f(0.0, 0, 0, 0.75)

glVertex2d(0.0, 0.0);
glVertex2d(view[2], 0.0);
glVertex2d(view[2], view[3]);
glVertex2d(0.0, view[3]);
glEnd()

glColor4f(1, 1, 1, 1)

glBlendFunc(GL_ZERO, GL_SRC_ALPHA)

def DrawCircle(x, y, radius, fill = 1, step = 11):

glVertex2f(x, y)

if fill:
glBegin(GL_TRIANGLE_FAN)
else:
glBegin(GL_LINE_LOOP)

for a in range(0, 360, step):

glEnd()

#for layer in range(len(logic.lights['layers'])):
#   size =
for light in logic.lights['points']:
DrawCircle(light[0][0] * view[2], (1 - light[0][1]) * view[3], light[1], step = light[2])
if logic.lights['smooth']:
DrawCircle(light[0][0] * view[2], (1 - light[0][1]) * view[3], light[1], 0, light[2])

# Reset the state
glPopMatrix()
glMatrixMode(GL_PROJECTION)
glPopMatrix()

• Without seeing your code, it's hard to tell. The blend modes should do what you need, but perhaps your implementation is bugged or incomplete. – Engineer Oct 20 '11 at 7:56
• Most probably you need to look at glBlendFunc and it's parameters – Kromster Oct 20 '11 at 9:34
• Huh, okay. Lemme see if I can post the code... – SolarLune Oct 20 '11 at 14:09
• Okay, so after doing a little research, it would appear that I need to render the lights and darkness to a texture first, and then draw that over my actual scene... – SolarLune Oct 20 '11 at 14:25