├── .gitignore ├── .gitmodules ├── Doxyfile ├── LICENSE.md ├── Makefile ├── Makefile.vc ├── README.md ├── etc ├── bin2c.c ├── extra.css ├── moon.jpg └── scm.svg ├── scm-cache.cpp ├── scm-cache.hpp ├── scm-deque.cpp ├── scm-deque.hpp ├── scm-fifo.hpp ├── scm-file.cpp ├── scm-file.hpp ├── scm-frame.cpp ├── scm-frame.hpp ├── scm-guard.hpp ├── scm-image.cpp ├── scm-image.hpp ├── scm-index.cpp ├── scm-index.hpp ├── scm-item.hpp ├── scm-label-font.h ├── scm-label-icons.h ├── scm-label.cpp ├── scm-label.hpp ├── scm-log.cpp ├── scm-log.hpp ├── scm-path.cpp ├── scm-path.hpp ├── scm-queue.hpp ├── scm-render.cpp ├── scm-render.hpp ├── scm-sample.cpp ├── scm-sample.hpp ├── scm-scene.cpp ├── scm-scene.hpp ├── scm-set.cpp ├── scm-set.hpp ├── scm-sphere.cpp ├── scm-sphere.hpp ├── scm-state.cpp ├── scm-state.hpp ├── scm-system.cpp ├── scm-system.hpp ├── scm-task.cpp ├── scm-task.hpp ├── scm.vcxproj ├── scm_label_circle_frag.glsl ├── scm_label_circle_vert.glsl ├── scm_label_sprite_frag.glsl ├── scm_label_sprite_vert.glsl ├── scm_render_atmo_frag.glsl ├── scm_render_atmo_vert.glsl ├── scm_render_blur_frag.glsl ├── scm_render_blur_vert.glsl ├── scm_render_both_frag.glsl ├── scm_render_both_vert.glsl ├── scm_render_fade_frag.glsl └── scm_render_fade_vert.glsl /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.d 3 | *.DS_Store 4 | html 5 | libscm.a 6 | scm-*-frag.h 7 | scm-*-vert.h 8 | *.obj 9 | *.lib 10 | *.exe 11 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "util3d"] 2 | path = util3d 3 | url = https://github.com/rlk/util3d.git 4 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # GNU General Public License 2 | 3 | Version 2, June 1991 4 | 5 | Copyright © 1989, 1991 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. 6 | 7 | ## Preamble 8 | 9 | The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too. 10 | 11 | When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. 12 | 13 | To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. 14 | 15 | For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. 16 | 17 | We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. 18 | 19 | Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. 20 | 21 | Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. 22 | 23 | The precise terms and conditions for copying, distribution and 24 | modification follow. 25 | 26 | ## Terms And Conditions For Copying, Distribution And Modification 27 | 28 | 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program"means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". 29 | 30 | Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 31 | 32 | 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. 33 | 34 | You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 35 | 36 | 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: 37 | 38 | a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. 39 | 40 | b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. 41 | 42 | c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) 43 | 44 | These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. 45 | 46 | Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. 47 | 48 | In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 49 | 50 | 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: 51 | 52 | a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, 53 | 54 | b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, 55 | 56 | c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) 57 | 58 | The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. 59 | 60 | If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 61 | 62 | 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 63 | 64 | 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 65 | 66 | 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 67 | 68 | 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. 69 | 70 | If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. 71 | 72 | It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. 73 | 74 | This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 75 | 76 | 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 77 | 78 | 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. 79 | 80 | Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 81 | 82 | 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. 83 | 84 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 85 | 86 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 87 | 88 | ## How to Apply These Terms to Your New Programs 89 | 90 | If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. 91 | 92 | To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. 93 | 94 | 95 | Copyright (C) 96 | 97 | This program is free software; you can redistribute it and/or modify 98 | it under the terms of the GNU General Public License as published by 99 | the Free Software Foundation; either version 2 of the License, or 100 | (at your option) any later version. 101 | 102 | This program is distributed in the hope that it will be useful, 103 | but WITHOUT ANY WARRANTY; without even the implied warranty of 104 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 105 | GNU General Public License for more details. 106 | 107 | You should have received a copy of the GNU General Public License along 108 | with this program; if not, write to the Free Software Foundation, Inc., 109 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 110 | 111 | Also add information on how to contact you by electronic and paper mail. 112 | 113 | If the program is interactive, make it output a short notice like this 114 | when it starts in an interactive mode: 115 | 116 | Gnomovision version 69, Copyright (C) year name of author 117 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 118 | This is free software, and you are welcome to redistribute it 119 | under certain conditions; type `show c' for details. 120 | 121 | The hypothetical commands `show w` and `show c` should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w` and `show c`; they could even be mouse-clicks or menu items--whatever suits your program. 122 | 123 | You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: 124 | 125 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 126 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 127 | 128 | , 1 April 1989 129 | Ty Coon, President of Vice 130 | 131 | This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. 132 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # libscm -- Linux / OS X Makefile 2 | 3 | ifdef DEBUG 4 | CONFIG = Debug 5 | CFLAGS += -g 6 | else 7 | CONFIG = Release 8 | CFLAGS += -O2 -DNDEBUG 9 | endif 10 | 11 | #------------------------------------------------------------------------------ 12 | 13 | OBJS= \ 14 | util3d/glsl.o \ 15 | util3d/math3d.o \ 16 | util3d/type.o \ 17 | scm-cache.o \ 18 | scm-deque.o \ 19 | scm-file.o \ 20 | scm-frame.o \ 21 | scm-image.o \ 22 | scm-index.o \ 23 | scm-label.o \ 24 | scm-log.o \ 25 | scm-path.o \ 26 | scm-render.o \ 27 | scm-sample.o \ 28 | scm-scene.o \ 29 | scm-set.o \ 30 | scm-sphere.o \ 31 | scm-state.o \ 32 | scm-system.o \ 33 | scm-task.o 34 | 35 | DEPS= $(OBJS:.o=.d) 36 | 37 | LABEL_GLSL= \ 38 | scm_label_circle_frag.h \ 39 | scm_label_circle_vert.h \ 40 | scm_label_sprite_frag.h \ 41 | scm_label_sprite_vert.h 42 | RENDER_GLSL= \ 43 | scm_render_blur_frag.h \ 44 | scm_render_blur_vert.h \ 45 | scm_render_both_frag.h \ 46 | scm_render_both_vert.h \ 47 | scm_render_atmo_frag.h \ 48 | scm_render_atmo_vert.h \ 49 | scm_render_fade_frag.h \ 50 | scm_render_fade_vert.h 51 | 52 | GLSL= $(LABEL_GLSL) $(RENDER_GLSL) 53 | 54 | #------------------------------------------------------------------------------ 55 | 56 | SDLCONF = $(firstword $(wildcard /usr/local/bin/sdl2-config \ 57 | /opt/local/bin/sdl2-config \ 58 | /usr/bin/sdl2-config) \ 59 | sdl2-config) 60 | FT2CONF = $(firstword $(wildcard /usr/local/bin/freetype-config \ 61 | /opt/local/bin/freetype-config \ 62 | /usr/bin/freetype-config) \ 63 | freetype-config) 64 | 65 | CONF = $(shell $(SDLCONF) --cflags) \ 66 | $(shell $(FT2CONF) --cflags) 67 | 68 | TARGDIR = $(CONFIG) 69 | TARG = libscm.a 70 | 71 | #------------------------------------------------------------------------------ 72 | 73 | $(TARGDIR)/$(TARG) : $(TARGDIR) $(OBJS) 74 | ar -r $(TARGDIR)/$(TARG) $(OBJS) 75 | 76 | $(TARGDIR) : 77 | mkdir -p $(TARGDIR) 78 | 79 | clean: 80 | $(RM) $(TARGDIR)/$(TARG) $(GLSL) $(OBJS) $(DEPS) 81 | 82 | #------------------------------------------------------------------------------ 83 | # The bin2c tool embeds binary data in C sources. 84 | 85 | B2C = etc/bin2c 86 | 87 | $(B2C) : etc/bin2c.c 88 | $(CC) -o $(B2C) etc/bin2c.c 89 | 90 | #------------------------------------------------------------------------------ 91 | 92 | %.o : %.cpp 93 | $(CXX) $(CFLAGS) $(CONF) -c $< -o $@ 94 | 95 | %.o : %.c 96 | $(CC) $(CFLAGS) $(CONF) -c $< -o $@ 97 | 98 | %.h : %.glsl $(B2C) 99 | $(B2C) $(basename $@) < $^ > $@ 100 | 101 | #------------------------------------------------------------------------------ 102 | 103 | scm-render.o : $(RENDER_GLSL) 104 | scm-label.o : $(LABEL_GLSL) 105 | 106 | ifneq ($(MAKECMDGOALS),clean) 107 | -include $(DEPS) 108 | endif 109 | -------------------------------------------------------------------------------- /Makefile.vc: -------------------------------------------------------------------------------- 1 | # scm.lib -- Visual Studio Makefile 2 | 3 | LOCAL_INCLUDE = C:\Include 4 | 5 | #------------------------------------------------------------------------------ 6 | 7 | CPPFLAGS = /nologo /W3 /EHsc \ 8 | /DGLEW_STATIC \ 9 | /D_CRT_SECURE_NO_WARNINGS \ 10 | /D_CRT_NONSTDC_NO_DEPRECATE \ 11 | /D_USE_MATH_DEFINES \ 12 | /DHAVE_M_PI \ 13 | /DNOMINMAX \ 14 | /DWIN32 15 | 16 | LINK = lib /nologo 17 | TARGET = scm.lib 18 | TARGDIR = $(CONFIG) 19 | 20 | !ifdef DEBUG 21 | CONFIG = Debug 22 | CPPFLAGS = $(CPPFLAGS) /Od /MTd /Z7 23 | !else 24 | CONFIG = Release 25 | CPPFLAGS = $(CPPFLAGS) /Ox /MT 26 | !endif 27 | 28 | #------------------------------------------------------------------------------ 29 | 30 | OBJS = \ 31 | scm-cache.obj \ 32 | scm-deque.obj \ 33 | scm-file.obj \ 34 | scm-frame.obj \ 35 | scm-image.obj \ 36 | scm-index.obj \ 37 | scm-label.obj \ 38 | scm-log.obj \ 39 | scm-path.obj \ 40 | scm-render.obj \ 41 | scm-sample.obj \ 42 | scm-scene.obj \ 43 | scm-set.obj \ 44 | scm-sphere.obj \ 45 | scm-state.obj \ 46 | scm-system.obj \ 47 | scm-task.obj \ 48 | glsl.obj \ 49 | type.obj \ 50 | math3d.obj 51 | 52 | LABEL_GLSL = \ 53 | scm_label_circle_frag.h \ 54 | scm_label_circle_vert.h \ 55 | scm_label_sprite_frag.h \ 56 | scm_label_sprite_vert.h 57 | 58 | RENDER_GLSL = \ 59 | scm_render_blur_frag.h \ 60 | scm_render_blur_vert.h \ 61 | scm_render_both_frag.h \ 62 | scm_render_both_vert.h \ 63 | scm_render_atmo_frag.h \ 64 | scm_render_atmo_vert.h \ 65 | scm_render_fade_frag.h \ 66 | scm_render_fade_vert.h 67 | 68 | GLSL = $(LABEL_GLSL) $(RENDER_GLSL) 69 | 70 | CPPFLAGS = $(CPPFLAGS) \ 71 | /I$(LOCAL_INCLUDE)\freetype2 \ 72 | /I$(LOCAL_INCLUDE)\SDL2 \ 73 | /I$(LOCAL_INCLUDE) 74 | 75 | $(TARGDIR)\$(TARGET) : $(TARGDIR) $(OBJS) 76 | $(LINK) /out:$(TARGDIR)\$(TARGET) $(OBJS) 77 | 78 | $(TARGDIR) : 79 | mkdir $(TARGDIR) 80 | 81 | #------------------------------------------------------------------------------ 82 | 83 | # Compile the bin2c tool, which helps embed binary data in C headers. 84 | 85 | B2C = etc\bin2c.exe 86 | 87 | $(B2C) : etc\bin2c.c 88 | $(CC) /nologo /Fe$(B2C) etc\bin2c.c 89 | 90 | #------------------------------------------------------------------------------ 91 | 92 | clean: 93 | -del $(TARGET) $(GLSL) $(OBJS) $(B2C) 94 | 95 | #------------------------------------------------------------------------------ 96 | 97 | # Embed GLSL sources in C headers. (An inference rule would help here.) 98 | 99 | scm_label_circle_frag.h : scm_label_circle_frag.glsl 100 | $(B2C) scm_label_circle_frag < $? > $@ 101 | scm_label_circle_vert.h : scm_label_circle_vert.glsl 102 | $(B2C) scm_label_circle_vert < $? > $@ 103 | scm_label_sprite_frag.h : scm_label_sprite_frag.glsl 104 | $(B2C) scm_label_sprite_frag < $? > $@ 105 | scm_label_sprite_vert.h : scm_label_sprite_vert.glsl 106 | $(B2C) scm_label_sprite_vert < $? > $@ 107 | 108 | scm_render_atmo_frag.h : scm_render_atmo_frag.glsl 109 | $(B2C) scm_render_atmo_frag < $? > $@ 110 | scm_render_atmo_vert.h : scm_render_atmo_vert.glsl 111 | $(B2C) scm_render_atmo_vert < $? > $@ 112 | scm_render_blur_frag.h : scm_render_blur_frag.glsl 113 | $(B2C) scm_render_blur_frag < $? > $@ 114 | scm_render_blur_vert.h : scm_render_blur_vert.glsl 115 | $(B2C) scm_render_blur_vert < $? > $@ 116 | scm_render_both_frag.h : scm_render_both_frag.glsl 117 | $(B2C) scm_render_both_frag < $? > $@ 118 | scm_render_both_vert.h : scm_render_both_vert.glsl 119 | $(B2C) scm_render_both_vert < $? > $@ 120 | scm_render_fade_frag.h : scm_render_fade_frag.glsl 121 | $(B2C) scm_render_fade_frag < $? > $@ 122 | scm_render_fade_vert.h : scm_render_fade_vert.glsl 123 | $(B2C) scm_render_fade_vert < $? > $@ 124 | 125 | scm-render.obj : $(B2C) $(RENDER_GLSL) 126 | scm-label.obj : $(B2C) $(LABEL_GLSL) 127 | 128 | #------------------------------------------------------------------------------ 129 | 130 | # An inference rule to compile C source in the util3d directory. 131 | 132 | {util3d\}.c{}.obj :: 133 | $(CC) $(CPPFLAGS) /c $< 134 | 135 | math3d.obj : util3d\math3d.c 136 | glsl.obj : util3d\glsl.c 137 | type.obj : util3d\type.c 138 | 139 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Spherical Cube Map Rendering 2 | 3 | Copyright © 2011-2014 [Robert Kooima](http://kooima.net) 4 | 5 | SCM is a C++ class library that implements a rendering engine and non-homogeneous data representation for the interactive display of spherical data sets at scales of hundreds of gigapixels and beyond. Applications include panoramic image display and planetary rendering. The SCM data representation enables out-of-core data access at real-time rates. The spherical geometry tessellator supports displacement mapping and enables the display of planetary terrain data of arbitrary resolution. 6 | 7 | ## Documentation 8 | 9 | - [Complete Documentation](http://rlk.github.io/scm/) 10 | 11 | Here are a few YouTube videos of this renderer in action: 12 | 13 | - [Lunar rendering with discussion](http://www.youtube.com/watch?v=OPJDxEkmjJo) 14 | - [Lunar rendering demonstration](http://www.youtube.com/watch?v=Km9_RMPdwR8) 15 | - [Stereoscopic panorama rendering with discussion](http://www.youtube.com/watch?v=5dTpLCXRCfA) 16 | - [Stereoscopic panoramic rendering in virtual reality](http://www.youtube.com/watch?v=0Gi2qZltdtc) 17 | 18 | ## Build 19 | 20 | This module has a submodule that must be initialized after a new clone: 21 | 22 | git submodule update --init 23 | 24 | ### Linux and OS X 25 | 26 | To build `Release/libscm.a` under Linux or OS X: 27 | 28 | make 29 | 30 | To build `Debug/libscm.a`: 31 | 32 | make DEBUG=1 33 | 34 | ### Windows 35 | 36 | To build `Release\scm.lib` under Windows, use the Visual Studio project or the included `Makefile.vc`: 37 | 38 | nmake /f Makefile.vc 39 | 40 | To build `Debug\scm.lib`: 41 | 42 | nmake /f Makefile.vc DEBUG=1 43 | 44 | ## Dependencies 45 | 46 | Dependencies for SCM include 47 | 48 | - Freetype2 49 | - SDL2 50 | 51 | The SCM repo has a submodule ([util3d](https://github.com/rlk/util3d)) that must be explicitly added to a fresh clone: 52 | 53 | git submodule update --init 54 | 55 | -------------------------------------------------------------------------------- /etc/bin2c.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #ifdef _WIN32 5 | #include 6 | #endif 7 | 8 | int main(int argc, char **argv) 9 | { 10 | int n = 0; 11 | int c; 12 | 13 | if (argc > 1) 14 | printf("unsigned char %s[] = {\n", argv[1]); 15 | 16 | #ifdef _WIN32 17 | _setmode(_fileno(stdin), _O_BINARY); 18 | #endif 19 | 20 | do 21 | { 22 | printf("\t"); 23 | 24 | while ((c = getchar()) != EOF) 25 | { 26 | printf("0x%02x, ", c); 27 | if (++n % 12 == 0) 28 | break; 29 | } 30 | 31 | printf("\n"); 32 | } 33 | while (c != EOF); 34 | 35 | if (argc > 1) 36 | printf("};\nunsigned int %s_len = %d;\n", argv[1], n); 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /etc/extra.css: -------------------------------------------------------------------------------- 1 | 2 | /* Open up the margins. */ 3 | 4 | div.contents { 5 | margin-left: 4em; 6 | margin-right: 4em; 7 | } 8 | 9 | /* Replace gradient images with flat colors. */ 10 | 11 | .tabs, .tabs2, .tabs3 { 12 | background-image: none; 13 | background-color: #E2E8F2; 14 | } 15 | 16 | .tablist li { 17 | background-image: none; 18 | background-color: #E2E8F2; 19 | } 20 | 21 | .tablist a { 22 | background-image: none; 23 | background-color: #E2E8F2; 24 | } 25 | 26 | .tablist li.current { 27 | background-image: none; 28 | } 29 | 30 | .tablist a:hover { 31 | background-image: none; 32 | background-color: #EBEFF6; 33 | } 34 | 35 | .memproto, dl.reflist dt { 36 | background-image: none; 37 | } 38 | 39 | .memdoc, dl.reflist dd { 40 | background-image: none; 41 | } 42 | -------------------------------------------------------------------------------- /etc/moon.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rlk/scm/bee8ae4f769b638bc9f2cfa82c3ef82b21445ea2/etc/moon.jpg -------------------------------------------------------------------------------- /etc/scm.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | scene 24 | 25 | image 26 | 27 | frame 28 | 29 | file 30 | 31 | cache 32 | 33 | step 34 | 35 | system 36 | 37 | render 38 | 39 | sphere 40 | 41 | label 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | -------------------------------------------------------------------------------- /scm-cache.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2011-2012 Robert Kooima 2 | // 3 | // LIBSCM is free software; you can redistribute it and/or modify it under the 4 | // terms of the GNU General Public License as published by the Free Software 5 | // Foundation; either version 2 of the License, or (at your option) any later 6 | // version. 7 | // 8 | // This program is distributed in the hope that it will be useful, but WITH- 9 | // OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11 | // more details. 12 | 13 | #include 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | #include "scm-cache.hpp" 20 | #include "scm-system.hpp" 21 | #include "scm-index.hpp" 22 | #include "scm-log.hpp" 23 | 24 | //------------------------------------------------------------------------------ 25 | 26 | /// The cache grid size. The texture atlas will have size N x N where 27 | /// N = grid_size * page_size. Too large a value may exceed the maximum texture 28 | /// size or available VRAM, resulting in peer performance. Too small a value may 29 | /// result in poor image quality and unnecessary data traffic. 30 | 31 | int scm_cache::cache_size = 16; 32 | 33 | /// The number of loader threads servicing page load requests for each cache. 34 | 35 | int scm_cache::cache_threads = 2; 36 | 37 | /// The maximum number of page load requests allowed at any moment. (Requests 38 | /// from the render thread to the loader threads.) If this limit is exceeded 39 | /// the render thread will abandon the request and repeat it later. 40 | 41 | int scm_cache::need_queue_size = 32; 42 | 43 | /// The maximum number of page load results allowed at any moment. (Results 44 | /// from the loader threads to the render thread.) If this limit is exceeded 45 | /// the loader thread will block on load queue. 46 | 47 | int scm_cache::load_queue_size = 8; 48 | 49 | /// The maximum number of page load results that may be uploaded to the atlas 50 | /// by the render thread each frame. A large value may impact frame rate. A 51 | /// small value may increase frame latency and/or block the loader threads. 52 | 53 | int scm_cache::loads_per_cycle = 2; 54 | 55 | //------------------------------------------------------------------------------ 56 | 57 | /// Create a new page cache with a queue for making page requests 58 | /// 59 | /// Initialize all OpenGL state including the texture atlas and a ring of 60 | /// pixel buffer objects for use in asynchronous upload of page data. 61 | /// 62 | /// @param sys SCM system 63 | /// @param n Page size in pixels 64 | /// @param c Channels per pixel 65 | /// @param b Bits per channel 66 | 67 | scm_cache::scm_cache(scm_system *sys, int n, int c, int b) : 68 | sys(sys), 69 | pages(), 70 | waits(), 71 | loads(load_queue_size), 72 | texture(0), 73 | s(cache_size), 74 | l(1), 75 | n(n), 76 | c(c), 77 | b(b) 78 | { 79 | // Generate pixel buffer objects. 80 | 81 | for (int i = 0; i < 2 * need_queue_size; ++i) 82 | { 83 | GLuint o; 84 | glGenBuffers(1, &o); 85 | pbos.push_back(o); 86 | } 87 | 88 | // Generate the array texture object. 89 | 90 | GLenum i = scm_internal_form(c, b); 91 | GLenum e = scm_external_form(c, b); 92 | GLenum y = scm_external_type(c, b); 93 | 94 | glGenTextures (1, &texture); 95 | glBindTexture (GL_TEXTURE_2D, texture); 96 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 97 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 98 | 99 | // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 100 | // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16); 101 | // glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); 102 | 103 | // Initialize it with a buffer of zeros. 104 | 105 | const int m = s * (n + 2); 106 | 107 | if (GLubyte *p = (GLubyte *) calloc(m * m, scm_pixel_size(c, b))) 108 | { 109 | glTexImage2D(GL_TEXTURE_2D, 0, i, m, m, 0, e, y, p); 110 | free(p); 111 | } 112 | 113 | scm_log("scm_cache constructor %d %d %d", n, c, b); 114 | } 115 | 116 | /// Destroy a page cache and finalize all OpenGL state 117 | 118 | scm_cache::~scm_cache() 119 | { 120 | scm_log("scm_cache destructor %d %d %d", n, c, b); 121 | 122 | // Drain any completed loads to ensure that the loaders aren't blocked. 123 | 124 | update(0, true); 125 | 126 | // Release the pixel buffer objects. 127 | 128 | while (!pbos.empty()) 129 | { 130 | glDeleteBuffers(1, &pbos.back()); 131 | pbos.pop_back(); 132 | } 133 | 134 | // Release the texture. 135 | 136 | glDeleteTextures(1, &texture); 137 | } 138 | 139 | /// Add a page request to the load queue 140 | 141 | void scm_cache::add_load(scm_task& task) 142 | { 143 | loads.insert(task); 144 | } 145 | 146 | //------------------------------------------------------------------------------ 147 | 148 | /// Return the OpenGL texture object representing the cache 149 | 150 | GLuint scm_cache::get_texture() const 151 | { 152 | return texture; 153 | } 154 | 155 | /// Return the cache line of a loaded page 156 | /// 157 | /// Cache lines are indexed from left to right and top to bottom. Request the 158 | /// page if necessary. Return 0 if the page is not available (line 0 is always 159 | /// transparent blank and will thus appear invisible). 160 | /// 161 | /// @param f File index 162 | /// @param i Page index 163 | /// @param t Current time 164 | /// @param u Time at which the page was loaded. 165 | 166 | int scm_cache::get_page(int f, long long i, int t, int& u) 167 | { 168 | if (scm_file *file = sys->get_file(f)) 169 | { 170 | // If this page does not exist, return the filler. 171 | 172 | uint64 o = file->get_page_offset(i); 173 | 174 | if (o == 0) 175 | return 0; 176 | 177 | // If this page is waiting, return the filler. 178 | 179 | scm_page wait = waits.search(scm_page(f, i), t); 180 | 181 | if (wait.is_valid()) 182 | { 183 | u = wait.t; 184 | return wait.l; 185 | } 186 | 187 | // If this page is loaded, return the index. 188 | 189 | scm_page page = pages.search(scm_page(f, i), t); 190 | 191 | if (page.is_valid()) 192 | { 193 | u = page.t; 194 | return page.l; 195 | } 196 | 197 | // Otherwise request the page and add it to the waiting set. 198 | 199 | if (!pbos.empty()) 200 | { 201 | scm_task task(f, i, o, n, c, b, pbos.deq(), this); 202 | scm_page page(f, i, 0); 203 | 204 | if (file->add_need(task)) 205 | { 206 | waits.insert(page, t); 207 | } 208 | else 209 | { 210 | task.dump_page(); 211 | pbos.enq(task.u); 212 | } 213 | } 214 | } 215 | 216 | // If all else fails, punt and let the app request again. 217 | 218 | u = t; 219 | return 0; 220 | } 221 | 222 | /// Find a slot for an incoming page 223 | /// 224 | /// Either take the next unused slot or eject a page to make room. Return 0 225 | // on failure. @see scm_set::eject 226 | /// 227 | /// @param t Current time 228 | /// @param i Page index 229 | 230 | int scm_cache::get_slot(int t, long long i) 231 | { 232 | if (l < s * s) 233 | return l++; 234 | else 235 | { 236 | scm_page victim = pages.eject(t, i); 237 | 238 | if (victim.is_valid()) 239 | return victim.l; 240 | else 241 | return 0; 242 | } 243 | } 244 | 245 | //------------------------------------------------------------------------------ 246 | 247 | /// Handle incoming textures on the loads queue, copying them to the atlas. 248 | /// 249 | /// This should be called by the render thread every frame. If invoked with 250 | /// the synchronous flag, loop until all page requests in the load queue are 251 | /// handled. 252 | /// 253 | /// @param t Current time 254 | /// @param b Synchronous? 255 | 256 | void scm_cache::update(int t, bool b) 257 | { 258 | int c; 259 | 260 | scm_task task; 261 | 262 | glBindTexture(GL_TEXTURE_2D, texture); 263 | 264 | for (c = 0; (b || c < loads_per_cycle) && loads.try_remove(task); ++c) 265 | { 266 | if (task.d) 267 | { 268 | scm_page page(task.f, task.i); 269 | 270 | waits.remove(page); 271 | 272 | if (int l = get_slot(t, page.i)) 273 | { 274 | page.l = l; 275 | page.t = t; 276 | pages.insert(page, t); 277 | task.make_page((l % s) * (n + 2), 278 | (l / s) * (n + 2)); 279 | } 280 | else task.dump_page(); 281 | } 282 | else task.dump_page(); 283 | 284 | pbos.enq(task.u); 285 | } 286 | } 287 | 288 | /// Render a 2D overlay of the contents of all caches. 289 | /// 290 | /// The parameters are used to format an optimal on-screen array of caches. 291 | /// 292 | /// @param ii Cache index 293 | /// @param nn Cache count 294 | 295 | void scm_cache::render(int ii, int nn) 296 | { 297 | glPushAttrib(GL_ENABLE_BIT); 298 | { 299 | GLint v[4]; 300 | 301 | glGetIntegerv(GL_VIEWPORT, v); 302 | 303 | const GLdouble a = GLdouble(v[3]) / GLdouble(v[2]); 304 | 305 | glDisable(GL_LIGHTING); 306 | glDisable(GL_DEPTH_TEST); 307 | glEnable(GL_TEXTURE_2D); 308 | glEnable(GL_BLEND); 309 | 310 | glMatrixMode(GL_PROJECTION); 311 | glPushMatrix(); 312 | glLoadIdentity(); 313 | glOrtho(0, nn, 0, nn * a, -1, +1); 314 | 315 | glMatrixMode(GL_MODELVIEW); 316 | glPushMatrix(); 317 | glLoadIdentity(); 318 | 319 | glUseProgram(0); 320 | glBindTexture(GL_TEXTURE_2D, texture); 321 | glColor4f(1.f, 1.f, 1.f, 1.f); 322 | 323 | glBegin(GL_QUADS); 324 | { 325 | GLfloat a = 0.05f; 326 | GLfloat b = 0.95f; 327 | 328 | glTexCoord2i(0, 1); glVertex2f(ii + a, a); 329 | glTexCoord2i(1, 1); glVertex2f(ii + b, a); 330 | glTexCoord2i(1, 0); glVertex2f(ii + b, b); 331 | glTexCoord2i(0, 0); glVertex2f(ii + a, b); 332 | } 333 | glEnd(); 334 | 335 | glMatrixMode(GL_PROJECTION); 336 | glPopMatrix(); 337 | glMatrixMode(GL_MODELVIEW); 338 | glPopMatrix(); 339 | } 340 | glPopAttrib(); 341 | } 342 | 343 | /// Eject all pages 344 | /// 345 | /// All page requests in the load queue remain, so a flush is unlikely to 346 | /// be 100% effective unless performed directly after a synchronous update. 347 | 348 | void scm_cache::flush() 349 | { 350 | while (!pages.empty()) 351 | pages.eject(0, -1); 352 | 353 | l = 1; 354 | } 355 | 356 | //------------------------------------------------------------------------------ 357 | -------------------------------------------------------------------------------- /scm-cache.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2011-2012 Robert Kooima 2 | // 3 | // LIBSCM is free software; you can redistribute it and/or modify it under the 4 | // terms of the GNU General Public License as published by the Free Software 5 | // Foundation; either version 2 of the License, or (at your option) any later 6 | // version. 7 | // 8 | // This program is distributed in the hope that it will be useful, but WITH- 9 | // OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11 | // more details. 12 | 13 | #ifndef SCM_CACHE_HPP 14 | #define SCM_CACHE_HPP 15 | 16 | #include 17 | #include 18 | 19 | #include 20 | 21 | #include "scm-queue.hpp" 22 | #include "scm-fifo.hpp" 23 | #include "scm-task.hpp" 24 | #include "scm-set.hpp" 25 | 26 | //------------------------------------------------------------------------------ 27 | 28 | class scm_system; 29 | 30 | //------------------------------------------------------------------------------ 31 | 32 | /// An scm_cache is a virtual texture, demand-paged with threaded data access, 33 | /// represented as a single large OpenGL texture atlas. 34 | 35 | class scm_cache 36 | { 37 | public: 38 | 39 | static int cache_size; 40 | static int cache_threads; 41 | static int need_queue_size; 42 | static int load_queue_size; 43 | static int loads_per_cycle; 44 | 45 | scm_cache(scm_system *, int, int, int); 46 | ~scm_cache(); 47 | 48 | void add_load(scm_task&); 49 | 50 | int get_grid_size() const { return s; } 51 | int get_page_size() const { return n; } 52 | 53 | GLuint get_texture() const; 54 | int get_page(int, long long, int, int&); 55 | 56 | void update(int, bool); 57 | void render(int, int); 58 | void flush (); 59 | 60 | private: 61 | 62 | scm_system *sys; 63 | scm_set pages; // Page set currently active 64 | scm_set waits; // Page set currently being loaded 65 | scm_queue loads; // Page loader queue 66 | scm_fifo pbos; // Asynchronous upload ring 67 | 68 | GLuint texture; // Atlas texture object 69 | int s; // Atlas width and height in pages 70 | int l; // Atlas current page 71 | int n; // Page width and height in pixels 72 | int c; // Channels per pixel 73 | int b; // Bits per channel 74 | 75 | int get_slot(int, long long); 76 | }; 77 | 78 | typedef std::vector scm_cache_v; 79 | typedef std::vector::iterator scm_cache_i; 80 | 81 | //------------------------------------------------------------------------------ 82 | 83 | #endif 84 | -------------------------------------------------------------------------------- /scm-deque.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2011-2016 Robert Kooima 2 | // 3 | // LIBSCM is free software; you can redistribute it and/or modify it under the 4 | // terms of the GNU General Public License as published by the Free Software 5 | // Foundation; either version 2 of the License, or (at your option) any later 6 | // version. 7 | // 8 | // This program is distributed in the hope that it will be useful, but WITH- 9 | // OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11 | // more details. 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | #include "util3d/math3d.h" 18 | 19 | #include "scm-deque.hpp" 20 | 21 | //------------------------------------------------------------------------------ 22 | 23 | /// Create a new, empty state deque. 24 | 25 | scm_deque::scm_deque() 26 | { 27 | } 28 | 29 | //------------------------------------------------------------------------------ 30 | 31 | -------------------------------------------------------------------------------- /scm-deque.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2011-2016 Robert Kooima 2 | // 3 | // LIBSCM is free software; you can redistribute it and/or modify it under the 4 | // terms of the GNU General Public License as published by the Free Software 5 | // Foundation; either version 2 of the License, or (at your option) any later 6 | // version. 7 | // 8 | // This program is distributed in the hope that it will be useful, but WITH- 9 | // OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11 | // more details. 12 | 13 | #ifndef SCM_DEQUE_HPP 14 | #define SCM_DEQUE_HPP 15 | 16 | #include 17 | #include 18 | 19 | #include "scm-state.hpp" 20 | 21 | //------------------------------------------------------------------------------ 22 | 23 | /// An scm_deque defines a sequence of scm_state objects 24 | 25 | class scm_deque 26 | { 27 | public: 28 | 29 | /// @name Constructors 30 | /// @{ 31 | 32 | scm_deque(); 33 | 34 | /// @} 35 | /// @name Basic accessors 36 | /// @{ 37 | 38 | scm_state& front() { return sequence.front(); } 39 | scm_state& back() { return sequence.back(); } 40 | const scm_state& front() const { return sequence.front(); } 41 | const scm_state& back() const { return sequence.back(); } 42 | bool empty() const { return sequence.empty(); } 43 | 44 | /// @} 45 | /// @name Basic mutators 46 | /// @{ 47 | 48 | void push_front(const scm_state& s) { sequence.push_front(s); } 49 | void push_back (const scm_state& s) { sequence.push_back (s); } 50 | 51 | void pop_front() { sequence.pop_front(); } 52 | void pop_back () { sequence.pop_back (); } 53 | 54 | void clear() { sequence.clear(); } 55 | 56 | private: 57 | 58 | std::list sequence; ///< Sequence of states 59 | }; 60 | 61 | //------------------------------------------------------------------------------ 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /scm-fifo.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2011-2012 Robert Kooima 2 | // 3 | // LIBSCM is free software; you can redistribute it and/or modify it under the 4 | // terms of the GNU General Public License as published by the Free Software 5 | // Foundation; either version 2 of the License, or (at your option) any later 6 | // version. 7 | // 8 | // This program is distributed in the hope that it will be useful, but WITH- 9 | // OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11 | // more details. 12 | 13 | #ifndef SCM_FIFO_HPP 14 | #define SCM_FIFO_HPP 15 | 16 | #include 17 | 18 | //------------------------------------------------------------------------------ 19 | 20 | /// An scm_fifo implements a simple first-in-first-out templated queue. 21 | 22 | template class scm_fifo : public std::list 23 | { 24 | public: 25 | 26 | /// Enqueue a value. 27 | 28 | void enq(T p) { 29 | this->push_back(p); 30 | } 31 | 32 | /// Dequeue and return a value. 33 | 34 | T deq() { 35 | T p = this->front(); 36 | this->pop_front(); 37 | return p; 38 | } 39 | }; 40 | 41 | //------------------------------------------------------------------------------ 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /scm-file.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2011-2012 Robert Kooima 2 | // 3 | // LIBSCM is free software; you can redistribute it and/or modify it under the 4 | // terms of the GNU General Public License as published by the Free Software 5 | // Foundation; either version 2 of the License, or (at your option) any later 6 | // version. 7 | // 8 | // This program is distributed in the hope that it will be useful, but WITH- 9 | // OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11 | // more details. 12 | 13 | #ifndef SCM_FILE_HPP 14 | #define SCM_FILE_HPP 15 | 16 | #include 17 | #include 18 | 19 | #include 20 | #include 21 | #include 22 | 23 | #include "scm-queue.hpp" 24 | #include "scm-guard.hpp" 25 | #include "scm-task.hpp" 26 | #include "scm-sample.hpp" 27 | 28 | //------------------------------------------------------------------------------ 29 | 30 | typedef std::vector thread_v; 31 | typedef std::vector::iterator thread_i; 32 | 33 | //------------------------------------------------------------------------------ 34 | 35 | /// An scm_file encapsulates an open SCM data file. 36 | 37 | class scm_file 38 | { 39 | public: 40 | 41 | scm_file(const std::string&, 42 | const std::string&); 43 | 44 | virtual ~scm_file(); 45 | 46 | void activate(scm_cache *); 47 | void deactivate(); 48 | bool is_active() const; 49 | 50 | bool add_need(scm_task&); 51 | 52 | virtual bool get_page_status(uint64) const; 53 | virtual uint64 get_page_offset(uint64) const; 54 | virtual void get_page_bounds(uint64, float&, float&) const; 55 | virtual float get_page_sample(const double *); 56 | 57 | virtual uint32 get_w() const { return w; } 58 | virtual uint32 get_h() const { return h; } 59 | virtual uint16 get_c() const { return c; } 60 | virtual uint16 get_b() const { return b; } 61 | 62 | const char *get_path() const { return path.c_str(); } 63 | const char *get_name() const { return name.c_str(); } 64 | 65 | uint64 find_page(long long, double&, double&) const; 66 | 67 | protected: 68 | 69 | std::string path; 70 | std::string name; 71 | 72 | private: 73 | 74 | // IO handling and threading data 75 | 76 | scm_cache *cache; 77 | scm_queue needs; 78 | scm_guard active; 79 | scm_sample *sampler; 80 | thread_v threads; 81 | 82 | // Image parameters 83 | 84 | uint32 w; ///< Page width 85 | uint32 h; ///< Page height 86 | uint16 c; ///< Sample count 87 | uint16 b; ///< Sample depth 88 | 89 | uint64 *xv; ///< Page indices 90 | uint64 xc; ///< Page indices count 91 | 92 | uint64 *ov; ///< Page offsets 93 | uint64 oc; ///< Page offsets count 94 | 95 | void *av; ///< Page minima 96 | uint64 ac; ///< Page minima count 97 | 98 | void *zv; ///< Page maxima 99 | uint64 zc; ///< Page maxima count 100 | 101 | float tofloat(const void *, uint64) const; 102 | void fromfloat(const void *, uint64, float) const; 103 | 104 | uint64 toindex(uint64) const; 105 | 106 | friend int loader(void *); 107 | }; 108 | 109 | //------------------------------------------------------------------------------ 110 | /// @file 111 | 112 | bool scm_load_page(const char *, long long, 113 | TIFF *, uint64, int, int, int, int, void *); 114 | 115 | //------------------------------------------------------------------------------ 116 | 117 | #endif 118 | -------------------------------------------------------------------------------- /scm-frame.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2011-2012 Robert Kooima 2 | // 3 | // LIBSCM is free software; you can redistribute it and/or modify it under the 4 | // terms of the GNU General Public License as published by the Free Software 5 | // Foundation; either version 2 of the License, or (at your option) any later 6 | // version. 7 | // 8 | // This program is distributed in the hope that it will be useful, but WITH- 9 | // OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11 | // more details. 12 | 13 | #include "scm-frame.hpp" 14 | #include "scm-log.hpp" 15 | 16 | //------------------------------------------------------------------------------ 17 | 18 | /// Create a new OpenGL framebuffer object with color and depth textures 19 | /// 20 | /// @param w width in pixels 21 | /// @param h height in pixels 22 | 23 | scm_frame::scm_frame(GLsizei w, GLsizei h) : width(w), height(h) 24 | { 25 | glGenFramebuffers(1, &frame); 26 | glGenTextures (1, &color); 27 | glGenTextures (1, &depth); 28 | 29 | init_color(); 30 | init_depth(); 31 | init_frame(); 32 | } 33 | 34 | /// Delete the framebuffer object and its textures 35 | 36 | scm_frame::~scm_frame() 37 | { 38 | if (color) glDeleteTextures (1, &color); 39 | if (depth) glDeleteTextures (1, &depth); 40 | if (frame) glDeleteFramebuffers(1, &frame); 41 | } 42 | 43 | /// Bind the framebuffer as render target and set the Viewport and Scissor 44 | 45 | void scm_frame::bind_frame() const 46 | { 47 | glBindFramebuffer(GL_FRAMEBUFFER, frame); 48 | glViewport(0, 0, width, height); 49 | glScissor (0, 0, width, height); 50 | } 51 | 52 | /// Bind the color texture 53 | 54 | void scm_frame::bind_color() const 55 | { 56 | glBindTexture(GL_TEXTURE_RECTANGLE, color); 57 | } 58 | 59 | /// Bind the depth texture 60 | 61 | void scm_frame::bind_depth() const 62 | { 63 | glBindTexture(GL_TEXTURE_RECTANGLE, depth); 64 | } 65 | 66 | //------------------------------------------------------------------------------ 67 | 68 | /// Initialize the storage and parameters of a color buffer. 69 | 70 | void scm_frame::init_color() 71 | { 72 | glBindTexture(GL_TEXTURE_RECTANGLE, color); 73 | 74 | glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA, width, height, 75 | 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); 76 | 77 | glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 78 | glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 79 | glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 80 | glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 81 | } 82 | 83 | /// Initialize the storage and parameters of a depth buffer. 84 | 85 | void scm_frame::init_depth() 86 | { 87 | glBindTexture(GL_TEXTURE_RECTANGLE, depth); 88 | 89 | glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_DEPTH_COMPONENT24, width, height, 90 | 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0); 91 | 92 | glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 93 | glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 94 | glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 95 | glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 96 | } 97 | 98 | /// Generate and initialize a framebuffer object with color and depth buffers 99 | /// and confirm its status. 100 | 101 | void scm_frame::init_frame() 102 | { 103 | GLint previous; 104 | 105 | glGetIntegerv(GL_FRAMEBUFFER_BINDING, &previous); 106 | 107 | glBindTexture(GL_TEXTURE_RECTANGLE, 0); 108 | 109 | glBindFramebuffer(GL_FRAMEBUFFER, frame); 110 | { 111 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, 112 | GL_TEXTURE_RECTANGLE, color, 0); 113 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, 114 | GL_TEXTURE_RECTANGLE, depth, 0); 115 | 116 | if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) 117 | scm_log("* init_framebuffer incomplete"); 118 | } 119 | glBindFramebuffer(GL_FRAMEBUFFER, previous); 120 | } 121 | 122 | //------------------------------------------------------------------------------ 123 | -------------------------------------------------------------------------------- /scm-frame.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2011-2013 Robert Kooima 2 | // 3 | // LIBSCM is free software; you can redistribute it and/or modify it under the 4 | // terms of the GNU General Public License as published by the Free Software 5 | // Foundation; either version 2 of the License, or (at your option) any later 6 | // version. 7 | // 8 | // This program is distributed in the hope that it will be useful, but WITH- 9 | // OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11 | // more details. 12 | 13 | #ifndef SCM_FRAME_HPP 14 | #define SCM_FRAME_HPP 15 | 16 | #include 17 | 18 | //------------------------------------------------------------------------------ 19 | 20 | /// An scm_frame abstracts the OpenGL framebuffer object. 21 | 22 | class scm_frame 23 | { 24 | public: 25 | 26 | scm_frame(GLsizei, GLsizei); 27 | ~scm_frame(); 28 | 29 | void bind_frame() const; 30 | void bind_color() const; 31 | void bind_depth() const; 32 | 33 | private: 34 | 35 | GLuint frame; 36 | GLuint color; 37 | GLuint depth; 38 | GLsizei width; 39 | GLsizei height; 40 | 41 | void init_color(); 42 | void init_depth(); 43 | void init_frame(); 44 | }; 45 | 46 | //------------------------------------------------------------------------------ 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /scm-guard.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2011-2012 Robert Kooima 2 | // 3 | // LIBSCM is free software; you can redistribute it and/or modify it under the 4 | // terms of the GNU General Public License as published by the Free Software 5 | // Foundation; either version 2 of the License, or (at your option) any later 6 | // version. 7 | // 8 | // This program is distributed in the hope that it will be useful, but WITH- 9 | // OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11 | // more details. 12 | 13 | #ifndef SCM_GUARD_HPP 14 | #define SCM_GUARD_HPP 15 | 16 | #include 17 | #include 18 | 19 | //------------------------------------------------------------------------------ 20 | 21 | /// The scm_guard template enforces mutual exclusion on a single value of its 22 | /// templated type. 23 | 24 | template class scm_guard 25 | { 26 | SDL_mutex *data_mutex; 27 | T data; 28 | 29 | public: 30 | 31 | /// Create the mutex for this guard 32 | 33 | scm_guard(T d) : data(d) 34 | { 35 | data_mutex = SDL_CreateMutex(); 36 | } 37 | 38 | /// Destroy this guard's mutex 39 | 40 | ~scm_guard() 41 | { 42 | SDL_DestroyMutex(data_mutex); 43 | } 44 | 45 | /// Atomically set the guarded value. 46 | 47 | void set(T d) 48 | { 49 | SDL_mutexP(data_mutex); 50 | data = d; 51 | SDL_mutexV(data_mutex); 52 | } 53 | 54 | /// Atomically get the guarded value. 55 | 56 | T get() const 57 | { 58 | T d; 59 | SDL_mutexP(data_mutex); 60 | d = data; 61 | SDL_mutexV(data_mutex); 62 | return d; 63 | } 64 | }; 65 | 66 | //------------------------------------------------------------------------------ 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /scm-image.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2011-2012 Robert Kooima 2 | // 3 | // LIBSCM is free software; you can redistribute it and/or modify it under the 4 | // terms of the GNU General Public License as published by the Free Software 5 | // Foundation; either version 2 of the License, or (at your option) any later 6 | // version. 7 | // 8 | // This program is distributed in the hope that it will be useful, but WITH- 9 | // OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11 | // more details. 12 | 13 | #include 14 | 15 | #include "util3d/glsl.h" 16 | 17 | #include "scm-system.hpp" 18 | #include "scm-cache.hpp" 19 | #include "scm-image.hpp" 20 | #include "scm-index.hpp" 21 | #include "scm-log.hpp" 22 | 23 | //------------------------------------------------------------------------------ 24 | 25 | /// Initialize a new empty image for use in the given SCM system. 26 | 27 | scm_image::scm_image(scm_system *sys) : 28 | sys(sys), 29 | channel(-1), 30 | height(false), 31 | k0 ( 0), 32 | k1 ( 1), 33 | uS (-1), 34 | ur (-1), 35 | uk0(-1), 36 | uk1(-1), 37 | index(-1) 38 | { 39 | } 40 | 41 | /// Finalize this image's SCM file. 42 | 43 | scm_image::~scm_image() 44 | { 45 | set_scm(""); 46 | } 47 | 48 | //------------------------------------------------------------------------------ 49 | 50 | /// Configure this image to read data from the named SCM file. 51 | /// 52 | /// This method can have ripple effects throughout the SCM system... 53 | /// 54 | /// 1. If this image was previously configured to read from a different SCM, 55 | /// then that SCM is released. This might trigger the destruction of an scm_file 56 | /// if its reference count goes to zero, and might also trigger the destruction 57 | /// of an scm_cache if the file count of that cache goes to zero. In the event 58 | /// of an scm_cache destruction, all of that cache's loader threads are asked 59 | /// to exit and waited upon. @see scm_system::release_scm 60 | /// 61 | /// 2. The nemed SCM is aquired. If it is not already open, this will trigger 62 | /// the construction of a new scm_file object, and possibly the construction of 63 | /// an scm_cache. In the event of an scm_cache construction, loader threads for 64 | /// that cache are launched. @see scm_system::acquire_scm 65 | /// 66 | /// So, while the scm_system makes every effort to minimize the effort of SCM 67 | /// data access, significant setup may be necessary, and it all starts here. 68 | 69 | void scm_image::set_scm(const std::string& s) 70 | { 71 | if (!scm.empty()) index = sys->release_scm(scm); 72 | scm = s; 73 | if (!scm.empty()) index = sys->acquire_scm(scm); 74 | 75 | cache = (index < 0) ? 0 : sys->get_cache(index); 76 | } 77 | 78 | /// Set the name by which GLSL sampler uniforms may access this image. 79 | /// 80 | /// The name "height" indicates that this image gives height map data, and that 81 | /// an subsequent ground-level queries may use this image. 82 | /// 83 | /// @see scm_scene::get_minimum_ground 84 | /// @see scm_scene::get_current_ground 85 | 86 | void scm_image::set_name(const std::string& s) 87 | { 88 | name = s; 89 | height = (name == "height"); 90 | } 91 | 92 | /// Set the channel index for this image. 93 | /// 94 | /// In this case "channel" indicates left-eye (0), right-eye (1), etc. rather 95 | /// than red (0), green (1), or similar. 96 | 97 | void scm_image::set_channel(int c) 98 | { 99 | channel = c; 100 | } 101 | 102 | /// Set the input value to be mapped onto 0 in the output. 103 | 104 | void scm_image::set_normal_min(float k) 105 | { 106 | k0 = k; 107 | } 108 | 109 | /// Set the input value to be mapped onto 1 in the output. 110 | 111 | void scm_image::set_normal_max(float k) 112 | { 113 | k1 = k; 114 | } 115 | 116 | //------------------------------------------------------------------------------ 117 | 118 | /// Request and store GLSL uniform locations for this image's parameters. 119 | 120 | void scm_image::init_uniforms(GLuint program) 121 | { 122 | scm_log("scm_image init_uniforms %s %s %d", scm.c_str(), 123 | name.c_str(), program); 124 | 125 | if (program && !name.empty()) 126 | { 127 | uS = glsl_uniform(program, "%s_sampler", name.c_str()); 128 | ur = glsl_uniform(program, "%s.r", name.c_str()); 129 | uk0 = glsl_uniform(program, "%s.k0", name.c_str()); 130 | uk1 = glsl_uniform(program, "%s.k1", name.c_str()); 131 | 132 | for (int d = 0; d < 16; d++) 133 | { 134 | ua[d] = glsl_uniform(program, "%s.a[%d]", name.c_str(), d); 135 | ub[d] = glsl_uniform(program, "%s.b[%d]", name.c_str(), d); 136 | } 137 | } 138 | } 139 | 140 | /// Set all GLSL uniform values for this image and bind the cache's texture. 141 | 142 | void scm_image::bind(GLuint unit, GLuint program) const 143 | { 144 | glUniform1i(uS, unit); 145 | glUniform1f(uk0, k0); 146 | glUniform1f(uk1, k1); 147 | 148 | if (cache) 149 | { 150 | const GLfloat r = GLfloat(cache->get_page_size()) 151 | / GLfloat(cache->get_page_size() + 2) 152 | / GLfloat(cache->get_grid_size()); 153 | 154 | glUniform2f(ur, r, r); 155 | glActiveTexture(GL_TEXTURE0 + unit); 156 | glBindTexture(GL_TEXTURE_2D, cache->get_texture()); 157 | } 158 | } 159 | 160 | /// Unbind the cache's texture by binding the texture unit to zero. 161 | 162 | void scm_image::unbind(GLuint unit) const 163 | { 164 | glActiveTexture(GL_TEXTURE0 + unit); 165 | glBindTexture(GL_TEXTURE_2D, 0); 166 | } 167 | 168 | //------------------------------------------------------------------------------ 169 | 170 | /// Set the GLSL uniforms necessary to map a page of texture data. 171 | /// 172 | /// @param program GLSL program object 173 | /// @param d SCM page depth. 174 | /// @param t Current time 175 | /// @param i SCM page index. 176 | 177 | void scm_image::bind_page(GLuint program, int d, int t, long long i) const 178 | { 179 | if (cache) 180 | { 181 | // Get the page index and the time of its loading. 182 | 183 | int u, l = cache->get_page(index, i, t, u); 184 | 185 | // Compute the page age. 186 | 187 | double a = l ? 1.0 : 0.0; 188 | 189 | if (l && sys->get_synchronous()) 190 | { 191 | a = (t - u) / 60.0; 192 | a = std::min(a, 1.0); 193 | a = std::max(a, 0.0); 194 | } 195 | 196 | // Compute texture coordinate offsets and set the uniforms. 197 | 198 | const int s = cache->get_grid_size(); 199 | const int n = cache->get_page_size(); 200 | 201 | glUniform1f(ua[d], GLfloat(a)); 202 | glUniform2f(ub[d], GLfloat((l % s) * (n + 2) + 1) / (s * (n + 2)), 203 | GLfloat((l / s) * (n + 2) + 1) / (s * (n + 2))); 204 | } 205 | } 206 | 207 | /// Set the texture mapping uniforms to reference cache line zero (which is 208 | /// always blank). 209 | 210 | void scm_image::unbind_page(GLuint program, int d) const 211 | { 212 | glUniform1f(ua[d], 0.f); 213 | glUniform2f(ub[d], 0.f, 0.f); 214 | } 215 | 216 | /// Set the last-used time of a page. 217 | 218 | void scm_image::touch_page(int t, long long i) const 219 | { 220 | if (cache) 221 | { 222 | int ignored; 223 | cache->get_page(index, i, t, ignored); 224 | } 225 | } 226 | 227 | //------------------------------------------------------------------------------ 228 | 229 | /// Sample this image at the given location, returning a normalized result. 230 | /// @see scm_scene::get_page_sample 231 | 232 | float scm_image::get_page_sample(const double *v) const 233 | { 234 | if (index < 0) 235 | return k1; 236 | else 237 | return sys->get_page_sample(index, v) * (k1 - k0) + k0; 238 | } 239 | 240 | /// Determine the minimum and maximum values of one page, returning a 241 | /// normalized result. @see scm_scene::get_page_bounds 242 | 243 | void scm_image::get_page_bounds(long long i, float& r0, float& r1) const 244 | { 245 | if (index < 0) 246 | { 247 | r0 = k0; 248 | r1 = k1; 249 | } 250 | else 251 | { 252 | sys->get_page_bounds(index, i, r0, r1); 253 | 254 | r0 = k0 + (k1 - k0) * r0; 255 | r1 = k0 + (k1 - k0) * r1; 256 | } 257 | } 258 | 259 | /// Return true if a page is present in this image. 260 | /// @see scm_scene::get_page_status 261 | 262 | bool scm_image::get_page_status(long long i) const 263 | { 264 | if (index < 0) 265 | return false; 266 | else 267 | return sys->get_page_status(index, i); 268 | } 269 | 270 | //------------------------------------------------------------------------------ 271 | -------------------------------------------------------------------------------- /scm-image.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2011-2012 Robert Kooima 2 | // 3 | // LIBSCM is free software; you can redistribute it and/or modify it under the 4 | // terms of the GNU General Public License as published by the Free Software 5 | // Foundation; either version 2 of the License, or (at your option) any later 6 | // version. 7 | // 8 | // This program is distributed in the hope that it will be useful, but WITH- 9 | // OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11 | // more details. 12 | 13 | #ifndef SCM_IMAGE_HPP 14 | #define SCM_IMAGE_HPP 15 | 16 | #include 17 | 18 | #include 19 | #include 20 | 21 | //------------------------------------------------------------------------------ 22 | 23 | class scm_system; 24 | class scm_cache; 25 | 26 | //------------------------------------------------------------------------------ 27 | 28 | /// An scm_image represents an SCM data file in use by an scm_scene. 29 | /// 30 | /// This object is largely responsible for mapping SCM data onto OpenGL state, 31 | /// including OpenGL textures and GLSL uniforms. Notably, this includes those 32 | /// parameters mapping texture coordinates onto a scm_cache texture atlas. 33 | 34 | class scm_image 35 | { 36 | public: 37 | 38 | scm_image(scm_system *); 39 | ~scm_image(); 40 | 41 | /// @name Configuration modifiers 42 | /// @{ 43 | 44 | void set_scm (const std::string& s); 45 | void set_name (const std::string& s); 46 | void set_channel (int c); 47 | void set_normal_min(float k); 48 | void set_normal_max(float k); 49 | 50 | /// @} 51 | /// @name Configuration queries 52 | /// @{ 53 | 54 | const std::string& get_scm() const { return scm; } 55 | const std::string& get_name() const { return name; } 56 | int get_channel() const { return channel; } 57 | float get_normal_min() const { return k0; } 58 | float get_normal_max() const { return k1; } 59 | 60 | bool is_channel(int c) const { return (channel == c || channel == -1); } 61 | bool is_height() const { return (height); } 62 | 63 | /// @} 64 | /// @name Internal Interface 65 | /// @{ 66 | 67 | void init_uniforms(GLuint); 68 | 69 | void bind(GLuint, GLuint) const; 70 | void unbind(GLuint) const; 71 | 72 | void bind_page(GLuint, int, int, long long) const; 73 | void unbind_page(GLuint, int) const; 74 | void touch_page( int, long long) const; 75 | 76 | float get_page_sample(const double *) const; 77 | void get_page_bounds(long long, float &, float &) const; 78 | bool get_page_status(long long) const; 79 | 80 | /// @} 81 | 82 | private: 83 | 84 | scm_system *sys; 85 | std::string scm; 86 | std::string name; 87 | 88 | int channel; 89 | bool height; 90 | float k0; 91 | float k1; 92 | 93 | GLint uS; 94 | GLint ur; 95 | GLint uk0; 96 | GLint uk1; 97 | GLint ua[16]; 98 | GLint ub[16]; 99 | 100 | scm_cache *cache; 101 | int index; 102 | }; 103 | 104 | //------------------------------------------------------------------------------ 105 | 106 | #endif 107 | -------------------------------------------------------------------------------- /scm-index.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2011-2012 Robert Kooima 2 | // 3 | // LIBSCM is free software; you can redistribute it and/or modify it under the 4 | // terms of the GNU General Public License as published by the Free Software 5 | // Foundation; either version 2 of the License, or (at your option) any later 6 | // version. 7 | // 8 | // This program is distributed in the hope that it will be useful, but WITH- 9 | // OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11 | // more details. 12 | 13 | #include 14 | 15 | #include "scm-index.hpp" 16 | 17 | // Transform between a face-local vector u and a world vector v. --------------- 18 | 19 | static inline void face_to_world(long long a, const double *u, double *v) 20 | { 21 | switch (a) 22 | { 23 | case 0: v[0] = u[2]; v[1] = u[1]; v[2] = -u[0]; break; 24 | case 1: v[0] = -u[2]; v[1] = u[1]; v[2] = u[0]; break; 25 | case 2: v[0] = u[0]; v[1] = u[2]; v[2] = -u[1]; break; 26 | case 3: v[0] = u[0]; v[1] = -u[2]; v[2] = u[1]; break; 27 | case 4: v[0] = u[0]; v[1] = u[1]; v[2] = u[2]; break; 28 | case 5: v[0] = -u[0]; v[1] = u[1]; v[2] = -u[2]; break; 29 | } 30 | } 31 | 32 | static inline void world_to_face(long long a, const double *v, double *u) 33 | { 34 | switch (a) 35 | { 36 | case 0: u[2] = v[0]; u[1] = v[1]; u[0] = -v[2]; break; 37 | case 1: u[2] = -v[0]; u[1] = v[1]; u[0] = v[2]; break; 38 | case 2: u[0] = v[0]; u[2] = v[1]; u[1] = -v[2]; break; 39 | case 3: u[0] = v[0]; u[2] = -v[1]; u[1] = v[2]; break; 40 | case 4: u[0] = v[0]; u[1] = v[1]; u[2] = v[2]; break; 41 | case 5: u[0] = -v[0]; u[1] = v[1]; u[2] = -v[2]; break; 42 | } 43 | } 44 | 45 | // Calculate the vector v toward (x, y) on root face a. ------------------------ 46 | 47 | void scm_vector(long long a, double y, double x, double *v) 48 | { 49 | const double s = x * M_PI_2 - M_PI_4; 50 | const double t = y * M_PI_2 - M_PI_4; 51 | 52 | double u[3]; 53 | 54 | u[0] = sin(s) * cos(t); 55 | u[1] = -cos(s) * sin(t); 56 | u[2] = cos(s) * cos(t); 57 | 58 | double k = 1.0 / sqrt(u[0] * u[0] + u[1] * u[1] + u[2] * u[2]); 59 | 60 | u[0] *= k; 61 | u[1] *= k; 62 | u[2] *= k; 63 | 64 | face_to_world(a, u, v); 65 | } 66 | 67 | // Calculate the root face a and coordinate (x, y) along vector v. ------------- 68 | 69 | void scm_locate(long long *a, double *y, double *x, const double *v) 70 | { 71 | double u[3]; 72 | 73 | u[0] = v[0]; 74 | u[1] = v[1]; 75 | u[2] = v[2]; 76 | 77 | if (v[0] >= fabs(v[1]) && v[0] >= fabs(v[2])) *a = 0; 78 | else if (v[0] <= -fabs(v[1]) && v[0] <= -fabs(v[2])) *a = 1; 79 | else if (v[1] >= fabs(v[0]) && v[1] >= fabs(v[2])) *a = 2; 80 | else if (v[1] <= -fabs(v[0]) && v[1] <= -fabs(v[2])) *a = 3; 81 | else if (v[2] >= fabs(v[0]) && v[2] >= fabs(v[1])) *a = 4; 82 | else if (v[2] <= -fabs(v[0]) && v[2] <= -fabs(v[1])) *a = 5; 83 | 84 | world_to_face(*a, v, u); 85 | 86 | double s = -atan2(u[0], u[2]); 87 | double t = -atan2(u[1], u[2]); 88 | 89 | *x = (s + M_PI_4) / M_PI_2; 90 | *y = (t + M_PI_4) / M_PI_2; 91 | } 92 | 93 | // Determine the page to the north of page i. ---------------------------------- 94 | 95 | long long scm_page_north(long long i) 96 | { 97 | long long l = scm_page_level(i); 98 | long long a = scm_page_root(i); 99 | long long r = scm_page_row(i); 100 | long long c = scm_page_col(i); 101 | 102 | long long n = 1LL << l, m = n - 1, t = m - c; 103 | 104 | if (r > 0) { r = r - 1; } 105 | else if (a == 0) { a = 2; r = t; c = m; } 106 | else if (a == 1) { a = 2; r = c; c = 0; } 107 | else if (a == 2) { a = 5; r = 0; c = t; } 108 | else if (a == 3) { a = 4; r = m; } 109 | else if (a == 4) { a = 2; r = m; } 110 | else { a = 2; r = 0; c = t; } 111 | 112 | return scm_page_index(a, l, r, c); 113 | } 114 | 115 | // Determine the page to the south of page i. ---------------------------------- 116 | 117 | long long scm_page_south(long long i) 118 | { 119 | long long l = scm_page_level(i); 120 | long long a = scm_page_root(i); 121 | long long r = scm_page_row(i); 122 | long long c = scm_page_col(i); 123 | 124 | long long n = 1LL << l, m = n - 1, t = m - c; 125 | 126 | if (r < m) { r = r + 1; } 127 | else if (a == 0) { a = 3; r = c; c = m; } 128 | else if (a == 1) { a = 3; r = t; c = 0; } 129 | else if (a == 2) { a = 4; r = 0; } 130 | else if (a == 3) { a = 5; r = m; c = t; } 131 | else if (a == 4) { a = 3; r = 0; } 132 | else { a = 3; r = m; c = t; } 133 | 134 | return scm_page_index(a, l, r, c); 135 | } 136 | 137 | // Determine the page to the west of page i. ----------------------------------- 138 | 139 | long long scm_page_west(long long i) 140 | { 141 | long long l = scm_page_level(i); 142 | long long a = scm_page_root(i); 143 | long long r = scm_page_row(i); 144 | long long c = scm_page_col(i); 145 | 146 | long long n = 1LL << l, m = n - 1, t = m - r; 147 | 148 | if (c > 0) { c = c - 1; } 149 | else if (a == 0) { a = 4; c = m; } 150 | else if (a == 1) { a = 5; c = m; } 151 | else if (a == 2) { a = 1; c = r; r = 0; } 152 | else if (a == 3) { a = 1; c = t; r = m; } 153 | else if (a == 4) { a = 1; c = m; } 154 | else { a = 0; c = m; } 155 | 156 | return scm_page_index(a, l, r, c); 157 | } 158 | 159 | // Determine the page to the east of page i. ----------------------------------- 160 | 161 | long long scm_page_east(long long i) 162 | { 163 | long long l = scm_page_level(i); 164 | long long a = scm_page_root(i); 165 | long long r = scm_page_row(i); 166 | long long c = scm_page_col(i); 167 | 168 | long long n = 1LL << l, m = n - 1, t = m - r; 169 | 170 | if (c < m) { c = c + 1; } 171 | else if (a == 0) { a = 5; c = 0; } 172 | else if (a == 1) { a = 4; c = 0; } 173 | else if (a == 2) { a = 0; c = t; r = 0; } 174 | else if (a == 3) { a = 0; c = r; r = m; } 175 | else if (a == 4) { a = 0; c = 0; } 176 | else { a = 1; c = 0; } 177 | 178 | return scm_page_index(a, l, r, c); 179 | } 180 | 181 | // Calculate the four corner vectors of page i. -------------------------------- 182 | 183 | void scm_page_corners(long long i, double *v) 184 | { 185 | long long l = scm_page_level(i); 186 | long long a = scm_page_root(i); 187 | long long r = scm_page_row(i); 188 | long long c = scm_page_col(i); 189 | 190 | long long n = 1LL << l; 191 | 192 | scm_vector(a, (double) (r + 0) / n, (double) (c + 0) / n, v + 0); 193 | scm_vector(a, (double) (r + 0) / n, (double) (c + 1) / n, v + 3); 194 | scm_vector(a, (double) (r + 1) / n, (double) (c + 0) / n, v + 6); 195 | scm_vector(a, (double) (r + 1) / n, (double) (c + 1) / n, v + 9); 196 | } 197 | 198 | // Calculate the center vector of page i. -------------------------------------- 199 | 200 | void scm_page_center(long long i, double *v) 201 | { 202 | long long l = scm_page_level(i); 203 | long long a = scm_page_root(i); 204 | long long r = scm_page_row(i); 205 | long long c = scm_page_col(i); 206 | 207 | long long n = 1LL << l; 208 | 209 | scm_vector(a, (double) (r + 0.5) / n, (double) (c + 0.5) / n, v + 0); 210 | } 211 | 212 | //------------------------------------------------------------------------------ 213 | -------------------------------------------------------------------------------- /scm-index.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2011-2012 Robert Kooima 2 | // 3 | // LIBSCM is free software; you can redistribute it and/or modify it under the 4 | // terms of the GNU General Public License as published by the Free Software 5 | // Foundation; either version 2 of the License, or (at your option) any later 6 | // version. 7 | // 8 | // This program is distributed in the hope that it will be useful, but WITH- 9 | // OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11 | // more details. 12 | 13 | #ifndef SCM_INDEX_HPP 14 | #define SCM_INDEX_HPP 15 | 16 | // The following functions compute SCM page index relationships. There is quite 17 | // a bit of redundant computation in their implementation, and it is expected 18 | // that the compiler will aggressively inline and reduce common sub-expressions. 19 | 20 | // These calculations are performed using 64-bit signed indices. They're 64-bit 21 | // because 31-bit indices have already been found in the wild, and increasing 22 | // data set sizes are expected. The sign is useful for exception signaling. 23 | 24 | // Calculate the integer binary log of n. -------------------------------------- 25 | 26 | static inline long long log2(long long n) 27 | { 28 | unsigned long long v = (unsigned long long) n; 29 | unsigned long long r; 30 | unsigned long long s; 31 | 32 | r = (v > 0xFFFFFFFFULL) << 5; v >>= r; 33 | s = (v > 0xFFFFULL ) << 4; v >>= s; r |= s; 34 | s = (v > 0xFFULL ) << 3; v >>= s; r |= s; 35 | s = (v > 0xFULL ) << 2; v >>= s; r |= s; 36 | s = (v > 0x3ULL ) << 1; v >>= s; r |= s; 37 | 38 | return (long long) (r | (v >> 1)); 39 | } 40 | 41 | // Calculate the number of pages in an SCM of depth d. ------------------------- 42 | 43 | static inline long long scm_page_count(long long d) 44 | { 45 | return (1LL << (2 * d + 3)) - 2; 46 | } 47 | 48 | // Calculate the subdivision level at which page i appears. -------------------- 49 | 50 | static inline long long scm_page_level(long long i) 51 | { 52 | return (log2(i + 2) - 1) / 2; 53 | } 54 | 55 | // Calculate the root page in the ancestry of page i. -------------------------- 56 | 57 | static inline long long scm_page_root(long long i) 58 | { 59 | long long n = 1LL << (2 * scm_page_level(i)); 60 | return (i - 2 * (n - 1)) / n; 61 | } 62 | 63 | // Calculate the tile number (face index) of page i. --------------------------- 64 | 65 | static inline long long scm_page_tile(long long i) 66 | { 67 | long long n = 1LL << (2 * scm_page_level(i)); 68 | return (i - 2 * (n - 1)) % n; 69 | } 70 | 71 | // Calculate the tile row of page i. ------------------------------------------- 72 | 73 | static inline long long scm_page_row(long long i) 74 | { 75 | return scm_page_tile(i) / (1LL << scm_page_level(i)); 76 | } 77 | 78 | // Calculate the tile column of page i. ---------------------------------------- 79 | 80 | static inline long long scm_page_col(long long i) 81 | { 82 | return scm_page_tile(i) % (1LL << scm_page_level(i)); 83 | } 84 | 85 | // Calculate the index of the page on root a at level l, row r, column c. ----- 86 | 87 | static inline long long scm_page_index(long long a, long long l, 88 | long long r, long long c) 89 | { 90 | return scm_page_count(l - 1) + (a << (2 * l)) + (r << l) + c; 91 | } 92 | 93 | // Calculate the parent page of page i. ---------------------------------------- 94 | 95 | static inline long long scm_page_parent(long long i) 96 | { 97 | return scm_page_index(scm_page_root(i), scm_page_level(i) - 1, 98 | scm_page_row (i) / 2, 99 | scm_page_col (i) / 2); 100 | } 101 | 102 | // Calculate child page k of page i. ------------------------------------------- 103 | 104 | static inline long long scm_page_child(long long i, long long k) 105 | { 106 | return scm_page_index(scm_page_root(i), scm_page_level(i) + 1, 107 | scm_page_row (i) * 2 + k / 2, 108 | scm_page_col (i) * 2 + k % 2); 109 | } 110 | 111 | // Calculate the order (child index) of page i. -------------------------------- 112 | 113 | static inline long long scm_page_order(long long i) 114 | { 115 | return 2 * (scm_page_row(i) % 2) 116 | + (scm_page_col(i) % 2); 117 | } 118 | 119 | //------------------------------------------------------------------------------ 120 | 121 | void scm_locate(long long *, double *, double *, const double *); 122 | void scm_vector(long long, double, double, double *); 123 | 124 | long long scm_page_north(long long); 125 | long long scm_page_south(long long); 126 | long long scm_page_west (long long); 127 | long long scm_page_east (long long); 128 | 129 | void scm_page_corners(long long, double *); 130 | void scm_page_center (long long, double *); 131 | 132 | //------------------------------------------------------------------------------ 133 | 134 | #endif 135 | -------------------------------------------------------------------------------- /scm-item.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2011-2012 Robert Kooima 2 | // 3 | // LIBSCM is free software; you can redistribute it and/or modify it under the 4 | // terms of the GNU General Public License as published by the Free Software 5 | // Foundation; either version 2 of the License, or (at your option) any later 6 | // version. 7 | // 8 | // This program is distributed in the hope that it will be useful, but WITH- 9 | // OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11 | // more details. 12 | 13 | #ifndef SCM_ITEM_HPP 14 | #define SCM_ITEM_HPP 15 | 16 | //------------------------------------------------------------------------------ 17 | 18 | /// An scm_item is a reference to a specific page in a specific SCM file 19 | /// 20 | /// An scm_item is valid if both the file and page indices are non-negative. 21 | /// A partial ordering is defined that enables O(log n) searching of objects 22 | /// derived from scm_item. 23 | 24 | struct scm_item 25 | { 26 | int f; ///< File index 27 | long long i; ///< Page index 28 | 29 | /// Initialize an invalid SCM page reference. 30 | 31 | scm_item() : f(-1), i(-1) { } 32 | 33 | /// Initialize a valid SCM page reference. 34 | 35 | scm_item(int f, long long i) : f( f), i( i) { } 36 | 37 | /// Return true if this is a valid SCM page reference. 38 | 39 | bool is_valid() const { return (f >= 0 && i >= 0); } 40 | 41 | /// Determine the order of two SCM page references. 42 | 43 | bool operator<(const scm_item& that) const { 44 | if (i < that.i) return true; 45 | if (i > that.i) return false; 46 | return (f < that.f); 47 | } 48 | }; 49 | 50 | //------------------------------------------------------------------------------ 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /scm-label.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2011-2012 Robert Kooima 2 | // 3 | // LIBSCM is free software; you can redistribute it and/or modify it under the 4 | // terms of the GNU General Public License as published by the Free Software 5 | // Foundation; either version 2 of the License, or (at your option) any later 6 | // version. 7 | // 8 | // This program is distributed in the hope that it will be useful, but WITH- 9 | // OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11 | // more details. 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | #include 19 | 20 | #include "util3d/math3d.h" 21 | #include "util3d/type.h" 22 | #include "util3d/glsl.h" 23 | 24 | #include "scm-label.hpp" 25 | #include "scm-path.hpp" 26 | #include "scm-log.hpp" 27 | 28 | #include "scm-label-icons.h" 29 | #include "scm-label-font.h" 30 | 31 | #include "scm_label_circle_vert.h" 32 | #include "scm_label_circle_frag.h" 33 | #include "scm_label_sprite_vert.h" 34 | #include "scm_label_sprite_frag.h" 35 | 36 | //------------------------------------------------------------------------------ 37 | /// @cond INTERNAL 38 | 39 | struct point 40 | { 41 | float v[3]; 42 | float t[2]; 43 | unsigned char c[4]; 44 | }; 45 | 46 | struct matrix 47 | { 48 | double M[16]; 49 | 50 | matrix() 51 | { 52 | midentity(M); 53 | } 54 | 55 | void rotatey(double a) 56 | { 57 | double A[16], B[16]; 58 | mrotatey(A, a); 59 | mmultiply(B, M, A); 60 | mcpy(M, B); 61 | } 62 | 63 | void rotatex(double a) 64 | { 65 | double A[16], B[16]; 66 | mrotatex(A, a); 67 | mmultiply(B, M, A); 68 | mcpy(M, B); 69 | } 70 | 71 | void rotatez(double a) 72 | { 73 | double A[16], B[16]; 74 | mrotatez(A, a); 75 | mmultiply(B, M, A); 76 | mcpy(M, B); 77 | } 78 | 79 | void translate(double x, double y, double z) 80 | { 81 | double A[16], B[16], v[3] = { x, y, z }; 82 | mtranslate(A, v); 83 | mmultiply(B, M, A); 84 | mcpy(M, B); 85 | } 86 | 87 | void scale(double k) 88 | { 89 | double A[16], B[16], v[3] = { k, k, k }; 90 | mscale(A, v); 91 | mmultiply(B, M, A); 92 | mcpy(M, B); 93 | } 94 | }; 95 | 96 | struct circle 97 | { 98 | point p[4]; 99 | 100 | circle(matrix& M, const char *c) 101 | { 102 | const double s = 0.5; 103 | GLubyte a = 255; 104 | 105 | if (c[0] == 'A' && c[1] == 'A') a /= 1; 106 | if (c[0] == 'S' && c[1] == 'F') a /= 2; 107 | 108 | p[0].v[0] = float(M.M[0] * (-s) + M.M[4] * (-s) + M.M[12]); 109 | p[0].v[1] = float(M.M[1] * (-s) + M.M[5] * (-s) + M.M[13]); 110 | p[0].v[2] = float(M.M[2] * (-s) + M.M[6] * (-s) + M.M[14]); 111 | p[0].t[0] = 0; 112 | p[0].t[1] = 0; 113 | p[0].c[0] = 255; 114 | p[0].c[1] = 255; 115 | p[0].c[2] = 255; 116 | p[0].c[3] = a; 117 | 118 | p[1].v[0] = float(M.M[0] * ( s) + M.M[4] * (-s) + M.M[12]); 119 | p[1].v[1] = float(M.M[1] * ( s) + M.M[5] * (-s) + M.M[13]); 120 | p[1].v[2] = float(M.M[2] * ( s) + M.M[6] * (-s) + M.M[14]); 121 | p[1].t[0] = 0; 122 | p[1].t[1] = 1; 123 | p[1].c[0] = 255; 124 | p[1].c[1] = 255; 125 | p[1].c[2] = 255; 126 | p[1].c[3] = a; 127 | 128 | p[2].v[0] = float(M.M[0] * ( s) + M.M[4] * ( s) + M.M[12]); 129 | p[2].v[1] = float(M.M[1] * ( s) + M.M[5] * ( s) + M.M[13]); 130 | p[2].v[2] = float(M.M[2] * ( s) + M.M[6] * ( s) + M.M[14]); 131 | p[2].t[0] = 1; 132 | p[2].t[1] = 1; 133 | p[2].c[0] = 255; 134 | p[2].c[1] = 255; 135 | p[2].c[2] = 255; 136 | p[2].c[3] = a; 137 | 138 | p[3].v[0] = float(M.M[0] * (-s) + M.M[4] * ( s) + M.M[12]); 139 | p[3].v[1] = float(M.M[1] * (-s) + M.M[5] * ( s) + M.M[13]); 140 | p[3].v[2] = float(M.M[2] * (-s) + M.M[6] * ( s) + M.M[14]); 141 | p[3].t[0] = 1; 142 | p[3].t[1] = 0; 143 | p[3].c[0] = 255; 144 | p[3].c[1] = 255; 145 | p[3].c[2] = 255; 146 | p[3].c[3] = a; 147 | } 148 | }; 149 | 150 | struct sprite 151 | { 152 | point p; 153 | 154 | sprite(matrix& M, const char *c) 155 | { 156 | GLfloat x = 0.0; 157 | GLfloat y = 0.0; 158 | 159 | if (c[0] == 'M' && c[1] == 'O') x = 0.0; 160 | if (c[0] == 'L' && c[1] == 'F') x = 1.0; 161 | if (c[0] == '@' && c[1] == '*') x = 2.0; 162 | if (c[0] == '@' && c[1] == 'C') x = 3.0; 163 | 164 | p.v[0] = float(M.M[12]); 165 | p.v[1] = float(M.M[13]); 166 | p.v[2] = float(M.M[14]); 167 | p.t[0] = x; 168 | p.t[1] = y; 169 | p.c[0] = 255; 170 | p.c[1] = 255; 171 | p.c[2] = 255; 172 | p.c[3] = 255; 173 | } 174 | }; 175 | 176 | struct latlon 177 | { 178 | point p[360]; 179 | 180 | latlon(float lat, float lon, float rad) 181 | { 182 | double P = 0.0; 183 | double T = 0.0; 184 | 185 | for (int i = 0; i < 360; ++i) 186 | { 187 | if (lon) 188 | { 189 | P = radians(i); 190 | T = radians(lon); 191 | } 192 | else 193 | { 194 | P = radians(lat); 195 | T = radians(i); 196 | } 197 | 198 | p[i].v[0] = float(rad * sin(T) * cos(P)); 199 | p[i].v[1] = float(rad * sin(P)); 200 | p[i].v[2] = float(rad * cos(T) * cos(P)); 201 | p[i].t[0] = 0; 202 | p[i].t[1] = 0; 203 | p[i].c[0] = 127; 204 | p[i].c[1] = 127; 205 | p[i].c[2] = 127; 206 | p[i].c[3] = 127; 207 | } 208 | } 209 | }; 210 | 211 | static double clamp(double k, double a, double b) 212 | { 213 | if (k < a) return a; 214 | else if (k > b) return b; 215 | else return k; 216 | } 217 | 218 | /// @endcond 219 | //------------------------------------------------------------------------------ 220 | 221 | /// Create a new label 222 | /// 223 | /// Render all strings to a font atlas and initialize any OpenGL state needed 224 | /// to render all annotations. 225 | /// 226 | /// @param path Fully resolved path to CSV file 227 | /// @param size Icon size (in pixels) 228 | 229 | scm_label::scm_label(const std::string& path, int size) : 230 | label_line(0), 231 | num_circles(0), 232 | num_sprites(0), 233 | num_latlons(0), 234 | sprite_size(size), 235 | circle_vbo(0), 236 | sprite_vbo(0), 237 | latlon_vbo(0) 238 | { 239 | // Initialize the font. 240 | 241 | label_font = font_create(Vera_ttf, Vera_ttf_len, 64, 1.0); 242 | 243 | // Initialize the shaders. 244 | 245 | memset(&circle_glsl, 0, sizeof (glsl)); 246 | memset(&sprite_glsl, 0, sizeof (glsl)); 247 | 248 | glsl_source(&circle_glsl, (const char *) scm_label_circle_vert, 249 | scm_label_circle_vert_len, 250 | (const char *) scm_label_circle_frag, 251 | scm_label_circle_frag_len); 252 | glsl_source(&sprite_glsl, (const char *) scm_label_sprite_vert, 253 | scm_label_sprite_vert_len, 254 | (const char *) scm_label_sprite_frag, 255 | scm_label_sprite_frag_len); 256 | 257 | glUseProgram(sprite_glsl.program); 258 | glUniform1i(glGetUniformLocation(sprite_glsl.program, "icons"), 0); 259 | 260 | // Parse the data file into labels. 261 | 262 | parse(path); 263 | 264 | // Generate an annotation for each label. 265 | 266 | std::vector string_v; 267 | std::vector matrix_v; 268 | std::vector circle_v; 269 | std::vector sprite_v; 270 | std::vector latlon_v; 271 | 272 | for (int i = 0; i < int(labels.size()); ++i) 273 | { 274 | int w = line_length(labels[i].str, label_font); 275 | int h = font_height( label_font); 276 | 277 | matrix M; 278 | double x = -w / 2.0; 279 | double y = 0.0; 280 | double z = 0.0; 281 | 282 | double d = labels[i].dia; 283 | double r = labels[i].rad; 284 | double k = d / r; 285 | 286 | // Transform it into position 287 | 288 | M.rotatey( radians(labels[i].lon)); 289 | M.rotatex(-radians(labels[i].lat)); 290 | M.translate(0, 0, r); 291 | M.scale(d); 292 | 293 | // Create a sprite. 294 | 295 | if (labels[i].sprite()) 296 | { 297 | sprite S(M, labels[i].typ); 298 | sprite_v.push_back(S); 299 | y = +h / 3.0; 300 | } 301 | 302 | // Create a circle. 303 | 304 | if (labels[i].circle()) 305 | { 306 | circle C(M, labels[i].typ); 307 | circle_v.push_back(C); 308 | } 309 | 310 | // Create a line of latitude. 311 | 312 | if (labels[i].latlon()) 313 | { 314 | latlon L(labels[i].lat, labels[i].lon, labels[i].rad); 315 | latlon_v.push_back(L); 316 | } 317 | 318 | // Add the string and matrix to the list. 319 | 320 | double e = 0.001 * clamp(k, 0.0, 0.5) / k; 321 | 322 | M.scale(e); 323 | M.translate(x, y, z); 324 | 325 | string_v.push_back(labels[i].str); 326 | matrix_v.push_back(M); 327 | } 328 | 329 | num_circles = circle_v.size(); 330 | num_sprites = sprite_v.size(); 331 | num_latlons = latlon_v.size(); 332 | 333 | size_t sz = sizeof (point); 334 | 335 | // Typeset the labels. 336 | 337 | if (!string_v.empty()) 338 | label_line = line_layout(string_v.size(), &string_v.front(), NULL, 339 | matrix_v.front().M, label_font); 340 | 341 | // Create a VBO for the circles. 342 | 343 | if (!circle_v.empty()) 344 | { 345 | glGenBuffers(1, &circle_vbo); 346 | glBindBuffer(GL_ARRAY_BUFFER, circle_vbo); 347 | glBufferData(GL_ARRAY_BUFFER, 4 * sz * circle_v.size(), 348 | &circle_v.front(), GL_STATIC_DRAW); 349 | } 350 | 351 | // Create a VBO for the sprites. 352 | 353 | if (!sprite_v.empty()) { 354 | glGenBuffers(1, &sprite_vbo); 355 | glBindBuffer(GL_ARRAY_BUFFER, sprite_vbo); 356 | glBufferData(GL_ARRAY_BUFFER, 1 * sz * sprite_v.size(), 357 | &sprite_v.front(), GL_STATIC_DRAW); 358 | } 359 | 360 | // Create a VBO for the latlons. 361 | 362 | if (!latlon_v.empty()) { 363 | glGenBuffers(1, &latlon_vbo); 364 | glBindBuffer(GL_ARRAY_BUFFER, latlon_vbo); 365 | glBufferData(GL_ARRAY_BUFFER, 360 * sz * latlon_v.size(), 366 | &latlon_v.front(), GL_STATIC_DRAW); 367 | } 368 | 369 | // Create a texture for the sprites. 370 | 371 | glGenTextures(1, &sprite_tex); 372 | glBindTexture(GL_TEXTURE_2D, sprite_tex); 373 | glTexImage2D (GL_TEXTURE_2D, 0, GL_LUMINANCE, 128, 32, 0, 374 | GL_LUMINANCE, GL_UNSIGNED_BYTE, scm_label_icons); 375 | 376 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 377 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 378 | 379 | scm_log("scm_label constructor %s", path.c_str()); 380 | } 381 | 382 | /// Finalize all OpenGL state 383 | 384 | scm_label::~scm_label() 385 | { 386 | scm_log("scm_label destructor"); 387 | 388 | if (latlon_vbo) glDeleteBuffers(1, &latlon_vbo); 389 | if (sprite_vbo) glDeleteBuffers(1, &sprite_vbo); 390 | if (circle_vbo) glDeleteBuffers(1, &circle_vbo); 391 | 392 | line_delete(label_line); 393 | font_delete(label_font); 394 | 395 | glsl_delete(&sprite_glsl); 396 | glsl_delete(&circle_glsl); 397 | } 398 | 399 | //------------------------------------------------------------------------------ 400 | 401 | /// Draw all annotations using the given color and transparency 402 | /// 403 | /// @param r Red 404 | /// @param g Green 405 | /// @param b Blue 406 | /// @param a Alpha 407 | 408 | void scm_label::draw(GLubyte r, GLubyte g, GLubyte b, GLubyte a) 409 | { 410 | size_t sz = sizeof (point); 411 | 412 | glPushAttrib(GL_ENABLE_BIT | GL_POLYGON_BIT); 413 | { 414 | // Ensure we're in clockwise mode regardless of sphere winding. 415 | 416 | glDisable(GL_DEPTH_TEST); 417 | glEnable(GL_LINE_SMOOTH); 418 | glEnable(GL_COLOR_MATERIAL); 419 | glEnable(GL_CULL_FACE); 420 | glFrontFace(GL_CCW); 421 | glColor4ub(r, g, b, a); 422 | glLineWidth(0.5); 423 | 424 | // Blend for antialiasing but preserve dest alpha for the blur shader. 425 | 426 | glEnable(GL_BLEND); 427 | glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE); 428 | 429 | glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT); 430 | { 431 | glEnableClientState(GL_VERTEX_ARRAY); 432 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); 433 | 434 | // Draw the latlons. 435 | 436 | glEnable(GL_CLIP_PLANE0); 437 | 438 | glBindBuffer(GL_ARRAY_BUFFER, latlon_vbo); 439 | glVertexPointer (3, GL_FLOAT, sz, (GLvoid *) 0); 440 | glTexCoordPointer(2, GL_FLOAT, sz, (GLvoid *) 12); 441 | glColorPointer (4, GL_UNSIGNED_BYTE, sz, (GLvoid *) 20); 442 | 443 | glUseProgram(0); 444 | for (int i = 0; i < num_latlons; i++) 445 | glDrawArrays(GL_LINE_LOOP, i * 360, 360); 446 | 447 | // Draw the circles. 448 | 449 | glBindBuffer(GL_ARRAY_BUFFER, circle_vbo); 450 | glVertexPointer (3, GL_FLOAT, sz, (GLvoid *) 0); 451 | glTexCoordPointer(2, GL_FLOAT, sz, (GLvoid *) 12); 452 | glColorPointer (4, GL_UNSIGNED_BYTE, sz, (GLvoid *) 20); 453 | 454 | glUseProgram(circle_glsl.program); 455 | glDrawArrays(GL_QUADS, 0, 4 * num_circles); 456 | 457 | // Draw the sprites. 458 | 459 | glBindBuffer(GL_ARRAY_BUFFER, sprite_vbo); 460 | glVertexPointer (3, GL_FLOAT, sz, (GLvoid *) 0); 461 | glTexCoordPointer(2, GL_FLOAT, sz, (GLvoid *) 12); 462 | glColorPointer (4, GL_UNSIGNED_BYTE, sz, (GLvoid *) 20); 463 | 464 | glPointSize(GLfloat(sprite_size)); 465 | glEnable(GL_POINT_SPRITE); 466 | glBindTexture(GL_TEXTURE_2D, sprite_tex); 467 | 468 | glUseProgram(sprite_glsl.program); 469 | glDrawArrays(GL_POINTS, 0, 1 * num_sprites); 470 | 471 | glDisable(GL_POINT_SPRITE); 472 | } 473 | glPopClientAttrib(); 474 | 475 | // Draw the labels. 476 | 477 | glUseProgram(0); 478 | glDisable(GL_LIGHTING); 479 | glEnable(GL_TEXTURE_2D); 480 | 481 | line_render(label_line); 482 | } 483 | glPopAttrib(); 484 | } 485 | 486 | //------------------------------------------------------------------------------ 487 | 488 | /// Scan one record of the label definition 489 | 490 | int scm_label::scan(FILE *fp, label& L) 491 | { 492 | int n; 493 | 494 | if (fscanf(fp, "\"%63[^\"]\",%f,%f,%f,%f,%c%c\n%n", 495 | L.str, &L.lat, &L.lon, &L.dia, &L.rad, &L.typ[0], &L.typ[1], &n) > 5) 496 | return n; 497 | 498 | if (fscanf(fp, "%63[^,],%f,%f,%f,%f,%c%c\n%n", 499 | L.str, &L.lat, &L.lon, &L.dia, &L.rad, &L.typ[0], &L.typ[1], &n) > 5) 500 | return n; 501 | 502 | return 0; 503 | } 504 | 505 | /// Parse the label definition 506 | 507 | void scm_label::parse(const std::string& path) 508 | { 509 | if (!path.empty()) 510 | { 511 | FILE *fp; 512 | label L; 513 | 514 | if ((fp = fopen(path.c_str(), "r"))) 515 | { 516 | while (scan(fp, L)) 517 | labels.push_back(L); 518 | 519 | fclose(fp); 520 | } 521 | } 522 | } 523 | 524 | //------------------------------------------------------------------------------ 525 | -------------------------------------------------------------------------------- /scm-label.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2011-2012 Robert Kooima 2 | // 3 | // LIBSCM is free software; you can redistribute it and/or modify it under the 4 | // terms of the GNU General Public License as published by the Free Software 5 | // Foundation; either version 2 of the License, or (at your option) any later 6 | // version. 7 | // 8 | // This program is distributed in the hope that it will be useful, but WITH- 9 | // OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11 | // more details. 12 | 13 | #ifndef SCM_LABEL_HPP 14 | #define SCM_LABEL_HPP 15 | 16 | #include 17 | #include 18 | 19 | #include 20 | 21 | #include "util3d/type.h" 22 | #include "util3d/glsl.h" 23 | 24 | //----------------------------------------------------------------------------- 25 | 26 | /// An scm_label renders annotations on the sphere 27 | /// 28 | /// The label renderer reads data from a CSV file giving a list of 29 | /// surface features, each with the following elements: 30 | /// 31 | /// Key | Type 32 | /// --------- | -------- 33 | /// Name | a string 34 | /// Latitude | in degrees 35 | /// Longitude | in degrees 36 | /// Diameter | in meters 37 | /// Radius | distance from the center of the globe, in meters 38 | /// Feature | a two-letter code 39 | /// 40 | /// The name string is rendered in text scaled to fit the given diameter. 41 | /// The currently-defined two-letter codes are: 42 | /// 43 | /// Code | Feature | Representation 44 | /// ---- | ---------------- | -------------- 45 | /// AA | Crater | Circle of given diameter 46 | /// SF | Secondary Crater | Circle of given diameter with half opacity 47 | /// MO | Mountain | Triangle icon 48 | /// LF | Landing Site | Flag icon 49 | /// \@* | Star | Star icon 50 | /// \@C | Circle | Circle icon 51 | 52 | class scm_label 53 | { 54 | public: 55 | 56 | scm_label(const std::string&, int); 57 | ~scm_label(); 58 | 59 | void draw(GLubyte r, GLubyte g, GLubyte b, GLubyte a); 60 | 61 | private: 62 | 63 | static const int strmax = 64; 64 | 65 | struct label 66 | { 67 | char str[strmax]; 68 | float lat; 69 | float lon; 70 | float dia; 71 | float rad; 72 | char typ[2]; 73 | 74 | bool circle() const { 75 | return ((typ[0] == 'A' && typ[1] == 'A') 76 | || (typ[0] == 'S' && typ[1] == 'F')); 77 | } 78 | bool sprite() const { 79 | return ((typ[0] == 'L' && typ[1] == 'F') 80 | || (typ[0] == 'M' && typ[1] == 'O') 81 | || (typ[0] == '@' && typ[1] == '*') 82 | || (typ[0] == '@' && typ[1] == 'C')); 83 | } 84 | bool latlon() const { 85 | return ((typ[0] == '@' && typ[1] == '#')); 86 | } 87 | }; 88 | 89 | int scan (FILE *, label&); 90 | void parse(const std::string&); 91 | void apply(label *); 92 | 93 | font *label_font; 94 | line *label_line; 95 | 96 | int num_circles; 97 | int num_sprites; 98 | int num_latlons; 99 | int sprite_size; 100 | 101 | glsl circle_glsl; 102 | glsl sprite_glsl; 103 | 104 | GLuint circle_vbo; 105 | GLuint sprite_vbo; 106 | GLuint latlon_vbo; 107 | GLuint sprite_tex; 108 | 109 | std::vector