2014-07-01 63 views
1

您可以提供一個關於如何在Haskell中加載texImage2D(來自OpenGL)和readImage(來自JuicyPixels庫)的紋理的例子嗎?如何使用Haskell中的JuicyPixels庫加載OpenGL紋理

我知道已經有類似的問題here但因爲我是一個新手,我無法使這個例子的作品。我需要完整的代碼,也許還解釋了它是如何工作的...

+0

所有這些都是爲了得到JP圖像(它只是一個'Vector')的底層表示,然後使用'texImage2D'。你可以在這裏找到'texImage2D'的文檔(http://www.cs.sfu.ca/~haoz/teaching/htmlman/teximage2d.html)。 – user2407038

回答

0

對不起,我不知道如何操作JuicyPixels,但我已經在OpenGL中進行紋理加載。

它有點複雜。它會從幾個文件中加載紋理,假設它們是紋理類(包含紋理中的多個圖像)。

您可以檢查一下這個方案的具體紋理渲染的部分(由切割不必要的部分),並找出如何將JuicePixels圖像轉換爲原始像素顏色陣列(在utils模塊中查看bindBMPTexture函數)

祝您好運!

import   Data.ByteString   (ByteString(..)) 
import qualified Data.ByteString as BS 
import qualified Data.ByteString.Unsafe as BSU 
import   Graphics.UI.GLUT 
import   Graphics.Rendering.OpenGL (($=)) 
import qualified Graphics.Rendering.OpenGL as GL 
import qualified Codec.BMP as BMP 
import   Foreign.ForeignPtr 
import   Foreign.Ptr 
import   Control.Monad 
import   Unsafe.Coerce 
import qualified Graphics.UI.GLFW as GLFW 
import   Data.IORef 
import   Data.List 
import qualified Data.Map as Map 
import   Data.Maybe 
import OpenGLUtils 

data TextureInfo = TextureInfo GL.TextureObject (Int, Int) 
data ImageTexture = ImageTexture String (Int, Int) (Int, Int) 
images = [ ImageTexture "data/bricks.bmp" (0, 0) (64, 16) 
     , ImageTexture "data/bricks.bmp" (0, 16) (64, 16) 
     , ImageTexture "data/bricks.bmp" (0, 32) (64, 16) 
     , ImageTexture "data/bricks.bmp" (0, 48) (64, 16) 
     ] 

main = do 
    GLFW.initialize 
    GLFW.openWindow (GL.Size (gsizei oSCREEN_WIDTH) (gsizei oSCREEN_HEIGHT)) [GLFW.DisplayAlphaBits 8] GLFW.Window 
    GLFW.windowTitle $= "Texture demo" 
    GL.shadeModel $= GL.Smooth 
    GL.lineSmooth $= GL.Enabled 
    GL.blend  $= GL.Enabled 
    GL.blendFunc $= (GL.SrcAlpha, GL.OneMinusSrcAlpha) 
    GL.lineWidth $= 1.5 
    GL.clearColor $= Color4 0 0 0 0 

    GLFW.windowSizeCallback $= \ [email protected](GL.Size w h) -> do 
     GL.viewport $= (GL.Position 0 0, size) 
     GL.matrixMode $= GL.Projection 
     GL.loadIdentity 

    texturesMap <- loadTextures 
    glfwStaticRender $ do 
    drawTexture_ texturesMap $ images !! 0 

    GLFW.closeWindow 
    GLFW.terminate 

loadTextures :: IO (Map.Map String TextureInfo) 
loadTextures = do 
    let paths = nub $ map (\(ImageTexture path _ _) -> path) images 
    texs <- GL.genObjectNames (length paths) 
    let zippedMap = zip paths texs 
    sizes <- forM zippedMap $ \(path, tex) -> 
     bindBMPTexture tex path 
    return $ Map.fromList (zipWith (\(path,tex) size -> (path, TextureInfo tex size)) zippedMap sizes) 

drawTexture_ textureMap (ImageTexture path (posX, posY) (sizeX, sizeY)) = do 
    let (TextureInfo tex (tSizeX, tSizeY)) = fromJust $ Map.lookup path textureMap 
    drawTexture tex (tSizeX, tSizeY) (posX, posY) (sizeX, sizeY) 
+1

我只是看了一下代碼,我發現在bindBMPTexture中有一些我需要做的/知道加載紋理,我甚至不需要JuicyPixels庫,我可以使用BMP包作爲utils庫!非常感謝,我可以從你的代碼中學到很多東西! – Manuel