1 module wavefront.obj; 2 3 import utga; 4 import std.conv; 5 import std.stdio; 6 import std.array; 7 import std.algorithm; 8 9 public import wavefront.geometry; 10 11 struct Face 12 { 13 int v; 14 int t; 15 int n; 16 } 17 18 class Model 19 { 20 private: 21 Vec2f[] textures_; 22 Vec3f[] verts_; 23 Face[][] faces_; 24 25 public: 26 27 this(string filename) 28 { 29 auto f = File(filename, "r"); 30 foreach (line; f.byLine) { 31 if (line.startsWith("v ")) { 32 Vec3f v; 33 v.raw = line[2..$].splitter.map!(to!float).array; 34 verts_ ~= v; 35 } 36 else if (line.startsWith("vt ")){ 37 Vec2f t; 38 t.raw = line[2..$].splitter.map!(to!float).array; 39 textures_ ~= t; 40 } 41 else if (line.startsWith("f ")) { 42 Face tmp; 43 Face[] face; 44 foreach (pol; line[2..$].splitter) 45 { 46 tmp.v = to!int(pol.splitter("/").array[0]) - 1; 47 tmp.t = to!int(pol.splitter("/").array[1]) - 1; 48 tmp.n = to!int(pol.splitter("/").array[2]) - 1; 49 face ~= tmp; 50 } 51 faces_ ~= face; 52 } 53 } 54 stderr.writefln("# v# %d t# %d f# %d", verts_.length, textures_.length, faces_.length); 55 } 56 57 @property 58 auto nverts() 59 { 60 return verts_.length; 61 } 62 63 @property 64 auto nfaces() 65 { 66 return faces_.length; 67 } 68 69 @property 70 auto ntextures() 71 { 72 return textures_.length; 73 } 74 75 auto face(int idx) 76 { 77 return faces_[idx]; 78 } 79 80 auto vert(int idx) 81 { 82 return verts_[idx]; 83 } 84 85 auto texture(int idx) 86 { 87 return textures_[idx]; 88 } 89 90 @property 91 auto faces() 92 { 93 return faces_; 94 } 95 96 @property 97 auto textures() 98 { 99 return textures_; 100 } 101 }