Mercurial > hg > forks > gldragon
comparison dmmodel.h @ 19:a329f0216491
Implement PLY file format parsing and extremely simplistic scene setup file format.
Not finished yet.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 07 Nov 2019 20:15:33 +0200 |
parents | |
children | 1404dfcee7b8 |
comparison
equal
deleted
inserted
replaced
18:b1e75c65016d | 19:a329f0216491 |
---|---|
1 // | |
2 // | |
3 // | |
4 #ifndef DMMODEL_H | |
5 #define DMMODEL_H 1 | |
6 | |
7 #include "dmutil.h" | |
8 #include <cstdint> | |
9 #include <fstream> | |
10 #include <unordered_map> | |
11 | |
12 | |
13 #define PLY_PROP_VERTEX_INDICES "vertex_indices" | |
14 #define PLY_ELEM_FACE "face" | |
15 #define PLY_ELEM_VERTEX "vertex" | |
16 | |
17 | |
18 enum DMPLYFormat | |
19 { | |
20 PLY_FMT_UNKNOWN, | |
21 PLY_FMT_ASCII, | |
22 PLY_FMT_BIN_LE, | |
23 PLY_FMT_BIN_BE | |
24 }; | |
25 | |
26 | |
27 enum DMPLYPropType | |
28 { | |
29 PLY_TYPE_NONE, | |
30 PLY_TYPE_LIST, | |
31 | |
32 PLY_TYPE_UINT8, | |
33 PLY_TYPE_INT8, | |
34 PLY_TYPE_INT16, | |
35 PLY_TYPE_UINT16, | |
36 PLY_TYPE_INT32, | |
37 PLY_TYPE_UINT32, | |
38 PLY_TYPE_FLOAT, | |
39 PLY_TYPE_DOUBLE | |
40 }; | |
41 | |
42 | |
43 /* Structures | |
44 */ | |
45 union DMPLYPropValue | |
46 { | |
47 double v_double; | |
48 float v_float; | |
49 unsigned int v_uint; | |
50 int v_int; | |
51 }; | |
52 | |
53 | |
54 struct DMPLYFileProperty | |
55 { | |
56 std::string name; | |
57 DMPLYPropType | |
58 type, | |
59 list_num_type, | |
60 list_values_type; | |
61 | |
62 DMPLYPropValue value, list_num_value; | |
63 std::vector<DMPLYPropValue> list_values; | |
64 }; | |
65 | |
66 | |
67 struct DMPLYFileElement | |
68 { | |
69 int value; | |
70 std::string name; | |
71 | |
72 std::unordered_map<std::string, DMPLYFileProperty> prop_map; | |
73 std::vector<DMPLYFileProperty *> properties; | |
74 | |
75 DMPLYFileProperty *checkProp(const std::string &prop) | |
76 { | |
77 if (prop_map.count(prop)) | |
78 return &prop_map[prop]; | |
79 else | |
80 return 0; | |
81 } | |
82 }; | |
83 | |
84 | |
85 struct DMTextFileInfo | |
86 { | |
87 int nline, state; | |
88 std::string filename; | |
89 std::string line; | |
90 std::ifstream file; | |
91 }; | |
92 | |
93 | |
94 struct DMPLYFileInfo : DMTextFileInfo | |
95 { | |
96 DMPLYFormat format; | |
97 | |
98 std::unordered_map<std::string, DMPLYFileElement> elem_map; | |
99 std::vector<DMPLYFileElement *> elements; | |
100 DMPLYFileElement *element; | |
101 | |
102 DMPLYFileInfo() | |
103 { | |
104 element = 0; | |
105 format = PLY_FMT_UNKNOWN; | |
106 } | |
107 | |
108 DMPLYFileElement *checkElem(const std::string &elem) | |
109 { | |
110 if (elem_map.count(elem)) | |
111 return &elem_map[elem]; | |
112 else | |
113 return 0; | |
114 } | |
115 }; | |
116 | |
117 | |
118 struct DMColor | |
119 { | |
120 int r, g, b, alpha; | |
121 }; | |
122 | |
123 | |
124 struct DMVertex | |
125 { | |
126 float x, y, z; | |
127 }; | |
128 | |
129 | |
130 struct DMModel | |
131 { | |
132 int nvertices, nfaces; | |
133 std::vector<DMVertex> vertices, normals; | |
134 std::vector<unsigned int> faces; | |
135 | |
136 DMColor color; | |
137 DMVertex translate, scale, rotate; | |
138 | |
139 unsigned int id_prog, id_fs, id_vs; | |
140 | |
141 bool loadFromPLY(const std::string &filename); | |
142 bool loadFromPLY(const std::string &filename, DMPLYFileInfo &info); | |
143 | |
144 void printInfo() | |
145 { | |
146 printf( | |
147 "MODEL: scale <%1.5f, %1.5f, %1.5f>\n" | |
148 "MODEL: translate <%1.5f, %1.5f, %1.5f>\n" | |
149 "MODEL: rotate <%1.5f, %1.5f, %1.5f>\n" | |
150 , | |
151 scale.x, scale.y, scale.z, | |
152 translate.x, translate.y, translate.z, | |
153 rotate.x, rotate.y, rotate.z); | |
154 } | |
155 | |
156 DMModel() | |
157 { | |
158 translate.x = translate.y = translate.z = 0; | |
159 rotate.x = rotate.y = rotate.z = 0; | |
160 scale.x = scale.y = scale.z = 1; | |
161 } | |
162 }; | |
163 | |
164 | |
165 struct DMLight | |
166 { | |
167 DMVertex pos; | |
168 float ambient[4], diffuse[4], specular[4]; | |
169 }; | |
170 | |
171 | |
172 struct DMCamera | |
173 { | |
174 DMVertex pos, lookAt; | |
175 }; | |
176 | |
177 | |
178 struct DMSimpleScene | |
179 { | |
180 DMModel model; | |
181 DMCamera camera; | |
182 std::vector<DMLight> lights; | |
183 | |
184 bool loadInfo(const std::string &filename); | |
185 }; | |
186 | |
187 #endif |