Display texture on screen

I am new to Haskell and OpenGL, and I am trying to display a texture on the screen.

Here is what I have so far:

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)

Where Entityit looks like this:

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
        }
      }

And when I initialize, I have:

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

And the resulting image:

enter image description here

It has two triangles because I have two "entities" set in my code. But I do not see my quad and do not see any signs of texturing.

+4
source share
1 answer

It seems I had the wrong coordinates because this works well:

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)
0
source

All Articles