# HG changeset patch # User Santeri Piippo # Date 1376640321 -10800 # Node ID a70dd25dd4bbd7da642a5ab878a96ae580962419 # Parent 4852e815df2949ba53e8d4d8c9b727eecc3818cc rework diff -r 4852e815df29 -r a70dd25dd4bb src/gldata.cpp --- a/src/gldata.cpp Fri Aug 09 04:29:37 2013 +0300 +++ b/src/gldata.cpp Fri Aug 16 11:05:21 2013 +0300 @@ -26,16 +26,14 @@ void VertexCompiler::Array::clear() { delete[] m_data; - m_data = new DataType[64]; + m_data = new Vertex[64]; m_size = 64; m_ptr = &m_data[0]; } // ============================================================================= // ----------------------------------------------------------------------------- -void VertexCompiler::Array::resizeToFit ( - VertexCompiler::Array::SizeType newSize -) { +void VertexCompiler::Array::resizeToFit (Size newSize) { if (allocatedSize() >= newSize) return; @@ -44,17 +42,17 @@ // Add some lee-way space to reduce the amount of resizing. newSize += 256; - const SizeType oldSize = allocatedSize(); + const Size oldSize = allocatedSize(); // We need to back up the data first - DataType* copy = new DataType[oldSize]; + Vertex* copy = new Vertex[oldSize]; memcpy (copy, m_data, oldSize); // Re-create the buffer delete[] m_data; - m_data = new DataType[newSize]; + m_data = new Vertex[newSize]; m_size = newSize; - m_ptr = &m_data[cachedWriteSize / sizeof (DataType)]; + m_ptr = &m_data[cachedWriteSize / sizeof (Vertex)]; // Copy the data back memcpy (m_data, copy, oldSize); @@ -63,28 +61,26 @@ // ============================================================================= // ----------------------------------------------------------------------------- -const VertexCompiler::Array::DataType* VertexCompiler::Array::data() const { +const VertexCompiler::Vertex* VertexCompiler::Array::data() const { return m_data; } // ============================================================================= // ----------------------------------------------------------------------------- -const VertexCompiler::Array::SizeType& VertexCompiler::Array::allocatedSize() const { +const VertexCompiler::Array::Size& VertexCompiler::Array::allocatedSize() const { return m_size; } // ============================================================================= // ----------------------------------------------------------------------------- -VertexCompiler::Array::SizeType VertexCompiler::Array::writtenSize() const { - return (m_ptr - m_data) * sizeof (DataType); +VertexCompiler::Array::Size VertexCompiler::Array::writtenSize() const { + return (m_ptr - m_data) * sizeof (Vertex); } // ============================================================================= // ----------------------------------------------------------------------------- -void VertexCompiler::Array::write ( - VertexCompiler::Array::DataType f -) { - // Ensure there's enoughspace for the new float +void VertexCompiler::Array::write (const Vertex& f) { + // Ensure there's enoughspace for the new vertex resizeToFit (writtenSize() + sizeof f); // Write the float in @@ -100,14 +96,12 @@ resizeToFit (writtenSize() + other->writtenSize()); memcpy (m_ptr, other->data(), other->writtenSize()); - m_ptr += other->writtenSize() / sizeof (DataType); + m_ptr += other->writtenSize() / sizeof (Vertex); } // ============================================================================= // ----------------------------------------------------------------------------- -VertexCompiler::VertexCompiler() : - m_file (null) -{ +VertexCompiler::VertexCompiler() : m_file (null) { for (int i = 0; i < NumArrays; ++i) { m_mainArrays[i] = new Array; m_changed[i] = false; @@ -123,14 +117,10 @@ // ============================================================================= // ----------------------------------------------------------------------------- -void VertexCompiler::compileVertex ( - vertex v, - QColor col, - Array* array -) { +void VertexCompiler::compileVertex (::vertex v, QColor col, Array* array) { VertexCompiler::Vertex glvertex; glvertex.x = v.x(); - glvertex.y = v.y(); + glvertex.y = -v.y(); glvertex.z = v.z(); glvertex.color = (col.red() & 0xFF) << 0x00 | @@ -143,17 +133,14 @@ // ============================================================================= // ----------------------------------------------------------------------------- -void VertexCompiler::compilePolygon ( - LDObject* drawobj, - LDObject* trueobj -) { - // Note: we use the true object's color but the draw object's vertices. This - // is so that the index color is generated correctly - it has to reference - // the true object's ID, this is crucial for picking to work. +// Note: we use the true object's color but the draw object's vertices. This is +// so that the index color is generated correctly - it has to reference the true +// object's ID. This is crucial for picking to work. +void VertexCompiler::compilePolygon (LDObject* drawobj, LDObject* trueobj) { Array** arrays = m_objArrays[trueobj]; LDObject::Type objtype = drawobj->getType(); - bool isline = (objtype == LDObject::Line || objtype == LDObject::CondLine); - int verts = isline ? 2 : drawobj->vertices(); + const bool isline = (objtype == LDObject::Line || objtype == LDObject::CondLine); + const int verts = isline ? 2 : drawobj->vertices(); QColor normalColor = getObjectColor (trueobj, Normal), pickColor = getObjectColor (trueobj, PickColor); @@ -163,13 +150,15 @@ compileVertex (drawobj->getVertex (i), pickColor, arrays[isline ? EdgePickArray : PickArray]); } - // For non-lines, compile BFC data + // For non-lines, compile BFC data. Note that the front objects are what get + // reversed here! This is because we invert the Y axis, which inverts the + // entire part model, so we remedy that here. if (!isline) { - QColor col = getObjectColor (trueobj, BFCFront); + QColor col = getObjectColor (trueobj, BFCBack); for (int i = 0; i < verts; ++i) compileVertex (drawobj->getVertex(i), col, arrays[BFCArray]); - col = getObjectColor (trueobj, BFCBack); + col = getObjectColor (trueobj, BFCFront); for (int i = verts - 1; i >= 0; --i) compileVertex (drawobj->getVertex(i), col, arrays[BFCArray]); } @@ -177,9 +166,7 @@ // ============================================================================= // ----------------------------------------------------------------------------- -void VertexCompiler::compileObject ( - LDObject* obj -) { +void VertexCompiler::compileObject (LDObject* obj, LDObject* topobj) { if (m_objArrays.find (obj) == m_objArrays.end()) { m_objArrays[obj] = new Array*[NumArrays]; @@ -190,20 +177,31 @@ m_objArrays[obj][i]->clear(); } + List objs; + switch (obj->getType()) { case LDObject::Triangle: - compilePolygon (obj, obj); + compilePolygon (obj, topobj); m_changed[MainArray] = true; break; case LDObject::Quad: for (LDTriangleObject* triangle : static_cast (obj)->splitToTriangles()) - compilePolygon (triangle, obj); + compilePolygon (triangle, topobj); m_changed[MainArray] = true; break; case LDObject::Line: - compilePolygon (obj, obj); + compilePolygon (obj, topobj); + break; + + case LDObject::Subfile: + objs = static_cast (obj)->inlineContents (true, true); + + for (LDObject* obj : objs) { + compileObject (obj, topobj); + delete obj; + } break; default: @@ -215,14 +213,12 @@ // ----------------------------------------------------------------------------- void VertexCompiler::compileFile() { for (LDObject* obj : *m_file) - compileObject (obj); + compileObject (obj, obj); } // ============================================================================= // ----------------------------------------------------------------------------- -void VertexCompiler::forgetObject ( - LDObject* obj -) { +void VertexCompiler::forgetObject (LDObject* obj) { for (int i = 0; i < NumArrays; ++i) delete m_objArrays[obj][i]; @@ -232,17 +228,13 @@ // ============================================================================= // ----------------------------------------------------------------------------- -void VertexCompiler::setFile ( - LDFile* file -) { +void VertexCompiler::setFile (LDFile* file) { m_file = file; } // ============================================================================= // ----------------------------------------------------------------------------- -VertexCompiler::Array* VertexCompiler::getMergedBuffer ( - VertexCompiler::MergedArrayType type -) { +VertexCompiler::Array* VertexCompiler::getMergedBuffer (ArrayType type) { assert (type < NumArrays); if (m_changed) { @@ -263,10 +255,7 @@ // ============================================================================= // ----------------------------------------------------------------------------- static List g_warnedColors; -QColor VertexCompiler::getObjectColor ( - LDObject* obj, - ColorType colotype -) const { +QColor VertexCompiler::getObjectColor (LDObject* obj, ColorType colotype) const { QColor qcol; if (!obj->isColored()) diff -r 4852e815df29 -r a70dd25dd4bb src/gldata.h --- a/src/gldata.h Fri Aug 09 04:29:37 2013 +0300 +++ b/src/gldata.h Fri Aug 16 11:05:21 2013 +0300 @@ -7,6 +7,33 @@ class QColor; class LDFile; +/* ============================================================================= + * ----------------------------------------------------------------------------- + * VertexCompiler + * + * This class manages vertex arrays for the GL renderer, compiling vertices into + * VAO-readable triangles which can be requested with getMergedBuffer. + * + * There are 5 main array types: + * - the normal polygon array, for triangles + * - edge line array, for lines + * - BFC array, this is the same as the normal polygon array except that the + * - polygons are listed twice, once normally and green and once reversed + * - and red, this allows BFC red/green view. + * - Picking array, this is the samea s the normal polygon array except the + * - polygons are compiled with their index color, this way the picking + * - method is capable of determining which object was selected by pixel + * - color. + * - Edge line picking array, the pick array version of the edge line array. + * + * There are also these same 5 arrays for every LDObject compiled. The main + * arrays are generated on demand from the ones in the current file's + * LDObjects and stored in cache for faster rendering. + * + * The nested Array class contains a vector-like buffer of the Vertex structs, + * these structs are the VAOs that get passed to the renderer. + */ + class VertexCompiler { public: enum ColorType { @@ -16,7 +43,7 @@ PickColor, }; - enum MergedArrayType { + enum ArrayType { MainArray, EdgeArray, BFCArray, @@ -33,8 +60,7 @@ class Array { public: - typedef int32 SizeType; - typedef Vertex DataType; + typedef int32 Size; Array(); Array (const Array& other) = delete; @@ -42,26 +68,26 @@ void clear(); void merge (Array* other); - void resizeToFit (SizeType newSize); - const SizeType& allocatedSize() const; - SizeType writtenSize() const; - const DataType* data() const; - void write (DataType f); + void resizeToFit (Size newSize); + const Size& allocatedSize() const; + Size writtenSize() const; + const Vertex* data() const; + void write (const VertexCompiler::Vertex& f); Array& operator= (const Array& other) = delete; private: - DataType* m_data; - DataType* m_ptr; - SizeType m_size; + Vertex* m_data; + Vertex* m_ptr; + Size m_size; }; VertexCompiler(); ~VertexCompiler(); void setFile (LDFile* file); void compileFile(); - void compileObject (LDObject* obj); + void compileObject (LDObject* obj, LDObject* topobj); void forgetObject (LDObject* obj); - Array* getMergedBuffer (MergedArrayType type); + Array* getMergedBuffer (ArrayType type); QColor getObjectColor (LDObject* obj, ColorType list) const; private: @@ -69,6 +95,7 @@ void compileVertex (vertex v, QColor col, VertexCompiler::Array* array); QMap m_objArrays; + QMap m_fileCache; Array* m_mainArrays[NumArrays]; LDFile* m_file; bool m_changed[NumArrays]; diff -r 4852e815df29 -r a70dd25dd4bb src/gldraw.cpp --- a/src/gldraw.cpp Fri Aug 09 04:29:37 2013 +0300 +++ b/src/gldraw.cpp Fri Aug 16 11:05:21 2013 +0300 @@ -39,7 +39,7 @@ #include "build/moc_gldraw.cpp" static const struct staticCameraMeta { - const char glrotate[3]; + const int8 glrotate[3]; const Axis axisX, axisY; const bool negX, negY; } g_staticCameras[6] = { @@ -279,7 +279,7 @@ g_staticCameras[m_camera].glrotate[2]); // Back camera needs to be handled differently - if (m_camera == GLRenderer::Back) { + if (m_camera == GL::Back) { glRotatef (180.0f, 1.0f, 0.0f, 0.0f); glRotatef (180.0f, 0.0f, 0.0f, 1.0f); } @@ -1075,7 +1075,7 @@ // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= void GLRenderer::compileObject (LDObject* obj) { - g_vertexCompiler.compileObject (obj); + g_vertexCompiler.compileObject (obj, obj); obj->m_glinit = true; }