Results 1 to 8 of 8

Thread: Problem with Blending

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2011
    Posts
    17

    Problem with Blending

    Hi,

    I am working on water surface simulation, as a newbie in openGL, with lots of help from this forum I have completed the dynamics and the refraction maps.

    Now, next step is reflection of sky in to my water surface. I have read tutorials and topics related to blending. I understand that blending is done when two textured(or colored) primitives overlap and then we can make them translucent so that we can see through them.

    My problem is I have only one primitive i.e my triangular mesh. I have created the mesh using glDrawElements(),
    and then Textured with the image of floor (pond). I have used the following code to blend the sky image with this image so as to give the effect of reflection, but there is some thing wrong with either my logic or code.

    glBlendFunc(GL_ONE, GL_ONE);
    glActiveTextureARB(GL_TEXTURE0_ARB);

    glBindTexture(GL_TEXTURE_2D, textureArray[0]); // first image


    glActiveTextureARB(GL_TEXTURE1_ARB);
    glBindTexture(GL_TEXTURE_2D, textureArray[1]); // second image
    glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_CO MBINE);

    glPushMatrix();

    glTranslatef(-(n/2),-(n/2),0);

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);
    glNormalPointer(GL_FLOAT, 0, normals);
    glTexCoordPointer(2,GL_FLOAT,0,tex);
    glVertexPointer(3,GL_FLOAT,0,vertices);
    glDrawElements(GL_TRIANGLES,6*(n-1)*(n-1),GL_UNSIGNED_INT,indices);
    glDisableClientState(GL_VERTEX_ARRAY);


    glPopMatrix();


    Also I am confused, in my case do I have to do Multitexturing or Blending?

  2. #2
    Senior Member Regular Contributor
    Join Date
    Nov 2010
    Location
    Brazil, Rio de Janeiro
    Posts
    147

    Re: Problem with Blending

    Did you enable GL_BLEND?

    Code :
    glEnable( GL_BLEND );

    The documentation of glBlendFunc says that GL_ONE set RGBA to (1,1,1,1). To mix things up I use the following configuration:

    Code :
    glBlendFunc(  GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

    More information can be found into documentation of glBlendFunc

  3. #3
    V-man
    Guest

    Re: Problem with Blending

    You have a lot of calls missing after
    glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_CO MBINE);

    Example:
    http://www.opengl.org/wiki/Texture_Combiners

  4. #4
    Junior Member Newbie
    Join Date
    Mar 2011
    Posts
    17

    Re: Problem with Blending

    Hi McLeary,

    Thankyou for your reply.

    Yes, I have enabled GL_BLEND in the calling function.

    I have not used

    glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

    because I am using .png images which are photographs, so I don't know or have control, over the alpha values of the images.


  5. #5
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,161

    Re: Problem with Blending

    You have several problems with your code.

    Blending is a multi-pass algorithum.
    Multitexturing is a single-pass algorithum.

    You are not clear which you are doing. I suggest the multitexturing is the approach you are after and can therefore disable GL_BLEND.

    In you code you need to improve several areas:
    1. Initialisation
    2. Rendering
    3. Finalisation/cleanup


    ...so pretty much all of it!



    Code :
    glActiveTextureARB(GL_TEXTURE0_ARB);
    glEnable (GL_TEXTURE_2D);			//should already be enabled - do we need this line?
    glBindTexture(GL_TEXTURE_2D, textureArray[0]); // first image

    Notice the need to enable the active texture unit for texturing.

    Code :
    glActiveTextureARB(GL_TEXTURE1_ARB);
    glEnable (GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, textureArray[1]); // second image
    glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_COMBINE);

    You also need to setup the combiners. You have been given the link for this - very useful I can tell you. I wish I had that information 6 years ago!


    The rendering now needs to enable the vertex arrays for each active texture:
    Code :
    glClientActiveTexture(GL_TEXTURE0); 
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glTexCoordPointer(2,GL_FLOAT,0,tex);
     
    glClientActiveTexture(GL_TEXTURE1); 
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glTexCoordPointer(2,GL_FLOAT,0,tex);


    and for clean up of the enabled client states....
    Code :
    //glClientActiveTexture(GL_TEXTURE1); 	//already active
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glClientActiveTexture(GL_TEXTURE0); 	
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
    glDisableClientState(GL_VERTEX_ARRAY);

    and finally the clean up of the enabled textures...
    Code :
    //glActiveTextureARB(GL_TEXTURE1_ARB);	//already active
    glDisable (GL_TEXTURE_2D);
    glActiveTextureARB(GL_TEXTURE0_ARB);	//ensure we finish on texture unit #0

    Notice we finish leaving GL_TEXTURE1_ARB disabled and GL_TEXTURE0_ARB as the current active (enabled) texture unit.

  6. #6
    Junior Member Newbie
    Join Date
    Mar 2011
    Posts
    17

    Re: Problem with Blending

    Hi BionicBytes,

    Thank you for your guidance, I embedded your code along with the code from the link.

    Now I am trying to blend the two images based upon the supplied blending factor alpha.

    This alpha is supplied through the last(4th) element of array 'mycolor[]'.

    Now the problem I am getting is, irrespective of alpha value I supply the second texture is appearing without any blending or trace of first texture.

    following is the code,

    void drawFloor(int pass)
    {

    glActiveTextureARB(GL_TEXTURE0);

    if (pass == PASS_NORMAL)
    glBindTexture(GL_TEXTURE_2D, textureArray[0]);
    else
    glCallList(100);
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

    glActiveTextureARB(GL_TEXTURE1_ARB);
    glEnable (GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, textureArray[1]); // second image
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
    glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE); //Interpolate RGB with RGB
    glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS);
    glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE);
    //GL_CONSTANT refers to the call we make with glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, mycolor)
    glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_CONSTANT);
    glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
    glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
    glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_COLOR);
    //------------------------
    glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_INTERPOLATE); //Interpolate ALPHA with ALPHA
    glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_PREVIOUS);
    glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA, GL_TEXTURE);
    //GL_CONSTANT refers to the call we make with glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, mycolor)
    glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA, GL_CONSTANT);
    glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);
    glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA);
    glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA, GL_SRC_ALPHA);
    //------------------------
    float mycolor[4];
    mycolor[0]=mycolor[1]=mycolor[2]=0.0;
    mycolor[3]=0.0; //Set the blend factor with this



    glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, mycolor);
    glPushMatrix();
    glTranslatef(-(n/2),-(n/2),0);

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);
    glNormalPointer(GL_FLOAT, 0, normals);

    glClientActiveTexture(GL_TEXTURE0);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glTexCoordPointer(2,GL_FLOAT,0,tex);

    glClientActiveTexture(GL_TEXTURE1);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glTexCoordPointer(2,GL_FLOAT,0,tex);



    glVertexPointer(3,GL_FLOAT,0,vertices);
    glDrawElements(GL_TRIANGLES,6*(n-1)*(n-1),GL_UNSIGNED_INT,indices);
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glClientActiveTexture(GL_TEXTURE0);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
    glDisableClientState(GL_VERTEX_ARRAY);

    glDisable (GL_TEXTURE_2D);
    glActiveTextureARB(GL_TEXTURE0_ARB); //ensure we finish on texture unit #0


    glPopMatrix();

    }

    I am really grateful for your time.

  7. #7
    Junior Member Newbie
    Join Date
    Mar 2011
    Posts
    17

    Re: Problem with Blending

    Finally finished blending, thanks bionic bytes for code and V-man for the link

  8. #8
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,161

    Re: Problem with Blending

    No problem. Glad I managed to help.

Similar Threads

  1. Problem with blending
    By Ralph in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 05-04-2008, 03:22 AM
  2. blending problem
    By 0stress in forum OpenGL ES
    Replies: 3
    Last Post: 09-21-2006, 03:36 PM
  3. I problem with blending HELP ME PLEASE!
    By Dol AI with 3D Bone system in forum OpenGL: Basic Coding
    Replies: 12
    Last Post: 10-14-2004, 07:40 AM
  4. Is this a blending problem?
    By in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 08-04-2003, 07:53 AM
  5. Blending problem
    By Imotiep in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 04-19-2001, 01:53 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