2014-05-08 54 views
3

我一直在試圖得到一些腳本,只是使用texcoords在正方形上顯示紋理。 如果可能的話,你可以編輯腳本,使它從那裏起作用,我可以鍛鍊你如何做到這一點,因爲我學習的方式。Haskell opengl紋理GLFW

import Control.Monad (unless, when) 
import Graphics.Rendering.OpenGL 
import qualified Graphics.UI.GLFW as G 
import System.Exit 
import System.IO 
import Texture 
import Data.IORef 
import Graphics.GLUtil 
import qualified Data.Set as Set 

main :: IO() 
main = do 
    let errorCallback err description = hPutStrLn stderr description 
    G.setErrorCallback (Just errorCallback) 
    successfulInit <- G.init 
    if not successfulInit 
     then exitFailure 
     else do 
      mw <- G.createWindow 640 480 "Simple example, haskell style" Nothing Nothing 
      case mw of Nothing -> (G.terminate >> exitFailure) 
        Just window -> do 
            G.makeContextCurrent mw 
            preMainLoop window 
            G.destroyWindow window 
            G.terminate 
            exitSuccess 



preMainLoop :: G.Window -> IO() 
preMainLoop window = do 
    tex <- loadGLTextureFromFile "texture/metal.png" 
    clearColor $= Color4 0.9 0.1243 0.2544564 1.0 
    depthFunc $= Just Lequal 
    blendFunc $= (SrcAlpha, OneMinusSrcAlpha) 
    normalize $= Enabled 
    texture Texture2D $= Enabled 
    shadeModel $= Smooth 
    mainLoop tex window 


mainLoop :: TextureObject -> G.Window -> IO() 
mainLoop tex window = do 
    action <- (G.windowShouldClose window) 
    unless action $ do 
     viewWindow window 
     cal tex 
     G.swapBuffers window 
     G.pollEvents 
     mainLoop tex window 

cal tex = do 
    preservingMatrix $ do 
     rotate 90 (Vector3 1 0 0 :: Vector3 GLfloat) 
     withTextures2D [tex] $ draw tex 

draw tex = do 
    textureBinding Texture2D $= Just tex 
    renderPrimitive Quads $ do 
    n 0 1 0 
    t 0 1 >> v 1 (-1) 1 
    t 1 1 >> v 1 (-1) (-1) 
    t 1 0 >> v (-1) (-1) (-1) 
    t 0 0 >> v (-1) (-1) 1 
    where v x y z = vertex (Vertex3 x y z :: Vertex3 GLfloat) 
      n x y z = normal (Normal3 x y z :: Normal3 GLfloat) 
      t u v = texCoord (TexCoord2 u v :: TexCoord2 GLfloat) 

viewWindow window = do 
    (width, height) <- G.getFramebufferSize window 
    viewport $= (Position 0 0, Size (fromIntegral width) (fromIntegral height)) 
    clear [ColorBuffer, DepthBuffer] 
    matrixMode $= Projection 
    loadIdentity 
    perspective 90 (fromIntegral(width)/fromIntegral(height)) 0.01 40 
    matrixMode $= Modelview 0 

這裏是我一直在使用enter image description here

module Texture 
    (
    loadGLTextureFromFile, 

    ) where 

import Graphics.Rendering.OpenGL 
import Graphics.GLUtil 
import Codec.Picture 
import qualified Graphics.Rendering.OpenGL as GL 
import qualified Graphics.GLUtil as GLU 
import qualified Codec.Picture as Pic 

loadGLTextureFromFile :: FilePath -> IO GL.TextureObject 
loadGLTextureFromFile fp = do 
    possibleImage <- Pic.readImage fp 
    case possibleImage of 
     Left err -> do 
       print err 
       loadGLTextureFromFile "data/fail texture.png" 
     Right (Pic.ImageRGBA8 x) -> convertToGL x GLU.TexRGBA 
     Right (Pic.ImageRGBA16 x) -> convertToGL x GLU.TexRGBA 
     Right (Pic.ImageRGB8 x) -> convertToGL x GLU.TexRGB 
     Right (Pic.ImageRGB16 x) -> convertToGL x GLU.TexRGB 
     Right (Pic.ImageYCbCr8 x) -> convertToGL x GLU.TexRGB 
     Right _ -> do 
       print "image not found" 
       loadGLTextureFromFile "data/fail texture.png" 
     where 
     convertToGL x texCol = do 
      tex <- GLU.loadTexture (GLU.TexInfo 
           (fromIntegral (Pic.imageWidth x)) 
           (fromIntegral (Pic.imageHeight x)) 
           texCol (Pic.imageData x)) 
      textureWrapMode Texture2D S $= (Mirrored, ClampToEdge) 
      textureWrapMode Texture2D T $= (Mirrored, ClampToEdge) 
      return tex 

回答

2

所以我固定的圖像,通過使用

loadGLTextureFromFile :: FilePath -> IO GL.TextureObject 
loadGLTextureFromFile f = do t <- either error id <$> readTexture f 
          textureFilter Texture2D $= ((Linear', Nothing), Linear') 
          texture2DWrap $= (Mirrored, ClampToEdge) 
          return t