├── README.md ├── .gitattributes ├── LICENSE.md ├── .gitignore └── obj_loader.lua /README.md: -------------------------------------------------------------------------------- 1 | Wavefront Object Loader 2 | == 3 | Wavefront Object Loader is a library for parsing Wavefront Object files into Lua data structures. 4 | 5 | Quick Example 6 | -- 7 | ```lua 8 | local obj_loader = require "loader" 9 | 10 | local object = obj_loader.load("some_object.obj") 11 | ``` 12 | 13 | License 14 | -- 15 | This code is licensed under the [**MIT Open Source License**][MIT]. Check out the LICENSE file for more information. 16 | 17 | [MIT]: http://www.opensource.org/licenses/mit-license.html 18 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Wavefront Object Loader 2 | == 3 | 4 | This code is licensed under the [**MIT Open Source License**][MIT]. 5 | 6 | 7 | Copyright (c) 2014 Landon Manning - LManning17@gmail.com - [LandonManning.com][LM] 8 | 9 | Permission is hereby granted, free of charge, to any person obtaining a copy 10 | of this software and associated documentation files (the "Software"), to deal 11 | in the Software without restriction, including without limitation the rights 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | copies of the Software, and to permit persons to whom the Software is 14 | furnished to do so, subject to the following conditions: 15 | 16 | The above copyright notice and this permission notice shall be included in 17 | all copies or substantial portions of the Software. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | THE SOFTWARE. 26 | 27 | 28 | [MIT]: http://www.opensource.org/licenses/mit-license.html 29 | [LM]: http://LandonManning.com 30 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | 46 | [Dd]ebug/ 47 | [Rr]elease/ 48 | x64/ 49 | build/ 50 | [Bb]in/ 51 | [Oo]bj/ 52 | 53 | # MSTest test Results 54 | [Tt]est[Rr]esult*/ 55 | [Bb]uild[Ll]og.* 56 | 57 | *_i.c 58 | *_p.c 59 | *.ilk 60 | *.meta 61 | *.obj 62 | *.pch 63 | *.pdb 64 | *.pgc 65 | *.pgd 66 | *.rsp 67 | *.sbr 68 | *.tlb 69 | *.tli 70 | *.tlh 71 | *.tmp 72 | *.tmp_proj 73 | *.log 74 | *.vspscc 75 | *.vssscc 76 | .builds 77 | *.pidb 78 | *.log 79 | *.scc 80 | 81 | # Visual C++ cache files 82 | ipch/ 83 | *.aps 84 | *.ncb 85 | *.opensdf 86 | *.sdf 87 | *.cachefile 88 | 89 | # Visual Studio profiler 90 | *.psess 91 | *.vsp 92 | *.vspx 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | 101 | # TeamCity is a build add-in 102 | _TeamCity* 103 | 104 | # DotCover is a Code Coverage Tool 105 | *.dotCover 106 | 107 | # NCrunch 108 | *.ncrunch* 109 | .*crunch*.local.xml 110 | 111 | # Installshield output folder 112 | [Ee]xpress/ 113 | 114 | # DocProject is a documentation generator add-in 115 | DocProject/buildhelp/ 116 | DocProject/Help/*.HxT 117 | DocProject/Help/*.HxC 118 | DocProject/Help/*.hhc 119 | DocProject/Help/*.hhk 120 | DocProject/Help/*.hhp 121 | DocProject/Help/Html2 122 | DocProject/Help/html 123 | 124 | # Click-Once directory 125 | publish/ 126 | 127 | # Publish Web Output 128 | *.Publish.xml 129 | *.pubxml 130 | 131 | # NuGet Packages Directory 132 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 133 | #packages/ 134 | 135 | # Windows Azure Build Output 136 | csx 137 | *.build.csdef 138 | 139 | # Windows Store app package directory 140 | AppPackages/ 141 | 142 | # Others 143 | sql/ 144 | *.Cache 145 | ClientBin/ 146 | [Ss]tyle[Cc]op.* 147 | ~$* 148 | *~ 149 | *.dbmdl 150 | *.[Pp]ublish.xml 151 | *.pfx 152 | *.publishsettings 153 | 154 | # RIA/Silverlight projects 155 | Generated_Code/ 156 | 157 | # Backup & report files from converting an old project file to a newer 158 | # Visual Studio version. Backup files are not needed, because we have git ;-) 159 | _UpgradeReport_Files/ 160 | Backup*/ 161 | UpgradeLog*.XML 162 | UpgradeLog*.htm 163 | 164 | # SQL Server files 165 | App_Data/*.mdf 166 | App_Data/*.ldf 167 | 168 | ############# 169 | ## Windows detritus 170 | ############# 171 | 172 | # Windows image file caches 173 | Thumbs.db 174 | ehthumbs.db 175 | 176 | # Folder config file 177 | Desktop.ini 178 | 179 | # Recycle Bin used on file shares 180 | $RECYCLE.BIN/ 181 | 182 | # Mac crap 183 | .DS_Store 184 | 185 | 186 | ############# 187 | ## Python 188 | ############# 189 | 190 | *.py[co] 191 | 192 | # Packages 193 | *.egg 194 | *.egg-info 195 | dist/ 196 | build/ 197 | eggs/ 198 | parts/ 199 | var/ 200 | sdist/ 201 | develop-eggs/ 202 | .installed.cfg 203 | 204 | # Installer logs 205 | pip-log.txt 206 | 207 | # Unit test / coverage reports 208 | .coverage 209 | .tox 210 | 211 | #Translations 212 | *.mo 213 | 214 | #Mr Developer 215 | .mr.developer.cfg 216 | -------------------------------------------------------------------------------- /obj_loader.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | ------------------------------------------------------------------------------ 3 | Wavefront Object Loader is licensed under the MIT Open Source License. 4 | (http://www.opensource.org/licenses/mit-license.html) 5 | ------------------------------------------------------------------------------ 6 | 7 | Copyright (c) 2014 Landon Manning - LManning17@gmail.com - LandonManning.com 8 | 9 | Permission is hereby granted, free of charge, to any person obtaining a copy 10 | of this software and associated documentation files (the "Software"), to deal 11 | in the Software without restriction, including without limitation the rights 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | copies of the Software, and to permit persons to whom the Software is 14 | furnished to do so, subject to the following conditions: 15 | 16 | The above copyright notice and this permission notice shall be included in 17 | all copies or substantial portions of the Software. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | THE SOFTWARE. 26 | ]]-- 27 | 28 | local path = ... .. "." 29 | local loader = {} 30 | 31 | loader.version = "0.0.2" 32 | 33 | function loader.load(file) 34 | assert(file_exists(file), "File not found: " .. file) 35 | 36 | local get_lines 37 | 38 | if love then 39 | get_lines = love.filesystem.lines 40 | else 41 | get_lines = io.lines 42 | end 43 | 44 | local lines = {} 45 | 46 | for line in get_lines(file) do 47 | table.insert(lines, line) 48 | end 49 | 50 | return loader.parse(lines) 51 | end 52 | 53 | function loader.parse(object) 54 | local obj = { 55 | v = {}, -- List of vertices - x, y, z, [w]=1.0 56 | vt = {}, -- Texture coordinates - u, v, [w]=0 57 | vn = {}, -- Normals - x, y, z 58 | vp = {}, -- Parameter space vertices - u, [v], [w] 59 | f = {}, -- Faces 60 | } 61 | 62 | for _, line in ipairs(object) do 63 | local l = string_split(line, "%s+") 64 | 65 | if l[1] == "v" then 66 | local v = { 67 | x = tonumber(l[2]), 68 | y = tonumber(l[3]), 69 | z = tonumber(l[4]), 70 | w = tonumber(l[5]) or 1.0 71 | } 72 | table.insert(obj.v, v) 73 | elseif l[1] == "vt" then 74 | local vt = { 75 | u = tonumber(l[2]), 76 | v = tonumber(l[3]), 77 | w = tonumber(l[4]) or 0 78 | } 79 | table.insert(obj.vt, vt) 80 | elseif l[1] == "vn" then 81 | local vn = { 82 | x = tonumber(l[2]), 83 | y = tonumber(l[3]), 84 | z = tonumber(l[4]), 85 | } 86 | table.insert(obj.vn, vn) 87 | elseif l[1] == "vp" then 88 | local vp = { 89 | u = tonumber(l[2]), 90 | v = tonumber(l[3]), 91 | w = tonumber(l[4]), 92 | } 93 | table.insert(obj.vp, vp) 94 | elseif l[1] == "f" then 95 | local f = {} 96 | 97 | for i=2, #l do 98 | local split = string_split(l[i], "/") 99 | local v = {} 100 | 101 | v.v = tonumber(split[1]) 102 | if split[2] ~= "" then v.vt = tonumber(split[2]) end 103 | v.vn = tonumber(split[3]) 104 | 105 | table.insert(f, v) 106 | end 107 | 108 | table.insert(obj.f, f) 109 | end 110 | end 111 | 112 | return obj 113 | end 114 | 115 | function file_exists(file) 116 | if love then return love.filesystem.getInfo(file) ~= nil end 117 | 118 | local f = io.open(file, "r") 119 | if f then f:close() end 120 | return f ~= nil 121 | end 122 | 123 | -- http://wiki.interfaceware.com/534.html 124 | function string_split(s, d) 125 | local t = {} 126 | local i = 0 127 | local f 128 | local match = '(.-)' .. d .. '()' 129 | 130 | if string.find(s, d) == nil then 131 | return {s} 132 | end 133 | 134 | for sub, j in string.gmatch(s, match) do 135 | i = i + 1 136 | t[i] = sub 137 | f = j 138 | end 139 | 140 | if i ~= 0 then 141 | t[i+1] = string.sub(s, f) 142 | end 143 | 144 | return t 145 | end 146 | 147 | return loader 148 | --------------------------------------------------------------------------------