Jump to content
  • Advertisement
Sign in to follow this  
JohnHurt

simple flashlight

This topic is 4891 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm developing a 2d game and I've been trying to implement a very simple flashlight system like that seen in Codename: Gordon (the half-life 2 platformer). What I want to do is overlay a dark (or black) quad over the screen and then "cut away" the area of the flashlight with a textured primitive. My knowlege of alpha blending modes is letting me down here, so if anyone could help me out a bit that would be great! This is what I mean.

Share this post


Link to post
Share on other sites
Advertisement
I would do as you say and put a dark quad overtop with the color black with a dark alpha(use glColor4f and at the end put the alpha number. Not 1.0 is dark and opaque, 0.0 is clear. Make it more dark). For the flash light make a triangle that has the color completely clear(last argument of glColor4f.

P.S. I haven't done this so this is all theory but try it out anyways. Its basically what you said anyways and you gave me a great idea. Thanks!

Share this post


Link to post
Share on other sites
Problem is that I don't know what glBlendFunc arguments to use for each primitive, and whether i need to use glColorMask.

Share this post


Link to post
Share on other sites
i used this:

glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.5f);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Share this post


Link to post
Share on other sites
I'm doing this in my game too, here what it looks like in mine:



If thats what you want it to look like, then here is how I did it:

Render the Level

Draw a "flashlight primitive" into the stencil buffer

Render an overly dark quad over, the screen, but not where the stencil buffer was drawn into.


That will make the dark quad cover everything but the primitive you used for a light beam.
If you need more specific stuff, like code and what not, let me know.

Share this post


Link to post
Share on other sites
thats what i'm after. not done anything with the stencil buffer yet, back to google i guess. would this work if you were to vary the alpha of the light source (i.e. to fade it at the edges)?

AdamGL: does that code work for you? 'cos I can't get yours to work for me

Share this post


Link to post
Share on other sites
Heres some psuedocode/real code for using the stencil buffer:


//Render the Background here

glEnable(GL_STENCIL_TEST);
glDisable(GL_DEPTH_TEST);

glColorMask(GL_FALSE, GL_FALSE ,GL_FALSE , GL_FALSE);

glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
glStencilFunc(GL_ALWAYS, 1, 0xffffffff);


//Here's where you draw your flashlight's "beam" into the stencil buffer
//Note: Its not actually being drawn to the screen
glPushMatrix();
glTranslatef(player1->getX() , player1->getY() , 0);
glRotatef(player1->getAngle() , 0 , 0 , 1);
glBegin(GL_TRIANGLES);
glVertex3f(0 , 0 , 0);
glVertex3f(-4 , 14 , 0);
glVertex3f(4 , 14 , 0);
glEnd();
glPopMatrix();


glColorMask(GL_TRUE, GL_TRUE ,GL_TRUE , GL_TRUE);

glEnable(GL_DEPTH_TEST);

glStencilFunc(GL_NOTEQUAL , 1 , 0xFFFFFFFFL);
glStencilOp(GL_KEEP , GL_KEEP , GL_KEEP);

//This is where you draw your dark quad
//Note: Some of the stuff in here is specific to my game such as the translation
glPushMatrix();
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA);
glColor4f(1 , 1 , 1 , 0.6);
glPushMatrix();
glTranslatef(player1->getX() , player1->getY() , 0);
glBegin(GL_QUADS);
glVertex3f(-25 ,-20 , 1);
glVertex3f(25 ,-20 , 1);
glVertex3f(25 ,20 , 1);
glVertex3f(-25 ,20 , 1);
glEnd();
glPopMatrix();
glDisable(GL_BLEND);
glPopMatrix();


glDisable(GL_STENCIL_TEST);

//Draw Stuff here that you want to be visible no matter where they are

/* If you want a beam that gets darker at the end, just redraw the flashlight "beam" here , and change the colors and transparencies at each vertex to get the effect you want*/







Hope that helps, let me know if it didn't.

EDIT: I forgot, you have to set the stencil buffer in the PIXELFORMATDESRCIPTOR thing when you first create your window:


static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be
{
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
32, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
0, // No Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
24, // 24Bit Z-Buffer (Depth Buffer)
8, // 8Bit Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};


Share this post


Link to post
Share on other sites
Yeah, that works great. Only trouble is that I'm not sure this would work if you wanted to use the alpha channel of a texture to produce the light source. Say I want a light bulb to produce a soft glow for instance.

Share this post


Link to post
Share on other sites
Have you read the 2D soft shadowing article on gamedev?

http://www.gamedev.net/reference/articles/article2032.asp

If you want a very simple flashlight, all you would really have to do is the first part of the article, where it describes Light Attenuition. Follow all the steps, but replace the section where you draw the light attenuition into the buffer with a simple texture draw and you whould be done!

I don't have the time at this exact moment, but if you'd like to persue this path I can help with any code you may not understand.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!