Results 1 to 3 of 3

Thread: Mixing 2 blending functions for 1 scene 2d

  1. #1
    Member Newbie
    Join Date
    Jul 2006
    Posts
    39

    Mixing 2 blending functions for 1 scene 2d

    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 :-


    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)
    Code :
    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

  2. #2
    Senior Member Frequent Contributor
    Join Date
    Feb 2006
    Location
    Sweden
    Posts
    744

    Re: Mixing 2 blending functions for 1 scene 2d

    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.

  3. #3
    Member Newbie
    Join Date
    Jul 2006
    Posts
    39

    Re: Mixing 2 blending functions for 1 scene 2d

    Nope now i et tis

    i used

    Code :
    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

    Code :
    //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);
     
    }

Similar Threads

  1. Replies: 9
    Last Post: 10-18-2014, 02:00 PM
  2. Built-in Functions: Work-Item Functions
    By sean.settle in forum OpenCL
    Replies: 7
    Last Post: 11-17-2011, 07:39 AM
  3. Mixing 2d and 3d
    By Mitikoro in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 05-31-2008, 03:01 AM
  4. Replies: 4
    Last Post: 07-26-2002, 12:09 PM
  5. mixing lightmaps ?
    By tomslee in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 01-23-2002, 03:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean