View Full Version : Draw Border around texture - How ?

mehmet osi
01-27-2006, 11:17 PM

I want to draw a border (color is not important, want to see the border) around a texture using border parameter of glTexImage. When border parameter is 1, border will be drawn, and border parameter is 0, border will not be drawn.
Around the investigation and manuals, it seems the below might work, but it didnt.

GLuint tex[1];

glOrtho(0.0, TU_RC_WIDTH, 0.0, TU_RC_HEIGHT, -1.0, 1.0);

glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

GLubyte imageWithBorder[130][130][4];

for (i=0;i<130;i++)
for (j=0;j<130;j++)



glTexImage2D(GL_TEXTURE_2D, 0, 4, 130, 130, 1, GL_RGBA, GL_UNSIGNED_BYTE,

glTexCoord2f(0.0, 1.0); glVertex2f(100.0, 100.0);
glTexCoord2f(1.0, 1.0); glVertex2f(300.0, 100.0);
glTexCoord2f(1.0, 0.0); glVertex2f(300.0, 300.0);
glTexCoord2f(0.0, 0.0); glVertex2f(100.0, 300.0);
glEnd();This code does not draw a border (at Windows platform. )
what is wrong here ? Is there a way to draw (enable / disable) the border using border parameter ?

(Note: without giving GL_CLAMP_TO_BORDER as parameter to glTexParameter)


01-28-2006, 04:51 PM
Why not just set the edge texels to be your chosen colour when filling out your texture array?

As to why your example might not work, well the border is initially set to be of colour r=0,g=0,b=0,a=0

So you may not notice it.

Try adding this to your code:

float colour[4] = {0.0f, 0.0f, 1.0f, 1.0f};
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, colour);

mehmet osi
01-29-2006, 10:02 PM
as you say, that default border color is black.
My color is yellow, it must be seen at the edges. But it does not.

Also i tried adding your code line, it doesn't work either.

01-30-2006, 12:27 PM
Out of interest, is there a specific reason why your texture is 130x130?

If your card / drivers dont support nonPowerOfTwo textures, then perhaps that could be giving be causing your problem?

01-30-2006, 07:24 PM
mehmet osi,
"border" param in glTexImage2D is for calculating correct linear interpolation at the edges in order to avoid seams.

Have you seen noticable edge lines (seams) on skybox of old games? It results from linear interpolation error at the edges.

In your case with yellow borders and bilinear filtering, you will see yellowish colour is blended very little around edges. But it cannot be a single pixel solid line.

If you still want to see border lines, you need to add border onto the original image, or to draw extra lines around polygon.

BTW, the size of texture becomes non-power of 2 if you set border to 1, for example, 128x128 to 130x130. It may badly hurt the performance.

It is better to use glTexParameter*() with GL_TEXTURE_BORDER_COLOR, because the texture size remains power of 2. The drawback is you can only use a single colour with it.

mehmet osi
01-31-2006, 06:34 AM
"border" param in glTexImage2D is for calculating correct linear interpolation at the edges in order to avoid seams. I'm curious about the job of border parameter of glTexImage2D. Since it seems useless. When i give border parameter "1", it interpolates the edge pixels, when i give border parameter "0", it interpolates again the edge pixels with texture color.
So what is the job of border parameter ?
(the only usage it seems is changing the width and height of the texture !!!)


01-31-2006, 09:24 AM
"border" is very useful to remove visual artifacts around edges.
See the screen shot;

Monotone yellow texture is applied on both quads. You can notice black lines around edges on the left side. It is artifacts caused by interpolation error. (by default, border color is black)

On right side, I added yellow border to remove the artifacts with GL_TEXTURE_BORDER_COLOR in glTexParameter*().

glTexParameter*(...GL_TEXTURE_BORDER_COLOR...) is valid only for single color border, but not good for arbitrary real textures. That's why glTexImage2D(...border...) needed to control random colors around edges.

01-31-2006, 12:22 PM
Border indicates whether texture border has image data associated with it for the GL_CLAMP filtering. The border must be specified with the image.

Textures also have a border color as a parameter. This is used if no border image data specified, however due to legacy spec & implementation issues this is often ignored (there are often desktop driver settings to 'do the right thing' or 'emulate the legacy bug' on GL_CLAMP border filters).

There is a new token to get rid of the undesirable borders in the situations where your driver/graphics hardware is 'doing the right thing'. That is GL_CLAMP_TO_DEGE, where you can eliminate off colored borders and have correctly filtered clamped edges on textures, without border image data and ignoring border color.

mehmet osi
02-06-2006, 04:24 AM