Advertisement
Guest User

Untitled

a guest
Jul 15th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- required pre-existing state: A window context, a bound vertex array object, and an active shader.
  2. -- Does not clear the back buffer or swap it to the front, only adds extra data to it.
  3. drawShape :: (VertexAttribComponent v, Storable v) => [VertexAttribData v] -> PrimitiveMode -> IO ()
  4. drawShape vertexAttribs mode = drawShapeR vertexAttribs mode 0 []
  5.   where drawShapeR [] _ _ _ = putStrLn "drawShape encountered empty vertex list, doing nothing"
  6.         drawShapeR (headAttribs:tailAttribs) mode n buffers = do
  7.           vertexBuffer <- genObjectName
  8.           bindBuffer ArrayBuffer $= (Just vertexBuffer)
  9.           let vertexArrayLength = numVertices headAttribs
  10.           (size, bufPtr) <- getVertexAttribArray headAttribs
  11.           bufferData ArrayBuffer $= (fromIntegral (vertexArrayLength * size), bufPtr, StreamDraw)
  12.          
  13.           vertexAttribPointer (AttribLocation n) $=
  14.             (ToFloat, VertexArrayDescriptor
  15.                       (numAttributeComponents headAttribs)
  16.                       Float
  17.                       0
  18.                       (plusPtr nullPtr . fromIntegral $ 0))
  19.           vertexAttribArray (AttribLocation n) $= Enabled
  20.           -- if all vertex attributes have been processed, draw the object and clean up the buffers
  21.           case tailAttribs of [] -> drawArrays mode 0 (fromIntegral vertexArrayLength) >>
  22.                                     forM_ buffers deleteObjectName
  23.                               _  -> drawShapeR tailAttribs mode (n + 1) (vertexBuffer:buffers)
  24.  
  25. data VertexAttribData a = PosData2D NumComponents [Vertex2 a]
  26.                         | ColorData NumComponents [Color4 a]
  27.                         | TexUVData NumComponents [TexCoord2 a]
  28.                         deriving( Eq, Show, Read )
  29.  
  30. numAttributeComponents :: VertexAttribData a -> NumComponents
  31. numAttributeComponents (PosData2D n _) = n
  32. numAttributeComponents (ColorData n _) = n
  33. numAttributeComponents (TexUVData n _) = n
  34.  
  35. numVertices            :: VertexAttribData a -> Int
  36. numVertices (PosData2D _ vertices) = length vertices
  37. numVertices (ColorData _ vertices) = length vertices
  38. numVertices (TexUVData _ vertices) = length vertices
  39.  
  40.  
  41. getVertexAttribArray :: (Storable a) => VertexAttribData a -> IO (Int, Ptr a)
  42. getVertexAttribArray (PosData2D _ vertices) = newArray vertices
  43.   >>= (\ptr -> (peekElemOff ptr 0 >>=
  44.                  (\fstElem -> return (sizeOf fstElem, castPtr ptr))))
  45. getVertexAttribArray (ColorData _ vertices) = newArray vertices
  46.   >>= (\ptr -> (peekElemOff ptr 0 >>=
  47.                  (\fstElem -> return (sizeOf fstElem, castPtr ptr))))
  48. getVertexAttribArray (TexUVData _ vertices) = newArray vertices
  49.   >>= (\ptr -> (peekElemOff ptr 0 >>=
  50.                  (\fstElem -> return (sizeOf fstElem, castPtr ptr))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement