상세 컨텐츠

본문 제목

[OpenGL] GL_POLYGON_SMOOTH 안티 엘리어싱 적용후 네모난 사각형도형의 POLYGON 또는 GL_QUADS의 텍스쳐에 깨진 대각선 선이 보이는경우

프로그래밍 관련/3D,2D DRAW 관련

by AlrepondTech 2017. 6. 8. 09:40

본문

반응형

 

 

 

=================================

=================================

=================================

 

 

 

 

네모난 사각형 POLYGON 또는 GL_QUADS 으로 그려줄때 GL_POLYGON_SMOOTH 안티 엘리어싱 적용후 텍스쳐에 깨진 대각선 선이 보이는경우

 

ex) 이런식으로  glBegin(GL_QUADS); glTexCoord2d(0.0f, 0.0f);  glVertex2d(0, 0); glTexCoord2d(1.0f, 0.0f);  glVertex2d(

width

, 0); glTexCoord2d(1.0f, -1.0f); glVertex2d(

width

, -

height

); glTexCoord2d(0.0f, -1.0f); glVertex2d(0, -

height

);

 

glEnd();
또는
glBegin(

GL_POLYGON

); glTexCoord2d(0.0f, 0.0f);  glVertex2d(0, 0); glTexCoord2d(1.0f, 0.0f);  glVertex2d(

width

, 0); glTexCoord2d(1.0f, -1.0f); glVertex2d(

width

, -

height

); glTexCoord2d(0.0f, -1.0f); glVertex2d(0, -

height

);

 

glEnd();

 

 

 

GL_POLYGON_SMOOTH 안티 엘리어싱 적용후 텍스쳐가 대각선 선이 보이는경우

 

glEnable(GL_POLYGON_SMOOTH);

 

 

 

 

 

------------------------------------------------------------------------------------------------------------------------------------------------------해결방안

 

OpenGL로 도형을 그릴시 앞부분에 glClearColor 을 해줄때

 

glClearColor(1.0, 1.0, 1.0, 0.0); <- 이런식으로 알파값이 "0" 인경우 깨지는 경우가 있다.

 

GL_POLYGON_SMOOTH  적용시 텍스쳐에 깨진 대각선이 보이는경우가 있다 


""  glClearColor(0.5, 0.5, 0.5, 1.0);  "" 이런식으로 값의 (0.5)RGB와 (1.0)알파값을 모두 채운채로 옵션을 설정해주면

 

깨진 대각선이 해결되는 경우가 있다.

 

 

 

 

 

 

=================================

=================================

=================================

 

 

 

 

GL_POLYGON_SMOOTH 텍스쳐 깨짐

 

https://gamedev.stackexchange.com/questions/110286/how-to-fix-texture-edge-artefacts

 

번역: https://translate.googleusercontent.com/translate_c?depth=1&hl=ko&ie=UTF8&prev=_t&rurl=translate.google.co.kr&sl=auto&sp=nmt4&tl=ko&u=https://gamedev.stackexchange.com/questions/110286/how-to-fix-texture-edge-artefacts&usg=ALkJrhg369qAtub8K8B7mrjLM18OoadJrg

 

 

 

 

This issue has been annoying me for a long time now and even after reading a lot of articles about it, I am still unable to fix the issue.

First, to my setup. I'm using LWJGL for a 2D project, rendering in immediate mode (yes, I know, FBOs, but I feel like it's too late to change it now). Textures can be scaled, rotated tinted and cropped in any way and are read from packed spritesheets that are multiples of 2 (the one in question is 512x1024 for example).

The Problem

Because there is no smoothing applied to the textures, scaled and rotated textures look quite ugly, showing jagged lines and black outlines. I tried improving it with using glEnable(GL_POLYGON_SMOOTH)(even though I know now that this is apparantly a bad practice, but I have no idea how to add multisampling to immediate mode rendering, other sites even state that multisampling is probably overkill for 2D games, which leaves me even more confused). This is what it looks like, with and without POLYGON_SMOOTH:

 

