Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL triangles unwanted edge border

Tags:

haskell

opengl

I'm trying to render a square with texture. It's working, except I'm getting a pixelated line on the diagonal edge where two triangles meet each other.

The texture:

Texture used on the squares

The render result of three squares, each square is made of two triangles:

Render result

The line only shows up on diagonal lines. Nothing appears on the vertical or horizontal edges.

Code:

import qualified Graphics.Rendering.OpenGL as GL
import qualified Graphics.UI.GLFW          as GLFW

Initialization:

GL.lineSmooth    $= GL.Enabled
GL.polygonSmooth $= GL.Enabled
GL.blend         $= GL.Enabled -- A
GL.blendFunc     $= (GL.SrcAlpha, GL.OneMinusSrcAlpha)
GL.lineWidth     $= 1.5

Loading the texture:

GL.texture GL.Texture2D $= GL.Enabled
(texName:_) <- GL.genObjectNames 1
GL.textureBinding GL.Texture2D $= Just texName
GL.textureFilter GL.Texture2D $= ((GL.Nearest, Nothing), GL.Nearest)
_ <- GLFW.loadTexture2D "wall.tga" [GLFW.BuildMipMaps]

Rendering the quad:

GL.textureBinding GL.Texture2D $= Just texName
GL.renderPrimitive GL.TriangleStrip $ do
    GL.texCoord $ GL.TexCoord2 0 (1::GL.GLfloat)
    GL.vertex $ vertex3 20 0 0
    GL.texCoord $ GL.TexCoord2 0 (0::GL.GLfloat)
    GL.vertex $ vertex3 20 20 0
    GL.texCoord $ GL.TexCoord2 1 (1::GL.GLfloat)
    GL.vertex $ vertex3 0 0 0
    GL.texCoord $ GL.TexCoord2 1 (0::GL.GLfloat)
    GL.vertex $ vertex3 0 20 0
GL.textureBinding GL.Texture2D $= Nothing

I've tried rendering GL.Polygons and GL.Quads: same result.

They go away when I comment the line tagged with -- A. Why?

like image 291
Thiago Negri Avatar asked Jan 07 '13 18:01

Thiago Negri


1 Answers

The problem is with the line GL.polygonSmooth $= GL.Enabled. It seems to smooth all edges of the polygon, even if there is another edge over it.

Removing this line works, even maintaining the GL.blend $= GL.Enabled.

The Common Mistakes page at OpenGL wiki says:

[Polygon smooth] is not a recommended method for anti-aliasing. Use Multisampling instead.

like image 124
Thiago Negri Avatar answered Oct 13 '22 07:10

Thiago Negri