在屏幕上渲染纹理

4

我对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

并得到以下图片:

输入图像描述

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


你的着色器在哪里?它是哪个上下文版本? - Bartek Banachewicz
1个回答

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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接