Bazując na this post, próbowałem dowiedzieć się, jak używać VBO w Haskell. Próbowałam wypełnić bitów, które nie zostały tam objęty:OpenGL VBO w 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
można znaleźć pełny kod here, w przypadku trzeba.
Na master branch, ta sama funkcja używa normalnych wywołań vertex
do rysowania Sprites
i działa idealnie. Ale używając VBO, sprite'a właśnie tam nie ma; Dostaję pusty ekran.
Czy ktoś może mi wyjaśnić, co zrobiłem źle tutaj?
Czy możesz nam wyjaśnić, co jest nie tak? Czy kompiluje się i źle się zachowuje? –
@ ThomasM.DuBuisson: O tak, przepraszam. Kompiluje się idealnie, ale ekran pozostaje pusty. Sprite nie pojawia się - i musi znajdować się w części VBO powyżej, ponieważ to samo bez VBO działa dobrze. – Lanbo