Thu, 21 Mar 2013 16:25:03 +0200
Parsing stability, finally figured that dumb crash
bbox.cpp | file | annotate | diff | comparison | revisions | |
bbox.h | file | annotate | diff | comparison | revisions | |
file.cpp | file | annotate | diff | comparison | revisions | |
gldraw.cpp | file | annotate | diff | comparison | revisions | |
gldraw.h | file | annotate | diff | comparison | revisions | |
misc.cpp | file | annotate | diff | comparison | revisions | |
misc.h | file | annotate | diff | comparison | revisions | |
str.cpp | file | annotate | diff | comparison | revisions | |
str.h | file | annotate | diff | comparison | revisions |
--- a/bbox.cpp Thu Mar 21 13:00:44 2013 +0200 +++ b/bbox.cpp Thu Mar 21 16:25:03 2013 +0200 @@ -21,13 +21,19 @@ #include "ldtypes.h" #include "file.h" +#define CHECK_DIMENSION(V,X) \ + if (V.X < v0.X) v0.X = V.X; \ + if (V.X > v1.X) v1.X = V.X; + +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= void bbox::calculate () { + reset (); + if (!g_CurrentFile) return; - // The bounding box, bbox for short, is the - // box that encompasses the model we have open. - // v0 is the minimum vertex, v1 is the maximum vertex. for (uint i = 0; i < g_CurrentFile->objects.size(); i++) { LDObject* obj = g_CurrentFile->objects[i]; switch (obj->getType ()) { @@ -70,17 +76,44 @@ } } -#define CHECK_DIMENSION(V,X) \ - if (V.X < v0.X) v0.X = V.X; \ - if (V.X > v1.X) v1.X = V.X; +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= void bbox::checkVertex (vertex v) { CHECK_DIMENSION (v, x) CHECK_DIMENSION (v, y) CHECK_DIMENSION (v, z) } -#undef CHECK_DIMENSION +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= bbox::bbox () { + reset (); +} + +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= +void bbox::reset () { memset (&v0, 0, sizeof v0); memset (&v1, 0, sizeof v1); -} \ No newline at end of file +} + +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= +double bbox::calcSize () { + double fXScale = (v0.x - v1.x); + double fYScale = (v0.y - v1.y); + double fZScale = (v0.z - v1.z); + double* fpSize = &fZScale; + + if (fXScale > fYScale) { + if (fXScale > fZScale) + fpSize = &fXScale; + } else if (fYScale > fZScale) + fpSize = &fYScale; + + return (*fpSize) / 2; +}
--- a/bbox.h Thu Mar 21 13:00:44 2013 +0200 +++ b/bbox.h Thu Mar 21 16:25:03 2013 +0200 @@ -21,13 +21,22 @@ #include "common.h" +// ============================================================================= +// The bounding box, bbox for short, is the +// box that encompasses the model we have open. +// v0 is the minimum vertex, v1 is the maximum vertex. +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= class bbox { public: - vertex v0; - vertex v1; + vertex v0, v1; bbox (); + + void reset (); void calculate (); + double calcSize (); private: void checkVertex (vertex v);
--- a/file.cpp Thu Mar 21 13:00:44 2013 +0200 +++ b/file.cpp Thu Mar 21 16:25:03 2013 +0200 @@ -168,6 +168,10 @@ closeAll (); OpenFile* pFile = openDATFile (zPath); + + if (!pFile) + return; + g_CurrentFile = pFile; // Recalculate the bounding box @@ -215,27 +219,31 @@ // ============================================================================= // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= -LDObject* parseLine (str zLine) { - printf ("%s\n", zLine.chars()); - +static vertex parseVertex (vector<str>& s, const ushort n) { // Disable the locale while parsing the line or atof's behavior changes // between locales (i.e. fails to read decimals properly). That is // quite undesired... setlocale (LC_NUMERIC, "C"); - str zNoWhitespace = zLine; - stripWhitespace (zNoWhitespace); - if (!~zNoWhitespace) { + vertex v; + v.x = atof (s[n]); + v.y = atof (s[n + 1]); + v.z = atof (s[n + 2]); + + return v; +} + +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= +LDObject* parseLine (str zLine) { + vector<str> tokens = zLine.split (" ", true); + + if (!tokens.size ()) { // Line was empty, or only consisted of whitespace return new LDEmpty; } - vector<str> tokens = zLine / " "; - - // Rid leading all-whitespace tokens - while (tokens.size() && !(~tokens[0])) - tokens.erase (tokens.begin()); - if (~tokens[0] != 1) return new LDGibberish (zLine, "Illogical line code"); @@ -246,7 +254,7 @@ // Comment str zComment = zLine.substr (2, -1); - if (tokens[1] == "!LDFORGE") { + if (tokens.size() > 2 && tokens[1] == "!LDFORGE") { // Handle LDForge-specific types, they're embedded into comments if (tokens[2] == "VERTEX") { @@ -263,7 +271,6 @@ return obj; } } - LDComment* obj = new LDComment; obj->zText = zComment; @@ -288,8 +295,8 @@ return new LDGibberish (zLine, "Could not open referred file"); LDSubfile* obj = new LDSubfile; - obj->dColor = atoi (tokens[1]); - obj->vPosition = parseVertex (zLine, 2); // 2 - 4 + obj->dColor = atol (tokens[1]); + obj->vPosition = parseVertex (tokens, 2); // 2 - 4 for (short i = 0; i < 9; ++i) obj->faMatrix[i] = atof (tokens[i + 5]); // 5 - 13 @@ -306,9 +313,9 @@ // Line LDLine* obj = new LDLine; - obj->dColor = getWordInt (zLine, 1); + obj->dColor = atol (tokens[1]); for (short i = 0; i < 2; ++i) - obj->vaCoords[i] = parseVertex (zLine, 2 + (i * 3)); // 2 - 7 + obj->vaCoords[i] = parseVertex (tokens, 2 + (i * 3)); // 2 - 7 return obj; } @@ -319,10 +326,10 @@ // Triangle LDTriangle* obj = new LDTriangle; - obj->dColor = getWordInt (zLine, 1); + obj->dColor = atol (tokens[1]); for (short i = 0; i < 3; ++i) - obj->vaCoords[i] = parseVertex (zLine, 2 + (i * 3)); // 2 - 10 + obj->vaCoords[i] = parseVertex (tokens, 2 + (i * 3)); // 2 - 10 return obj; } @@ -334,10 +341,10 @@ // Quadrilateral LDQuad* obj = new LDQuad; - obj->dColor = getWordInt (zLine, 1); + obj->dColor = atol (tokens[1]); for (short i = 0; i < 4; ++i) - obj->vaCoords[i] = parseVertex (zLine, 2 + (i * 3)); // 2 - 13 + obj->vaCoords[i] = parseVertex (tokens, 2 + (i * 3)); // 2 - 13 return obj; } @@ -349,18 +356,16 @@ // Conditional line LDCondLine* obj = new LDCondLine; - obj->dColor = getWordInt (zLine, 1); + obj->dColor = atol (tokens[1]); for (short i = 0; i < 4; ++i) - obj->vaCoords[i] = parseVertex (zLine, 2 + (i * 3)); // 2 - 13 + obj->vaCoords[i] = parseVertex (tokens, 2 + (i * 3)); // 2 - 13 return obj; } default: // Strange line we couldn't parse return new LDGibberish (zLine, "Unknown line code number"); } - - setlocale (LC_NUMERIC, ""); } // =============================================================================
--- a/gldraw.cpp Thu Mar 21 13:00:44 2013 +0200 +++ b/gldraw.cpp Thu Mar 21 16:25:03 2013 +0200 @@ -25,18 +25,16 @@ #include "bbox.h" #include "colors.h" -#define GL_VERTEX(V) glVertex3d (V.x + g_faObjectOffset[0], \ - -(V.y + g_faObjectOffset[1]), V.z + g_faObjectOffset[2]); - -double g_faObjectOffset[3]; +static double g_faObjectOffset[3]; +static double g_StoredBBoxSize; // ============================================================================= // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= renderer::renderer (QWidget* parent) { parent = parent; // shhh, GCC - fRotX = fRotY = fRotZ = 0.0; - fZoom = 1.0; + fRotX = fRotY = fRotZ = 0.0f; + fZoom = 1.0f; } // ============================================================================= @@ -144,12 +142,6 @@ glPushMatrix (); glLoadIdentity (); - glTranslatef ( - (g_BBox.v0.x + g_BBox.v1.x) / -2.0, - (g_BBox.v0.y + g_BBox.v1.y) / -2.0, - (g_BBox.v0.z + g_BBox.v1.z) / -2.0 - ); - glTranslatef (0.0f, 0.0f, -5.0f); glTranslatef (0.0f, 0.0f, -fZoom); @@ -176,10 +168,11 @@ // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= void renderer::compileObjects () { - g_faObjectOffset[0] = -(g_BBox.v0.x + g_BBox.v1.x) / 2; g_faObjectOffset[1] = -(g_BBox.v0.y + g_BBox.v1.y) / 2; g_faObjectOffset[2] = -(g_BBox.v0.z + g_BBox.v1.z) / 2; + g_StoredBBoxSize = g_BBox.calcSize (); + printf ("bbox size is %f\n", g_StoredBBoxSize); if (!g_CurrentFile) { printf ("renderer: no files loaded, cannot compile anything\n"); @@ -221,7 +214,7 @@ LDLine* line = static_cast<LDLine*> (obj); glBegin (GL_LINES); for (short i = 0; i < 2; ++i) - GL_VERTEX (line->vaCoords[i]) + compileVertex (line->vaCoords[i]); glEnd (); } break; @@ -236,7 +229,7 @@ glBegin (GL_LINES); for (short i = 0; i < 2; ++i) - GL_VERTEX (line->vaCoords[i]) + compileVertex (line->vaCoords[i]); glEnd (); glDisable (GL_LINE_STIPPLE); @@ -249,7 +242,7 @@ setObjectColor (obj, bBack); glBegin (GL_TRIANGLES); for (short i = 0; i < 3; ++i) - GL_VERTEX (tri->vaCoords[i]) + compileVertex (tri->vaCoords[i]); glEnd (); } break; @@ -260,7 +253,7 @@ setObjectColor (obj, bBack); glBegin (GL_QUADS); for (short i = 0; i < 4; ++i) - GL_VERTEX (quad->vaCoords[i]) + compileVertex (quad->vaCoords[i]); glEnd (); } break; @@ -273,6 +266,16 @@ // ============================================================================= // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= +void renderer::compileVertex (vertex& vrt) { + glVertex3d ( + (vrt.x + g_faObjectOffset[0]) / g_StoredBBoxSize, + -(vrt.y + g_faObjectOffset[1]) / g_StoredBBoxSize, + (vrt.z + g_faObjectOffset[2]) / g_StoredBBoxSize); +} + +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= void renderer::clampAngle (double& fAngle) { while (fAngle < 0) fAngle += 360.0;
--- a/gldraw.h Thu Mar 21 13:00:44 2013 +0200 +++ b/gldraw.h Thu Mar 21 16:25:03 2013 +0200 @@ -46,6 +46,7 @@ private: GLuint uObjList, uObjListBack; void compileOneObject (LDObject* obj, bool bBack); + void compileVertex (vertex& vrt); void clampAngle (double& fAngle); void setObjectColor (LDObject* obj, bool bBack); };
--- a/misc.cpp Thu Mar 21 13:00:44 2013 +0200 +++ b/misc.cpp Thu Mar 21 16:25:03 2013 +0200 @@ -20,26 +20,6 @@ #include <locale.h> #include "common.h" -double getWordFloat (str& s, const ushort n) { - return atof ((s / " ")[n]); -} - -long getWordInt (str& s, const ushort n) { - return atol ((s / " ")[n]); -} - -// ============================================================================= -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -// ============================================================================= -vertex parseVertex (str& s, const ushort n) { - vertex v; - v.x = getWordFloat (s, n); - v.y = getWordFloat (s, n + 1); - v.z = getWordFloat (s, n + 2); - - return v; -} - // ============================================================================= // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // =============================================================================
--- a/misc.h Thu Mar 21 13:00:44 2013 +0200 +++ b/misc.h Thu Mar 21 16:25:03 2013 +0200 @@ -26,9 +26,6 @@ return (zString / " ")[ulIndex]; } -double getWordFloat (str& s, const ushort n); -long getWordInt (str& s, const ushort n); -vertex parseVertex (str& s, const ushort n); void stripWhitespace (str& s); // Returns whether a given string represents a floating point number
--- a/str.cpp Thu Mar 21 13:00:44 2013 +0200 +++ b/str.cpp Thu Mar 21 16:25:03 2013 +0200 @@ -473,7 +473,7 @@ } // ============================================================================ -std::vector<str> str::split (str del) { +std::vector<str> str::split (str del, bool bNoBlanks) { std::vector<str> res; unsigned int a = 0; @@ -484,12 +484,15 @@ if (b == -1) break; - res.push_back (substr (a, b)); + if (!bNoBlanks || (b - a)) + res.push_back (substr (a, b)); + a = b + strlen (del); } // Add the string at the right of the last separator - res.push_back (substr (a, len())); + if (!bNoBlanks || (len () - a)) + res.push_back (substr (a, len ())); return res; }