- refactoring

Tue, 21 Jan 2014 01:29:12 +0200

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Tue, 21 Jan 2014 01:29:12 +0200
changeset 674
3d8ab0f89102
parent 673
8e6f5b3f9d38
child 675
450827da2376

- refactoring

src/GLCompiler.cc file | annotate | diff | comparison | revisions
src/GLCompiler.h file | annotate | diff | comparison | revisions
src/GLShared.h file | annotate | diff | comparison | revisions
src/gldraw.cc file | annotate | diff | comparison | revisions
src/gldraw.h file | annotate | diff | comparison | revisions
--- a/src/GLCompiler.cc	Tue Jan 21 01:09:22 2014 +0200
+++ b/src/GLCompiler.cc	Tue Jan 21 01:29:12 2014 +0200
@@ -7,7 +7,7 @@
 #include "gldraw.h"
 #include <QDate>
 
-#define DEBUG_PRINT(...) fprint (stdout, __VA_ARGS__)
+#define DE_BUG_PRINT(...) fprint (stdout, __VA_ARGS__)
 
 extern_cfg (Bool, gl_blackedges);
 static QList<short> g_warnedColors;
@@ -30,7 +30,7 @@
 //
 void GLCompiler::compilePolygon (LDObject* drawobj, LDObject* trueobj, QList< GLCompiler::CompiledTriangle >& data)
 {
-	const QColor pickColor = getObjectColor (trueobj, PickColor);
+	const QColor pickColor = getObjectColor (trueobj, E_PickColor);
 	LDObject::Type type = drawobj->getType();
 	LDObjectList objs;
 
@@ -49,7 +49,7 @@
 		const LDObject::Type objtype = obj->getType();
 		const bool isline = (objtype == LDObject::ELine || objtype == LDObject::ECondLine);
 		const int verts = isline ? 2 : obj->vertices();
-		QColor normalColor = getObjectColor (obj, Normal);
+		QColor normalColor = getObjectColor (obj, E_NormalColor);
 
 		assert (isline || objtype == LDObject::ETriangle);
 
@@ -80,20 +80,20 @@
 	QTime t0;
 
 	t0 = QTime::currentTime();
-	for (int i = 0; i < GL::ENumArrays; ++i)
+	for (int i = 0; i < E_NumVertexArrays; ++i)
 		m_objArrays[obj][i].clear();
-	DEBUG_PRINT ("INIT: %1ms\n", t0.msecsTo (QTime::currentTime()));
+	DE_BUG_PRINT ("INIT: %1ms\n", t0.msecsTo (QTime::currentTime()));
 
 	t0 = QTime::currentTime();
 	compileSubObject (obj, obj, data);
-	DEBUG_PRINT ("COMPILATION: %1ms\n", t0.msecsTo (QTime::currentTime()));
+	DE_BUG_PRINT ("COMPILATION: %1ms\n", t0.msecsTo (QTime::currentTime()));
 
 	t0 = QTime::currentTime();
 
-	for (int i = 0; i < GL::ENumArrays; ++i)
+	for (int i = 0; i < E_NumVertexArrays; ++i)
 	{
-		GL::VAOType type = (GL::VAOType) i;
-		const bool islinearray = (type == GL::EEdgeArray || type == GL::EEdgePickArray);
+		E_VertexArrayType type = (E_VertexArrayType) i;
+		const bool islinearray = (type == E_EdgeArray || type == E_EdgePickArray);
 
 		for (const CompiledTriangle& poly : data)
 		{
@@ -101,29 +101,36 @@
 			{
 				// Conditional lines go to the edge pick array and the array
 				// specifically designated for conditional lines and nowhere else.
-				if (type != GL::EEdgePickArray && type != GL::ECondEdgeArray)
+				// They are kept separate from the regular edge array so that
+				// they can be drawn as dashed lines.
+				//
+				// However we don't need nor desire that behavior for the pick
+				// scene, so they can be grouped with normal edgelines for that.
+				if (type != E_EdgePickArray && type != E_CondEdgeArray)
 					continue;
 			}
 			else
 			{
-				// Lines and only lines go to the line array and only to the line array.
-				if ( (poly.numVerts == 2) ^ islinearray)
+				// Lines and only lines go to line arrays and only to line arrays.
+				if ((poly.numVerts == 2) ^ islinearray)
 					continue;
 
 				// Only conditional lines go into the conditional line array
-				if (type == GL::ECondEdgeArray)
+				if (type == E_CondEdgeArray)
 					continue;
 			}
 
-			Array* verts = postprocess (poly, type);
+			// Postprocess the polygon into a VAO and add it in
+			VertexArray* verts = postprocess (poly, type);
 			m_objArrays[obj][type] += *verts;
+
+			// The array is changed, it needs a merge now.
+			m_changed[type] = true;
 			delete verts;
 		}
 	}
 
-	DEBUG_PRINT ("POST-PROCESS: %1ms\n", t0.msecsTo (QTime::currentTime()));
-
-	needMerge();
+	DE_BUG_PRINT ("POST-PROCE_SS: %1ms\n", t0.msecsTo (QTime::currentTime()));
 }
 
 // =============================================================================
