comparison dmmodel.cpp @ 47:9909014498f0

Add helper functions dmError() and dmMsg() and use them.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 05 Dec 2019 23:52:45 +0200
parents d640f2a34031
children 0ae1ff609626
comparison
equal deleted inserted replaced
46:0c75c5f5c6b6 47:9909014498f0
8 // 8 //
9 #include "dmmodel.h" 9 #include "dmmodel.h"
10 #include <SDL_endian.h> 10 #include <SDL_endian.h>
11 11
12 12
13 static bool dmError(DMTextFileInfo &info, const std::string &msg) 13 static bool dmTextError(DMTextFileInfo &info, const std::string &msg)
14 { 14 {
15 printf("ERROR: %s on line #%d: %s\n", 15 dmError("%s on line #%d: %s\n",
16 msg.c_str(), info.nline, info.line.c_str()); 16 msg.c_str(), info.nline, info.line.c_str());
17 return false; 17 return false;
18 } 18 }
19 19
20 20
21 static bool dmSyntaxError(DMTextFileInfo &info, const std::string &msg) 21 static bool dmSyntaxError(DMTextFileInfo &info, const std::string &msg)
22 { 22 {
23 printf("ERROR: Syntax error on line #%d: %s\n", 23 dmError("Syntax error on line #%d: %s\n",
24 info.nline, msg.c_str()); 24 info.nline, msg.c_str());
25 return false; 25 return false;
26 } 26 }
27 27
28 28
95 case PLY_TYPE_DOUBLE: 95 case PLY_TYPE_DOUBLE:
96 pval.v_double = std::stod(val, &len); 96 pval.v_double = std::stod(val, &len);
97 break; 97 break;
98 98
99 default: 99 default:
100 return dmError(info, 100 return dmTextError(info,
101 "Internal error, unimplemented PLY property type"); 101 "Internal error, unimplemented PLY property type");
102 } 102 }
103 103
104 pos += len; 104 pos += len;
105 105
144 size_t nprop = 0, pos = 0; 144 size_t nprop = 0, pos = 0;
145 145
146 // Read one line 146 // Read one line
147 if (!dmReadLine(info)) 147 if (!dmReadLine(info))
148 { 148 {
149 return dmError(info, 149 return dmTextError(info,
150 "Unexpected end of file"); 150 "Unexpected end of file");
151 } 151 }
152 152
153 // Skip empty lines 153 // Skip empty lines
154 if (info.line.empty()) 154 if (info.line.empty())
223 info.file.read(reinterpret_cast<char *>(&tmpFloat), 4); 223 info.file.read(reinterpret_cast<char *>(&tmpFloat), 4);
224 pval.v_float = (info.format == PLY_FMT_BIN_LE) ? SDL_SwapFloatLE(tmpFloat) : SDL_SwapFloatBE(tmpFloat); 224 pval.v_float = (info.format == PLY_FMT_BIN_LE) ? SDL_SwapFloatLE(tmpFloat) : SDL_SwapFloatBE(tmpFloat);
225 break; 225 break;
226 226
227 default: 227 default:
228 return dmError(info, 228 return dmTextError(info,
229 "Internal error, unimplemented PLY property type"); 229 "Internal error, unimplemented PLY property type");
230 } 230 }
231 231
232 return true; 232 return true;
233 } 233 }
299 { 299 {
300 info.filename = filename; 300 info.filename = filename;
301 info.nline = info.state = 0; 301 info.nline = info.state = 0;
302 info.file.open(info.filename.c_str(), std::fstream::in | std::fstream::binary); 302 info.file.open(info.filename.c_str(), std::fstream::in | std::fstream::binary);
303 303
304 printf("INFO: Trying to read mesh from '%s'.\n", 304 dmMsg("Trying to read mesh from '%s'.\n",
305 info.filename.c_str()); 305 info.filename.c_str());
306 306
307 if (!info.file.is_open()) 307 if (!info.file.is_open())
308 { 308 {
309 printf("ERROR: Unable to open file '%s'.\n", 309 dmError("Unable to open file '%s'.\n",
310 info.filename.c_str()); 310 info.filename.c_str());
311 return false; 311 return false;
312 } 312 }
313 313
314 // Parse the PLY header 314 // Parse the PLY header
356 info.format = PLY_FMT_BIN_BE; 356 info.format = PLY_FMT_BIN_BE;
357 357
358 if (info.format == PLY_FMT_UNKNOWN || 358 if (info.format == PLY_FMT_UNKNOWN ||
359 tokens[2] != "1.0") 359 tokens[2] != "1.0")
360 { 360 {
361 printf("ERROR: Unknown or unsupported PLY file format '%s'.\n", 361 dmError("Unknown or unsupported PLY file format '%s'.\n",
362 (tokens[1] +" "+ tokens[2]).c_str()); 362 (tokens[1] +" "+ tokens[2]).c_str());
363 return false; 363 return false;
364 } 364 }
365 365
366 info.state = 2; 366 info.state = 2;
478 (prop = elem->checkProp("x")) == 0 || prop->type != PLY_TYPE_FLOAT || 478 (prop = elem->checkProp("x")) == 0 || prop->type != PLY_TYPE_FLOAT ||
479 (prop = elem->checkProp("y")) == 0 || prop->type != PLY_TYPE_FLOAT || 479 (prop = elem->checkProp("y")) == 0 || prop->type != PLY_TYPE_FLOAT ||
480 (prop = elem->checkProp("z")) == 0 || prop->type != PLY_TYPE_FLOAT 480 (prop = elem->checkProp("z")) == 0 || prop->type != PLY_TYPE_FLOAT
481 ) 481 )
482 { 482 {
483 printf("ERROR: PLY file did not contain expected information.\n"); 483 dmError("PLY file did not contain expected information.\n");
484 return false; 484 return false;
485 } 485 }
486 486
487 nvertices = info.elem_map[PLY_ELEM_VERTEX].value; 487 nvertices = info.elem_map[PLY_ELEM_VERTEX].value;
488 nfaces = info.elem_map[PLY_ELEM_FACE].value; 488 nfaces = info.elem_map[PLY_ELEM_FACE].value;
489 489
490 490
491 if (nvertices < 3 || nfaces < 1) 491 if (nvertices < 3 || nfaces < 1)
492 { 492 {
493 printf("ERROR: Invalid nvertices (%d) and/or nfaces (%d).\n", 493 dmError("Invalid nvertices (%d) and/or nfaces (%d).\n",
494 nvertices, nfaces); 494 nvertices, nfaces);
495 return false; 495 return false;
496 } 496 }
497 497
498 printf("INFO: Should have %d vertices, %d faces\n", 498 dmMsg("Should have %d vertices, %d faces\n",
499 nvertices, nfaces); 499 nvertices, nfaces);
500 500
501 // Pre-allocate space 501 // Pre-allocate space
502 vertices.reserve(nvertices); 502 vertices.reserve(nvertices);
503 normals.reserve(nvertices); 503 normals.reserve(nvertices);
558 normals.push_back(normal); 558 normals.push_back(normal);
559 } 559 }
560 } 560 }
561 } 561 }
562 562
563 printf("INFO: Found %ld vertices, %ld normals, %ld faces\n", 563 dmMsg("Found %ld vertices, %ld normals, %ld faces\n",
564 vertices.size(), 564 vertices.size(),
565 normals.size(), 565 normals.size(),
566 faces.size() / 3); 566 faces.size() / 3);
567 567
568 return true; 568 return true;
627 627
628 info.filename = filename; 628 info.filename = filename;
629 info.nline = info.state = 0; 629 info.nline = info.state = 0;
630 info.file.open(info.filename.c_str(), std::fstream::in | std::fstream::binary); 630 info.file.open(info.filename.c_str(), std::fstream::in | std::fstream::binary);
631 631
632 printf("INFO: Trying to read scene data from '%s'.\n", 632 dmMsg("Trying to read scene data from '%s'.\n",
633 info.filename.c_str()); 633 info.filename.c_str());
634 634
635 if (!info.file.is_open()) 635 if (!info.file.is_open())
636 { 636 {
637 printf("ERROR: Unable to open file '%s'.\n", 637 dmError("Unable to open file '%s'.\n",
638 info.filename.c_str()); 638 info.filename.c_str());
639 return false; 639 return false;
640 } 640 }
641 641
642 while (info.state >= 0) 642 while (info.state >= 0)
753 { 753 {
754 DMLight newlight; 754 DMLight newlight;
755 755
756 if (lights.size() >= 4) 756 if (lights.size() >= 4)
757 { 757 {
758 return dmError(info, 758 return dmTextError(info,
759 "Too many lights defined (max 4)"); 759 "Too many lights defined (max 4)");
760 } 760 }
761 761
762 lights.push_back(newlight); 762 lights.push_back(newlight);
763 light = &lights.back(); 763 light = &lights.back();