# HG changeset patch # User Santeri Piippo # Date 1364079959 -7200 # Node ID 915fc477cb6ac31f3ed2dab4c3cb9a98a2b5b0f7 # Parent 109b07334fa0fe35db18c8138f2765043e4353d0 Got inlining working. 3002.dat renders properly now! Now just to iron out the bugs and hone the behavior.. diff -r 109b07334fa0 -r 915fc477cb6a bbox.cpp --- a/bbox.cpp Thu Mar 21 18:26:57 2013 +0200 +++ b/bbox.cpp Sun Mar 24 01:05:59 2013 +0200 @@ -115,7 +115,8 @@ } else if (fYScale > fZScale) fSize = fYScale; - if (fSize >= 2.0f) - return (fSize / 2); + printf ("fsize: %f\n", fSize); + if (abs (fSize) >= 2.0f) + return abs (fSize / 2); return 1.0f; } diff -r 109b07334fa0 -r 915fc477cb6a common.h --- a/common.h Thu Mar 21 18:26:57 2013 +0200 +++ b/common.h Sun Mar 24 01:05:59 2013 +0200 @@ -78,10 +78,18 @@ public: double x, y, z; + vertex () {} + vertex (double fX, double fY, double fZ) { + x = fX; + y = fY; + z = fZ; + } + // ========================================================================= // Midpoint between this vertex and another vertex. vertex midpoint (vertex& other); str getStringRep (const bool bMangled); + void transform (double* matrix, vertex pos); }; // ============================================================================= @@ -124,6 +132,9 @@ LOG_Error, }; +// Vertex at (0, 0, 0) +extern const vertex g_Origin; + void logf (const char* fmt, ...) FORMAT_PRINTF (1, 2); void logf (logtype_e eType, const char* fmt, ...) FORMAT_PRINTF (2, 3); diff -r 109b07334fa0 -r 915fc477cb6a gldraw.cpp --- a/gldraw.cpp Thu Mar 21 18:26:57 2013 +0200 +++ b/gldraw.cpp Sun Mar 24 01:05:59 2013 +0200 @@ -84,12 +84,12 @@ 1.0f); } -void renderer::setObjectColor (LDObject* obj, bool bBack) { +void renderer::setObjectColor (LDObject* obj, bool bBackSide) { if (gl_colorbfc && obj->getType () != OBJ_Line && obj->getType () != OBJ_CondLine) { - if (bBack) + if (bBackSide) glColor4f (0.9f, 0.0f, 0.0f, 1.0f); else glColor4f (0.0f, 0.8f, 0.0f, 1.0f); @@ -201,60 +201,56 @@ // ============================================================================= // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= -void renderer::compileOneObject (LDObject* obj, bool bBack) { +templatevoid renderer::compileSubObject (LDObject* obj, + const bool bBackSide, const GLenum eGLType, const short dVerts) +{ + setObjectColor (obj, bBackSide); + T* newobj = static_cast (obj); + glBegin (eGLType); + + for (short i = 0; i < dVerts; ++i) + compileVertex (newobj->vaCoords[i]); + + glEnd (); +} + +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= +void renderer::compileOneObject (LDObject* obj, bool bBackSide) { if (!obj) return; switch (obj->getType ()) { case OBJ_Line: - { - setObjectColor (obj, bBack); - - // draw lines - LDLine* line = static_cast (obj); - glBegin (GL_LINES); - for (short i = 0; i < 2; ++i) - compileVertex (line->vaCoords[i]); - glEnd (); - } + compileSubObject (obj, bBackSide, GL_LINES, 2); break; case OBJ_CondLine: - { - glLineStipple (1, 0x6666); - glEnable (GL_LINE_STIPPLE); - - setObjectColor (obj, bBack); - LDCondLine* line = static_cast (obj); - - glBegin (GL_LINES); - for (short i = 0; i < 2; ++i) - compileVertex (line->vaCoords[i]); - glEnd (); - - glDisable (GL_LINE_STIPPLE); - } + glLineStipple (1, 0x6666); + glEnable (GL_LINE_STIPPLE); + + compileSubObject (obj, bBackSide, GL_LINES, 2); + + glDisable (GL_LINE_STIPPLE); break; case OBJ_Triangle: - { - LDTriangle* tri = static_cast (obj); - setObjectColor (obj, bBack); - glBegin (GL_TRIANGLES); - for (short i = 0; i < 3; ++i) - compileVertex (tri->vaCoords[i]); - glEnd (); - } + compileSubObject (obj, bBackSide, GL_TRIANGLES, 3); + break; + + case OBJ_Quad: + compileSubObject (obj, bBackSide, GL_QUADS, 4); break; - - case OBJ_Quad: + + case OBJ_Subfile: { - LDQuad* quad = static_cast (obj); - setObjectColor (obj, bBack); - glBegin (GL_QUADS); - for (short i = 0; i < 4; ++i) - compileVertex (quad->vaCoords[i]); - glEnd (); + LDSubfile* ref = static_cast (obj); + + vector objs = ref->inlineContents (ref->faMatrix, ref->vPosition, true); + + for (ulong i = 0; i < (ulong)objs.size(); ++i) + compileOneObject (objs[i], bBackSide); } break; @@ -270,7 +266,7 @@ glVertex3d ( (vrt.x + g_faObjectOffset[0]) / g_StoredBBoxSize, -(vrt.y + g_faObjectOffset[1]) / g_StoredBBoxSize, - (vrt.z + g_faObjectOffset[2]) / g_StoredBBoxSize); + -(vrt.z + g_faObjectOffset[2]) / g_StoredBBoxSize); } // ============================================================================= diff -r 109b07334fa0 -r 915fc477cb6a gldraw.h --- a/gldraw.h Thu Mar 21 18:26:57 2013 +0200 +++ b/gldraw.h Sun Mar 24 01:05:59 2013 +0200 @@ -45,10 +45,12 @@ private: GLuint uObjList, uObjListBack; - void compileOneObject (LDObject* obj, bool bBack); + void compileOneObject (LDObject* obj, bool bBackSide); + template void compileSubObject (LDObject* obj, const bool bBackSide, + const GLenum eGLType, const short dVerts); void compileVertex (vertex& vrt); void clampAngle (double& fAngle); - void setObjectColor (LDObject* obj, bool bBack); + void setObjectColor (LDObject* obj, bool bBackSide); }; #endif // __GLDRAW_H__ \ No newline at end of file diff -r 109b07334fa0 -r 915fc477cb6a ldtypes.cpp --- a/ldtypes.cpp Thu Mar 21 18:26:57 2013 +0200 +++ b/ldtypes.cpp Sun Mar 24 01:05:59 2013 +0200 @@ -106,6 +106,9 @@ commonInit (); } +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= ulong LDObject::getIndex () { if (!g_CurrentFile) return -1u; @@ -183,6 +186,9 @@ return str (); } +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= void LDQuad::splitToTriangles () { // Find the index of this quad ulong ulIndex; @@ -227,6 +233,9 @@ delete this; } +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= void LDObject::replace (LDObject* replacement) { // Replace all instances of the old object with the new object for (ulong i = 0; i < g_CurrentFile->objects.size(); ++i) { @@ -254,4 +263,72 @@ LDQuad::~LDQuad () {} LDSubfile::~LDSubfile () {} LDTriangle::~LDTriangle () {} -LDVertex::~LDVertex () {} \ No newline at end of file +LDVertex::~LDVertex () {} + +#define ADD_TYPE(T,N) \ + case OBJ_##T: \ + { \ + LD##T* newobj = static_cast (obj)->makeClone (); \ + for (short i = 0; i < N; ++i) \ + newobj->vaCoords[i].transform (matrix, pos); \ + \ + objs.push_back (newobj); \ + } \ + break; + +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= +static uint g_uTabs = 0; +vector LDSubfile::inlineContents (double* matrix, vertex pos, bool bCache) { + // If we have this cached, just return that. + if (objCache.size ()) + return objCache; + + vector objs; + + for (ulong i = 0; i < pFile->objects.size(); ++i) { + LDObject* obj = pFile->objects[i]; + + switch (obj->getType()) { + case OBJ_Comment: + case OBJ_Empty: + case OBJ_Gibberish: + case OBJ_Unidentified: + case OBJ_Vertex: + break; // Skip non-essentials + + ADD_TYPE (Line, 2) + ADD_TYPE (Triangle, 3) + ADD_TYPE (Quad, 4) + ADD_TYPE (CondLine, 4) + + case OBJ_Subfile: + // Got another sub-file reference, inline it. + LDSubfile* ref = static_cast (obj); + + double faNewMatrix[9]; + + for (short i = 0; i < 9; ++i) + faNewMatrix[i] = matrix[i] * ref->faMatrix[i]; + + vertex vNewPos = ref->vPosition; + vNewPos.transform (matrix, pos); + + // Only cache immediate subfiles, this is not one. Yay recursion! + g_uTabs++; + vector otherobjs = ref->inlineContents (faNewMatrix, vNewPos, false); + g_uTabs--; + + for (ulong i = 0; i < otherobjs.size(); ++i) + objs.push_back (otherobjs[i]); + break; + } + } + + // If we cache this stuff, keep it around + if (bCache) + objCache = objs; + + return objs; +} \ No newline at end of file diff -r 109b07334fa0 -r 915fc477cb6a ldtypes.h --- a/ldtypes.h Thu Mar 21 18:26:57 2013 +0200 +++ b/ldtypes.h Sun Mar 24 01:05:59 2013 +0200 @@ -152,6 +152,10 @@ double faMatrix[9]; // Transformation matrix for the subpart str zFileName; // Filename of the subpart OpenFile* pFile; // Pointer to opened file for this subfile. nullptr if unopened. + vector objCache; // Cache of this file's contents, if desired + + // Gets the inlined contents of this subfile. + std::vector inlineContents (double* matrix, vertex pos, bool bCache); }; // ============================================================================= diff -r 109b07334fa0 -r 915fc477cb6a main.cpp --- a/main.cpp Thu Mar 21 18:26:57 2013 +0200 +++ b/main.cpp Sun Mar 24 01:05:59 2013 +0200 @@ -30,6 +30,8 @@ bbox g_BBox; QApplication* g_qMainApp = nullptr; +const vertex g_Origin (0.0f, 0.0f, 0.0f); + int main (int dArgC, char* saArgV[]) { // Load or create the configuration if (!config::load()) { @@ -71,6 +73,17 @@ ftoa (z).chars()); } +void vertex::transform (double* matrix, vertex pos) { + double x2, y2, z2; + x2 = (matrix[0] * x) + (matrix[1] * y) + (matrix[2] * z) + pos.x; + y2 = (matrix[3] * x) + (matrix[4] * y) + (matrix[5] * z) + pos.y; + z2 = (matrix[6] * x) + (matrix[7] * y) + (matrix[8] * z) + pos.z; + + x = x2; + y = y2; + z = z2; +} + // ============================================================================= // void logVA (logtype_e, const char*, va_list) [static] // diff -r 109b07334fa0 -r 915fc477cb6a misc.cpp --- a/misc.cpp Thu Mar 21 18:26:57 2013 +0200 +++ b/misc.cpp Sun Mar 24 01:05:59 2013 +0200 @@ -85,4 +85,15 @@ } return true; +} + +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= +template bool in (T needle, std::initializer_list haystack) { + for (size_t i = 0; i < haystack.size(); ++i) + if (needle = haystack[i]) + return true; + + return false; } \ No newline at end of file diff -r 109b07334fa0 -r 915fc477cb6a misc.h --- a/misc.h Thu Mar 21 18:26:57 2013 +0200 +++ b/misc.h Sun Mar 24 01:05:59 2013 +0200 @@ -35,4 +35,6 @@ // Converts a float value to a string value. str ftoa (double fCoord); +template bool in (T needle, std::initializer_list haystack); + #endif // __MISC_H__ \ No newline at end of file