Quite clearly, there are multiple issues visible:

  1. With POLYGON_SMOOTH disabled, there are horribly jagged lines and dark edges (that aren't there in the texture)
  2. With POLYGON_SMOOTH enabled, the edges are no longer jagged, but the dark edges are still there and now there are those diagonal lines. After reading up on that a bit, I found out that it has something to do with how graphics cards render quads, which is as two triangles, and when smoothing is applied, this happens. To test that out, instead of drawing a single quad I draw two triangles which indeed moves the diagonal lines around.

The dark edges seem to be caused by something like like pre-multiplied alpha (according to this), but when I check the raw pixel data, it doesn't look pre-multiplied to me. A completely white texture with 0.5f alpha for example is represented as (in ARGB) 0x80FFFFFF. So applying the "fix" (using glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA) instead of the default glBlendFunc(GL_SRC, GL_ONE_MINUS_SRC_ALPHA)suggested in the article doesn't really work, because my textures aren't premultiplied alpha, so alpha components don't work anymore (same texture, black background):

Texture without alpha:

 

 

 

 

 

I do use custom shaders, so I guess I probably could make alpha work in some way by passing it to the shader and then multiplying the color by it, would that fix the dark edges (which would enable me to use GL_ONE, the "better" blend function)? That would still leave me with the issue of the diagonal lines though, which I seem to be able to move around, but not remove.

 

Don't use POLYGON_SMOOTH as you just can't get proper antialiasing with that. Instead enable antialiasing for the surface. Check LWJGL documentation on how to enable that. – msell Oct 25 '15 at 12:45

 

 

    
@msell Thanks for your comment. What exactly is the problem with POLGYON_SMOOTH? Why is it still there if it doesn't work properly? What do you mean with surface anti aliasing? I never heard of that before, and a simply googling for it doesn't seem to give me proper results, just general threads about antialiasing. – 1337 Oct 25 '15 at 12:48
    
POLYGON_SMOOTH is there for backwards compatibility and those rare cases when someone knows where and how to apply it – Kromster Oct 25 '15 at 15:53
1  
I mean MSAA. You didn't specify the version of LWJGL you are using. In LWJGL 2 you enable it by providing number of samples for PixelFormat when calling Display.create(...),. LWJGL 3 uses GLFW for window management, where you enable multisampling with glfwWindowHint(GLFW_SAMPLES, ...). – msell Oct 26 '15 at 5:57 
    
@msell Sorry, forgot to add that. I'm using LWJGL 2.9.0.
 
 
 

 

To get rid of the jagged lines add 1 line of transparent pixels around the rectangle texture.

The issue with the dark lines is that you are most likely using black transparent pixels (color #000000). The issue is most likely to be gone if you use colored transparency instead. - Repeating the border pixels of your sprite - but simply with alpha=0

 

 

I'm kind of already doing the first thing. This is how the spritesheet looks like: imgur.com/WsIM6rd (look at the 1 pixel transparent border). I don't quite understand how that would help with the jagged lines though, isn't some kind of anti-aliasing still required? As for the black transparent pixels, that seems very plausible, I read about that in multiple places. How would I go about doing that? What do you mean with the border of the sprite? Where the transparancy starts (the visible shape) or the literal border of the image (the rectangle)? How could I apply that? Thanks :) – 1337 Oct 25 '15 at 18:03 
    
Update: Wow, you were right, that actually fixed both issues, I'm impressed. Is that actually common practice? I can't imagine that everybody doing this kind of thing does it to be honest. But hey, it works. So now I just need to find a way to do that for textures that aren't rectangularm (well the image is, but the visible area isn't) and apply that to all the assets. Is there any nice way for doing that? – 1337 Oct 25 '15 at 19:05
    
You can use TexturePacker. It contains the features you need. See codeandweb.com/blog/2015/09/16/… Make sure to enable "Reduce border artifacts". – Andreas Löw Oct 25 '15 at 20:59 
    
That's quite an amazing piece of software. Had I only known about it two months ago... unfortunately I already wrote my own version of a texture packer, which is obviously far less advanced. So, I realize that this is probably kind of the point of selling the software and I don't need to know exactly how it was written, but a basic idea of how it works might help :) – 1337 Oct 25 '15 at 21:09

 

 

 

 

 

=================================

=================================

=================================

 

 

 

 

출처: https://stackoverflow.com/questions/9314148/opengl-gl-polygon-smooth-2d-antialiasing-creating-tris-out-of-quads

 

 

OpenGL GL_POLYGON_SMOOTH 2D Antialiasing creating tris out of quads

 

UPDATE:

I have located it to when I install NVIDIA Control Panel, if I uninstall it it works properly.

When you rotate a quad in OpenGL the edges become jagged.

If I call glEnable(GL_POLYGON_SMOOTH) the edges become smooth, but OpenGL then draws a white diagonal line through all my images as if it's creating trisout of my quads.