@@ -146,15 +153,15 @@
 			QTime t0 = QTime::currentTime();
 			for (LDTriangle* triangle : static_cast<LDQuad*> (obj)->splitToTriangles())
 				compilePolygon (triangle, topobj, data);
-			DEBUG_PRINT ("\t- QUAD COMPILE: %1ms\n", t0.msecsTo (QTime::currentTime()));
+			DE_BUG_PRINT ("\t- QUAD COMPILE: %1ms\n", t0.msecsTo (QTime::currentTime()));
 		} break;
 
 		case LDObject::ESubfile:
 		{
 			QTime t0 = QTime::currentTime();
 			objs = static_cast<LDSubfile*> (obj)->inlineContents (LDSubfile::RendererInline | LDSubfile::DeepCacheInline);
-			DEBUG_PRINT ("\t- INLINE: %1ms\n", t0.msecsTo (QTime::currentTime()));
-			DEBUG_PRINT ("\t- %1 objects\n", objs.size());
+			DE_BUG_PRINT ("\t- INLINE: %1ms\n", t0.msecsTo (QTime::currentTime()));
+			DE_BUG_PRINT ("\t- %1 objects\n", objs.size());
 
 			t0 = QTime::currentTime();
 
@@ -164,7 +171,7 @@
 				obj->deleteSelf();
 			}
 
-			DEBUG_PRINT ("\t- SUB-COMPILATION: %1ms\n", t0.msecsTo (QTime::currentTime()));
+			DE_BUG_PRINT ("\t- SUB-COMPILATION: %1ms\n", t0.msecsTo (QTime::currentTime()));
 		} break;
 
 		default:
@@ -201,7 +208,7 @@
 
 // =============================================================================
 //
-const GLCompiler::Array* GLCompiler::getMergedBuffer (GL::VAOType type)
+const GLCompiler::VertexArray* GLCompiler::getMergedBuffer (E_VertexArrayType type)
 {
 	// If there are objects staged for compilation, compile them now.
 	if (m_staged.size() > 0)
@@ -212,7 +219,7 @@
 		m_staged.clear();
 	}
 
-	assert (type < GL::ENumArrays);
+	assert (type < E_NumVertexArrays);
 
 	if (m_changed[type])
 	{
@@ -230,7 +237,7 @@
 				m_mainArrays[type] += (*it)[type];
 		}
 
-		DEBUG_PRINT ("merged array %1: %2 entries\n", (int) type, m_mainArrays[type].size());
+		DE_BUG_PRINT ("merged array %1: %2 entries\n", (int) type, m_mainArrays[type].size());
 	}
 
 	return &m_mainArrays[type];
@@ -239,45 +246,45 @@
 // =============================================================================
 // This turns a compiled triangle into usable VAO vertices
 //
-GLCompiler::Array* GLCompiler::postprocess (const CompiledTriangle& poly, GLRenderer::VAOType type)
+GLCompiler::VertexArray* GLCompiler::postprocess (const CompiledTriangle& poly, E_VertexArrayType type)
 {
-	Array* va = new Array;
-	Array verts;
+	VertexArray* va = new VertexArray;
+	VertexArray verts;
 
 	for (int i = 0; i < poly.numVerts; ++i)
 	{
-		alias v0 = poly.verts[i];
-		VAO v;
-		v.x = v0.x();
-		v.y = v0.y();
-		v.z = v0.z();
+		VAO vao;
+		const auto& v0 = poly.verts[i];
+		vao.x = v0.x();
+		vao.y = v0.y();
+		vao.z = v0.z();
 
 		switch (type)
 		{
-			case GL::ESurfaceArray:
-			case GL::EEdgeArray:
-			case GL::ECondEdgeArray:
+			case E_SurfaceArray:
+			case E_EdgeArray:
+			case E_CondEdgeArray:
 			{
-				v.color = poly.rgb;
+				vao.color = poly.rgb;
 			} break;
 
-			case GL::EPickArray:
-			case GL::EEdgePickArray:
+			case E_PickArray:
+			case E_EdgePickArray:
 			{
-				v.color = poly.pickrgb;
+				vao.color = poly.pickrgb;
 			} break;
 
-			case GL::EBFCArray:
-			case GL::ENumArrays:
-				break; // handled separately
+			case E_BFCArray:
+			case E_NumVertexArrays:
+				break;
 		}
 
-		verts << v;
+		verts << vao;
 	}
 
-	if (type == GL::EBFCArray)
+	if (type == E_BFCArray)
 	{
-		int32 rgb = getColorRGB (getObjectColor (poly.obj, BFCFront));
+		int32 rgb = getColorRGB (getObjectColor (poly.obj, E_BFCFrontColor));
 
 		for (VAO vao : verts)
 		{
@@ -285,7 +292,7 @@
 			*va << vao;
 		}
 
-		rgb = getColorRGB (getObjectColor (poly.obj, BFCBack));
+		rgb = getColorRGB (getObjectColor (poly.obj, E_BFCBackColor));
 
 		for (int i = verts.size() - 1; i >= 0; --i)
 		{
@@ -313,14 +320,14 @@
 
 // =============================================================================
 //
-QColor GLCompiler::getObjectColor (LDObject* obj, ColorType colotype) const
+QColor GLCompiler::getObjectColor (LDObject* obj, GLCompiler::E_ColorType colortype) const
 {
 	QColor qcol;
 
 	if (!obj->isColored())
 		return QColor();
 
-	if (colotype == PickColor)
+	if (colortype == E_PickColor)
 	{
 		// Make the color by the object's ID if we're picking, so we can make the
 		// ID again from the color we get from the picking results. Be sure to use
@@ -338,12 +345,11 @@
 		return QColor (r, g, b);
 	}
 
-	if ( (colotype == BFCFront || colotype == BFCBack) &&
-			obj->getType() != LDObject::ELine &&
-			obj->getType() != LDObject::ECondLine
-	   )
+	if ((colortype == E_BFCFrontColor || colortype == E_BFCBackColor) &&
+		obj->getType() != LDObject::ELine &&
+		obj->getType() != LDObject::ECondLine)
 	{
-		if (colotype == BFCFront)
+		if (colortype == E_BFCFrontColor)
 			qcol = QColor (40, 192, 0);
 		else
 			qcol = QColor (224, 0, 0);
@@ -351,7 +357,7 @@
 	else
 	{
 		if (obj->getColor() == maincolor)
-			qcol = GL::getMainColor();
+			qcol = GLRenderer::getMainColor();
 		else
 		{
 			LDColor* col = getColor (obj->getColor());
@@ -374,7 +380,7 @@
 			// The color was unknown. Use main color to make the object at least
 			// not appear pitch-black.
 			if (obj->getColor() != edgecolor)
-				qcol = GL::getMainColor();
+				qcol = GLRenderer::getMainColor();
 			else
 				qcol = Qt::black;
 
@@ -415,7 +421,7 @@
 void GLCompiler::initObject (LDObject* obj)
 {
 	if (m_objArrays.find (obj) == m_objArrays.end())
-		m_objArrays[obj] = new Array[GL::ENumArrays];
+		m_objArrays[obj] = new VertexArray[E_NumVertexArrays];
 }
 
 // =============================================================================
--- a/src/GLCompiler.h	Tue Jan 21 01:09:22 2014 +0200
+++ b/src/GLCompiler.h	Tue Jan 21 01:29:12 2014 +0200
@@ -1,52 +1,71 @@
-#ifndef LDFORGE_GLDATA_H
-#define LDFORGE_GLDATA_H
-
-#include "types.h"
-#include "gldraw.h"
-#include <QMap>
-#include <QRgb>
-
-/* =============================================================================
- * -----------------------------------------------------------------------------
- * GLCompiler
- *
- * This class manages vertex arrays for the GL renderer, compiling vertices into
- * VAO-readable triangles which can be requested with getMergedBuffer.
+/*
+ *  LDForge: LDraw parts authoring CAD
+ *  Copyright (C) 2013, 2014 Santeri Piippo
  *
- * 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.
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
  *
- * 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 renmm dering.
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
  *
- * The nested Array class contains a vector-like buffer of the Vertex structs,
- * these structs are the VAOs that get passed to the renderer.
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#ifndef LDFORGE_GLCOMPILER_H
+#define LDFORGE_GLCOMPILER_H
+
+#include "main.h"
+#include "gldraw.h"
+#include "GLShared.h"
+#include <QMap>
+
+// =============================================================================
+// GLCompiler
+//
+// This class manages vertex arrays for the GL renderer, compiling vertices into
+// VAO-readable triangles which can be requested with getMergedBuffer.
+//
+// There are 6 main array types:
+// - the normal polygon array, for triangles
+// - edge line array, for lines
+// - conditional line array, for conditional lines. Kept separate so that they
+//       can be drawn as dashed liness
+// - 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 same as 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.
+//       Conditional lines are grouped with normal edgelines here.
+//
+// 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 renmm dering.
+//
+// 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 GLCompiler
 {
 	public:
-		enum ColorType
+		enum E_ColorType
 		{
-			Normal,
-			BFCFront,
-			BFCBack,
-			PickColor,
+			E_NormalColor,
+			E_BFCFrontColor,
+			E_BFCBackColor,
+			E_PickColor,
 		};
 
 		struct CompiledTriangle
 		{
-			Vertex	verts[3];
+			Vertex		verts[3];
 			uint8		numVerts;	// 2 if a line
 			uint32		rgb;		// Color of this poly normally
 			uint32		pickrgb;	// Color of this poly while picking
@@ -62,7 +81,7 @@
 		};
 
 		using PolygonList = QList<CompiledTriangle>;
-		using Array = QVector<VAO>;
+		using VertexArray = QVector<VAO>;
 
 		GLCompiler();
 		~GLCompiler();
@@ -70,8 +89,8 @@
 		void compileDocument();
 		void forgetObject (LDObject* obj);
 		void initObject (LDObject* obj);
-		const Array* getMergedBuffer (GL::VAOType type);
-		QColor getObjectColor (LDObject* obj, ColorType list) const;
+		const VertexArray* getMergedBuffer (E_VertexArrayType type);
+		QColor getObjectColor (LDObject* obj, E_ColorType colortype) const;
 		void needMerge();
 		void stageForCompilation (LDObject* obj);
 
@@ -81,15 +100,15 @@
 		void compilePolygon (LDObject* drawobj, LDObject* trueobj, PolygonList& data);
 		void compileObject (LDObject* obj);
 		void compileSubObject (LDObject* obj, LDObject* topobj, PolygonList& data);
-		Array* postprocess (const GLCompiler::CompiledTriangle& i, GLRenderer::VAOType type);
+		VertexArray* postprocess (const CompiledTriangle& poly, E_VertexArrayType type);
 
-		QMap<LDObject*, Array*>				m_objArrays;
-		Array								m_mainArrays[GL::ENumArrays];
+		QMap<LDObject*, VertexArray*>				m_objArrays;
+		VertexArray								m_mainArrays[E_NumVertexArrays];
 		LDDocument*							m_file;
-		bool								m_changed[GL::ENumArrays];
+		bool								m_changed[E_NumVertexArrays];
 		LDObjectList						m_staged; // Objects that need to be compiled
 };
 
 extern GLCompiler g_vertexCompiler;
 
-#endif // LDFORGE_GLDATA_H
+#endif // LDFORGE_GLCOMPILER_H
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/GLShared.h	Tue Jan 21 01:29:12 2014 +0200
@@ -0,0 +1,33 @@
+/*
+ *  LDForge: LDraw parts authoring CAD
+ *  Copyright (C) 2013, 2014 Santeri Piippo
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef LDFORGE_GLSHARED_H
+#define LDFORGE_GLSHARED_H
+
+enum E_VertexArrayType
+{
+	E_SurfaceArray,
+	E_EdgeArray,
+	E_CondEdgeArray,
+	E_BFCArray,
+	E_PickArray,
+	E_EdgePickArray,
+	E_NumVertexArrays
+};
+
+#endif // LDFORGE_GLSHARED_H
\ No newline at end of file
--- a/src/gldraw.cc	Tue Jan 21 01:09:22 2014 +0200
+++ b/src/gldraw.cc	Tue Jan 21 01:29:12 2014 +0200
@@ -375,16 +375,16 @@
 	} else
 		glDisable (GL_CULL_FACE);
 
-	drawVAOs ((isPicking() ? EPickArray : gl_colorbfc ? EBFCArray : ESurfaceArray), GL_TRIANGLES);
-	drawVAOs ((isPicking() ? EEdgePickArray : EEdgeArray), GL_LINES);
+	drawVAOs ((isPicking() ? E_PickArray : gl_colorbfc ? E_BFCArray : E_SurfaceArray), GL_TRIANGLES);
+	drawVAOs ((isPicking() ? E_EdgePickArray : E_EdgeArray), GL_LINES);
 
 	// Draw conditional lines. Note that conditional lines are drawn into
-	// EdgePickArray in the picking scene, so when picking, don't do anything
-	// here.
+	// EdgePickArray in the picking scene so we only draw this array when
+	// not using the pick scene.
 	if (!isPicking())
 	{
 		glEnable (GL_LINE_STIPPLE);
-		drawVAOs (ECondEdgeArray, GL_LINES);
+		drawVAOs (E_CondEdgeArray, GL_LINES);
 		glDisable (GL_LINE_STIPPLE);
 	}
 
@@ -396,11 +396,11 @@
 
 // =============================================================================
 //
-void GLRenderer::drawVAOs (VAOType arrayType, GLenum type)
+void GLRenderer::drawVAOs (E_VertexArrayType arrayType, GLenum type)
 {
-	const GLCompiler::Array* array = g_vertexCompiler.getMergedBuffer (arrayType);
-	glVertexPointer (3, GL_FLOAT, sizeof (GLCompiler::Vertex), &array->data()[0].x);
-	glColorPointer (4, GL_UNSIGNED_BYTE, sizeof (GLCompiler::Vertex), &array->data()[0].color);
+	const GLCompiler::VertexArray* array = g_vertexCompiler.getMergedBuffer (arrayType);
+	glVertexPointer (3, GL_FLOAT, sizeof (GLCompiler::VAO), &array->data()[0].x);
+	glColorPointer (4, GL_UNSIGNED_BYTE, sizeof (GLCompiler::VAO), &array->data()[0].color);
 	glDrawArrays (type, 0, array->size());
 }
 
--- a/src/gldraw.h	Tue Jan 21 01:09:22 2014 +0200
+++ b/src/gldraw.h	Tue Jan 21 01:29:12 2014 +0200
@@ -23,6 +23,7 @@
 #include "main.h"
 #include "ldtypes.h"
 #include "document.h"
+#include "GLShared.h"
 
 class MessageManager;
 class QDialogButtonBox;
@@ -115,17 +116,6 @@
 			BFCBackList
 		};
 
-		enum VAOType
-		{
-			ESurfaceArray,
-			EEdgeArray,
-			ECondEdgeArray,
-			EBFCArray,
-			EPickArray,
-			EEdgePickArray,
-			ENumArrays
-		};
-
 		// CameraIcon::img is a heap-allocated QPixmap because otherwise it gets
 		// initialized before program gets to main() and constructs a QApplication
 		// and Qt doesn't like that.
@@ -257,7 +247,7 @@
 		Vertex         coordconv2_3 (const QPoint& pos2d, bool snap) const;
 
 		// Draw a VAO array
-		void           drawVAOs (VAOType arrayType, GLenum type);
+		void           drawVAOs (E_VertexArrayType arrayType, GLenum type);
 
 		// Determine which color to draw text with
 		QColor         getTextPen() const;

mercurial