在Haskell中使用OpenGL渲染PNG图像

问题描述 投票:3回答:2

我是Haskell的新手,我正在使用OpenGL(使用Graphics.UI.GLUT)为UI构建一个国际象棋游戏。我正在尝试为棋子渲染PNG图像。

我读到图像可以转换为TextureObject然后渲染,但找不到任何有用的资源来知道如何做。

这就是我的代码生成国际象棋棋盘的样子

drawSquare :: BoardSquare -> IO ()
drawSquare ((x,y,z),(r,g,b)) = preservingMatrix $ do
    color $ Color3 r g b
    translate $ Vector3 x y z
    drawCube -- this will draw a square of appropriate size

-- Display Callback
display :: IORef GameState -> DisplayCallback
display gameState = do
    gstate <- get gameState
    clear [ColorBuffer]
    forM_ (getBoardPoints gstate) $ drawSquare -- drawing all 64 squares here
    flush

任何人都可以帮助我在给定文件路径的窗口的任何给定xy坐标处渲染PNG图像?

haskell opengl png glut
2个回答
3
投票

建议:既然你是Haskell的新手,而不是直接潜入原始OpenGL为你的国际象棋游戏,你有没有看过可以帮助你更容易使OpenGL的库?我会推荐gloss,它看起来像gloss-game有一个helper function加载.png文件到内存准备用于您的游戏。祝好运! :-)


1
投票

这是我使用的方式。

首先,使用包gl-capture。它很旧,但效果很好。它生成ppm图像。

import           Graphics.Rendering.OpenGL.Capture (capturePPM)

申请包裹需要帮助吗?你需要一个键盘回调。如果有必要,我可以帮忙,请问。

现在,一旦你有了ppm图像,你有两个选择:用ImageMagick转换它,或者使用Haskell包来转换它。有一个很好的:它被称为hip。这是我使用的模块:

module Utils.ConvertPPM
  where
import           Control.Monad    (when)
import           Graphics.Image
import           System.Directory (removeFile)

convert :: FilePath -> FilePath -> Bool -> IO ()
convert input output remove = do
  ppm <- readImageRGBA VU input
  writeImage output ppm
  when remove $ removeFile input

如果您需要更多帮助,请不要犹豫。

这是我使用的那种键盘回调:

keyboard :: IORef GLfloat -> IORef GLfloat -> IORef GLfloat -> IORef GLint
         -> KeyboardCallback
keyboard rot1 rot2 rot3 capture c _ =
  case c of
    'r' -> rot1 $~! subtract 1
    't' -> rot1 $~! (+1)
    'f' -> rot2 $~! subtract 1
    'g' -> rot2 $~! (+1)
    'v' -> rot3 $~! subtract 1
    'b' -> rot3 $~! (+1)
    'c' -> do
      i <- get capture
      let ppm = printf "pic%04d.ppm" i
          png = printf "pic%04d.png" i
      (>>=) capturePPM (B.writeFile ppm)
      convert ppm png True
      capture $~! (+1)
    'q' -> leaveMainLoop
    _   -> return ()

然后按'c'捕捉图像。请注意,从ppmpng的转换很慢。特别是如果你打算做一些动画。对于动画,我宁愿只使用ppm然后我用ImageMagick转换。

© www.soinside.com 2019 - 2024. All rights reserved.