This is how it looks: 

 

 

Is there a way to disable that line, or can I get antialiasing in another easy way? I tried GL_MULTISAMPLE but nothing happened.

In my code I have also:

glShadeModel(GL_SMOOTH);  glMatrixMode(GL_PROJECTION);  glLoadIdentity();  glDisable(GL_DEPTH_TEST);  glEnable(GL_BLEND);  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

It looks like the vertices on the two triangles just don't match up, creating the thin line. Instead of drawing a quad, try drawing a triangle fan. IMO, this line should go away because OpenGL now know which vertices are shared (and exactly equal). – Ani Feb 16 '12 at 15:53
    
Hmm okay, I understand the not matching up part. But I don't understand how I would draw two triangles holding half the image each and then rotating/scaling those individually. at the moment I draw the texture onto a regular quad: I'm pretty new to OpenGL and I haven't worked with triangles at all since I only work with 2D and images. So please point me in the direction of how draw a triangle fan. Thanks, Markus – OrujimaruFeb 16 '12 at 16:24
    
Have you tested this on more than one computer with different graphics cards and/or tested with different drivers? It looks to me like it could be a driver bug. – Jerry Coffin Feb 16 '12 at 16:45
    
It looks like your blend function is the issue. Have you tried other options from opengl.org/sdk/docs/man/xhtml/glBlendFunc.xml ? Specifically, something like glBlendFunc(GL_SRC_ALPHA_SATURATE,GL_ONE) ? – Kyle Feb 16 '12 at 18:48 
    
@Jerry Coffin, Good call, I tested it on my laptop and the white lines were gone. I've downloaded 3 different drivers for my desktop, 285.62, 295.51 and 295.53 but there's no difference. I use 460SLI and tried disabling one at a time but it still doesn't work. Do you have any suggestion of what I could try to fix this? Thanks, – Orujimaru Feb 16 '12 at 18:52 
    
@Orujimaru: If updating drivers does no good, about all you can do is complain to the vendor, and see if they will fix it or suggest a workaround. Those sound like nVidia version numbers, and I've had pretty good response from them in the past (but obviously can't guarantee anything). Some depends though -- they seem to put a lot more effort into their Windows drivers than Linux drivers. – Jerry Coffin Feb 16 '12 at 18:59 
    
@Jerry Coffin: Sorry, yes those are nvidia drivers for windows. 285 is the latest stable build and the 295 ones are more recent beta builds. I'll contact Nvidia. Thanks a lot for your help! //Markus – Orujimaru Feb 16 '12 at 19:07

 

 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

 

Ok, I'll write this up as an answer, let me know if it works after trying it out.
GL_TRIANGLE_FAN: If the application specifies a sequence of vertices v, OpenGL renders a triangle using v 0, v 1, and v 2; another triangle using v 0, v 2, and v 3; another triangle using v 0, v 3, and v 4; and so on. If the application specifies n vertices, OpenGL renders n–2 connected triangles.
So to draw a unit quad centered around the origin,
glBegin(GL_TRIANGLE_FAN); glTexCoord2f(0f, 0f); glVertex3f(-halfWidth, -halfHeight, 0f); glTexCoord2f(0f, 1f); glVertex3f(-halfWidth, halfHeight, 0f); glTexCoord2f(1f, 1f); glVertex3f(halfWidth, halfHeight, 0f); glTexCoord2f(1f, 0f); glVertex3f(halfWidth, -halfHeight, 0f); glEnd();
Now you can put the same transformations around this that you used around your quad! Hope that helps!

 

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

Sorry, but this gives the exact same output as drawing a quad. Thanks for the effort though. //Markus – Orujimaru Feb 16 '12 at 18:11
    
At this point I begin to suspect the driver and/or GPU as @jerrycoffin says above. Try it on some other GPU/driver. – Ani Feb 16 '12 at 18:23
    
Yep, I'm pretty sure that's it. Which is really strange. I have never had any trouble with mu GPU's at all since I bought them 18 month ago. I tried a few different drivers, but that didn't help. I tried disabling one GPU at a time but that didn't help either. I have no idea how to troubleshoot the GPU itself. I can live with it if it's just my computer, but since I'm hoping to release the game I'm working on it's not acceptable if this occurs frequently =/ – Orujimaru Feb 16 '12 at 19:05

 

 

 

 

 

 

 

=================================

=================================

=================================

 

 

 

 

 

 

 

 

반응형


관련글 더보기

댓글 영역