Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Texture transparency borders

  1. #1
    Intern Newbie
    Join Date
    Dec 2010
    Posts
    33

    Texture transparency borders

    Hi, I'm using a texture applyed to a simple square drawn with GL_TRIANGLE_FAN like this:

    Code :
      glTranslate(X, Y, Z);
      glRotate(A, 0, 0, 1);
     
      glEnable(GL_TEXTURE_2D);
      glBindTexture(GL_TEXTURE_2D, Image);
      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
     
      glBegin(GL_TRIANGLE_FAN);
        glNormal3f(0, 0, 1);
        glTexCoord2f(0, 0); glVertex2f( - SizeX,  - SizeY);
        glTexCoord2f(1, 0); glVertex2f( + SizeX,  - SizeY);
        glTexCoord2f(1, 1); glVertex2f( + SizeX,  + SizeY);
        glTexCoord2f(0, 1); glVertex2f( - SizeX,  + SizeY);
      glEnd;
     
      glRotate(-A, 0, 0, 1);
      glDisable(GL_TEXTURE_2D);
      glTranslate(-X, -Y, -Z);

    but for some reason I get this result:



    I've tried GL_CLAMP_TO_EDGE, GL_CLAMP, GL_CLAMP_TO_BORDER_ARB and GL_REPEAT but I always get the same result.

    Is there any way to remove this border effect without changing the image tranparency?

    appreciate any help

  2. #2
    Junior Member Regular Contributor tksuoran's Avatar
    Join Date
    Mar 2008
    Location
    Cambridge, UK
    Posts
    223

    Re: Texture transparency borders

    Is the original image shown in full size, does your rendering code magnify or minify it?

    Do you have mipmaps, and how are they generated?

  3. #3
    Intern Newbie
    Join Date
    Dec 2010
    Posts
    33

    Re: Texture transparency borders

    Even when scaled or at normal size, the image gets that result
    I'm using this to scale:

    Code :
      GLSize       = 200; <- zoom value
      GLInvSize    = 1/GLSize*300;
      CreateViewport(GLSize, GLWidth, GLHeight);
      ScreenWidth  = GLSize * GLRatio * 0.5;
      ScreenHeight = GLSize * 0.5;
      glMatrixMode(GL_MODELVIEW);

    Code :
    CreateViewport(double Value; Integer Width; Integer Height);
    begin
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity;
     
      Double Val = Width / Height;
      glViewport(0, 0, Width, Height);
      glOrtho((-Val/2)*Value, (Val/2)*Value, -0.5*Value, 0.5*Value, 0.01, 500);
    end;

    and this creates the texture:

    Code :
      glGenTextures(1, @Texture);
      glPixelStorei(GL_UNPACK_ALIGNMENT, GL_RGB);
      glBindTexture(GL_TEXTURE_2D, Texture);
      glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
     
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
     
      gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, pData);

    does it have to do with it?

  4. #4
    Advanced Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: Texture transparency borders

    Could u try GL_LINEAR or GL_NEAREST fitering for GL_TEXTURE_MIN_FILTER and tell us if u still get these lines. Another thing is, is blending enabled?
    Regards,
    Mobeen

  5. #5
    Intern Newbie
    Join Date
    Dec 2010
    Posts
    33

    Re: Texture transparency borders

    Yes, I've tried all of these combinations:

    Code :
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
     
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
     
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
     
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
     
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
     
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    and still the same

    Yes, this is what is enabled:

    Code :
      glEnable(GL_ALPHA_TEST);
      glAlphaFunc(GL_ALWAYS, 0);
      glEnable(GL_BLEND);
      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
     
      glShadeModel(GL_SMOOTH);		
      glEnable(GL_DEPTH_TEST);			
      glDepthFunc(GL_LEQUAL);			
      glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
     
      glEnable(GL_POINT_SMOOTH);
      glEnable(GL_LINE_SMOOTH);
      glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
      glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);

    thanks

  6. #6
    Advanced Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: Texture transparency borders

    What output do u get if u comment all of the enables i.e.
    Code :
    /*
    glEnable(GL_ALPHA_TEST);
      glAlphaFunc(GL_ALWAYS, 0);
      glEnable(GL_BLEND);
      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
     
      glShadeModel(GL_SMOOTH);		
      glEnable(GL_DEPTH_TEST);			
      glDepthFunc(GL_LEQUAL);			
      glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
     
      glEnable(GL_POINT_SMOOTH);
      glEnable(GL_LINE_SMOOTH);
      glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
      glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
    */
    Regards,
    Mobeen

  7. #7
    Intern Newbie
    Join Date
    Dec 2010
    Posts
    33

    Re: Texture transparency borders

    No alpha blending, wich gives me the entire image without transparency:



  8. #8
    Intern Newbie
    Join Date
    Dec 2010
    Posts
    33

    Re: Texture transparency borders

    This is my entire code:

    to create the viewer:
    Code :
    void CreateViewer(Handle ViewHandle; Double Value; Integer Width; Integer Height);
    {
      GLHandle = ViewHandle;
      GLDC     = GetDC(GLHandle);
      PixelFormat(GLDC, 24);
      GLRC     = wglCreateContext(GLDC);
      wglMakeCurrent(GLDC, GLRC);
      BuildFont;
     
      CreateViewport(Value, Width, Height);
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity;
      ColorDS(1, 1, 1, 1);
     
      glEnable(GL_ALPHA_TEST);
      glAlphaFunc(GL_ALWAYS, 0);
      glEnable(GL_BLEND);
      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
     
      glShadeModel(GL_SMOOTH);
      glClearColor(0, 0, 0, 0);
      glClearDepth(1);Buffer Setup
      glEnable(GL_DEPTH_TEST);Testing
      glDepthFunc(GL_LEQUAL);
      glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
     
      glEnable(GL_POINT_SMOOTH);
      glEnable(GL_LINE_SMOOTH);
      glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
      glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
     
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity;
    };
     
    void CreateViewport(double Value; Integer Width; Integer Height);
    {
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity;
     
      Double Val = Width / Height;
      glViewport(0, 0, Width, Height);
      glOrtho((-Val/2)*Value, (Val/2)*Value, -0.5*Value, 0.5*Value, 0.01, 500);
    };

    to draw a square:
    Code :
    void Draw;
    {
      glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
      glLoadIdentity;
      glTranslate(0, 0, 0);
     
      glEnable(GL_TEXTURE_2D);
      glBindTexture(GL_TEXTURE_2D, Image);
      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
     
      glBegin(GL_TRIANGLE_FAN);
        glNormal3f(0, 0, 1);
        glTexCoord2f(0, 0); glVertex2f( - SizeX,  - SizeY);
        glTexCoord2f(1, 0); glVertex2f( + SizeX,  - SizeY);
        glTexCoord2f(1, 1); glVertex2f( + SizeX,  + SizeY);
        glTexCoord2f(0, 1); glVertex2f( - SizeX,  + SizeY);
      glEnd;
     
      glDisable(GL_TEXTURE_2D);
     
      SwapBuffers(wglGetCurrentDC);
    };

    and it gives me a bordered texture on the square limits like the image.
    I've searched for this but I can't find anything that can help.

  9. #9
    Intern Newbie
    Join Date
    Dec 2010
    Posts
    33

    Re: Texture transparency borders

    I really need any help solving these, I cent' seem to find anything related on this.

    this is an important project that I'm working on

    Thanks for the help so far

  10. #10
    Senior Member OpenGL Lord
    Join Date
    May 2009
    Posts
    6,008

    Re: Texture transparency borders

    and this creates the texture:
    There are many things wrong there.

    Code :
    glPixelStorei(GL_UNPACK_ALIGNMENT, GL_RGB);

    This should have given you an OpenGL error. UNPACK_ALIGNMENT takes integer values, not enumerators. And GL_RGB is certainly not 1, 2, 4, or 8, which are the only allowed values of the UNPACK_ALIGNMENT.

    Code :
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

    As previously mentioned, if you want to get exact values from your texture, you don't want LINEAR filtering. Just NEAREST.

    I suspect however that your *real* trouble is this:

    Code :
    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, pData);

    I'm guessing your copy-and-pasted this code from some website. So that's what you get for regurgitating code without knowing where it came from or what it does. At all times be aware of what functions are and what they do. If you don't know, look up the information online; don't just copy some code that works and poke at it, hoping that it'll work for you.

    In any case, there are two problems with using this function. The first is conceptual: if you want texel-accurate results (no filtering), why are you building mipmaps? That kinda defeats the whole point of texel-accurate results; you're wasting precious texture space on mipmaps that you don't need and will never use.

    But that's minor compared to the real problem. If "Width" and "Height" are not powers of two (and I'm guessing that they aren't, in your case), then `gluBuild2DMipmaps` will scale your image up to the nearest power of 2. This scaling will necessarily blur the various pixels in the image.

    The silly part of course, is that most GL implementations will handle arbitrarily sized textures just fine. But because you're using `gluBuild2DMipmaps` to load your texture, you can't use that feature. It will automatically tell OpenGL that the texture is bigger than it really is, and it will scale up your data accordingly.

    So, the lesson here (besides not using code you don't understand) is: avoid `gluBuild2DMipmaps` unless you have no other choice. If you need mipmaps, then either generate them offline (perhaps in a DDS file) and load them with `glTex(Sub)Image2D`, or use `glTex(Sub)Image2D` to load the base layer and use `glGenerateMipmap` (which requires GL 3.0 or above, or ARB_framebuffer_object) to generate the rest.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •