Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell OpenGL: Transparency not working

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
    mainLoop

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?

like image 552
Sam Stern Avatar asked Feb 18 '23 22:02

Sam Stern


1 Answers

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

like image 55
Luke B. Avatar answered Feb 27 '23 15:02

Luke B.