There seems to be a bug in Haskell OpenGL binding
我编写了一个程序来对氢原子的电子云进行可视化。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | import System.Exit import Graphics.UI.GLUT probDensity :: Double -> Double probDensity r = abs $ (1 - r) * exp (-r/2.0) myInit :: IO () myInit = clearColor $= Color4 1 1 1 0 grid :: [(GLint,GLint)] grid = [(x,y) | x <- [-200..200],y <- [-200..200]] density :: [Double] density = map (\\(i',j') -> probDensity $ sqrt $ (fromIntegral i' ** 2 + fromIntegral j' ** 2 ) / 324) grid cloud = zip density grid display :: DisplayCallback display = do clear [ColorBuffer] color $ Color4 1 1 1 (0::GLfloat) renderPrimitive Points $ mapM_ (\\(c,(x,y)) -> color (Color3 c c 0) >> vertex (Vertex2 x y)) cloud flush idle :: IdleCallback idle = postRedisplay Nothing reshape :: ReshapeCallback reshape (Size _ _) = do viewport $= (Position 0 0, Size 400 400) matrixMode $= Projection loadIdentity ortho2D (-200.0) 200.0 (-200.0) 200.0 matrixMode $= Modelview 0 loadIdentity keyboard :: KeyboardMouseCallback keyboard (Char '\\27') Down _ _ = exitSuccess keyboard _ _ _ _ = return () main :: IO () main = do (_, _args) <- getArgsAndInitialize initialDisplayMode $= [ RGBMode ] initialWindowSize $= Size 400 400 initialWindowPosition $= Position 100 100 _ <- createWindow"Cloud" shadeModel $= Smooth myInit displayCallback $= display reshapeCallback $= Just reshape keyboardMouseCallback $= Just keyboard idleCallback $= Just idle mainLoop |
但是结果在图形的右侧有很多行。
我一次又一次地检查代码,没有发现任何错误。
这是程序包的错误吗?
我猜这是因为浮点错误导致在栅格化过程中某些列被遗漏了。您有401列样本分布在400列像素上,并且顶点位置以整数形式发送。当整数在图形管线中转换为浮点数时,它们将不是精确的。如果将视口和窗口的大小更改为其他值,则看起来应该很好:
399x399:
400x400:
401x401(要采样的一对一像素):
402x402:
注意,如果您要增加采样数量,这也可以正常工作:
1 2 3 4 5 6 7 | grid = [(x,y) | x <- [-400..400],y <- [-400..400]] density = map (\\(i',j') -> probDensity $ sqrt $ (fromIntegral i' ** 2 + fromIntegral j' ** 2 ) / 648) grid renderPrimitive Points $ mapM_ (\\(c,(x,y)) -> do color (Color3 c c 0) vertex (Vertex2 (fromIntegral x / 2) (fromIntegral y / 2) :: Vertex2 GLfloat)) cloud |
另一种解决方法是使用浮点值顶点位置定位像素中心。变更
1 | vertex (Vertex2 x y) |
到
1 |