PDA

View Full Version : Unable to set sampler states when get the shader's sampler parameter.



Spankenstein
09-05-2012, 04:01 AM
I can set my texture with the code below but the part that sets the sampler parameters has no effect on the texture drawn (it is still linear lookup)



// Get sampler
GLuint textureSamplerParam = glGetUniformLocation(m_effect, "AtlasTextureSampler");

// Bind our texture in Texture Unit 0
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);

// Bind the texture to the sampler
glBindSampler(texture, textureSamplerParam);

// Set sampler parameters
glSamplerParameteri(textureSamplerParam, GL_TEXTURE_WRAP_S, GL_CLAMP);
glSamplerParameteri(textureSamplerParam, GL_TEXTURE_WRAP_T, GL_CLAMP);
glSamplerParameteri(textureSamplerParam, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glSamplerParameteri(textureSamplerParam, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);


I'm currently automating my texture loading through the glload library, does that affect the texture at all?



std::string filename(LOCAL_FILE_DIR);
filename.append("TextureAtlasTexture.png");

// 'glload' must be initialised for 'glimg::CreateTexture' to work
if (glload::LoadFunctions() == glload::LS_LOAD_FAILED)
{
// Initialisation failed
}

std::auto_ptr<glimg::ImageSet> pImgSet(glimg::loaders::stb::LoadFromFile(filename ));

m_texture = glimg::CreateTexture(pImgSet.get(), 0);


I have tried creating it myself but that returns nothing but an array of 0's. The dimensions of the array match that of the image though.



std::auto_ptr<glimg::ImageSet> pImgSet(glimg::loaders::stb::LoadFromFile(filename ));

glimg::SingleImage image = pImgSet->GetImage(0, 0, 0);
glimg::Dimensions dims = image.GetDimensions();

// Allocate a new texture
glGenTextures(1, &m_texture);

// Bind the new texture
glBindTexture(GL_TEXTURE_2D, m_texture);

// Bind the data to the currently bound texture
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RGBA8,
dims.width,
dims.height,
0,
GL_RED,
GL_UNSIGNED_BYTE,
image.GetImageData() // Returns nothing but an array of '0'
);

// Automatically generate mipmaps
glGenerateMipmap(GL_TEXTURE_2D);

// Unbind texture
glBindTexture(GL_TEXTURE_2D, 0);

Alfonse Reinheart
09-05-2012, 06:39 AM
glBindSampler(texture, textureSamplerParam);

You don't bind samplers to texture objects. You bind them to the context. The first parameter to `glBindSampler` (http://www.opengl.org/wiki/GLAPI/glBindSampler) is the texture unit you want to bind the sampler to, not a texture object name.

Also, you don't have to bind samplers in order to modify them. That's why `glSamplerParameter` (http://www.opengl.org/wiki/GLAPI/glSamplerParameter) takes a sampler object and not a context target. So there's no reason to constantly change your sampler object in your rendering function. You should set it up once, after you create it, then just bind it when it's time to render.

Spankenstein
09-05-2012, 08:09 AM
Thanks Alfonse, let me see if I've understood you correctly.

My texture setting function now looks like



void QuadrangleForward::setTexture(GLuint texture)
{
// Texture is set separately as it doesn't always need to be set each time 'apply' is called

// Get sampler
GLuint textureSamplerParam = glGetUniformLocation(m_effect, "AtlasTextureSampler");

// Set sampler texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D , texture);
glUniform1i(textureSamplerParam, 0);
}


The sampler states are set to the texture



// Select texture object
glBindTexture(GL_TEXTURE_2D , m_texture);

// Set sampler states
glTexParameteri(GL_TEXTURE_2D , GL_TEXTURE_WRAP_S , GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D , GL_TEXTURE_WRAP_T , GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_NEAREST_MIPMAP_NEAREST);

// Switch to default texture object
glBindTexture(GL_TEXTURE_2D , 0);


Which leaves me with the next question. How can I change the sampler state for different draw calls using the following?



glSamplerParameteri(textureSamplerParam, GL_TEXTURE_WRAP_S, GL_CLAMP);
glSamplerParameteri(textureSamplerParam, GL_TEXTURE_WRAP_T, GL_CLAMP);
glSamplerParameteri(textureSamplerParam, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glSamplerParameteri(textureSamplerParam, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);


EDIT:

I've got it.... Sampler Objects! Forget the last question :)