Sunday, May 1, 2016

Samplers


___ To Be Continued...
___
___
___
URL: http://www.mbsoftworks.sk/index.php?page=tutorials&series=1&tutorial=9 (Tut source)

Now that we have data sent to GPU, we need to tell OpenGL how to filter the texture. Well, for those who remember OpenGL in older days (2.1 and below), we would do something like this to set filtering:
// Set magnification filter
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
// Set minification filter
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER, GL_LINEAR);
But not now. Now we are ready to move on. Problem of the above was, that if we wanted to use the same texture with different filteringswe could do it by constantly changing its parameters. Well it could be done somehow, but isn't there a nicer, more elegant way ? Yes there is - Samplers

[ Samplers ]

I couldn't find a definition of sampler on them internets , but I will try to explain it as easy as possible. Sampling is the process of fetching a value from a texture at a given position, so sampler is an object where we store info of how to do it. Like which texture to use and all filtering parameters. If we want to change filtering, we just bind different samplers with different propertiees, and we're done. This line is copied from spec: 

"If a sampler object is bound to a texture unit and that unit is used to sample from a texture, the parameters in the sampler are used to sample from the texture, rather than the equivalent parameters in the texture object bound to that unit."
One part of it basically says, that if a sampler is bound to the texture, its parameters supersedes texture parameters. So instead of setting texture parameters, we will create a sampler, which will do exactly this. Even though in this tutorial we create one sampler per one texture (so it's like without samplers), it's a more general solution and thus it's better. As all OpenGL objects, samplers are generated (we get their names), and then we access them with that name. So when loading texture, we just call glGenerateSamplers(), and then we set its parameters with our member function:

void  CTexture::setFiltering(int isTFMag, int isTFMin)
    // magnification filter setting
    if (Texture_Filter_Mag_Nearest == isTFMag)
        glSamplerParameteri(m_idSampler
                                           , GL_Texture_Mag_Filter
                                           , GL_Nearest
        );
    else
    if (Texture_FIlter_Mag_Bilinear == isTFMag)
        glSamplerParameteri(m_idSampler
                                           , GL_Texture_Mag_Filter
                                           , GL_Linear
        );
    //
    //--
    // minification filter setting
    if (Texture_FIlter_Min_Nearest == isTFMin )
        glSamplerParameteri(m_idSampler
                                           , GL_Texture_Min_Filter
                                           , GL_Nearest
        );
    else
    if (Texture_FIlter_Min_Bilinear == isTFMin)
        glSamplerParameteri(m_idSampler
                                           , GL_Texture_Min_Filter
                                           , GL_Linear
        );
    else
    if (Texture_Filter_Min_Nearest_Mipmap == isTFMin)
        glSamplerParameteri(m_idSampler
                                           , GL_Texture_Min_Filter
                                           , GL_Nearest_Mipmap_Nearest
        );
    else
    if (Texture_Filter_Min_Bilinear_Mipmap == isTFMin)
        glSamplerParameteri(m_idSampler
                                           , GL_Texture_Min_Filter
                                           , GL_Linear_Mipmap_Nearest
        );
    else
    if (Texture_Filter_Min_Trilinear == isTFMin)
        glSamplerParameteri(m_idSampler
                                           , GL_Texture_Min_Filter
                                           , GL_Linear_Mipmap_Linear
        );
    //
    //----
    m_idTF_Minification = isTFMin;
    m_idTF_Magnification = isTFMag;

___
___
___


___
___
___



_________________________________________________________________________
supersede:
  1. to take the place or position of something that is less efficient, lessmodern, or less appropriate, or cause something to do this

No comments:

Post a Comment