├── .gitignore ├── LICENSE ├── README.md ├── Setup.hs ├── boids.cabal ├── screenshot1.png ├── screenshot2.png ├── src ├── Boid.hs ├── EventManager.hs ├── Log.hs ├── Main.hs ├── Octree.hs ├── OctreePar.hs ├── Shader.hs └── Utils.hs └── stack.yaml /.gitignore: -------------------------------------------------------------------------------- 1 | dist 2 | *.o 3 | *.hi 4 | *.chi 5 | *.chs.h 6 | *.swp 7 | *.exe 8 | *.dll 9 | cabal.config 10 | .stack-work/ 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Flocking Simulation 2 | =================== 3 | 4 | A 3D Boids-like flocking simulation coded in Haskell. 5 | 6 | 7 | Bulding 8 | ------- 9 | 10 | Assuming you have GHC 7.10 and stack installed, building the program should be as easy as running `stack build`. 11 | 12 | You can then run the simulation with the command `stack exec boids`. 13 | 14 | 15 | Demo and Screenshots 16 | -------------------- 17 | 18 | [Here is a video demonstrating a typical run with the default configuration.](https://youtu.be/jn4hNzcmsZU) 19 | 20 | This is just after starting the program, when the boids are beginning to organize into groups. 21 | ![Screenshot](screenshot1.png) 22 | 23 | 24 | After running for a while, much larger groups of boids form. 25 | ![Screenshot](screenshot2.png) 26 | 27 | 28 | Usage 29 | ----- 30 | 31 | The controls are very simple: 32 | 33 | Click and drag with the mouse to move the camera around. 34 | Use the 'w' and 's' keys to zoom in and out. 35 | Quit the application by pressing 'q', the escape key, or closing the window. 36 | 37 | 38 | Details 39 | ------- 40 | 41 | As stated above, the entire program is written in Haskell in a purely functional style. To do 3D rendering, I use the newly released [GPipe 2](https://hackage.haskell.org/package/GPipe-2.1.4) library which creates a type safe way to interface with OpenGL 3.3. GPipe also provides a DSL for writing shaders, so all the shader code is written in Haskell as well. 42 | 43 | The main data structure used to keep track of and efficiently update the boids is an octree. Every frame, the octree is flattened to a list and each boid is updated. This action is performed in parallel using all available CPU cores. The list of updated boids is then used to build a new octree (also in parallel), and the boids are rendered. I suspect that there's a lot of room for efficiency improvements by switching the lists in this process to vectors. That change would also (I think) allow me to utilize the Repa library and push boid updating to the GPU, potentially resulting in an enormous increase in the number of boids I'm able to simulate. 44 | 45 | 46 | TODO List 47 | --------- 48 | 49 | Here are some things I'd like to improve (in no particular order): 50 | 51 | * Replace lists with vectors 52 | * Add predators and prey 53 | * Integrate with the Repa library to exploit GPU parallelization 54 | * Add a GUI and allow customizable parameters on the fly 55 | * Add more sophisticated boid behavior 56 | * Optimizations! 57 | -------------------------------------------------------------------------------- /Setup.hs: -------------------------------------------------------------------------------- 1 | import Distribution.Simple 2 | main = defaultMain 3 | -------------------------------------------------------------------------------- /boids.cabal: -------------------------------------------------------------------------------- 1 | name: boids 2 | version: 0.1.0.0 3 | cabal-version: >=1.8 4 | author: Chase Cummings 5 | maintainer: chaseecummings@gmail.com 6 | license: GPL-2 7 | license-file: LICENSE 8 | category: Game 9 | build-type: Simple 10 | synopsis: A 3D Boids-like flocking simulation coded in Haskell 11 | 12 | executable boids 13 | hs-source-dirs: src 14 | main-is: Main.hs 15 | ghc-options: -O2 -Wall -threaded -rtsopts -with-rtsopts=-N 16 | extensions: DoAndIfThenElse 17 | build-depends: base 18 | , random >= 1.0 19 | , GLFW-b 20 | , GPipe >= 2.1.3 21 | , GPipe-GLFW >= 1.2.1 22 | , linear >= 1.19 23 | , transformers 24 | , containers 25 | , deepseq >= 1.4.1 26 | , parallel >= 3.2.0 27 | , monad-par >= 0.3.4 28 | , Boolean >= 0.2.3 29 | , ReadArgs >= 1.2.2 30 | -------------------------------------------------------------------------------- /screenshot1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chase-C/Flocking-Simulation/897ee7adbe2cd7ae27faf37d65c41e6c2d189b0c/screenshot1.png -------------------------------------------------------------------------------- /screenshot2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chase-C/Flocking-Simulation/897ee7adbe2cd7ae27faf37d65c41e6c2d189b0c/screenshot2.png -------------------------------------------------------------------------------- /src/Boid.hs: -------------------------------------------------------------------------------- 1 | {-# LANGUAGE DeriveGeneric, DeriveAnyClass #-} 2 | 3 | module Boid where 4 | 5 | -------------------------------------------------------------------------------- 6 | 7 | import Data.List 8 | import Control.Monad (forM) 9 | import Control.DeepSeq 10 | import GHC.Generics (Generic) 11 | 12 | import Linear 13 | import Utils 14 | import Log 15 | 16 | -------------------------------------------------------------------------------- 17 | 18 | data Boid = Boid 19 | { bPos :: V3 Float 20 | , bVel :: V3 Float 21 | , bTar :: V3 Float 22 | , bRad :: !Float 23 | , bPred :: !Bool 24 | } deriving (Show, Eq, Generic, NFData) 25 | 26 | makeBoids :: (Int, Int, Int) -> (Int, Int, Int) -> Int -> IO [Boid] 27 | makeBoids (lx, ly, lz) (hx, hy, hz) n = forM [1..n] (\_ -> do 28 | x <- getRandom (rtf lx) (rtf hx) 29 | y <- getRandom (rtf ly) (rtf hy) 30 | z <- getRandom (rtf lz) (rtf hz) 31 | vx <- getRandom (rtf lx) (rtf hx) 32 | vy <- getRandom (rtf ly) (rtf hy) 33 | vz <- getRandom (rtf lz) (rtf hz) 34 | return $ Boid 35 | { bPos = V3 x y z 36 | , bVel = (V3 vx vy vz) * 0.001 37 | , bTar = zero 38 | , bRad = 16 39 | , bPred = False 40 | }) 41 | where rtf = realToFrac 42 | 43 | makeModel :: Float -> [(V3 Float, V3 Float)] 44 | makeModel scale = map (\(v, n) -> (scale *^ v, vNorm n)) boidModel 45 | 46 | -------------------------------------------------------------------------------- 47 | 48 | updateBoidRadius :: Boid -> [(Boid, Float)] -> Boid 49 | updateBoidRadius (Boid pos vel tar rad pred) neighbors = 50 | let velUpdate = vClamp (foldl (updateVelocityRadius pos vel) zero neighbors) 0.002 51 | bndUpdate = vClamp (updateBounds pos) 0.005 52 | newVel = vScaleTo (vel + velUpdate + bndUpdate) 0.05 53 | in Boid 54 | { bPos = pos + newVel 55 | , bVel = newVel 56 | , bTar = zero 57 | , bRad = rad 58 | , bPred = pred 59 | } 60 | 61 | updateVelocityRadius :: V3 Float -> V3 Float -> V3 Float -> (Boid, Float) -> V3 Float 62 | updateVelocityRadius pos vel dv (boid, radius) 63 | | pos == bPos boid = dv 64 | | otherwise = 65 | let dir = (bPos boid) ^-^ pos 66 | nDir = dir ^/ radius 67 | separation = separationVector nDir radius 68 | cohesion = cohesionVector nDir radius 69 | alignment = alignmentVector vel $ bVel boid 70 | sn = 6 71 | cn = 3 72 | an = 4 73 | in dv ^+^ (sn *^ separation) 74 | ^+^ (cn *^ cohesion) 75 | ^+^ (an *^ alignment) 76 | 77 | separationVector :: V3 Float -> Float -> V3 Float 78 | separationVector nDir dist = (-nDir) ^/ dist 79 | 80 | cohesionVector :: V3 Float -> Float -> V3 Float 81 | cohesionVector nDir dist = nDir ^* dist 82 | 83 | alignmentVector :: V3 Float -> V3 Float -> V3 Float 84 | alignmentVector vel1 vel2 = vel2 - vel1 85 | 86 | updateBounds :: V3 Float -> V3 Float 87 | updateBounds pos 88 | | len > 32 = (zero - pos) ^/ len 89 | | otherwise = zero 90 | where len = vLen pos 91 | 92 | --updateBoid :: Boid -> [Boid] -> Boid 93 | --updateBoid (Boid pos vel tar rad pred) neighbors = 94 | -- let velUpdate = vClamp (foldl (updateVelocity pos) zero neighbors) 0.01 95 | -- --tarUpdate = vClamp (updateTarget pos tar) 0.01 96 | -- bndUpdate = vClamp (updateBounds pos) 0.015 97 | -- newVel = vScaleTo (vel + velUpdate + bndUpdate) 0.05 98 | -- in Boid 99 | -- { bPos = pos + newVel 100 | -- , bVel = newVel 101 | -- , bTar = zero 102 | -- , bRad = rad 103 | -- , bPred = pred 104 | -- } 105 | -- 106 | --updateVelocity :: V3 Float -> V3 Float -> Boid -> V3 Float 107 | --updateVelocity pos vel boid 108 | -- | pos == bPos boid = vel 109 | -- | otherwise = 110 | -- let deltaP = (bPos boid) - pos 111 | -- len = vLen deltaP 112 | -- in (vel + deltaP + ((bVel boid) ^/ (10 * len))) - (deltaP ^* (2.5 / len)) 113 | 114 | -------------------------------------------------------------------------------- 115 | 116 | boidModel :: [(V3 Float, V3 Float)] 117 | boidModel = [ (V3 0 0 4, V3 (-0.4402) 0.8805 0.1761) 118 | , (V3 0 1 (-1), V3 (-0.4402) 0.8805 0.1761) 119 | , (V3 (-2) 0 (-1), V3 (-0.4402) 0.8805 0.1761) 120 | 121 | , (V3 0 0 4, V3 0.4402 0.8805 0.1761) 122 | , (V3 2 0 (-1), V3 0.4402 0.8805 0.1761) 123 | , (V3 0 1 (-1), V3 0.4402 0.8805 0.1761) 124 | 125 | , (V3 0 0 4, V3 0.4402 (-0.8805) 0.1761) 126 | , (V3 0 (-1) (-1), V3 0.4402 (-0.8805) 0.1761) 127 | , (V3 2 0 (-1), V3 0.4402 (-0.8805) 0.1761) 128 | 129 | , (V3 0 0 4, V3 (-0.4402) (-0.8805) 0.1761) 130 | , (V3 (-2) 0 (-1), V3 (-0.4402) (-0.8805) 0.1761) 131 | , (V3 0 (-1) (-1), V3 (-0.4402) (-0.8805) 0.1761) 132 | 133 | , (V3 0 1 (-1), V3 0 0 (-1)) 134 | , (V3 0 (-1) (-1), V3 0 0 (-1)) 135 | , (V3 (-2) 0 (-1), V3 0 0 (-1)) 136 | 137 | , (V3 0 1 (-1), V3 0 0 (-1)) 138 | , (V3 2 0 (-1), V3 0 0 (-1)) 139 | , (V3 0 (-1) (-1), V3 0 0 (-1)) 140 | ] 141 | -------------------------------------------------------------------------------- /src/EventManager.hs: -------------------------------------------------------------------------------- 1 | module EventManager 2 | ( InputStore (..) 3 | , GLFWInput (..) 4 | , InputState (..) 5 | , EventAction (..) 6 | , InputStateMap 7 | , makeMouseEvent 8 | , makeKeyEvent 9 | , processEvents 10 | ) where 11 | 12 | import Control.Monad.IO.Class (MonadIO) 13 | import Control.Monad.Trans.Class (MonadTrans, lift) 14 | import Control.Monad (mapM, liftM, when) 15 | import Data.Maybe (catMaybes) 16 | import Graphics.GPipe (ContextT) 17 | 18 | import qualified Graphics.GPipe.Context.GLFW as GLFW 19 | import qualified Data.Map.Strict as M 20 | 21 | import Log 22 | 23 | -------------------------------------------------------------------------------- 24 | 25 | class (Monad m) => InputStore m where 26 | getStateMap :: m InputStateMap 27 | setStateMap :: InputStateMap -> m () 28 | 29 | -------------------------------------------------------------------------------- 30 | 31 | data GLFWInput = KeyInput GLFW.Key 32 | | MouseInput GLFW.MouseButton 33 | deriving (Show, Eq, Ord) 34 | 35 | data InputState = KeyState GLFW.KeyState 36 | | MouseState GLFW.MouseButtonState 37 | deriving (Show, Eq, Ord) 38 | 39 | data EventAction m = InputAction GLFWInput InputState (m ()) 40 | | CursorAction ((Double, Double) -> m ()) 41 | 42 | type InputStateMap = M.Map GLFWInput InputState 43 | 44 | -------------------------------------------------------------------------------- 45 | 46 | makeMouseEvent :: InputStore m => GLFW.MouseButton -> GLFW.MouseButtonState -> m () -> EventAction m 47 | makeMouseEvent button state action = InputAction (MouseInput button) (MouseState state) action 48 | 49 | makeKeyEvent :: InputStore m => GLFW.Key -> GLFW.KeyState -> m () -> EventAction m 50 | makeKeyEvent key state action = InputAction (KeyInput key) (KeyState state) action 51 | 52 | processEvents :: (MonadTrans t, MonadIO m, InputStore (t (ContextT GLFW.GLFWWindow os f m))) 53 | => [EventAction (t (ContextT GLFW.GLFWWindow os f m))] 54 | -> t (ContextT GLFW.GLFWWindow os f m) () 55 | processEvents events = do 56 | stateMap <- getStateMap 57 | states <- liftM catMaybes $ mapM (processEvent stateMap) events 58 | setStateMap $ M.fromList states 59 | 60 | -------------------------------------------------------------------------------- 61 | 62 | processEvent :: (MonadTrans t, MonadIO m, Monad (t (ContextT GLFW.GLFWWindow os f m))) 63 | => InputStateMap 64 | -> EventAction (t (ContextT GLFW.GLFWWindow os f m)) 65 | -> t (ContextT GLFW.GLFWWindow os f m) (Maybe (GLFWInput, InputState)) 66 | processEvent imap (InputAction input state action) = do 67 | currentState <- lift $ currentInputState input 68 | let (stateChanged, imap') = changedState input currentState imap 69 | when (stateChanged && (currentState == state)) action 70 | return $ Just (input, currentState) 71 | processEvent imap (CursorAction action) = do 72 | coords <- lift $ GLFW.getCursorPos 73 | action coords 74 | return Nothing 75 | 76 | defaultState :: GLFWInput -> InputState 77 | defaultState (KeyInput _) = KeyState GLFW.KeyState'Released 78 | defaultState (MouseInput _) = MouseState GLFW.MouseButtonState'Released 79 | 80 | changedState :: GLFWInput -> InputState -> InputStateMap -> (Bool, InputStateMap) 81 | changedState input state imap = 82 | case M.lookup input imap of 83 | Just member -> (member /= state, M.insert input state imap) 84 | Nothing -> (False, M.insert input (defaultState input) imap) 85 | 86 | currentInputState :: MonadIO m => GLFWInput -> ContextT GLFW.GLFWWindow os f m InputState 87 | currentInputState (KeyInput key) = liftM KeyState (GLFW.getKey key) 88 | currentInputState (MouseInput button) = liftM MouseState (GLFW.getMouseButton button) 89 | -------------------------------------------------------------------------------- /src/Log.hs: -------------------------------------------------------------------------------- 1 | module Log where 2 | 3 | import System.IO.Unsafe (unsafePerformIO) 4 | import System.IO (putStrLn) 5 | 6 | slog :: (Show a) => String -> a -> a 7 | slog s var = unsafePerformIO $ do 8 | putStrLn $ s ++ ": " ++ show var 9 | return var 10 | 11 | nlog :: String -> a -> a 12 | nlog s var = unsafePerformIO $ do 13 | putStrLn s 14 | return var 15 | 16 | vlog :: (Show a) => a -> a 17 | vlog var = unsafePerformIO $ do 18 | print var 19 | return var -------------------------------------------------------------------------------- /src/Main.hs: -------------------------------------------------------------------------------- 1 | {-# LANGUAGE ScopedTypeVariables, FlexibleContexts, TypeFamilies, Rank2Types, FlexibleInstances #-} 2 | 3 | module Main (main) where 4 | 5 | -------------------------------------------------------------------------------- 6 | 7 | import System.IO 8 | import Control.Parallel.Strategies (parMap, rdeepseq) 9 | import Control.Monad (unless, when, void) 10 | import Control.Monad.Trans.RWS.Strict (RWST, ask, asks, evalRWST, get, gets, modify, put) 11 | import Control.Monad.IO.Class (liftIO) 12 | import Control.Monad.Trans.Class (lift) 13 | import ReadArgs (readArgs) 14 | 15 | import Graphics.GPipe 16 | import qualified Graphics.GPipe.Context.GLFW as GLFW 17 | import Graphics.UI.GLFW (swapInterval) 18 | import Linear 19 | 20 | import Boid 21 | import EventManager 22 | import Shader 23 | import Utils 24 | import Log 25 | 26 | import qualified Data.Map as M 27 | import qualified OctreePar as O 28 | 29 | -------------------------------------------------------------------------------- 30 | 31 | data Env os c ds = Env 32 | { envFPS :: !Int 33 | , envZDistClosest :: !Float 34 | , envZDistFarthest :: !Float 35 | , envMVP :: Buffer os (Uniform (V4 (B4 Float))) 36 | , envBoidPositions :: Buffer os (B3 Float, B3 Float) 37 | , envBoidVerts :: Buffer os (B3 Float, B3 Float) 38 | , envShader :: BoidShader os c ds 39 | } 40 | 41 | data State = State 42 | { stateRunning :: !Bool 43 | , stateInputMap :: !InputStateMap 44 | , stateXAngle :: !Float 45 | , stateYAngle :: !Float 46 | , stateZDist :: !Float 47 | , stateZoomDir :: !Float 48 | , stateMouseDown :: !Bool 49 | , stateDragging :: !Bool 50 | , stateDragX :: !Float 51 | , stateDragY :: !Float 52 | , stateDragOldX :: !Float 53 | , stateDragOldY :: !Float 54 | , stateOctree :: !O.Octree 55 | } 56 | 57 | instance (Monad m, Monoid w) => InputStore (RWST r w State m) where 58 | getStateMap = gets stateInputMap 59 | setStateMap sm = modify $ \s -> s { stateInputMap = sm } 60 | 61 | type Sim os c ds = RWST (Env os c ds) () State (ContextT GLFW.GLFWWindow os (ContextFormat c ds) IO) 62 | 63 | -------------------------------------------------------------------------------- 64 | 65 | actions :: [EventAction (Sim os c ds)] 66 | actions = [ CursorAction cursorAction 67 | , makeMouseEvent GLFW.MouseButton'1 GLFW.MouseButtonState'Pressed mouseDownAction 68 | , makeMouseEvent GLFW.MouseButton'1 GLFW.MouseButtonState'Released mouseUpAction 69 | , makeKeyEvent GLFW.Key'W GLFW.KeyState'Pressed (zoomOnAction True) 70 | , makeKeyEvent GLFW.Key'W GLFW.KeyState'Released zoomOffAction 71 | , makeKeyEvent GLFW.Key'S GLFW.KeyState'Pressed (zoomOnAction False) 72 | , makeKeyEvent GLFW.Key'S GLFW.KeyState'Released zoomOffAction 73 | , makeKeyEvent GLFW.Key'Q GLFW.KeyState'Pressed closeAction 74 | , makeKeyEvent GLFW.Key'Escape GLFW.KeyState'Pressed closeAction 75 | ] 76 | 77 | main :: IO () 78 | main = do 79 | ( maybeNumBoids :: Maybe Int ) <- readArgs 80 | let width = 1600 81 | height = 1000 82 | numBoids = case maybeNumBoids of 83 | (Just n) -> n 84 | _ -> 1000 85 | bounds = 28 86 | winConf :: GLFW.WindowConf 87 | winConf = GLFW.WindowConf width height "Flocking Simulation" 88 | 89 | runContextT (GLFW.newContext' [] winConf) (ContextFormatColorDepth RGB8 Depth16) $ do 90 | uMVP :: Buffer os (Uniform (V4 (B4 Float))) <- newBuffer 1 91 | boidPos :: Buffer os (B3 Float, B3 Float) <- newBuffer numBoids 92 | boidVerts :: Buffer os (B3 Float, B3 Float) <- newBuffer 18 93 | writeBuffer boidVerts 0 $ makeModel 0.15 94 | 95 | boids <- liftIO $ makeBoids (-bounds, -bounds, -bounds) 96 | ( bounds, bounds, bounds) numBoids 97 | 98 | shader <- compileShader $ do 99 | primitiveStream <- toPrimitiveStream id 100 | mvp <- getUniform (const (uMVP, 0)) 101 | let primitiveStream' = fmap (transformStream mvp) primitiveStream 102 | colorOption = ContextColorOption NoBlending (V3 True True True) 103 | depthOption = DepthOption Less True 104 | rasterOptions = (FrontAndBack, ViewPort 0 (V2 width height), DepthRange 0 1) 105 | 106 | fragmentStream <- rasterize (const rasterOptions) primitiveStream' 107 | let fragmentStream' = withRasterizedInfo getColorDepth fragmentStream 108 | 109 | drawContextColorDepth (const (colorOption, depthOption)) fragmentStream' 110 | 111 | let zDistClosest = 10 112 | zDistFarthest = zDistClosest + 80 113 | zDist = zDistClosest + ((zDistFarthest - zDistClosest) / 2) 114 | env = Env 115 | { envFPS = 60 116 | , envZDistClosest = zDistClosest 117 | , envZDistFarthest = zDistFarthest 118 | , envMVP = uMVP 119 | , envBoidPositions = boidPos 120 | , envBoidVerts = boidVerts 121 | , envShader = shader 122 | } 123 | state = State 124 | { stateRunning = True 125 | , stateInputMap = M.empty 126 | , stateXAngle = 0 127 | , stateYAngle = 0 128 | , stateZDist = zDist 129 | , stateZoomDir = 0 130 | , stateMouseDown = False 131 | , stateDragging = False 132 | , stateDragX = 0 133 | , stateDragY = 0 134 | , stateDragOldX = 0 135 | , stateDragOldY = 0 136 | , stateOctree = O.splitWith (O.fromList boids (V3 0 0 0) (fromIntegral bounds)) ((> 8) . O.count) 137 | } 138 | 139 | liftIO $ swapInterval 1 140 | runSim env state 141 | 142 | -------------------------------------------------------------------------------- 143 | 144 | runSim :: (ContextColorFormat c, Color c Float ~ V3 Float, DepthRenderable ds) 145 | => Env os c ds -> State -> ContextT GLFW.GLFWWindow os (ContextFormat c ds) IO () 146 | runSim env state = 147 | void $ evalRWST run env state 148 | 149 | run :: (ContextColorFormat c, Color c Float ~ V3 Float, DepthRenderable ds) 150 | => Sim os c ds () 151 | run = do 152 | processEvents actions 153 | handleCamera 154 | 155 | env <- ask 156 | tree <- gets stateOctree 157 | viewProjMat <- makeMVP 158 | 159 | let (bs, tree') = updateOctree tree 160 | positions = map (\b -> (bPos b, bVel b)) bs 161 | uMVP = envMVP env 162 | posB = envBoidPositions env 163 | vertB = envBoidVerts env 164 | shader = envShader env 165 | 166 | modify $ \s -> s 167 | { stateOctree = tree' 168 | } 169 | 170 | lift $ do 171 | writeBuffer posB 0 $ positions 172 | writeBuffer uMVP 0 [viewProjMat] 173 | render $ do 174 | clearContextColor (V3 0.005 0.020 0.035) 175 | clearContextDepth 1 176 | vertArray <- newVertexArray vertB 177 | posArray <- newVertexArray posB 178 | shader $ toPrimitiveArrayInstanced TriangleList (,) vertArray posArray 179 | swapContextBuffers 180 | 181 | closeRequested <- lift $ GLFW.windowShouldClose 182 | running <- gets stateRunning 183 | unless (closeRequested || not running) run 184 | 185 | -------------------------------------------------------------------------------- 186 | 187 | handleCamera :: Sim os c ds () 188 | handleCamera = do 189 | state <- get 190 | env <- ask 191 | let zmin = envZDistClosest env 192 | zmax = envZDistFarthest env 193 | zdir = stateZoomDir state 194 | z = stateZDist state 195 | x = stateDragX state 196 | y = stateDragY state 197 | x' = stateDragOldX state 198 | y' = stateDragOldY state 199 | xa = stateXAngle state 200 | ya = stateYAngle state 201 | (xa', ya') = if stateDragging state 202 | then (xa + ((x - x') / 512), ya + ((y - y') / 512)) 203 | else (xa, ya) 204 | put $ state 205 | { stateXAngle = xa' 206 | , stateYAngle = ya' 207 | , stateZDist = min zmax (max zmin (z + (zdir))) 208 | } 209 | 210 | makeMVP :: Sim os c ds (M44 Float) 211 | makeMVP = do 212 | state <- get 213 | (V2 w h) <- lift getContextBuffersSize 214 | let xa = stateXAngle state 215 | ya = stateYAngle state 216 | zDist = stateZDist state 217 | xQuat = axisAngle (V3 0 1 0) xa 218 | yQuat = axisAngle (V3 1 0 0) ya 219 | modelRot = fromQuaternion $ yQuat * xQuat 220 | modelMat = mkTransformationMat modelRot (V3 0 0 0) 221 | projMat = perspective (pi/3) (fromIntegral w / fromIntegral h) 1 200 222 | viewMat = mkTransformationMat identity (- V3 0 0 zDist) 223 | return $ projMat !*! viewMat !*! modelMat 224 | 225 | updateOctree :: O.Octree -> ([Boid], O.Octree) 226 | updateOctree tree = (boids', tree') 227 | where center = O.center tree 228 | len = O.len tree 229 | boids = O.flattenTree tree 230 | neighborFunc = (\b -> O.kNearestNeighbors tree (bPos b) 7 (bRad b)) 231 | updateFunc = (\b -> updateBoidRadius b $ neighborFunc b) 232 | boids' = parMap rdeepseq updateFunc boids 233 | --boids' = map updateFunc boids 234 | tree' = O.splitWith (O.fromList boids' center len) ((> 8) . O.count) 235 | 236 | -------------------------------------------------------------------------------- 237 | 238 | mouseDownAction :: Sim os c ds () 239 | mouseDownAction = modify $ \s -> s { stateMouseDown = True } 240 | 241 | mouseUpAction :: Sim os c ds () 242 | mouseUpAction = modify $ \s -> s 243 | { stateMouseDown = False 244 | , stateDragging = False 245 | } 246 | 247 | cursorAction :: (Double, Double) -> Sim os c ds () 248 | cursorAction (x, y) = do 249 | state <- get 250 | when (stateMouseDown state) $ 251 | let (x', y') = if stateDragging state 252 | then (stateDragX state, stateDragY state) 253 | else (realToFrac x, realToFrac y) 254 | in put $ state 255 | { stateDragging = True 256 | , stateDragX = realToFrac x 257 | , stateDragY = realToFrac y 258 | , stateDragOldX = x' 259 | , stateDragOldY = y' 260 | } 261 | 262 | closeAction :: Sim os c ds () 263 | closeAction = modify $ \s -> s { stateRunning = False } 264 | 265 | zoomOnAction :: Bool -> Sim os c ds () 266 | zoomOnAction zoomIn = modify $ \s -> s { stateZoomDir = if zoomIn then (-0.3) else 0.3 } 267 | 268 | zoomOffAction :: Sim os c ds () 269 | zoomOffAction = modify $ \s -> s { stateZoomDir = 0 } 270 | -------------------------------------------------------------------------------- /src/Octree.hs: -------------------------------------------------------------------------------- 1 | module Octree where 2 | 3 | --------------------------------------------------------- 4 | 5 | import Control.Applicative 6 | import Data.Bits 7 | 8 | import qualified Data.List as L 9 | 10 | import Linear 11 | import Boid 12 | 13 | --------------------------------------------------------- 14 | 15 | data Octree = Node 16 | { center :: V3 Float 17 | , len :: Float 18 | , count :: Int 19 | , ftr, ftl, fbr, fbl, btr, btl, bbr, bbl :: Octree 20 | } -- front, back, top, bottom, right, left 21 | | Leaf 22 | { center :: V3 Float 23 | , len :: Float 24 | , count :: Int 25 | , objects :: [Boid] 26 | } 27 | deriving (Show) 28 | 29 | data Octant = FTR | FTL | FBR | FBL | BTR | BTL | BBR | BBL deriving (Show, Eq, Ord, Enum) 30 | 31 | --------------------------------------------------------- 32 | 33 | emptyOctree :: V3 Float -> Float -> Octree 34 | emptyOctree c l = Leaf c l 0 [] 35 | 36 | fromList :: [Boid] -> V3 Float -> Float -> Octree 37 | fromList boids c l = foldl insert (emptyOctree c l) boids 38 | 39 | {-# INLINE vSqLen #-} 40 | vSqLen :: (Floating a, Eq a) => V3 a -> a 41 | vSqLen (V3 x y z) = (x * x) + (y * y) + (z * z) 42 | 43 | {-# INLINE vDist #-} 44 | vDist :: (Floating a, Eq a) => V3 a -> V3 a -> a 45 | vDist (V3 x1 y1 z1) (V3 x2 y2 z2) = sqrt $ (x * x) + (y * y) + (z * z) 46 | where x = x2 - x1 47 | y = y2 - y1 48 | z = z2 - z1 49 | 50 | --------------------------------------------------------- 51 | 52 | octreeMap :: (Boid -> Boid) -> Octree -> Octree 53 | octreeMap func tree = insertList (emptyOctree (center tree) (len tree)) $ map func boids 54 | where boids = flattenTree tree 55 | 56 | octreeMapM_ :: (Boid -> IO a) -> Octree -> IO () 57 | octreeMapM_ func (Leaf _ _ _ objs) = mapM_ func objs 58 | octreeMapM_ func (Node _ _ _ a b c d e f g h) = do 59 | octreeMapM_ func a 60 | octreeMapM_ func b 61 | octreeMapM_ func c 62 | octreeMapM_ func d 63 | octreeMapM_ func e 64 | octreeMapM_ func f 65 | octreeMapM_ func g 66 | octreeMapM_ func h 67 | return () 68 | 69 | octreeFold :: (a -> Boid -> a) -> a -> Octree -> a 70 | octreeFold func i (Node _ _ _ a b c d e f g h) = octreeFold func p h 71 | where j = octreeFold func i a 72 | k = octreeFold func j b 73 | l = octreeFold func k c 74 | m = octreeFold func l d 75 | n = octreeFold func m e 76 | o = octreeFold func n f 77 | p = octreeFold func o g 78 | octreeFold func i (Leaf _ _ _ objs) = foldl func i objs 79 | 80 | --------------------------------------------------------- 81 | 82 | prettyPrint :: Octree -> String 83 | prettyPrint (Node cen l cnt a b c d e f g h) = "Node {\n\tcenter: " ++ (show cen) ++ "\n\tlength: " ++ (show l) ++ 84 | "\n\tcount: " ++ (show cnt) ++ "\n" ++ (concat $ L.intersperse "\n" $ map prettyPrint [a, b, c, d, e, f, g, h]) ++ "\n}" 85 | prettyPrint (Leaf cen l cnt objs) = "Leaf {\n\tcenter: " ++ (show cen) ++ "\n\tlength: " ++ (show l) ++ 86 | "\n\tcount: " ++ (show cnt) ++ "\n" ++ (concat $ L.intersperse "\n\t" $ map show objs) ++ "\n}" 87 | 88 | --------------------------------------------------------- 89 | 90 | getOctant :: V3 Float -> V3 Float -> Octant 91 | getOctant (V3 cx cy cz) (V3 px py pz) = toEnum $ (fromEnum right) + (2 * fromEnum top) + (4 * fromEnum front) 92 | where front = pz < cz 93 | top = py < cy 94 | right = px < cx 95 | 96 | getSubtree :: Octree -> Octant -> Octree 97 | getSubtree (Node _ _ _ a b c d e f g h) octant = 98 | case octant of 99 | FTR -> a 100 | FTL -> b 101 | FBR -> c 102 | FBL -> d 103 | BTR -> e 104 | BTL -> f 105 | BBR -> g 106 | BBL -> h 107 | getSubtree tree _ = tree 108 | 109 | replaceSubtree :: Octree -> Octant -> Octree -> Octree 110 | replaceSubtree t@(Node cen l cnt a b c d e f g h) octant subtree = 111 | case octant of 112 | FTR -> Node cen l nCnt subtree b c d e f g h 113 | FTL -> Node cen l nCnt a subtree c d e f g h 114 | FBR -> Node cen l nCnt a b subtree d e f g h 115 | FBL -> Node cen l nCnt a b c subtree e f g h 116 | BTR -> Node cen l nCnt a b c d subtree f g h 117 | BTL -> Node cen l nCnt a b c d e subtree g h 118 | BBR -> Node cen l nCnt a b c d e f subtree h 119 | BBL -> Node cen l nCnt a b c d e f g subtree 120 | where nCnt = cnt - (count $ getSubtree t octant) + (count subtree) 121 | replaceSubtree tree _ _ = tree 122 | 123 | flattenTree :: Octree -> [Boid] 124 | flattenTree = octreeFold (flip (:)) [] 125 | 126 | insert :: Octree -> Boid -> Octree 127 | insert (Leaf cen l cnt xs) obj = Leaf cen l (cnt + 1) $ obj:xs 128 | insert node obj = replaceSubtree node octant $ insert (getSubtree node octant) obj 129 | where octant = getOctant (center node) (bPos obj) 130 | 131 | insertList :: Octree -> [Boid] -> Octree 132 | insertList = foldl insert 133 | 134 | splitTree :: Octree -> Octree 135 | splitTree (Leaf c@(V3 cx cy cz) l cnt objs) = foldl insert tree objs 136 | where tree = Node 137 | { center = c 138 | , len = l 139 | , count = cnt 140 | , ftr = et rx ty fz, ftl = et lx ty fz 141 | , fbr = et rx by fz, fbl = et lx by fz 142 | , btr = et rx ty bz, btl = et lx ty bz 143 | , bbr = et rx by bz, bbl = et lx by bz 144 | } 145 | et x y z = emptyOctree (V3 x y z) hl 146 | hl = l / 2 147 | rx = cx + hl 148 | lx = cx - hl 149 | ty = cy + hl 150 | by = cy - hl 151 | fz = cz + hl 152 | bz = cz - hl 153 | splitTree tree = tree 154 | 155 | splitWith :: Octree -> (Octree -> Bool) -> Octree 156 | splitWith (Node cen len cnt i j k l m n o p) f = Node cen len cnt (s i) (s j) (s k) (s l) (s m) (s n) (s o) (s p) 157 | where s tree = splitWith tree f 158 | splitWith tree func 159 | | func tree = splitWith (splitTree tree) func 160 | | otherwise = tree 161 | 162 | getNearObjects :: Octree -> V3 Float -> [Boid] 163 | getNearObjects (Leaf _ _ _ objs) _ = objs 164 | getNearObjects node pos = getNearObjects subtree pos 165 | where subtree = getSubtree node $ getOctant (center node) pos 166 | 167 | xOppOctant, yOppOctant, zOppOctant :: Octant -> Octant 168 | xOppOctant octant = toEnum $ xor (fromEnum octant) 1 169 | yOppOctant octant = toEnum $ xor (fromEnum octant) 2 170 | zOppOctant octant = toEnum $ xor (fromEnum octant) 4 171 | 172 | getRadiusObjects :: Octree -> V3 Float -> Float -> [Boid] 173 | getRadiusObjects (Leaf _ l _ objs) pos r 174 | | r > l = objs 175 | | otherwise = filter (\obj -> (r * r) > (vSqLen $ pos - bPos obj)) objs 176 | getRadiusObjects node pos r = concat . (map (\t -> getRadiusObjects t pos r)) $ intersectingSubtrees node pos r 177 | 178 | -- Return True iff the sphere around the given position exceeds the bounds of 179 | -- the given Octree. 180 | {-# INLINE inBounds #-} 181 | inBounds :: Octree -> V3 Float -> Float -> Bool 182 | inBounds tree pos rad = lX && lY && lZ && uX && uY && uZ 183 | where V3 x y z = pos - center tree 184 | hl = len tree / 2 185 | lX = -hl < x - rad 186 | lY = -hl < y - rad 187 | lZ = -hl < z - rad 188 | uX = hl > x + rad 189 | uY = hl > y + rad 190 | uZ = hl > z + rad 191 | 192 | -- Return a list of the subtrees intersecting with the given bounding sphere 193 | -- Note: The last subtree in the list is always the the one containing the point 194 | {-# INLINE intersectingSubtrees #-} 195 | intersectingSubtrees :: Octree -> V3 Float -> Float -> [Octree] 196 | intersectingSubtrees l@(Leaf {}) _ _ = return l 197 | intersectingSubtrees node p@(V3 px py pz) rad = map (getSubtree node) octants 198 | where octant = getOctant c p 199 | octants = if rad > abs (pz - cz) then foldr (\o zs -> (zOppOctant o):o:zs) [] tmpY else tmpY 200 | tmpY = if rad > abs (py - cy) then foldr (\o ys -> (yOppOctant o):o:ys) [] tmpX else tmpX 201 | tmpX = if rad > abs (px - cx) then (xOppOctant octant):[octant] else [octant] 202 | c@(V3 cx cy cz) = center node 203 | 204 | kNearestNeighbors :: Octree -> V3 Float -> Int -> Float -> [(Boid, Float)] 205 | kNearestNeighbors (Leaf _ _ _ objs) pos k maxR = 206 | take k $ L.sortBy sortByDist $ filter filtFunc $ map (getObjDist pos) objs 207 | where filtFunc (_, rad) = rad < maxR 208 | kNearestNeighbors node pos k maxR 209 | | inBounds subtree pos topR && length nearest >= k = nearest 210 | | otherwise = take k $ foldl combineNeighbors nearest $ getOtherNeighbors node pos k topR 211 | where subtree = getSubtree node (getOctant (center node) pos) 212 | nearest = kNearestNeighbors subtree pos k maxR 213 | topR = if length nearest >= k then snd $ last nearest else maxR 214 | 215 | {-# INLINE getOtherNeighbors #-} 216 | getOtherNeighbors :: Octree -> V3 Float -> Int -> Float -> [[(Boid, Float)]] 217 | getOtherNeighbors tree pos k rad = 218 | map (\t -> kNearestNeighbors t pos k rad) $ init $ intersectingSubtrees tree pos rad 219 | 220 | {-# INLINE combineNeighbors #-} 221 | combineNeighbors :: [(Boid, Float)] -> [(Boid, Float)] -> [(Boid, Float)] 222 | combineNeighbors xs [] = xs 223 | combineNeighbors [] ys = ys 224 | combineNeighbors (x@(_, rx):xs) (y@(_, ry):ys) = 225 | if rx > ry 226 | then y : combineNeighbors (x:xs) ys 227 | else x : combineNeighbors xs (y:ys) 228 | 229 | {-# INLINE getObjDist #-} 230 | getObjDist :: V3 Float -> Boid -> (Boid, Float) 231 | getObjDist pos obj = (obj, vDist (bPos obj) pos) 232 | 233 | {-# INLINE sortByDist #-} 234 | sortByDist :: (Boid, Float) -> (Boid, Float) -> Ordering 235 | sortByDist (_, r1) (_, r2) = r1 `compare` r2 236 | -------------------------------------------------------------------------------- /src/OctreePar.hs: -------------------------------------------------------------------------------- 1 | {-# LANGUAGE DeriveGeneric, DeriveAnyClass #-} 2 | 3 | module OctreePar where 4 | 5 | --------------------------------------------------------- 6 | 7 | import Control.Monad.Par 8 | import Control.Applicative 9 | import Control.DeepSeq 10 | import GHC.Generics (Generic) 11 | import Data.Bits 12 | 13 | import qualified Data.List as L 14 | 15 | import Linear 16 | import Boid 17 | 18 | --------------------------------------------------------- 19 | 20 | data Octree = Node 21 | { center :: V3 Float 22 | , len :: Float 23 | , count :: Int 24 | , ftr, ftl, fbr, fbl, btr, btl, bbr, bbl :: Octree 25 | } -- front, back, top, bottom, right, left 26 | | Leaf 27 | { center :: V3 Float 28 | , len :: Float 29 | , count :: Int 30 | , objects :: [Boid] 31 | } 32 | deriving (Show, Generic, NFData) 33 | 34 | data Octant = FTR | FTL | FBR | FBL | BTR | BTL | BBR | BBL deriving (Show, Eq, Ord, Enum) 35 | 36 | --------------------------------------------------------- 37 | 38 | emptyOctree :: V3 Float -> Float -> Octree 39 | emptyOctree c l = Leaf c l 0 [] 40 | 41 | fromList :: [Boid] -> V3 Float -> Float -> Octree 42 | fromList boids c l = foldl insert (emptyOctree c l) boids 43 | 44 | {-# INLINE vSqLen #-} 45 | vSqLen :: (Floating a, Eq a) => V3 a -> a 46 | vSqLen (V3 x y z) = (x * x) + (y * y) + (z * z) 47 | 48 | {-# INLINE vDist #-} 49 | vDist :: (Floating a, Eq a) => V3 a -> V3 a -> a 50 | vDist (V3 x1 y1 z1) (V3 x2 y2 z2) = sqrt $ (x * x) + (y * y) + (z * z) 51 | where x = x2 - x1 52 | y = y2 - y1 53 | z = z2 - z1 54 | 55 | --------------------------------------------------------- 56 | 57 | octreeMap :: (Boid -> Boid) -> Octree -> Octree 58 | octreeMap func tree = insertList (emptyOctree (center tree) (len tree)) $ map func boids 59 | where boids = flattenTree tree 60 | 61 | octreeMapM_ :: (Boid -> IO a) -> Octree -> IO () 62 | octreeMapM_ func (Leaf _ _ _ objs) = mapM_ func objs 63 | octreeMapM_ func (Node _ _ _ a b c d e f g h) = do 64 | octreeMapM_ func a 65 | octreeMapM_ func b 66 | octreeMapM_ func c 67 | octreeMapM_ func d 68 | octreeMapM_ func e 69 | octreeMapM_ func f 70 | octreeMapM_ func g 71 | octreeMapM_ func h 72 | return () 73 | 74 | octreeFold :: (a -> Boid -> a) -> a -> Octree -> a 75 | octreeFold func i (Node _ _ _ a b c d e f g h) = octreeFold func p h 76 | where j = octreeFold func i a 77 | k = octreeFold func j b 78 | l = octreeFold func k c 79 | m = octreeFold func l d 80 | n = octreeFold func m e 81 | o = octreeFold func n f 82 | p = octreeFold func o g 83 | octreeFold func i (Leaf _ _ _ objs) = foldl func i objs 84 | 85 | --------------------------------------------------------- 86 | 87 | prettyPrint :: Octree -> String 88 | prettyPrint (Node cen l cnt a b c d e f g h) = "Node {\n\tcenter: " ++ (show cen) ++ "\n\tlength: " ++ (show l) ++ 89 | "\n\tcount: " ++ (show cnt) ++ "\n" ++ (concat $ L.intersperse "\n" $ map prettyPrint [a, b, c, d, e, f, g, h]) ++ "\n}" 90 | prettyPrint (Leaf cen l cnt objs) = "Leaf {\n\tcenter: " ++ (show cen) ++ "\n\tlength: " ++ (show l) ++ 91 | "\n\tcount: " ++ (show cnt) ++ "\n" ++ (concat $ L.intersperse "\n\t" $ map show objs) ++ "\n}" 92 | 93 | --------------------------------------------------------- 94 | 95 | getOctant :: V3 Float -> V3 Float -> Octant 96 | getOctant (V3 cx cy cz) (V3 px py pz) = toEnum $ (fromEnum right) + (2 * fromEnum top) + (4 * fromEnum front) 97 | where front = pz < cz 98 | top = py < cy 99 | right = px < cx 100 | 101 | getSubtree :: Octree -> Octant -> Octree 102 | getSubtree (Node _ _ _ a b c d e f g h) octant = 103 | case octant of 104 | FTR -> a 105 | FTL -> b 106 | FBR -> c 107 | FBL -> d 108 | BTR -> e 109 | BTL -> f 110 | BBR -> g 111 | BBL -> h 112 | getSubtree tree _ = tree 113 | 114 | replaceSubtree :: Octree -> Octant -> Octree -> Octree 115 | replaceSubtree t@(Node cen l cnt a b c d e f g h) octant subtree = 116 | case octant of 117 | FTR -> Node cen l nCnt subtree b c d e f g h 118 | FTL -> Node cen l nCnt a subtree c d e f g h 119 | FBR -> Node cen l nCnt a b subtree d e f g h 120 | FBL -> Node cen l nCnt a b c subtree e f g h 121 | BTR -> Node cen l nCnt a b c d subtree f g h 122 | BTL -> Node cen l nCnt a b c d e subtree g h 123 | BBR -> Node cen l nCnt a b c d e f subtree h 124 | BBL -> Node cen l nCnt a b c d e f g subtree 125 | where nCnt = cnt - (count $ getSubtree t octant) + (count subtree) 126 | replaceSubtree tree _ _ = tree 127 | 128 | flattenTree :: Octree -> [Boid] 129 | flattenTree = octreeFold (flip (:)) [] 130 | 131 | insert :: Octree -> Boid -> Octree 132 | insert (Leaf cen l cnt xs) obj = Leaf cen l (cnt + 1) $ obj:xs 133 | insert node obj = replaceSubtree node octant $ insert (getSubtree node octant) obj 134 | where octant = getOctant (center node) (bPos obj) 135 | 136 | insertList :: Octree -> [Boid] -> Octree 137 | insertList = foldl insert 138 | 139 | splitTree :: Octree -> Octree 140 | splitTree (Leaf c@(V3 cx cy cz) l cnt objs) = foldl insert tree objs 141 | where tree = Node 142 | { center = c 143 | , len = l 144 | , count = cnt 145 | , ftr = et rx ty fz, ftl = et lx ty fz 146 | , fbr = et rx by fz, fbl = et lx by fz 147 | , btr = et rx ty bz, btl = et lx ty bz 148 | , bbr = et rx by bz, bbl = et lx by bz 149 | } 150 | et x y z = emptyOctree (V3 x y z) hl 151 | hl = l / 2 152 | rx = cx + hl 153 | lx = cx - hl 154 | ty = cy + hl 155 | by = cy - hl 156 | fz = cz + hl 157 | bz = cz - hl 158 | splitTree tree = tree 159 | 160 | splitWith :: Octree -> (Octree -> Bool) -> Octree 161 | splitWith (Node cen len cnt i j k l m n o p) f = newNode 162 | where s tree = splitWith tree f 163 | getSpawn a = spawnP a >>= get 164 | newNode = runPar $ do 165 | a <- getSpawn $ s i 166 | b <- getSpawn $ s j 167 | c <- getSpawn $ s k 168 | d <- getSpawn $ s l 169 | e <- getSpawn $ s m 170 | f <- getSpawn $ s n 171 | g <- getSpawn $ s o 172 | h <- getSpawn $ s p 173 | return $ Node cen len cnt a b c d e f g h 174 | splitWith tree func 175 | | func tree = splitWith (splitTree tree) func 176 | | otherwise = tree 177 | 178 | getNearObjects :: Octree -> V3 Float -> [Boid] 179 | getNearObjects (Leaf _ _ _ objs) _ = objs 180 | getNearObjects node pos = getNearObjects subtree pos 181 | where subtree = getSubtree node $ getOctant (center node) pos 182 | 183 | xOppOctant, yOppOctant, zOppOctant :: Octant -> Octant 184 | xOppOctant octant = toEnum $ xor (fromEnum octant) 1 185 | yOppOctant octant = toEnum $ xor (fromEnum octant) 2 186 | zOppOctant octant = toEnum $ xor (fromEnum octant) 4 187 | 188 | getRadiusObjects :: Octree -> V3 Float -> Float -> [Boid] 189 | getRadiusObjects (Leaf _ l _ objs) pos r 190 | | r > l = objs 191 | | otherwise = filter (\obj -> (r * r) > (vSqLen $ pos - bPos obj)) objs 192 | getRadiusObjects node pos r = concat . (map (\t -> getRadiusObjects t pos r)) $ intersectingSubtrees node pos r 193 | 194 | -- Return True iff the sphere around the given position exceeds the bounds of 195 | -- the given Octree. 196 | {-# INLINE inBounds #-} 197 | inBounds :: Octree -> V3 Float -> Float -> Bool 198 | inBounds tree pos rad = lX && lY && lZ && uX && uY && uZ 199 | where V3 x y z = pos - center tree 200 | hl = len tree / 2 201 | lX = -hl < x - rad 202 | lY = -hl < y - rad 203 | lZ = -hl < z - rad 204 | uX = hl > x + rad 205 | uY = hl > y + rad 206 | uZ = hl > z + rad 207 | 208 | -- Return a list of the subtrees intersecting with the given bounding sphere 209 | -- Note: The last subtree in the list is always the the one containing the point 210 | {-# INLINE intersectingSubtrees #-} 211 | intersectingSubtrees :: Octree -> V3 Float -> Float -> [Octree] 212 | intersectingSubtrees l@(Leaf {}) _ _ = return l 213 | intersectingSubtrees node p@(V3 px py pz) rad = map (getSubtree node) octants 214 | where octant = getOctant c p 215 | octants = if rad > abs (pz - cz) then foldr (\o zs -> (zOppOctant o):o:zs) [] tmpY else tmpY 216 | tmpY = if rad > abs (py - cy) then foldr (\o ys -> (yOppOctant o):o:ys) [] tmpX else tmpX 217 | tmpX = if rad > abs (px - cx) then (xOppOctant octant):[octant] else [octant] 218 | c@(V3 cx cy cz) = center node 219 | 220 | kNearestNeighbors :: Octree -> V3 Float -> Int -> Float -> [(Boid, Float)] 221 | kNearestNeighbors (Leaf _ _ _ objs) pos k maxR = 222 | take k $ L.sortBy sortByDist $ filter filtFunc $ map (getObjDist pos) objs 223 | where filtFunc (_, rad) = rad < maxR 224 | kNearestNeighbors node pos k maxR 225 | | inBounds subtree pos topR && length nearest >= k = nearest 226 | | otherwise = take k $ foldl combineNeighbors nearest $ getOtherNeighbors node pos k topR 227 | where subtree = getSubtree node (getOctant (center node) pos) 228 | nearest = kNearestNeighbors subtree pos k maxR 229 | topR = if length nearest >= k then snd $ last nearest else maxR 230 | 231 | {-# INLINE getOtherNeighbors #-} 232 | getOtherNeighbors :: Octree -> V3 Float -> Int -> Float -> [[(Boid, Float)]] 233 | getOtherNeighbors tree pos k rad = 234 | map (\t -> kNearestNeighbors t pos k rad) $ init $ intersectingSubtrees tree pos rad 235 | 236 | {-# INLINE combineNeighbors #-} 237 | combineNeighbors :: [(Boid, Float)] -> [(Boid, Float)] -> [(Boid, Float)] 238 | combineNeighbors xs [] = xs 239 | combineNeighbors [] ys = ys 240 | combineNeighbors (x@(_, rx):xs) (y@(_, ry):ys) = 241 | if rx > ry 242 | then y : combineNeighbors (x:xs) ys 243 | else x : combineNeighbors xs (y:ys) 244 | 245 | {-# INLINE getObjDist #-} 246 | getObjDist :: V3 Float -> Boid -> (Boid, Float) 247 | getObjDist pos obj = (obj, vDist (bPos obj) pos) 248 | 249 | {-# INLINE sortByDist #-} 250 | sortByDist :: (Boid, Float) -> (Boid, Float) -> Ordering 251 | sortByDist (_, r1) (_, r2) = r1 `compare` r2 252 | -------------------------------------------------------------------------------- /src/Shader.hs: -------------------------------------------------------------------------------- 1 | module Shader where 2 | 3 | import Prelude hiding ((<*)) 4 | import Graphics.GPipe 5 | import Linear 6 | 7 | import Utils 8 | 9 | type BoidShader os c ds = PrimitiveArray Triangles ((B3 Float, B3 Float), (B3 Float, B3 Float)) -> Render os (ContextFormat c ds) () 10 | 11 | transformStream :: (IfB a, OrdB a, Floating a) => M44 a -> ((V3 a, V3 a), (V3 a, V3 a)) -> (V4 a, V3 a) 12 | transformStream mvp ((vert, norm), (pos, dir)) = (pos', color) 13 | where 14 | normDir = vNorm dir 15 | axis = vNorm $ cross (V3 0 0 1) normDir 16 | aAxis = vNorm $ cross normDir axis 17 | rotationMat = transpose $ V3 axis aAxis normDir 18 | transformMat = mvp !*! mkTransformationMat rotationMat pos 19 | pos' = transformMat !* makeV4 vert 20 | rNorm = rotationMat !* norm 21 | (V4 _ _ d _) = mvp !* makeV4 pos 22 | color = makeColor rNorm d 23 | 24 | {-# INLINE makeV4 #-} 25 | makeV4 :: Num a => V3 a -> V4 a 26 | makeV4 (V3 x y z) = V4 x y z 1 27 | 28 | {-# INLINE interpolate #-} 29 | interpolate :: (IfB a, OrdB a, Floating a) => V3 a -> V3 a -> a -> V3 a 30 | interpolate v1 v2 t = ((1 - t') *^ v1) ^+^ (t' *^ v2) 31 | where t' = sClamp 0 1 t 32 | 33 | {-# INLINE sClamp #-} 34 | sClamp :: (IfB a, OrdB a, Floating a) => a -> a -> a -> a 35 | sClamp lower upper val = minB upper (maxB lower val) 36 | 37 | {-# INLINE makeColor #-} 38 | makeColor :: (IfB a, OrdB a, Floating a) => V3 a -> a -> V3 a 39 | makeColor normal depth = color' 40 | where lightVal = normal `dot` V3 0 1 0 41 | cLight x = (0.6 + (x / 1.75)) *^ V3 0.2 0.4 0.8 42 | cDark x = (0.9 + (x / 1.75)) *^ V3 0.2 0.1 0.5 43 | fadeVal = (100 - depth) / 80 44 | color = caseB lightVal 45 | [ ((>* 0.1), cLight lightVal) 46 | , ((<* (-0.1)), cDark lightVal) 47 | ] 48 | (interpolate (cDark (-0.1)) (cLight 0.1) ((0.1 + lightVal) * 5)) 49 | color' = interpolate (V3 0.010 0.015 0.035) color fadeVal 50 | 51 | {-# INLINE getColorDepth #-} 52 | getColorDepth :: V3 FFloat -> RasterizedInfo -> (V3 FFloat, FFloat) 53 | getColorDepth color info = (color, depth) 54 | where (V4 _ _ depth _) = rasterizedFragCoord info 55 | -------------------------------------------------------------------------------- /src/Utils.hs: -------------------------------------------------------------------------------- 1 | module Utils where 2 | 3 | import System.Random 4 | --import Foreign.C.Types (CInt) 5 | --import Foreign.Marshal.Alloc (malloc, free) 6 | --import Foreign.Ptr (Ptr) 7 | --import Foreign.Storable (peek) 8 | 9 | import Linear 10 | 11 | getRandom :: (Random a) => a -> a -> IO a 12 | getRandom l u = do 13 | seed <- getStdGen 14 | let (n, s) = randomR (l, u) seed 15 | setStdGen s 16 | return n 17 | 18 | {-# INLINE vDot #-} 19 | {-# INLINE vDist #-} 20 | vDot, vDist :: (Floating a) => V3 a -> V3 a -> a 21 | vDot (V3 x1 y1 z1) (V3 x2 y2 z2) = (x1 * x2) + (y1 * y2) + (z1 * z2) 22 | vDist (V3 x1 y1 z1) (V3 x2 y2 z2) = sqrt $ (x * x) + (y * y) + (z * z) 23 | where x = x2 - x1 24 | y = y2 - y1 25 | z = z2 - z1 26 | 27 | {-# INLINE vLen #-} 28 | {-# INLINE vSqLen #-} 29 | vLen, vSqLen :: (Floating a) => V3 a -> a 30 | vSqLen (V3 x y z) = (x * x) + (y * y) + (z * z) 31 | vLen = sqrt . vSqLen 32 | 33 | {-# INLINE vScale #-} 34 | {-# INLINE vScaleTo #-} 35 | {-# INLINE vClamp #-} 36 | vScale, vScaleTo, vClamp :: (Floating a, Ord a) => V3 a -> a -> V3 a 37 | vScale (V3 0 0 0) _ = zero 38 | vScale (V3 x y z) n = V3 (n * x) (n * y) (n * z) 39 | vClamp (V3 0 0 0) _ = zero 40 | vClamp vec n = if sqlen > (n * n) then vec ^* (n / (sqrt sqlen)) else vec 41 | where sqlen = vSqLen vec 42 | vScaleTo vec n = vec ^* (n / (vLen vec)) 43 | 44 | {-# INLINE vNorm #-} 45 | vNorm :: (Floating a) => V3 a -> V3 a 46 | vNorm vec = vec ^/ (vLen vec) 47 | -------------------------------------------------------------------------------- /stack.yaml: -------------------------------------------------------------------------------- 1 | flags: {} 2 | packages: 3 | - '.' 4 | extra-deps: 5 | - GLFW-b-1.4.7.3 6 | - GPipe-2.1.4 7 | - bindings-GLFW-3.1.1.4 8 | - GPipe-GLFW-1.2.1 9 | resolver: lts-3.10 10 | --------------------------------------------------------------------------------