PDA

View Full Version : Texture transparency borders



Manel Goucha
11-30-2011, 06:41 AM
Hi, I'm using a texture applyed to a simple square drawn with GL_TRIANGLE_FAN like this:


glTranslate(X, Y, Z);
glRotate(A, 0, 0, 1);

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, Image);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glBegin(GL_TRIANGLE_FAN);
glNormal3f(0, 0, 1);
glTexCoord2f(0, 0); glVertex2f( - SizeX, - SizeY);
glTexCoord2f(1, 0); glVertex2f( + SizeX, - SizeY);
glTexCoord2f(1, 1); glVertex2f( + SizeX, + SizeY);
glTexCoord2f(0, 1); glVertex2f( - SizeX, + SizeY);
glEnd;

glRotate(-A, 0, 0, 1);
glDisable(GL_TEXTURE_2D);
glTranslate(-X, -Y, -Z);


but for some reason I get this result:

http://i.imgur.com/EwsU4.jpg

I've tried GL_CLAMP_TO_EDGE, GL_CLAMP, GL_CLAMP_TO_BORDER_ARB and GL_REPEAT but I always get the same result.

Is there any way to remove this border effect without changing the image tranparency?

appreciate any help

tksuoran
11-30-2011, 07:38 AM
Is the original image shown in full size, does your rendering code magnify or minify it?

Do you have mipmaps, and how are they generated?

Manel Goucha
11-30-2011, 08:45 AM
Even when scaled or at normal size, the image gets that result
I'm using this to scale:



GLSize = 200; <- zoom value
GLInvSize = 1/GLSize*300;
CreateViewport(GLSize, GLWidth, GLHeight);
ScreenWidth = GLSize * GLRatio * 0.5;
ScreenHeight = GLSize * 0.5;
glMatrixMode(GL_MODELVIEW);




CreateViewport(double Value; Integer Width; Integer Height);
begin
glMatrixMode(GL_PROJECTION);
glLoadIdentity;

Double Val = Width / Height;
glViewport(0, 0, Width, Height);
glOrtho((-Val/2)*Value, (Val/2)*Value, -0.5*Value, 0.5*Value, 0.01, 500);
end;


and this creates the texture:



glGenTextures(1, @Texture);
glPixelStorei(GL_UNPACK_ALIGNMENT, GL_RGB);
glBindTexture(GL_TEXTURE_2D, Texture);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, pData);


does it have to do with it?

mobeen
11-30-2011, 08:52 AM
Could u try GL_LINEAR or GL_NEAREST fitering for GL_TEXTURE_MIN_FILTER and tell us if u still get these lines. Another thing is, is blending enabled?

Manel Goucha
11-30-2011, 09:15 AM
Yes, I've tried all of these combinations:


glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
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_MIN_FILTER, GL_NEAREST);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

and still the same

Yes, this is what is enabled:


glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_ALWAYS, 0);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);

thanks

mobeen
11-30-2011, 09:42 AM
What output do u get if u comment all of the enables i.e.


/*
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_ALWAYS, 0);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
*/

Manel Goucha
11-30-2011, 09:59 AM
No alpha blending, wich gives me the entire image without transparency:

http://i.imgur.com/ibZ6F.jpg

Manel Goucha
11-30-2011, 01:29 PM
This is my entire code:

to create the viewer:

void CreateViewer(Handle ViewHandle; Double Value; Integer Width; Integer Height);
{
GLHandle = ViewHandle;
GLDC = GetDC(GLHandle);
PixelFormat(GLDC, 24);
GLRC = wglCreateContext(GLDC);
wglMakeCurrent(GLDC, GLRC);
BuildFont;

CreateViewport(Value, Width, Height);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
ColorDS(1, 1, 1, 1);

glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_ALWAYS, 0);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glShadeModel(GL_SMOOTH);
glClearColor(0, 0, 0, 0);
glClearDepth(1);Buffer Setup
glEnable(GL_DEPTH_TEST);Testing
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
};

void CreateViewport(double Value; Integer Width; Integer Height);
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity;

Double Val = Width / Height;
glViewport(0, 0, Width, Height);
glOrtho((-Val/2)*Value, (Val/2)*Value, -0.5*Value, 0.5*Value, 0.01, 500);
};

