gpt4 book ai didi

opengl - 使用基于递归的动画时在 drawPicture "Gloss/OpenGL Stack Overflow "之后出现错误 .""

转载 作者:行者123 更新时间:2023-12-02 03:31:15 25 4
gpt4 key购买 nike

为了弄乱 Haskell 的 Gloss 库,我写道:

import Graphics.Gloss

data World = World { worldBugs :: [Picture] }

bug :: Point -> Float -> Picture
bug (x, y) s =
let head = Translate x (y - s) $ Circle (s * 0.8)
body = Translate x (y + s) $ Circle (s * 1.2)
in pictures [head, body]

main = play (InWindow "Animation Test" (400, 400) (100, 100)) white 10
(World . map (\(n,b) -> Translate (n * 20) (n * 20) $ b) $ zip [0..] $ replicate 100 $ bug (0,0) 100)
(\world -> pictures $ worldBugs world)
(\event world -> world)
(\time (World bs) -> World $ map (Rotate (time * 10)) bs)

其中显示了一些随着时间的推移旋转的“ bug ”(两个圆圈形成头部和躯干)。问题是,运行几秒钟后,它崩溃了:

Gloss / OpenGL Stack Overflow "after drawPicture."
This program uses the Gloss vector graphics library, which tried to
draw a picture using more nested transforms (Translate/Rotate/Scale)
than your OpenGL implementation supports. The OpenGL spec requires
all implementations to have a transform stack depth of at least 32,
and Gloss tries not to push the stack when it doesn't have to, but
that still wasn't enough.

You should complain to your harware vendor that they don't provide
a better way to handle this situation at the OpenGL API level.

To make this program work you'll need to reduce the number of nested
transforms used when defining the Picture given to Gloss. Sorry.

如果我理解正确的话,这基本上意味着最终太多的转换被放到堆栈上,它会溢出。它指出这可能是硬件限制(我使用的是 Surface 2 Pro),所以我是 SOL 吗?它在使用 animate 时不会执行此操作,但这可能是因为它不会在每个 tick 时传递状态。

如果我要制作游戏,我将不得不使用 play 将状态传递到下一个 tick;我不能按时完成所有事情。有没有解决的办法?谷歌搜索错误几乎没有结果。

最佳答案

问题在于,在每次“滴答”时,它通过将图片包装在另一个转换中来进一步嵌套图片(最终导致溢出)。为了解决它,我只是将每个值存储在 Entity 对象中,然后在 fromEntity 中应用一次转换。

    {- LANGUAGE threaded -}
module GlossTest where

import Graphics.Gloss

data Entity = Entity { entRot :: Float, entTrans :: Point, entScale :: Point, entPict :: Picture }

data World = World { worldBugs :: [Entity] }

entTranslate :: Float -> Float -> Entity -> Entity
entTranslate x y (Entity r t s p) = Entity r (x,y) s p

entRotate :: Float -> Entity -> Entity
entRotate x (Entity r t s p) = Entity x t s p

entRotateBy :: Float -> Entity -> Entity
entRotateBy n (Entity r t s p) = Entity (r + n) t s p

entMove :: Float -> Float -> Entity -> Entity
entMove x y (Entity r (tX,tY) s p) = Entity r (tX + x, tY + y) s p

toEntity :: Picture -> Entity
toEntity = Entity 0 (0,0) 1

fromEntity :: Entity -> Picture
fromEntity (Entity r (tX,tY) (sX,sY) p) = Rotate r . Translate tX tY $ Scale sX sY p

bug :: Point -> Float -> Entity
bug (x, y) s =
let head = Rotate 0 $ Translate x (y - s) $ Circle (s * 0.8)
body = Rotate 0 $ Translate x (y + s) $ Circle (s * 1.2)
in toEntity $ pictures [head, body]

main = play
(InWindow "Animation Test" (400, 400) (100, 100)) white 1
(World . map (\(n,b) -> entTranslate (n * 1) (n * 1) $ b) $ zip [0..] $ replicate 10 $ bug (0,0) 100)
(\world -> pictures . map fromEntity $ worldBugs world)
(\event world -> world)
(\time (World bs) -> World $ map (\(n,b) -> entRotateBy (n * time) $ entMove time time b) $ zip [0..] bs)

关于opengl - 使用基于递归的动画时在 drawPicture "Gloss/OpenGL Stack Overflow "之后出现错误 ."",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26599365/

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