Render PNG images using OpenGL in Haskell

只谈情不闲聊 提交于 2019-12-05 18:24:21

Suggestion: Since you are new to Haskell, instead of diving straight into raw OpenGL for your chess game, have you looked at libraries that could help you make OpenGL easier? I would recommend gloss and it looks like gloss-game has a helper function to load a .png file into memory ready to be used for your game. Good luck! :-)

Here is the way I use.

First, use the package gl-capture. It is old but it works well. It generates ppm images.

import           Graphics.Rendering.OpenGL.Capture (capturePPM)

Do you need help to apply the package ? You need a keyboard callback. I can help if necessary, please just ask.

Now, once you have a ppm image, you have two options: convert it with ImageMagick, or use a Haskell package to convert it. There is a good one: it is called hip. Here is the module I use:

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

Do not hesitate if you need more help.

Here is the kind of keyboard callback I use:

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 ()

Then press 'c' to capture the image. Note that the conversion from ppm to png is slow. Especially if you intend to do some animations. For animations, I rather use ppm only and then I convert with ImageMagick.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!