PDA

View Full Version : Mixing 2 blending functions for 1 scene 2d



glBomb
08-25-2006, 12:07 AM
Is it possible to use different blending functions for different parts of te scene ?
Say i want to render sprites in a 2D ame & i use

glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
glBegin(GL_QUADS);

//Render sprites
glEnd();

then for light mapping to create lights in te 2D scene i do(immediately after above)

glBlendFunc(GL_SRC_ALPHA,GL_ONE);
glBegin(GL_QUADS);
//put light textures on pixels already in te screen
glEnd();

but i have noticed that only the last glBlendFunc(GL_SRC_ALPHA,GL_ONE) applies to whole scene!

the problem is this :-
http://img213.imageshack.us/img213/2410/s1dw5.png (http://imageshack.us)

Mixing 2 blending functions for 1 scene 2d lighting
I first draw the terrain & castle wit the appropriate regions transparent & blending enabled to get irregular outline.i use
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Then i use a light texture(visible clearly in right pic) & draw it with
glBlendFunc(GL_SRC_ALPHA,GL_ONE);
& i get the effect i want from the light but the castle has flared up too
With just
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPhHA);
i get a solid castle but lose the lighting effect!(right side)

I tried to use GL_LUMINANCE & stored the light values like this(for a uniform square shaped light..not the 1 above)

unsigned int img[32][32];
for (i = 0; i < 32; i++)
for (int j = 0; j < 32; j++){
img[i][j]= 255;

glTexImage2D(GL_TEXTURE_2D, 0, 1, 32,32, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, TextureImage[2]->pixels);But the program crashes

So what is the best way i can implement lightin here in my 2D game wit ortho views

zeoverlord
08-25-2006, 03:09 AM
You know, it should work, but you can try something like this.

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

//Render sprites
glEnd();
glDisable(GL_BLEND);


glBlendFunc(GL_ONE,GL_ONE);
glEnable(GL_BLEND);
glBegin(GL_QUADS);
//put light textures on pixels already in te screen
glEnd();
glDisable(GL_BLEND);

I don't know if it will work, but it doesn't hurt to try.

glBomb
08-25-2006, 07:51 AM
Nope now i et tis
http://img208.imageshack.us/img208/346/scrlz2.jpg (http://imageshack.us)
i used


glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindTexture(GL_TEXTURE_2D, textures[2]);//castle

glEnable(GL_BLEND);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);glVertex2i(100,100);
glTexCoord2f(1, 0.0f);glVertex2i(400, 100);
glTexCoord2f(1, 1); glVertex2i(400, 400);
glTexCoord2f(0.0f, 1); glVertex2i(100, 400);
glEnd();
glDisable(GL_BLEND);

glBlendFunc(GL_SRC_ALPHA,GL_ONE);
glBindTexture(GL_TEXTURE_2D, textures[1]);//lights


glEnable(GL_BLEND);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);glVertex2i(100,100);
glTexCoord2f(1, 0.0f);glVertex2i(150, 100);
glTexCoord2f(1, 1); glVertex2i(150, 150);
glTexCoord2f(0.0f, 1); glVertex2i(100, 150);
glEnd();
glDisable(GL_BLEND);


SDL_GL_SwapBuffers( );i set up view like tis


//Resize the scene
GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
{

if (height==0) // Prevent A Divide By Zero By
{
height=1; // Making Height Equal One
}

glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Calculate The Aspect Ratio Of The Window
gluOrtho2D(0, width, height, 0);
//glOrtho(0,640,480,0,-1,1);


glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix

// Nice perspective correction
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
// Allow 2D textures

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Set The Blending Function For Translucency

glEnable(GL_TEXTURE_2D);

}