Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Sampling on the texel centers doesn't give proper results, OpenGL, C++

  1. #1
    Newbie Newbie
    Join Date
    Dec 2012
    Posts
    1

    Sampling on the texel centers doesn't give proper results, OpenGL, C++

    Not so long ago I had a problem with displaying a texture as I was using edges of texels as texture coordinates instead of centers of texels. This, in combination with linear blending gave a smooth result of colors instead of pure colors given by a texture.

    Thanks to advices given to me, I thought my problem was solved as I started to use centers of texels as coordinates, but it only worked in one case and I have no idea why only in that one case.

    I'll show some pictures.

    This is a texture I'm using (32x32 pixels) with border of 2 pixels:



    My whole object that is being drawn consists of 9 quads, but they use only one texture (the one I showed). One of them will serve as an explanation to my problem:



    I think it is well visible that colors of that quad aren't properly set according to the texture.

    It looks like a problem with blending, but for setting texture coordinates I have used texels' centers, like this:

    Code :
    glBegin(GL_QUADS);
        // Bottom left
        glTexCoord2f(0.0 + texelCentersOffset, maxTexCoordBorderY + texelCentersOffset);
        glVertex2i(pos.x, pos.y + height - m_borderWidth);
     
        // Top left
        glTexCoord2f(0.0 + texelCentersOffset, (GLfloat)1.0 - maxTexCoordBorderY - texelCentersOffset);
        glVertex2i(pos.x, pos.y + m_borderWidth);
     
        // Top right
        glTexCoord2f(maxTexCoordBorderX - texelCentersOffset, (GLfloat)1.0 - maxTexCoordBorderY - texelCentersOffset);  
        glVertex2i(pos.x + m_borderWidth, pos.y + m_borderWidth);
     
        // Bottom right
        glTexCoord2f(maxTexCoordBorderX - texelCentersOffset, maxTexCoordBorderY + texelCentersOffset);
        glVertex2i(pos.x + m_borderWidth, pos.y + height - m_borderWidth);
    glEnd();

    For clarification:

    m_borderWidth = 2
    maxTexCoordBorderX = maxTexCoordBorderY = 2/32 = 0.0625
    texelCentersOffset = 1/64 = 0.015625

    Here are settings for my texture:

    Code :
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                     GL_LINEAR_MIPMAP_NEAREST );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
     
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
                        GL_REPEAT );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
                        GL_REPEAT );
    gluBuild2DMipmaps( GL_TEXTURE_2D, 4, m_width, m_height,
                       GL_RGBA, GL_UNSIGNED_BYTE, pixmap );

    When I change from GL_REPEAT to GL_NEAREST, it works properly so it's obviously a problem with blending, but still can't figure out why it is wrong.

    Could anyone tell me what is wrong in what I've wrote?

  2. #2
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,209
    Quote Originally Posted by Mosquito View Post
    When I change from GL_REPEAT to GL_NEAREST, it works properly so it's obviously a problem with blending, but still can't figure out why it is wrong.
    I hope you don't mean REPEAT to NEAREST, but LINEAR to NEAREST (or LINEAR_MIPMAP_LINEAR to NEAREST).

    And which are you changing? MIN_FILTER or MAG_FILTER?

    First, disable MSAA and BLENDING.

    Flip both MIN_FILTER and MAG_FILTER to NEAREST. Make sure you get what you expect.

    Flip one and only one to LINEAR. See what you get. Now try the other by itself.

    Flip both to LINEAR. See what you get.

    Flip MIN_FILTER to LINEAR_MIPMAP_LINEAR. See what you get.


    Ensure that you are drawing polygon edges on pixel edges, and ensure you are sampling texels at the center of each texel.

Posting Permissions

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