gpt4 book ai didi

opengl - Haskell OpenGL 绑定(bind)中似乎存在错误

转载 作者:行者123 更新时间:2023-12-04 22:00:26 25 4
gpt4 key购买 nike

我写了一个程序来可视化氢原子的电子云。

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

但是结果在图的右边部分有很多线。 enter image description here

我一遍又一遍地检查我的代码,没有发现任何错误。这是包的错误吗?

最佳答案

我猜这是因为浮点错误导致某些列在光栅化过程中丢失。您有 401 列样本分布在 400 列像素上,并且您的顶点位置作为整数发送。当整数在图形管道中转换为 float 时,they will not be exact .如果您将视口(viewport)和窗口大小更改为其他值,它应该看起来不错:

399x399:

enter image description here

400x400:

enter image description here

401x401(一对一像素采样):

enter image description here

402x402:

enter image description here

请注意,如果您增加所采集的样本数量,这也能正常工作:

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

另一种解决方法是使用浮点值顶点位置定位像素中心。改变

vertex (Vertex2 x y)

vertex (Vertex2 (fromIntegral x + 0.5) (fromIntegral y + 0.5) :: Vertex2 GLfloat)

关于opengl - Haskell OpenGL 绑定(bind)中似乎存在错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25758885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com