@@ -12,16 +12,108 @@ MeshSet::~MeshSet()
12
12
for (auto m : meshes) delete m;
13
13
}
14
14
15
+ int MeshSet::get (std::string name)
16
+ {
17
+ auto id = names.find (name);
18
+ if (id != names.end ())
19
+ return id->second ;
20
+ else
21
+ throw std::runtime_error (" Model not loaded.\n " );
22
+ }
23
+
24
+ int MeshSet::add (std::string name)
25
+ {
26
+ int id = meshes.size ();
27
+ names[name] = id;
28
+ return id;
29
+ }
30
+
15
31
int MeshSet::loadMesh (const char * fileName)
16
32
{
17
- size_t id = meshes. size ( );
33
+ int id = add (fileName );
18
34
loadOBJ (fileName);
19
35
return id;
20
36
}
21
37
22
- int MeshSet::generateTiledQuads ( size_t tileCount )
38
+ int MeshSet::loadOBJ ( const char * fileName )
23
39
{
24
- size_t id = meshes.size ();
40
+ std::ifstream file (fileName, std::ifstream::in | std::ifstream::binary);
41
+ if (!file.is_open ())
42
+ {
43
+ printf (" OBJ file could not be opened\n " );
44
+ return 0 ;
45
+ }
46
+
47
+ std::vector<unsigned int > vertexIndices, uvIndices, normalIndices;
48
+ std::vector<glm::vec3> temp_vertices;
49
+ std::vector<glm::vec2> temp_uvs;
50
+ std::vector<glm::vec3> temp_normals;
51
+
52
+ while (!file.eof ())
53
+ {
54
+ char line[128 ];
55
+ file.getline (line, 128 );
56
+
57
+ glm::vec3 vertex;
58
+ if (sscanf (line, " v %f %f %f\n " , &vertex.x , &vertex.y , &vertex.z ) == 3 )
59
+ temp_vertices.push_back (vertex);
60
+
61
+ glm::vec2 uv;
62
+ if (sscanf (line, " vt %f %f\n " , &uv.x , &uv.y ) == 2 )
63
+ {
64
+ uv.y = -uv.y ; // Invert V coordinate since we will only use DDS texture, which are inverted. Remove if you want to use TGA or BMP loaders.
65
+ temp_uvs.push_back (uv);
66
+ }
67
+
68
+ glm::vec3 normal ;
69
+ if (sscanf (line, " vn %f %f %f\n " , &normal .x , &normal .y , &normal .z ) == 3 )
70
+ temp_normals.push_back (normal );
71
+
72
+ std::string vertex1, vertex2, vertex3;
73
+ unsigned int vIndex[3 ], uvIndex[3 ], nIndex[3 ];
74
+ if (sscanf (line, " f %d/%d/%d %d/%d/%d %d/%d/%d\n " , &vIndex[0 ], &uvIndex[0 ], &nIndex[0 ], &vIndex[1 ], &uvIndex[1 ], &nIndex[1 ], &vIndex[2 ], &uvIndex[2 ], &nIndex[2 ]) == 9 )
75
+ {
76
+ vertexIndices.push_back (vIndex[0 ]);
77
+ vertexIndices.push_back (vIndex[1 ]);
78
+ vertexIndices.push_back (vIndex[2 ]);
79
+ uvIndices.push_back (uvIndex[0 ]);
80
+ uvIndices.push_back (uvIndex[1 ]);
81
+ uvIndices.push_back (uvIndex[2 ]);
82
+ normalIndices.push_back (nIndex[0 ]);
83
+ normalIndices.push_back (nIndex[1 ]);
84
+ normalIndices.push_back (nIndex[2 ]);
85
+ }
86
+ }
87
+
88
+ Mesh* M = new Mesh;
89
+
90
+ // where this mesh data starts in the global pool
91
+ M->vertexOffset = vertices.size ();
92
+
93
+ for (unsigned int i = 0 ; i < vertexIndices.size (); i++)
94
+ {
95
+ size_t vertexIndex = vertexIndices[i];
96
+ size_t uvIndex = uvIndices[i];
97
+ size_t normalIndex = normalIndices[i];
98
+
99
+ glm::vec3 vertex = temp_vertices[vertexIndex - 1 ];
100
+ glm::vec2 uv = temp_uvs[uvIndex - 1 ];
101
+ glm::vec3 normal = temp_normals[normalIndex - 1 ];
102
+
103
+ vertices.push_back (vertex);
104
+ uvs.push_back (uv);
105
+ normals.push_back (normal );
106
+ }
107
+
108
+ // this mesh data vertex count
109
+ M->vertexCount = vertices.size () - M->vertexOffset ;
110
+ meshes.push_back (M);
111
+ return 0 ;
112
+ }
113
+
114
+ int MeshSet::generateTiledQuads (std::string name, size_t tileCount)
115
+ {
116
+ int id = add (name);
25
117
size_t currentOffset = vertices.size ();
26
118
27
119
glm::vec3 quadVertices[6 ] = {
@@ -71,9 +163,9 @@ int MeshSet::generateTiledQuads(size_t tileCount)
71
163
return id;
72
164
}
73
165
74
- int MeshSet::generateSlideMeshes (std::vector<int > deltas)
166
+ int MeshSet::generateSlideMeshes (std::string name, std:: vector<int >& deltas)
75
167
{
76
- size_t id = meshes. size ( );
168
+ int id = add (name );
77
169
size_t currentOffset = vertices.size ();
78
170
79
171
glm::vec3 quadNormals[6 ] = {
@@ -143,80 +235,4 @@ void MeshSet::draw(size_t id)
143
235
glDrawArrays (GL_TRIANGLES, meshes[id]->vertexOffset , meshes[id]->vertexCount );
144
236
}
145
237
146
- int MeshSet::loadOBJ (const char * fileName)
147
- {
148
- std::ifstream file (fileName, std::ifstream::in | std::ifstream::binary);
149
- if (!file.is_open ())
150
- {
151
- printf (" OBJ file could not be opened\n " );
152
- return 0 ;
153
- }
154
-
155
- std::vector<unsigned int > vertexIndices, uvIndices, normalIndices;
156
- std::vector<glm::vec3> temp_vertices;
157
- std::vector<glm::vec2> temp_uvs;
158
- std::vector<glm::vec3> temp_normals;
159
-
160
- while (!file.eof ())
161
- {
162
- char line[128 ];
163
- file.getline (line, 128 );
164
-
165
- glm::vec3 vertex;
166
- if (sscanf (line, " v %f %f %f\n " , &vertex.x , &vertex.y , &vertex.z ) == 3 )
167
- temp_vertices.push_back (vertex);
168
-
169
- glm::vec2 uv;
170
- if (sscanf (line, " vt %f %f\n " , &uv.x , &uv.y ) == 2 )
171
- {
172
- uv.y = -uv.y ; // Invert V coordinate since we will only use DDS texture, which are inverted. Remove if you want to use TGA or BMP loaders.
173
- temp_uvs.push_back (uv);
174
- }
175
-
176
- glm::vec3 normal ;
177
- if (sscanf (line, " vn %f %f %f\n " , &normal .x , &normal .y , &normal .z ) == 3 )
178
- temp_normals.push_back (normal );
179
-
180
- std::string vertex1, vertex2, vertex3;
181
- unsigned int vIndex[3 ], uvIndex[3 ], nIndex[3 ];
182
- if (sscanf (line, " f %d/%d/%d %d/%d/%d %d/%d/%d\n " , &vIndex[0 ], &uvIndex[0 ], &nIndex[0 ], &vIndex[1 ], &uvIndex[1 ], &nIndex[1 ], &vIndex[2 ], &uvIndex[2 ], &nIndex[2 ]) == 9 )
183
- {
184
- vertexIndices.push_back (vIndex[0 ]);
185
- vertexIndices.push_back (vIndex[1 ]);
186
- vertexIndices.push_back (vIndex[2 ]);
187
- uvIndices.push_back (uvIndex[0 ]);
188
- uvIndices.push_back (uvIndex[1 ]);
189
- uvIndices.push_back (uvIndex[2 ]);
190
- normalIndices.push_back (nIndex[0 ]);
191
- normalIndices.push_back (nIndex[1 ]);
192
- normalIndices.push_back (nIndex[2 ]);
193
- }
194
- }
195
-
196
- Mesh* M = new Mesh;
197
-
198
- // where this mesh data starts in the global pool
199
- M->vertexOffset = vertices.size ();
200
-
201
- for (unsigned int i = 0 ; i < vertexIndices.size (); i++)
202
- {
203
- size_t vertexIndex = vertexIndices[i];
204
- size_t uvIndex = uvIndices[i];
205
- size_t normalIndex = normalIndices[i];
206
-
207
- glm::vec3 vertex = temp_vertices[vertexIndex - 1 ];
208
- glm::vec2 uv = temp_uvs[uvIndex - 1 ];
209
- glm::vec3 normal = temp_normals[normalIndex - 1 ];
210
-
211
- vertices.push_back (vertex);
212
- uvs.push_back (uv);
213
- normals.push_back (normal );
214
- }
215
-
216
- // this mesh data vertex count
217
- M->vertexCount = vertices.size () - M->vertexOffset ;
218
- meshes.push_back (M);
219
- return 0 ;
220
- }
221
-
222
238
0 commit comments