Haskell OpenGL: Transparency not working

Tag: opengl , haskell , transparency , rgba Author: jiaxin9912708 Date: 2012-12-23

I am making a very simple OpenGL application with Haskell, just making some polygons appear at the moment. My main function looks like this:

main :: IO ()
main = do
    (pname, _) <- getArgsAndInitialize
    createWindow $ "Haskellisa"
    initialDisplayMode $= [RGBAMode, WithAlphaComponent]
    displayCallback $= display

my 'display' function draws some triangles and sets the colors using a Color4 that has randomly generated RGBA values that are GLfloats between 0.0 and 1.0. Everything works but there is no transparency, overlapping polygons don't blend their colors.

I am drawing triangles using this function:

drawTri :: Tri Float -> Color4 GLfloat -> IO ()
drawTri ((x1,y1), (x2,y2), (x3,y3)) col = do
    renderPrimitive Triangles $ do
        color col
        vertex $ (Vertex3 (x1 :: GLfloat) (y1 :: GLfloat) 0)
        vertex $ (Vertex3 (x2 :: GLfloat) (y2 :: GLfloat) 0)
        vertex $ (Vertex3 (x3 :: GLfloat) (y3 :: GLfloat) 0)

Why isn't my transparency working here?

What is the depth buffer comparison? If you reject pixels with equal Z, and they all are at the same depth, no overlapping pixels will be rendered.
WithAlphaComponent enables GL_BLEND?

Best Answer

Im assuming [RGBAMode, WithAlphaComponent] is just to set the bit depth

I'm not sure how it's done in Haskell, but you have to call glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); and glEnable( GL_BLEND );. You will also have to draw your transparent faces back to front.

More on that here: http://www.opengl.org/wiki/Transparency_Sorting


It worked! Haskell Translation: blend $= Enabled' and blendFunc $= (SrcAlpha, OneMinusSrcAlpha)`
Also I had no idea why I was setting [RGBAMode, WithAlphaComponent] it was just a guess, I removed it and everything still works as I wanted it.
glad I could help :)

Other Answer1

Just add:

blend $= Enabled >>
blendFunc $= (SrcAlpha, OneMinusSrcAlpha) >>

to your display function.

It worked for me! :)