2013-04-03 14 views
5

Sulla base di this post, stavo cercando di capire come utilizzare i VBO in Haskell. Ho cercato di riempire i bit che non erano coperti lì:OpenGL VBO in Haskell

data Sprite = Sprite { spriteImage :: Image 
        , spritePosition :: Position 
        } deriving (Show, Eq) 

spriteBatch :: [Sprite] -> [(TextureObject, [Sprite])] 
spriteBatch = (map f) . toList . (groupedBy (imageTexture . spriteImage)) 
    where f (t, s) = (t, s) 

offset = plusPtr nullPtr 

renderSprites :: [Sprite] -> IO() 
renderSprites l = (flip mapM_) (spriteBatch l) $ \(t, sps) -> do 
     textureBinding Texture2D $= Just t 
     let l = concat $ map sprToList sps 
     vbo <- vboOfList ((length l)*4) l 
     displayVbo vbo $ fromIntegral $ length sps 
    where 
     sprToList :: Sprite -> [GLfloat] 
     sprToList (Sprite (Image _ (TexCoord2 u0 v0) (TexCoord2 u1 v1) (Size w h) _) (Position x y)) = 
      [fromIntegral x, fromIntegral y, u0, v0 
      ,fromIntegral (x+w), fromIntegral y, u1, v0 
      ,fromIntegral (x+w), fromIntegral (y+h), u1, v1 
      ,fromIntegral x, fromIntegral (y+h), u0, v1 
      ] 

vboOfList :: Int -> [GLfloat] -> IO BufferObject 
vboOfList size elems = do 
    let ptrsize = toEnum $ size * 4 
     arrayType = ElementArrayBuffer 
    (array:_) <- genObjectNames 1 
    bindBuffer arrayType $= Just array 
    arr <- newListArray (0, size - 1) elems 
    withStorableArray arr $ \ptr -> bufferData arrayType $= (ptrsize, ptr, StaticDraw) 
    bindBuffer ArrayBuffer $= Nothing 
    return array 

displayVbo buff size = do 
    let stride = 2*(2*4) 
     vxDesc = VertexArrayDescriptor 2 Float stride $ offset 0 
     texCoo = VertexArrayDescriptor 2 Float stride $ offset 8 
    bindBuffer ArrayBuffer $= Just buff 

    arrayPointer VertexArray $= vxDesc 
    arrayPointer TextureCoordArray $= texCoo 

    clientState VertexArray $= Enabled 
    clientState TextureCoordArray $= Enabled 

    drawArrays Quads 0 size 
    bindBuffer ArrayBuffer $= Nothing 

È possibile trovare il codice completo here, nel caso sia necessario.

Sul master branch, la stessa funzione utilizza le normali chiamate per disegnare il Sprites e funziona perfettamente. Ma usando VBO, lo sprite non c'è; Ottengo uno schermo vuoto.

Qualcuno può spiegarmi cosa ho fatto di sbagliato qui?

+1

Potresti spiegarci cosa c'è di sbagliato in primo luogo? Compila e si comporta male? –

+0

@ ThomasM.DuBuisson: Oh sì, mi dispiace. Si compila perfettamente, ma lo schermo rimane vuoto. Lo sprite non compare - e deve essere nella parte VBO sopra perché lo stesso senza VBO funziona bene. – Lanbo

risposta

1

Si sta utilizzando un buffer per i vertici e gli uv. Usa due buffer, uno per i vertici e uno per gli uv.

Problemi correlati