How to get GL_CLAMP_TO_BORDER effect?

#1

Hi All,

I am very new to OpenGL and for an application I am loading images as textures. One issue I am having is that I get this undesired ‘GL_CLAMP_TO_EDGE’ result (please look at attachment) when zooming out of my texture. The ‘GL_CLAMP_TO_BORDER’ result is exactly what I need but it is no longer in OpenGL. Does anyone know how I can achieve this?

#2

GL_CLAMP_TO_BORDER has never been removed; that clamps to the border color. It’s GL_CLAMP (which clamped to the border texels) which was removed, since border texels no longer exist.

#3

Thanks for getting back.

These are my texture parameters:

	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_WRAP_S, GL_CLAMP); //read outside texture width (clamping method)
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); //read outside texture height(clamping method)

Is it possible for me to get the desired effect I need through adjusting parameters on here?

#4

Are you using OpenGL ES, which has GL_CLAMP_TO_EDGE but lacks GL_CLAMP_TO_BORDER (and in fact lacks border colours altogether)?

If you are, you’ll have to implement this in the fragment shader: check whether either texture coordinate lies outside the range [0,1] and if so, use a colour read from a uniform variable in place of the value sampled from the texture. If you’re using mipmaps, note that the texture() call needs to be outside of the conditional in order for implicit derivatives (required for mipmap level selection) to be calculated correctly.

If you’re using desktop OpenGL, just use GL_CLAMP_TO_BORDER instead of GL_CLAMP, and use glTexParameter(GL_TEXTURE_BORDER_COLOR) to set the border colour.

#5

Yes: you adjust GL_CLAMP to GL_CLAMP_TO_BORDER.

#6

Thanks GClements, yes that was my issue I am not seeing GL_CLAMP_TO_BORDER.
I will try what you have mentioned.

#7

Both CLAMP and CLAMP_TO_BORDER clamp to border texels (if they exist, else border color.) The difference is CLAMP clamps the filter weight half-way into the border.

For OpenGL ES there are NV, EXT, and OES extensions restoring CLAMP_TO_BORDER.