PDA

View Full Version : Texture border problem



Devdept2
04-10-2012, 06:26 AM
Hi,

I'm having problems using textures with border.

If I run my application with software OpenGL (1.1) everything works fine, but if I enable hardware acceleration the graphics freezes and I don't see anything.

I'm loading a 258x258 RGBA texture (256x256 with 1 border pixel).

I have an ATI FirePro v3750, but I tested it also on a RADEON HD 5450 and the problem is the same.

This is how I generate the texture object:



uint[] names = new uint[1];
glGenTextures(1, names);
uint Name = names[0];
glBindTexture(GL_TEXTURE_2D, Name);
glTexParameteri(GL_EXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

Bitmap bitmap = ...; // 258x258 RGBA texture
BitmapData data = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);


glTexImage2D(gl_TEXTURE_2D, (int)0, GL_RGBA8, (int)data.Width, (int)data.Height, 1, GL_BGRA, GL_UNSIGNED_BYTE, (IntPtr)data.Scan0);


And this is how I draw a quad with the texture:



glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D, texture.Name);

glBegin(GL_QUADS);

glTexCoord2d(0, 1);
vertices[0].DrawGL();
glTexCoord2d(1, 1);
vertices[1].DrawGL();
gl.TexCoord2d(1, 0);
vertices[2].DrawGL();
glTexCoord2d(0, 0);
vertices[3].DrawGL();
glEnd();

glBindTexture(GL_TEXTURE_2D, 0);

glDisable(GL_TEXTURE_2D);


With Step by Step debugging I saw that the glBindTexture() call is taking much time.

What could be the problem, expecially given that in software OpenGL it works fine?

BionicBytes
04-10-2012, 07:31 AM
I'm loading a 258x258 RGBA texture (256x256 with 1 border pixel).
Could be a cut/paste error - but


Bitmap bitmap = ...; // 258x258 RGBA texture
BitmapData data = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

So is the bitmap 256*256 or 258*258? Your comments indicate that it's the latter in which case you don't need the border in glTextImage2D.



glBindTexture() call is taking much time

How much time?
...and what happens if you set the glTexImage border to 0?

Devdept2
04-10-2012, 07:54 AM
Hi,

If I set the glTexImage border to 0 it works fine.

It seems that is the unbind of the texture that takes time (around 2 seconds):



gl.BindTexture(gl.TEXTURE_2D, 0);


The image is 258x258: it's built from splitting a larger bitmap in 256x256 sub bitmaps, and adding a 1 pixel border around each one(with the rows/columns of the adjoining textures).

I need to use the texture border to have a correct linear filtering in the bitmaps junctions (and it works fine without hardware acceleration).

BionicBytes
04-10-2012, 10:25 AM
so if the bitmap is already 258 * 258 then there is no need to specify a border with gltextImage2D.
Problem solved.

Texture borders are a very rare thing; there may well be some driver bugs. Best thing to do is add the borders to the actual bitmap so that there is at least 1 pixel gap around the edges. Either that or change the clamping properties.

Devdept2
04-11-2012, 12:11 AM
Ok, after reading this post (http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=163821) I learned that texture borders are badly supported and that I can do without them by using a power of 2 texture and changing the texture coordinates of the corners of my quad like this:

0.5/TEXTURE_DIMENSION

1 - 0.5/TEXTURE_DIMENSION


Thank you anyway.