diff options
Diffstat (limited to 'PointPrimitiveRing.hs')
-rw-r--r-- | PointPrimitiveRing.hs | 28 |
1 files changed, 11 insertions, 17 deletions
diff --git a/PointPrimitiveRing.hs b/PointPrimitiveRing.hs index ee5a126..4c4b516 100644 --- a/PointPrimitiveRing.hs +++ b/PointPrimitiveRing.hs | |||
@@ -33,7 +33,7 @@ data Ring = Ring | |||
33 | , rStorage :: GLStorage | 33 | , rStorage :: GLStorage |
34 | , rObject :: Object | 34 | , rObject :: Object |
35 | , rSize :: IORef CPtrdiff -- Current count of Floats in the ring buffer. | 35 | , rSize :: IORef CPtrdiff -- Current count of Floats in the ring buffer. |
36 | , rStart :: IORef CPtrdiff -- Float-index where next vector will be added. TODO: rename this. | 36 | , rBack :: IORef Int -- Where next vector will be added. |
37 | , ringCapacity :: CPtrdiff -- Maximum number of floats in buffer. | 37 | , ringCapacity :: CPtrdiff -- Maximum number of floats in buffer. |
38 | , rPosition :: AttributeKey (GLVector 3 Float) | 38 | , rPosition :: AttributeKey (GLVector 3 Float) |
39 | } | 39 | } |
@@ -53,7 +53,7 @@ newRing storage sz = do | |||
53 | , rStorage = storage | 53 | , rStorage = storage |
54 | , rObject = obj | 54 | , rObject = obj |
55 | , rSize = sizeRef | 55 | , rSize = sizeRef |
56 | , rStart = startRef | 56 | , rBack = startRef |
57 | , ringCapacity = 3 * fromIntegral sz | 57 | , ringCapacity = 3 * fromIntegral sz |
58 | , rPosition = k | 58 | , rPosition = k |
59 | } | 59 | } |
@@ -62,30 +62,24 @@ newRing storage sz = do | |||
62 | 62 | ||
63 | updateRingCommands :: Ring -> IO () | 63 | updateRingCommands :: Ring -> IO () |
64 | updateRingCommands r = do | 64 | updateRingCommands r = do |
65 | start <- fmap (fromIntegral . (`div` 3)) $ readIORef $ rStart r | 65 | back <- fromIntegral <$> readIORef (rBack r) |
66 | size <- fmap (fromIntegral . (`div` 3)) $ readIORef $ rSize r | 66 | size <- fmap (fromIntegral . (`div` 3)) $ readIORef $ rSize r |
67 | let mask 0 = [] | 67 | let mask 0 = [] |
68 | mask cnt = case cnt + start - size of | 68 | mask cnt = case cnt + back - size of |
69 | st | st > cnt -> [(st - cnt,size)] | 69 | front | front > cnt -> [(front - cnt,size)] |
70 | | otherwise -> [(0,start), (st,cnt - st)] | 70 | | otherwise -> [(0,back), (front,cnt - front)] |
71 | updateCommands (rStorage r) (rObject r) mask | 71 | updateCommands (rStorage r) (rObject r) mask |
72 | readIORef (objCommands $ rObject r) >>= mapM_ print | 72 | readIORef (objCommands $ rObject r) >>= mapM_ print |
73 | return () | 73 | return () |
74 | 74 | ||
75 | pushBack :: Ring -> Float -> Float -> Float -> IO () | 75 | pushBack :: Ring -> Float -> Float -> Float -> IO () |
76 | pushBack r x y z = allocaArray 3 $ \ptr -> do | 76 | pushBack r x y z = do |
77 | pokeElemOff ptr 0 x | 77 | back <- readIORef $ rBack r |
78 | pokeElemOff ptr 1 y | 78 | writeIORef (rBack r) (mod (back + 1) (fromIntegral $ ringCapacity r `div` 3)) |
79 | pokeElemOff ptr 2 z | 79 | updateAttributes back $ do |
80 | start <- readIORef $ rStart r | ||
81 | writeIORef (rStart r) (mod (start + 3) (ringCapacity r)) | ||
82 | -- incrementalUpdateBuffer (rBufferObject r) (4*start) (4*3) ptr | ||
83 | updateAttributes (fromIntegral (start `div` 3)) $ do | ||
84 | rPosition r @<- V3 x y z -- (fromList [x,y,z] :: Vector Float) | 80 | rPosition r @<- V3 x y z -- (fromList [x,y,z] :: Vector Float) |
85 | -- glFlush | ||
86 | -- glFinish | ||
87 | sz <- readIORef (rSize r) | 81 | sz <- readIORef (rSize r) |
88 | putStrLn $ "pushBack "++show (sz,start,(x,y,z)) | 82 | putStrLn $ "pushBack "++show (sz,back,(x,y,z)) |
89 | when (sz < ringCapacity r) $ do | 83 | when (sz < ringCapacity r) $ do |
90 | writeIORef (rSize r) (sz + 3) | 84 | writeIORef (rSize r) (sz + 3) |
91 | updateRingCommands r | 85 | updateRingCommands r |