comparison glxdragon.cpp @ 14:62be2036f604

Read mesh vertices/faces information from "<modelfilename>.info" instead of having the values hardcoded.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 29 Oct 2019 12:48:36 +0200
parents c1e8057cc4d0
children 2d2aadfa3df3
comparison
equal deleted inserted replaced
13:c1e8057cc4d0 14:62be2036f604
288 288
289 return true; 289 return true;
290 } 290 }
291 291
292 292
293 bool dmLoadMesh(const std::string &filename, Mesh &mesh, int nvertices, int nfaces) 293 bool dmLoadMesh(const std::string &filename, const std::string &infofilename, Mesh &mesh)
294 { 294 {
295 std::ifstream info(infofilename.c_str());
296 int required = 0, nline = 0;
297
298 printf("INFO: Trying to read mesh meta from '%s'.\n",
299 infofilename.c_str());
300
301 if (!info.is_open())
302 {
303 printf("ERROR: Unable to open file '%s'.\n",
304 infofilename.c_str());
305 return false;
306 }
307
308 while (required != 0x03)
309 {
310 std::string tmp;
311 int value;
312 char key;
313
314 // Read one line
315 if (!std::getline(info, tmp))
316 {
317 printf("ERROR: Could not read from file '%s'.\n",
318 infofilename.c_str());
319 return false;
320 }
321
322 nline++;
323
324 // Skip empty lines and comments
325 if (tmp.empty() || tmp.substr(0, 1) == "#")
326 continue;
327
328 if (sscanf(tmp.c_str(), "%c:%d", &key, &value) != 2)
329 {
330 printf("ERROR: Syntax error in '%s' line #%d\n'%s'\n",
331 infofilename.c_str(),
332 nline,
333 tmp.c_str());
334 return false;
335 }
336
337 switch (key)
338 {
339 case 'v':
340 mesh.nvertices = value;
341 required |= 0x01;
342 break;
343
344 case 'f':
345 mesh.nfaces = value;
346 required |= 0x02;
347 break;
348
349 default:
350 printf("ERROR: Syntax error in '%s' line #%d\nUnknown key value '%c'.\n",
351 infofilename.c_str(),
352 nline,
353 key);
354 break;
355 }
356 }
357
358 if (mesh.nvertices < 3 || mesh.nfaces < 1)
359 {
360 printf("ERROR: Invalid nvertices (%d) and/or nfaces (%d) in '%s'.\n",
361 mesh.nvertices, mesh.nfaces,
362 infofilename.c_str());
363 return false;
364 }
365
366 printf("INFO: %d vertices, %d faces\n", mesh.nvertices, mesh.nfaces);
367 printf("INFO: Trying to read mesh data from '%s'.\n",
368 filename.c_str());
369
295 std::ifstream in(filename.c_str(), std::ios::binary); 370 std::ifstream in(filename.c_str(), std::ios::binary);
296 371
297 if (!in.is_open()) 372 if (!in.is_open())
298 { 373 {
299 printf("ERROR: Unable to open file '%s'.\n", 374 printf("ERROR: Unable to open file '%s'.\n",
300 filename.c_str()); 375 filename.c_str());
301 return false; 376 return false;
302 } 377 }
303 378
304 mesh.nvertices = nvertices;
305 mesh.vertices.resize(mesh.nvertices * 6); 379 mesh.vertices.resize(mesh.nvertices * 6);
306 in.read(reinterpret_cast<char*>(&mesh.vertices[0]), mesh.nvertices * 6 * 4); 380 in.read(reinterpret_cast<char*>(&mesh.vertices[0]), mesh.nvertices * 6 * 4);
307 381
308 mesh.nfaces = nfaces;
309 mesh.faces.resize(mesh.nfaces * 3); 382 mesh.faces.resize(mesh.nfaces * 3);
310 383
311 for (int i = 0; i < mesh.nfaces; i++) 384 for (int i = 0; i < mesh.nfaces; i++)
312 { 385 {
313 in.seekg(1, std::ios::cur); 386 in.seekg(1, std::ios::cur);
427 { 500 {
428 printf("Model file prefix empty.\n"); 501 printf("Model file prefix empty.\n");
429 goto exit; 502 goto exit;
430 } 503 }
431 504
432 if (!dmLoadMesh(optModelPrefix + ".mesh", modelMesh, 100139, 200198)) 505 if (!dmLoadMesh(optModelPrefix + ".mesh", optModelPrefix + ".info", modelMesh))
433 goto exit; 506 goto exit;
434 507
435 if (optUseShaders) 508 if (optUseShaders)
436 { 509 {
437 // Read shader files 510 // Read shader files