基于this post,我试图弄清楚如何在Haskell中使用VBO。我试着填写那里没有的位:
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
如果需要,您可以找到完整的代码here。
在master branch上,同一个函数使用普通的vertex
调用来绘制Sprites
,它完美无缺。但是使用VBO,精灵就不在那里;我得到一个空白的屏幕。
有谁可以向我解释我在这里做错了什么?
答案 0 :(得分:1)
你正在为顶点和uv使用一个缓冲区。使用两个缓冲区,一个用于顶点,一个用于uvs。