2014-06-09 147 views
1

而不是渲染完整紋理它只是呈現平均顏色。沒有opengl錯誤或其他錯誤。我正在檢查着色器錯誤,但沒有發生錯誤。Haskell opengl紋理只有平均顏色

import qualified Graphics.UI.GLFW as G 
import Graphics.Rendering.OpenGL.GL 
import Control.Applicative 
import System.Exit 
import System.IO 
import Control.Monad (unless, when) 
import Graphics.GLUtil 
import Foreign.Storable 

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 1000 700 "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 

loadTex :: FilePath -> IO TextureObject 
loadTex f = do t <- either error id <$> readTexture f 
       textureFilter Texture2D $= ((Linear', Nothing), Linear') 
       texture2DWrap $= (Repeated, Repeat) 
       return t 

mesh :: [GLfloat] 
mesh = [ 0.0, 0.5, 0, 1, 0.0, 0.5, 
      -0.5, -0.5, 0, 1, 0.0, 1.0, 
      0.5, -0.5, 0, 1, 1.0, 1.0] 

preMainLoop window = do 
    clearColor $= Color4 0.2 0.1243 0.5 1.0 
    blendFunc $= (SrcAlpha, OneMinusSrcAlpha) 
    p <- loadShaderProgram [(VertexShader, "vert4.vs"), (FragmentShader, "frag4.fs")] 
    currentProgram $= Just (program p) 
    printErrorMsg "program" 
    vao <- makeVAO $ 
     let meshVad = VertexArrayDescriptor 4 Float (stride 6) offset0 
      texCoord = VertexArrayDescriptor 2 Float (stride 6) (offsetPtr 3) 
      stride n = fromIntegral $ sizeOf (undefined::GLfloat) * n 
      mPos  = getAttrib p "position" 
      tc  = getAttrib p "tc" 
     in do 
      buffer <- makeBuffer ArrayBuffer mesh 
      vertexAttribArray mPos $= Enabled 
      vertexAttribPointer mPos $= (ToFloat, meshVad) 

      vertexAttribArray tc $= Enabled 
      vertexAttribPointer tc $= (ToFloat, texCoord) 
    tex <- loadTex "fail texture.png" 
    textureBinding Texture2D $= Nothing 
    activeTexture $= TextureUnit 0 
    textureBinding Texture2D $= Just tex 
    setUniform p "tex" (TextureUnit 0) 
    mainLoop window vao p 

mainLoop window vao p = do 
    action <- (G.windowShouldClose window) 
    unless action $ do 
     (width, height) <- G.getFramebufferSize window 
     viewport $= (Position 0 0, Size (fromIntegral width) (fromIntegral height)) 
     printErrorMsg "start of loop" 
     Just t <- G.getTime 
     clear [ColorBuffer, DepthBuffer] 
     withVAO vao $ drawArrays Triangles 0 3 
     G.swapBuffers window 
     G.pollEvents 
     mainLoop window vao p 

片段着色器

#version 430 core 
uniform sampler2D tex; 

in VS_OUT 
{ 
    vec2 tc; 
} fs_in; 

out vec4 color; 

void main (void) 
{ 
    color = texture(tex, fs_in.tc); 
} 

頂點着色器

#version 430 core 

in vec4 position; 
in vec2 tc; 

out VS_OUT 
{ 
    vec2 tc; 
} vs_out; 

void main (void) 
{ 
    gl_Position = position; 
    vs_out.tc = tc; 
} 
+0

你的實際繪圖代碼在哪裏?另外'nx'和'nl'是在哪裏定義的?另外爲什麼這些簡短的,不起作用的名字?這是一種很好的風格,特別是對於Haskell來說,讓事情變得容易理解並給出適當的名字。 – datenwolf

+0

這很可能不是您的主要問題,但您設置了兩次「WRAP_S」。你可能打算爲第二個使用'WRAP_T'。我最好的猜測是你的緩衝區中沒有有效的紋理座標,或者@datenwolf詢問的'nx/nl'值有問題。 –

+0

你怎麼能有無效的紋理座標,仍然沒有錯誤 – JoelWaterworth

回答

2

,因爲它是不佔一個GLfloat的大小的問題是偏移量。導致opengl使用負值。