to draw a square:

void Draw;
{
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glLoadIdentity;
glTranslate(0, 0, 0);

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, Image);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glBegin(GL_TRIANGLE_FAN);
glNormal3f(0, 0, 1);
glTexCoord2f(0, 0); glVertex2f( - SizeX, - SizeY);
glTexCoord2f(1, 0); glVertex2f( + SizeX, - SizeY);
glTexCoord2f(1, 1); glVertex2f( + SizeX, + SizeY);
glTexCoord2f(0, 1); glVertex2f( - SizeX, + SizeY);
glEnd;

glDisable(GL_TEXTURE_2D);

SwapBuffers(wglGetCurrentDC);
};

and it gives me a bordered texture on the square limits like the image.
I've searched for this but I can't find anything that can help.

Manel Goucha
12-01-2011, 09:29 PM
I really need any help solving these, I cent' seem to find anything related on this.

this is an important project that I'm working on

Thanks for the help so far

Alfonse Reinheart
12-02-2011, 01:00 AM
and this creates the texture:

There are many things wrong there.


glPixelStorei(GL_UNPACK_ALIGNMENT, GL_RGB);

This should have given you an OpenGL error. UNPACK_ALIGNMENT takes integer values, not enumerators. And GL_RGB is certainly not 1, 2, 4, or 8, which are the only allowed values of the UNPACK_ALIGNMENT.


glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

As previously mentioned, if you want to get exact values from your texture, you don't want LINEAR filtering. Just NEAREST.

I suspect however that your *real* trouble is this:


gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, pData);

I'm guessing your copy-and-pasted this code from some website. So that's what you get for regurgitating code without knowing where it came from or what it does. At all times be aware of what functions are and what they do. If you don't know, look up the information online; don't just copy some code that works and poke at it, hoping that it'll work for you.

In any case, there are two problems with using this function. The first is conceptual: if you want texel-accurate results (no filtering), why are you building mipmaps? That kinda defeats the whole point of texel-accurate results; you're wasting precious texture space on mipmaps that you don't need and will never use.

But that's minor compared to the real problem. If "Width" and "Height" are not powers of two (and I'm guessing that they aren't, in your case), then `gluBuild2DMipmaps` will scale your image up to the nearest power of 2. This scaling will necessarily blur the various pixels in the image.

The silly part of course, is that most GL implementations will handle arbitrarily sized textures just fine. But because you're using `gluBuild2DMipmaps` to load your texture, you can't use that feature. It will automatically tell OpenGL that the texture is bigger than it really is, and it will scale up your data accordingly.

So, the lesson here (besides not using code you don't understand) is: avoid `gluBuild2DMipmaps` unless you have no other choice. If you need mipmaps, then either generate them offline (perhaps in a DDS file) and load them with `glTex(Sub)Image2D`, or use `glTex(Sub)Image2D` to load the base layer and use `glGenerateMipmap` (which requires GL 3.0 or above, or ARB_framebuffer_object) to generate the rest.

Manel Goucha
12-02-2011, 01:23 PM
So after all, and I don't know much about OpenGL, should I remove glPixelStorei(GL_UNPACK_ALIGNMENT, GL_RGB) line from the code or replace the GL_RGB for other enumerator?

And I'm using mipmaps because I constatnly use diferent zooms and Texture scalings but yes you were right, I'm using diferent sizes, other than multiples of two, and that corrected the error, but is there any way I can create textures with the size I want?

thks again for the help

BionicBytes
12-02-2011, 04:34 PM
So after all, and I don't know much about OpenGL, should I remove glPixelStorei(GL_UNPACK_ALIGNMENT, GL_RGB) line from the code or replace the GL_RGB for other enumerator?

You are using RGB textures yes? So you need a value of 1 so the texels are byte aligned.

cobriaran
08-11-2012, 04:24 PM
You are using RGB textures yes? So you need a value of 1 so the texels are byte aligned.
Hi, Im using everything that you recommended to avoid weird lines in textures but I can avoid them:
C:\Users\antonio.briceno@gameloft.com\GameloftWork \ErrorSpaceVille\BordersBad.png