2D lighting
#1 Members - Reputation: 293
Posted 28 April 2012 - 12:34 AM
glColor4d(1.0, 1.0, 1.0, 0.01);
glBegin(GL_QUADS);
glVertex3d(-1.0, 0.0, 0.0);
glVertex3d(0.0, 0.0, 0.0);
glVertex3d(0.0, -1.0, 0.0);
glVertex3d(-1.0, -1.0, 0.0);
glEnd();
glColor4d(1.0, 1.0, 1.0, 1.0);
glBegin(GL_QUADS);
glVertex3d(-0.9, 0.0, 0.0);
glVertex3d(0.1, 0.0, 0.0);
glVertex3d(0.1, -1.0, 0.0);
glVertex3d(-0.9, -1.0, 0.0);
glEnd();
I currently have the following at least somewhat relevant config stuff:
glEnable(GL_BLEND); // allows blending (transparency)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // function for blending
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH); // allows smooth shading (gradients)
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.0);
I can't seem to find the right configuration for what I'm trying to do. I'm drawing my stage at a z level farther (higher z in my case) and drawing these masks over it. I want to eventually draw a black mask over everything and draw lights to cancel out bits of that mask to light appropriate areas (with an appropriate gradient texture). What am I doing wrong? This has been done innumerable times, but I can't seem to wrap my head around the examples I've seen.
#2 Members - Reputation: 293
Posted 29 April 2012 - 01:10 PM
#4 Members - Reputation: 293
Posted 29 April 2012 - 03:37 PM
I don't know the first thing about shaders... everything I do is in the fixed pipeline (horribly outdated, I know). (sidenote: If anyone knows of a way to update the opengl libraries available to mingw, that would be awesome. I know microsoft doesn't provide drivers for newer versions of opengl with windows, but the card manufacturers do, so I'd figure the libraries would be available... alternatively I suppose something like glew would give access to newer features, but I can't seem to get glew to work with mingw/Code::Blocks)
Edited by medevilenemy, 29 April 2012 - 03:39 PM.
#5 Members - Reputation: 398
Posted 29 April 2012 - 06:48 PM
Pretty sure the problem is ultimately that I don't understand what the different blend func configurations do in practical terms, or even if there is one for what I'm trying to accomplish... I'm sure there's a way, I've seen it done.
In practical terms:
- At time of blending you have 2 colours, destination and source. Destination being what is already drawn to that pixel, and Source being the colour you are currently drawing. (in some circumstances you can also have an additional constant-colour, but I'm not sure if that exists in OpenGL)
- There are multiple blending equations (glBlendEquation). The default is Add, which is SrcResult + DstResult, there is also Subtract, Multiply, Min and Max.
- SrcResult and DstResult come from using the functions specified in glBlendFunc to operate on Source and Destination.
- GL_ZERO: 0 * value // i.e. use zero
- GL_ONE: 1 * value // i.e. use value directly
- GL_SRC_ALPHA: Source.a * value // i.e. multiply the colour by the alpha of source (regardless of if value is Destination or Source)
- GL_DST_ALPHA: Destination.a * value // i.e. multiply the colour by the alpha of destination (regardless of if value is Destination or Source)
- GL_ONE_MINUS_SRC_ALPHA: (1.0f - Source.a) * value; // the inverse of GL_SRC_ALPHA, mutiply by the remainding fraction of sources' alpha
- GL_ONE_MINUS_DST_ALPHA: (1.0f - Destination.a) * value; // same as above, except using the remainder of destinations' alpha.
Using the default blending example of Add, with SrcAlpha, InvSrcAlpha, what you have is a linear-interpolation from the destiation to the source by sources alpha:
ResultingPixel = (Source.a * Source.rgba) + ((1 - Source.a) * Destination.rgba
Note that through extensions or OpenGL2+ you can also seperate the alpha channel to use a seperate equation.
#6 Members - Reputation: 293
Posted 29 April 2012 - 07:14 PM
At this point I suppose there are two things I need to know/figure out:
1) How to achieve the desired effect
2) How to prevent (1) from interfering with the underlying scene.
#7 Members - Reputation: 398
Posted 29 April 2012 - 07:21 PM
So if you draw your background, then your lighting filters, then your sprites you should be fine (Noting that your sprites blending requirements are source, not destination, so whatever has already been put into alpha is irrelevant).
#8 Members - Reputation: 293
Posted 29 April 2012 - 07:46 PM
Now all that remains is (1) as described above.
#10 Members - Reputation: 293
Posted 02 May 2012 - 11:48 PM
I'm now isolating the experimental code by drawing it last, with glpush/pop matrix, and drawing the following:
glColor4d(1.0, 1.0, 1.0, 1.0);
glBegin(GL_QUADS);
glVertex3d(-1.0, 0.0, 0.01);
glVertex3d(0.0, 0.0, 0.0);
glVertex3d(0.0, -1.0, 0.0);
glVertex3d(-1.0, -1.0, 0.0);
glEnd();
glColor4d(1.0, 1.0, 1.0, 1.0);
glBegin(GL_QUADS);
glVertex3d(-1.0, 0.0, 0.0);
glVertex3d(0.0, 0.0, 0.0);
glVertex3d(0.0, -1.0, 0.0);
glVertex3d(-1.0, -1.0, 0.0);
glEnd();
What I want to do is have the 2nd quad nullify the first quad (by subtracting the alpha) to reveal the stuff drawn earlier underneath. If I understand correctly, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA should do the trick but no variant I've tried has made a difference.
#11 Members - Reputation: 398
Posted 03 May 2012 - 01:52 AM
#12 Members - Reputation: 293
Posted 03 May 2012 - 05:07 PM
#13 Members - Reputation: 293
Posted 05 May 2012 - 02:10 PM
undefined reference to `__GLEW_ARB_framebuffer_object'
The relevant code is:
GLenum err = glewInit();
if (GLEW_OK != err)
{
}
else // This is not permanent... just a way for me to test to see if glew is working.
{
if (GLEW_ARB_framebuffer_object)
{
}
}
I have added glew32.lib to the linker options for my project -- but I suppose it isn't playing nice with the linker. I am using the newest version of glew and building in Code::Blocks 10.05 with a mingw backend.
Edited by medevilenemy, 05 May 2012 - 02:11 PM.
#14 Members - Reputation: 293
Posted 05 May 2012 - 03:40 PM
#15 Members - Reputation: 293
Posted 09 May 2012 - 07:40 PM
glPushMatrix(); // set rendering destination to FBO glBindFramebuffer(GL_FRAMEBUFFER, 92); // clear buffers //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glOrtho(-0.5 * width, 0.5*width, -1, 1, 1, 0); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // function for blending glColor4d(0.0, 1.0, 1.0, 1.0); glBegin(GL_QUADS); glVertex3d(-1.0, 0.0, 0.01); glVertex3d(0.1, 0.0, 0.01); glVertex3d(0.1, -1.0, 0.01); glVertex3d(-1.0, -1.0, 0.01); glEnd(); glColor4d(1.0, 1.0, 1.0, 1.0); glBegin(GL_QUADS); glVertex3d(-0.9, 0.0, 0.0); glVertex3d(0.0, 0.0, 0.0); glVertex3d(0.0, -0.9, 0.0); glVertex3d(-0.9, -0.9, 0.02); glEnd(); glBindFramebuffer(GL_FRAMEBUFFER, 0); glPopMatrix(); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, 90); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.1,-0.1,0.01); // Bottom-Left Vertex glTexCoord2f(1.0f, 0.0f); glVertex3f(0.1,-0.1,0.01); // Bottom-Right Vertex glTexCoord2f(1.0f, 1.0f); glVertex3f(0.1,0.1,0.01); // Top-Right Vertex glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.1,0.1,0.01); // Top-Left Vertex glEnd(); glDisable(GL_TEXTURE_2D);
In fact.. Nothing I do seems to make any difference (even making the first quad bigger and a different color than the first and playing with texcoords, it always just shows the second box.
Does anyone know what I might need to change in the setup or in the drawing code to get the depth/alpha blending working?
Edited by medevilenemy, 09 May 2012 - 11:00 PM.






