Hello to everyone,
[sorry for my english. It’s a disaster]
I’m trying to generate a 2D light area with the precious help of our friend “alpha mask”. Now, the process is very simple but I’m an opengl’s newbie and I hope that someone of you can just direct me to the right way/technique.
I’ve a certain amount of 2D textures (my sprites) witch composing my map: background, items, boxes … etc … etc. Once I’ve painted the map I use a large big black texture painted over the map as the foreground: this generate darkness.
Now, before painting the “darkness” image, I paint a “light texture” (a white image, completely visible from external to completely invisible to the center) using the following declaration:
glBlendFuncSeparate(GL_ZERO, GL_ONE, GL_DST_COLOR, GL_ZERO);
The darkness image instead is preceded by the following declaration:
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_DST_ALPHA);
The natural consequence of this is this image:
As you can see the cone of light effect is good (I need to adjust the transparency of my “light” texture, but it’s ok) but the alpha channel of others semi-transparent elements from map are involved in the equation generated by the function:
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_DST_ALPHA);
This is a big problem! The render code is this:
{…}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
foreach(singleton, &cengine::environment) {
/* every graphical element is formed by one or more textures */
while ((texture = singleton->get()) != EE_NULLTEXTURE) {
glLoadIdentity();
glBindTexture(GL_TEXTURE_RECTANGLE_EXT, texture);
glPushMatrix();
glTranslatef(...);
if (singleton->flipped[EFLIPDIRECTION_VERTICAL])
glRotatef(180.0, 1.0, 0.0, 0.0);
if (singleton->flipped[EFLIPDIRECTION_HORIZONTAL])
glRotatef(180.0, 0.0, 1.0, 0.0);
glRotatef(fmod(singleton->rotation, 360), 0.0, 0.0, 1.0);
glBegin(GL_QUADS);
{
glTexCoord2i(...);
glVertex3f(...);
glTexCoord2i(...);
glVertex3f(...);
glTexCoord2i(...);
glVertex3f(...);
glTexCoord2i(...);
glVertex3f(...);
}
glEnd();
glPopMatrix();
}
}
glBlendFuncSeparate(GL_ZERO, GL_ONE, GL_DST_COLOR, GL_ZERO);
singleton = light;
/* LIGHT CONE (just a test) */
while ((texture = singleton->get()) != EE_NULLTEXTURE) {
glLoadIdentity();
glBindTexture(GL_TEXTURE_RECTANGLE_EXT, texture);
glPushMatrix();
glTranslatef(...);
if (singleton->flipped[EFLIPDIRECTION_VERTICAL])
glRotatef(180.0, 1.0, 0.0, 0.0);
if (singleton->flipped[EFLIPDIRECTION_HORIZONTAL])
glRotatef(180.0, 0.0, 1.0, 0.0);
glRotatef(fmod(singleton->rotation, 360), 0.0, 0.0, 1.0);
glBegin(GL_QUADS);
{
glTexCoord2i(...);
glVertex3f(...);
glTexCoord2i(...);
glVertex3f(...);
glTexCoord2i(...);
glVertex3f(...);
glTexCoord2i(...);
glVertex3f(...);
}
glEnd();
glPopMatrix();
}
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_DST_ALPHA);
singleton = imgbackground;
/* FOREGROUND DARKNESS (just a test) */
while ((texture = singleton->get()) != EE_NULLTEXTURE) {
glLoadIdentity();
glBindTexture(GL_TEXTURE_RECTANGLE_EXT, texture);
glPushMatrix();
glTranslatef(...);
if (singleton->flipped[EFLIPDIRECTION_VERTICAL])
glRotatef(180.0, 1.0, 0.0, 0.0);
if (singleton->flipped[EFLIPDIRECTION_HORIZONTAL])
glRotatef(180.0, 0.0, 1.0, 0.0);
glRotatef(fmod(singleton->rotation, 360), 0.0, 0.0, 1.0);
glBegin(GL_QUADS);
{
glTexCoord2i(...);
glVertex3f(...);
glTexCoord2i(...);
glVertex3f(...);
glTexCoord2i(...);
glVertex3f(...);
glTexCoord2i(...);
glVertex3f(...);
}
glEnd();
glPopMatrix();
}
glDisable(GL_BLEND);
glutSwapBuffers();
glFlush();
{…}
So, my question is: there is a technique to, for instance, applying a mask (or masks, in this case the cone of light) to a single texture (in this case to the foreground) in a fast way?
Thanks in advice guys, really.
[/sorry for my english. It’s a disaster]