Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- required pre-existing state: A window context, a bound vertex array object, and an active shader.
- -- Does not clear the back buffer or swap it to the front, only adds extra data to it.
- drawShape :: (VertexAttribComponent v, Storable v) => [VertexAttribData v] -> PrimitiveMode -> IO ()
- drawShape vertexAttribs mode = drawShapeR vertexAttribs mode 0 []
- where drawShapeR [] _ _ _ = putStrLn "drawShape encountered empty vertex list, doing nothing"
- drawShapeR (headAttribs:tailAttribs) mode n buffers = do
- vertexBuffer <- genObjectName
- bindBuffer ArrayBuffer $= (Just vertexBuffer)
- let vertexArrayLength = numVertices headAttribs
- (size, bufPtr) <- getVertexAttribArray headAttribs
- bufferData ArrayBuffer $= (fromIntegral (vertexArrayLength * size), bufPtr, StreamDraw)
- vertexAttribPointer (AttribLocation n) $=
- (ToFloat, VertexArrayDescriptor
- (numAttributeComponents headAttribs)
- Float
- 0
- (plusPtr nullPtr . fromIntegral $ 0))
- vertexAttribArray (AttribLocation n) $= Enabled
- -- if all vertex attributes have been processed, draw the object and clean up the buffers
- case tailAttribs of [] -> drawArrays mode 0 (fromIntegral vertexArrayLength) >>
- forM_ buffers deleteObjectName
- _ -> drawShapeR tailAttribs mode (n + 1) (vertexBuffer:buffers)
- data VertexAttribData a = PosData2D NumComponents [Vertex2 a]
- | ColorData NumComponents [Color4 a]
- | TexUVData NumComponents [TexCoord2 a]
- deriving( Eq, Show, Read )
- numAttributeComponents :: VertexAttribData a -> NumComponents
- numAttributeComponents (PosData2D n _) = n
- numAttributeComponents (ColorData n _) = n
- numAttributeComponents (TexUVData n _) = n
- numVertices :: VertexAttribData a -> Int
- numVertices (PosData2D _ vertices) = length vertices
- numVertices (ColorData _ vertices) = length vertices
- numVertices (TexUVData _ vertices) = length vertices
- getVertexAttribArray :: (Storable a) => VertexAttribData a -> IO (Int, Ptr a)
- getVertexAttribArray (PosData2D _ vertices) = newArray vertices
- >>= (\ptr -> (peekElemOff ptr 0 >>=
- (\fstElem -> return (sizeOf fstElem, castPtr ptr))))
- getVertexAttribArray (ColorData _ vertices) = newArray vertices
- >>= (\ptr -> (peekElemOff ptr 0 >>=
- (\fstElem -> return (sizeOf fstElem, castPtr ptr))))
- getVertexAttribArray (TexUVData _ vertices) = newArray vertices
- >>= (\ptr -> (peekElemOff ptr 0 >>=
- (\fstElem -> return (sizeOf fstElem, castPtr ptr))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement