且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Haskell中使用OpenGL渲染PNG图像

更新时间:2022-10-15 09:45:55

建议:由于你是Haskell的新手,而不是直接进入你的国际象棋游戏的原始 OpenGL ,你看过图书馆帮助你让 OpenGL 更容易?我会推荐 gloss ,它看起来像 gloss-game 有一个助手函数 .png 文件加载到内存中准备好用于你的游戏。祝你好运! : - )

I am new to Haskell and I am building a chess game using OpenGL (using Graphics.UI.GLUT) for UI. I am trying to render PNG images for chess pieces.

I read that images can be converted to TextureObject and then rendered, but could not find any helpful resources to know how to do it.

This is what my code looks like for generating the chess board

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

Can anyone help me render PNG image at any given x and y coordinates of the window with given file path?

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! :-)