Question

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?

Was it helpful?

Solution

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

OTHER TIPS

Just add:

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

to your display function.

It worked for me! :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top