在屏幕上渲染纹理

时间:2015-07-14 09:07:08

标签: haskell opengl

我是Haskell和OpenGL的新手,我正试图在屏幕上渲染纹理。

这是我到目前为止所得到的:

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


renderEntity :: Entity -> IO ()
renderEntity e = do
    GL.activeTexture GL.$= GL.TextureUnit 0
    GL.textureBinding GL.Texture2D GL.$= Just (texture $ model e)
    -- I see a white triangle on the screen.
    renderTriangle $ fmap (vadd $ position e) (points $ model e :: [Vector2])

    -- I do not see this. Nor is the triangle textured either.
    GL.renderPrimitive GL.Quads $ do
        n 0 1 0
        t 0 1 >> v 10 (-10) 10
        t 1 1 >> v 10 (-10) (-10)
        t 1 0 >> v (-10) (-10) (-10)
        t 0 0 >> v (-10) (-10) 10
      where v x y z = GL.vertex (GL.Vertex3 x y z :: GL.Vertex3 GL.GLfloat)
            n x y z = GL.normal (GL.Normal3 x y z :: GL.Normal3 GL.GLfloat)
            t u v = GL.texCoord (GL.TexCoord2 u v :: GL.TexCoord2 GL.GLfloat)

Entity的样子:

texMetal <- makeTexture "texture/metal.jpg"

let 
    entity        = Entity
      { angle = 0
      , position = (0, 0) :: Vector2
      , velocity = (5, 5) :: Vector2
      , model = Model
        { points = [(-60, -40), (60, -40), (0, 60)] :: [Vector2]
        , texture = texMetal
        }
      }

当我初始化时,我有这些:

GL.viewport   GL.$= (pos, size)
GL.matrixMode GL.$= GL.Projection
GL.texture    GL.Texture2D GL.$= GL.Enabled
GL.normalize  GL.$= GL.Enabled
GL.loadIdentity
GL.ortho2D (-fromIntegral width / 2)
           (fromIntegral width / 2)
           (-fromIntegral height / 2)
           (fromIntegral height / 2)
GL.matrixMode GL.$= GL.Modelview 0
GL.loadIdentity

结果图片:

enter image description here

它有两个三角形,因为我的代码中设置了两个“实体”。但我没有看到我的四边形,也没有看到任何纹理的迹象。

1 个答案:

答案 0 :(得分:0)

看起来我的坐标不正确,因为效果很好:

renderEntity :: Entity -> IO ()
renderEntity e = do
    GL.activeTexture GL.$= GL.TextureUnit 0
    GL.textureBinding GL.Texture2D GL.$= Just (texture $ model e)
    GL.renderPrimitive GL.Quads $ do
       v 100 100
       t 0 1
       v 100 (-100)
       t 1 1
       v (-100) (-100)
       t 1 0
       v (-100) 100
       t 0 0
     where v x y = GL.vertex (GL.Vertex2 x y :: GL.Vertex2 GL.GLfloat)
           t u v = GL.texCoord (GL.TexCoord2 u v :: GL.TexCoord2 GL.GLfloat)
相关问题