Merge branch 'master' into gl, reworked stuff

Sat, 07 Sep 2013 13:23:09 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Sat, 07 Sep 2013 13:23:09 +0300
changeset 487
a350c4b25133
parent 443
a70dd25dd4bb (diff)
parent 486
25747c37c7be (current diff)
child 488
0ea49207a4ec

Merge branch 'master' into gl, reworked stuff

Conflicts:
src/gldraw.cpp
src/gldraw.h

src/aboutDialog.cpp file | annotate | diff | comparison | revisions
src/aboutDialog.h file | annotate | diff | comparison | revisions
src/gldata.cpp file | annotate | diff | comparison | revisions
src/gldata.h file | annotate | diff | comparison | revisions
src/gldraw.cpp file | annotate | diff | comparison | revisions
src/gldraw.h file | annotate | diff | comparison | revisions
src/labeledwidget.h file | annotate | diff | comparison | revisions
src/types.h file | annotate | diff | comparison | revisions
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/gldata.cpp	Sat Sep 07 13:23:09 2013 +0300
@@ -0,0 +1,391 @@
+#include "gldata.h"
+#include "ldtypes.h"
+#include "colors.h"
+#include "file.h"
+#include "misc.h"
+#include "gldraw.h"
+
+cfg (Bool, gl_blackedges, false);
+static List<short> g_warnedColors;
+
+// =============================================================================
+// -----------------------------------------------------------------------------
+VertexCompiler::Array::Array() :
+	m_data (null)
+{
+	clear();
+}
+
+// =============================================================================
+// -----------------------------------------------------------------------------
+VertexCompiler::Array::~Array() {
+	delete[] m_data;
+}
+
+// =============================================================================
+// -----------------------------------------------------------------------------
+void VertexCompiler::Array::clear() {
+	delete[] m_data;
+	
+	m_data = new Vertex[64];
+	m_size = 64;
+	m_ptr = &m_data[0];
+}
+
+// =============================================================================
+// -----------------------------------------------------------------------------
+void VertexCompiler::Array::resizeToFit (Size newSize) {
+	if (allocatedSize() >= newSize)
+		return;
+	
+	int32 cachedWriteSize = writtenSize();
+	
+	// Add some lee-way space to reduce the amount of resizing.
+	newSize += 256;
+	
+	const Size oldSize = allocatedSize();
+	
+	// We need to back up the data first
+	Vertex* copy = new Vertex[oldSize];
+	memcpy (copy, m_data, oldSize);
+	
+	// Re-create the buffer
+	delete[] m_data;
+	m_data = new Vertex[newSize];
+	m_size = newSize;
+	m_ptr = &m_data[cachedWriteSize / sizeof (Vertex)];
+	
+	// Copy the data back
+	memcpy (m_data, copy, oldSize);
+	delete[] copy;
+}
+
+// =============================================================================
+// -----------------------------------------------------------------------------
+const VertexCompiler::Vertex* VertexCompiler::Array::data() const {
+	return m_data;
+}
+
+// =============================================================================
+// -----------------------------------------------------------------------------
+const VertexCompiler::Array::Size& VertexCompiler::Array::allocatedSize() const {
+	return m_size;
+}
+
+// =============================================================================
+// -----------------------------------------------------------------------------
+VertexCompiler::Array::Size VertexCompiler::Array::writtenSize() const {
+	return (m_ptr - m_data) * sizeof (Vertex);
+}
+
+// =============================================================================
+// -----------------------------------------------------------------------------
+void VertexCompiler::Array::write (const Vertex& f) {
+	// Ensure there's enoughspace for the new vertex
+	resizeToFit (writtenSize() + sizeof f);
+	
+	// Write the float in
+	*m_ptr++ = f;
+}
+
+// =============================================================================
+// -----------------------------------------------------------------------------
+void VertexCompiler::Array::merge (Array* other) {
+	// Ensure there's room for both buffers
+	resizeToFit (writtenSize() + other->writtenSize());
+	
+	memcpy (m_ptr, other->data(), other->writtenSize());
+	m_ptr += other->writtenSize() / sizeof (Vertex);
+}
+
+// =============================================================================
+// -----------------------------------------------------------------------------
+VertexCompiler::VertexCompiler() :
+	m_file (null)
+{
+	memset (m_changed, 0xFF, sizeof m_changed);
+}
+
+VertexCompiler::~VertexCompiler() {}
+
+// =============================================================================
+// Note: we use the top level object's color but the draw object's vertices.
+// This is so that the index color is generated correctly - it has to reference
+// the top level object's ID. This is crucial for picking to work.
+// -----------------------------------------------------------------------------
+void VertexCompiler::compilePolygon (LDObject* drawobj, LDObject* trueobj) {
+	List<CompiledTriangle>& data = m_objArrays[trueobj];
+	
+	QColor normalColor = getObjectColor (trueobj, Normal),
+	       pickColor = getObjectColor (trueobj, PickColor);
+	
+	LDObject::Type type = drawobj->getType();
+	assert (type != LDObject::Subfile);
+	
+	List<LDObject*> objs;
+	if (type == LDObject::Quad) {
+		for (LDTriangle* t : static_cast<LDQuad*> (drawobj)->splitToTriangles())
+			objs << t;
+	} else
+		objs << drawobj;
+	
+	for (LDObject* obj : objs) {
+		const LDObject::Type objtype = obj->getType();
+		const bool isline = (objtype == LDObject::Line || objtype == LDObject::CndLine);
+		const int verts = isline ? 2 : obj->vertices();
+		
+		CompiledTriangle a;
+		a.rgb = normalColor.rgb();
+		a.pickrgb = pickColor.rgb();
+		a.numVerts = verts;
+		a.obj = trueobj;
+		
+		for (int i = 0; i < verts; ++i) {
+			a.verts[i] = obj->getVertex (i);
+			a.verts[i].y() = -a.verts[i].y();
+		}
+		
+		data << a;
+	}
+}
+
+// =============================================================================
+// -----------------------------------------------------------------------------
+void VertexCompiler::compileObject (LDObject* obj, LDObject* topobj) {
+	print ("compile %1 (%2)\n", obj->id(), topobj->id());
+	List<LDObject*> objs;
+	
+	switch (obj->getType()) {
+	case LDObject::Triangle:
+		compilePolygon (obj, topobj);
+		break;
+	
+	case LDObject::Quad:
+		for (LDTriangle* triangle : static_cast<LDQuad*> (obj)->splitToTriangles())
+			compilePolygon (triangle, topobj);
+		break;
+	
+	case LDObject::Line:
+		compilePolygon (obj, topobj);
+		break;
+	
+	case LDObject::Subfile:
+		objs = static_cast<LDSubfile*> (obj)->inlineContents (LDSubfile::RendererInline | LDSubfile::DeepCacheInline);
+		
+		for (LDObject* obj : objs) {
+			compileObject (obj, topobj);
+			delete obj;
+		}
+		break;
+	
+	default:
+		break;
+	}
+	
+	// Set all of m_changed to true
+	memset (m_changed, 0xFF, sizeof m_changed);
+}
+
+// =============================================================================
+// -----------------------------------------------------------------------------
+void VertexCompiler::compileFile() {
+	for (LDObject* obj : m_file->objects())
+		compileObject (obj, obj);
+}
+
+// =============================================================================
+// -----------------------------------------------------------------------------
+void VertexCompiler::forgetObject (LDObject* obj) {
+	m_objArrays.remove (obj);
+}
+
+// =============================================================================
+// -----------------------------------------------------------------------------
+void VertexCompiler::setFile (LDFile* file) {
+	m_file = file;
+}
+
+// =============================================================================
+// -----------------------------------------------------------------------------
+const VertexCompiler::Array* VertexCompiler::getMergedBuffer (ArrayType type) {
+	assert (type < NumArrays);
+	
+	if (m_changed[type]) {
+		m_changed[type] = false;
+		m_mainArrays[type].clear();
+		
+		print ("merge array %1\n", (int) type);
+		
+		for (LDObject* obj : m_file->objects()) {
+			if (!obj->isScemantic())
+				continue;
+			
+			const LDObject::Type objtype = obj->getType();
+			const bool isline = (objtype == LDObject::Line || objtype == LDObject::CndLine);
+			const bool islinearray = (type == EdgeArray || type == EdgePickArray);
+			
+			if ((isline && !islinearray) || (!isline && islinearray))
+				continue;
+			
+			auto it = m_objArrays.find (obj);
+			
+			if (it != m_objArrays.end()) {
+				const List<CompiledTriangle>& data = *it;
+				
+				for (const CompiledTriangle& i : data) {
+					Array* verts = postprocess (i, type);
+					m_mainArrays[type].merge (verts);
+					delete verts;
+				}
+			}
+		}
+		
+		print ("merged array: %1 bytes\n", m_mainArrays[type].writtenSize());
+	}
+	
+	return &m_mainArrays[type];
+}
+
+// =============================================================================
+// This turns a compiled triangle into usable VAO vertices
+// -----------------------------------------------------------------------------
+VertexCompiler::Array* VertexCompiler::postprocess (const CompiledTriangle& triangle, ArrayType type) {
+	Array* va = new Array;
+	List<Vertex> verts;
+	
+	for (int i = 0; i < triangle.numVerts; ++i) {
+		alias v0 = triangle.verts[i];
+		Vertex v;
+		v.x = v0.x();
+		v.y = v0.y();
+		v.z = v0.z();
+		
+		switch (type) {
+		case MainArray:
+		case EdgeArray:
+			v.color = triangle.rgb;
+			break;
+		
+		case PickArray:
+		case EdgePickArray:
+			v.color = triangle.pickrgb;
+		
+		case BFCArray:
+			break;
+		
+		case NumArrays:
+			assert (false);
+		}
+		
+		verts << v;
+	}
+	
+	if (type == BFCArray) {
+		int32 rgb = getObjectColor (triangle.obj, BFCFront).rgb();
+		for (Vertex v : verts) {
+			v.color = rgb;
+			va->write (v);
+		}
+		
+		rgb = getObjectColor (triangle.obj, BFCBack).rgb();
+		for (Vertex v : c_rev<Vertex> (verts)) {
+			v.color = rgb;
+			va->write (v);
+		}
+	} else {
+		for (Vertex v : verts)
+			va->write (v);
+	}
+	
+	return va;
+}
+
+// =============================================================================
+// -----------------------------------------------------------------------------
+uint32 VertexCompiler::getColorRGB (QColor& color) {
+	return
+		(color.red()   & 0xFF) << 0x00 |
+		(color.green() & 0xFF) << 0x08 |
+		(color.blue()  & 0xFF) << 0x10 |
+		(color.alpha() & 0xFF) << 0x18;
+}
+
+// =============================================================================
+// -----------------------------------------------------------------------------
+QColor VertexCompiler::getObjectColor (LDObject* obj, ColorType colotype) const {
+	QColor qcol;
+	
+	if (!obj->isColored())
+		return QColor();
+	
+	if (colotype == 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
+		// the top level parent's index since we want a subfile's children point
+		// to the subfile itself.
+		long i = obj->topLevelParent()->id();
+		
+		// Calculate a color based from this index. This method caters for
+		// 16777216 objects. I don't think that'll be exceeded anytime soon. :)
+		// ATM biggest is 53588.dat with 12600 lines.
+		int r = (i / (256 * 256)) % 256,
+			g = (i / 256) % 256,
+			b = i % 256;
+		
+		return QColor (r, g, b);
+	}
+	
+	if ((colotype == BFCFront || colotype == BFCBack) &&
+		obj->getType() != LDObject::Line &&
+		obj->getType() != LDObject::CndLine) {
+		
+		if (colotype == BFCFront)
+			qcol = QColor (40, 192, 0);
+		else
+			qcol = QColor (224, 0, 0);
+	} else {
+		if (obj->color() == maincolor)
+			qcol = GL::getMainColor();
+		else {
+			LDColor* col = getColor (obj->color());
+			
+			if (col)
+				qcol = col->faceColor;
+		}
+		
+		if (obj->color() == edgecolor) {
+			qcol = QColor (32, 32, 32); // luma (m_bgcolor) < 40 ? QColor (64, 64, 64) : Qt::black;
+			LDColor* col;
+			
+			if (!gl_blackedges && obj->parent() && (col = getColor (obj->parent()->color())))
+				qcol = col->edgeColor;
+		}
+		
+		if (qcol.isValid() == false) {
+			// The color was unknown. Use main color to make the object at least
+			// not appear pitch-black.
+			if (obj->color() != edgecolor)
+				qcol = GL::getMainColor();
+			
+			// Warn about the unknown colors, but only once.
+			for (short i : g_warnedColors)
+				if (obj->color() == i)
+					return Qt::black;
+			
+			print ("%1: Unknown color %2!\n", __func__, obj->color());
+			g_warnedColors << obj->color();
+			return Qt::black;
+		}
+	}
+	
+	if (obj->topLevelParent()->selected()) {
+		// Brighten it up for the select list.
+		const uchar add = 51;
+		
+		qcol.setRed (min (qcol.red() + add, 255));
+		qcol.setGreen (min (qcol.green() + add, 255));
+		qcol.setBlue (min (qcol.blue() + add, 255));
+	}
+	
+	return qcol;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/gldata.h	Sat Sep 07 13:23:09 2013 +0300
@@ -0,0 +1,116 @@
+#ifndef LDFORGE_GLDATA_H
+#define LDFORGE_GLDATA_H
+
+#include "types.h"
+#include <QMap>
+#include <QRgb>
+
+class QColor;
+class LDTriangle;
+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 {
+		Normal,
+		BFCFront,
+		BFCBack,
+		PickColor,
+	};
+	
+	enum ArrayType {
+		MainArray,
+		EdgeArray,
+		BFCArray,
+		PickArray,
+		EdgePickArray,
+		NumArrays
+	};
+	
+	struct CompiledTriangle {
+		vertex    verts[3];
+		uint8     numVerts;
+		QRgb      rgb;
+		QRgb      pickrgb;
+		LDObject* obj;
+	};
+	
+	struct Vertex {
+		float x, y, z;
+		uint32 color;
+		float pad[4];
+	};
+	
+	class Array {
+	public:
+		typedef int32 Size;
+		
+		Array();
+		Array (const Array& other) = delete;
+		~Array();
+		
+		void clear();
+		void merge (Array* other);
+		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:
+		Vertex* m_data;
+		Vertex* m_ptr;
+		Size m_size;
+	};
+	
+	VertexCompiler();
+	~VertexCompiler();
+	void setFile (LDFile* file);
+	void compileFile();
+	void compileObject (LDObject* obj, LDObject* topobj);
+	void forgetObject (LDObject* obj);
+	const Array* getMergedBuffer (ArrayType type);
+	QColor getObjectColor (LDObject* obj, ColorType list) const;
+	
+	static uint32 getColorRGB (QColor& color);
+	
+private:
+	void compilePolygon (LDObject* drawobj, LDObject* trueobj);
+	Array* postprocess (const CompiledTriangle& i, ArrayType type);
+	
+	QMap<LDObject*, List<CompiledTriangle>> m_objArrays;
+	QMap<LDFile*, Array*> m_fileCache;
+	Array m_mainArrays[NumArrays];
+	LDFile* m_file;
+	bool m_changed[NumArrays];
+};
+
+#endif // LDFORGE_GLDATA_H
\ No newline at end of file
--- a/src/gldraw.cpp	Wed Sep 04 11:54:17 2013 +0300
+++ b/src/gldraw.cpp	Sat Sep 07 13:23:09 2013 +0300
@@ -24,7 +24,6 @@
 #include <QToolTip>
 #include <QTimer>
 #include <GL/glu.h>
-
 #include "common.h"
 #include "config.h"
 #include "file.h"
@@ -36,9 +35,11 @@
 #include "dialogs.h"
 #include "addObjectDialog.h"
 #include "messagelog.h"
+#include "gldata.h"
+#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] = {
@@ -56,20 +57,19 @@
 cfg (Int, gl_linethickness, 2);
 cfg (Bool, gl_colorbfc, false);
 cfg (Int, gl_camera, GLRenderer::Free);
-cfg (Bool, gl_blackedges, false);
 cfg (Bool, gl_axes, false);
 cfg (Bool, gl_wireframe, false);
 cfg (Bool, gl_logostuds, false);
 
 // argh
 const char* g_CameraNames[7] = {
-	QT_TRANSLATE_NOOP ("GLRenderer",  "Top"),
-	QT_TRANSLATE_NOOP ("GLRenderer",  "Front"),
-	QT_TRANSLATE_NOOP ("GLRenderer",  "Left"),
-	QT_TRANSLATE_NOOP ("GLRenderer",  "Bottom"),
-	QT_TRANSLATE_NOOP ("GLRenderer",  "Back"),
-	QT_TRANSLATE_NOOP ("GLRenderer",  "Right"),
-	QT_TRANSLATE_NOOP ("GLRenderer",  "Free")
+	QT_TRANSLATE_NOOP ("GLRenderer", "Top"),
+	QT_TRANSLATE_NOOP ("GLRenderer", "Front"),
+	QT_TRANSLATE_NOOP ("GLRenderer", "Left"),
+	QT_TRANSLATE_NOOP ("GLRenderer", "Bottom"),
+	QT_TRANSLATE_NOOP ("GLRenderer", "Back"),
+	QT_TRANSLATE_NOOP ("GLRenderer", "Right"),
+	QT_TRANSLATE_NOOP ("GLRenderer", "Free")
 };
 
 const GL::Camera g_Cameras[7] = {
@@ -87,10 +87,15 @@
 	const vertex vert;
 } g_GLAxes[3] = {
 	{ QColor (255,   0,   0), vertex (10000, 0, 0) },
-	{ QColor (80, 192,   0), vertex (0, 10000, 0) },
+	{ QColor (80,  192,   0), vertex (0, 10000, 0) },
 	{ QColor (0,   160, 192), vertex (0, 0, 10000) },
 };
 
+
+// =============================================================================
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+#warning this should be a member
+static VertexCompiler g_vertexCompiler;
 static bool g_glInvert = false;
 static List<short> g_warnedColors;
 
@@ -195,7 +200,8 @@
 	setAutoFillBackground (false);
 	setMouseTracking (true);
 	setFocusPolicy (Qt::WheelFocus);
-	compileAllObjects();
+	
+	g_vertexCompiler.compileFile();
 }
 
 // =============================================================================
@@ -227,96 +233,6 @@
 
 // =============================================================================
 // -----------------------------------------------------------------------------
-void GLRenderer::setObjectColor (LDObject* obj, const ListType list) {
-	QColor qcol;
-	
-	if (!obj->isColored())
-		return;
-	
-	if (list == GL::PickList) {
-		// 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
-		// the top level parent's index since we want a subfile's children point
-		// to the subfile itself.
-		long i = obj->topLevelParent()->id();
-		
-		// Calculate a color based from this index. This method caters for
-		// 16777216 objects. I don't think that'll be exceeded anytime soon. :)
-		// ATM biggest is 53588.dat with 12600 lines.
-		double r = (i / (256 * 256)) % 256,
-			g = (i / 256) % 256,
-			b = i % 256;
-		
-		qglColor (QColor (r, g, b));
-		return;
-	}
-	
-	if ((list == BFCFrontList || list == BFCBackList) &&
-		obj->getType() != LDObject::Line &&
-		obj->getType() != LDObject::CndLine) {
-		
-		if (list == GL::BFCFrontList)
-			qcol = QColor (40, 192, 0);
-		else
-			qcol = QColor (224, 0, 0);
-	} else {
-		if (obj->color() == maincolor)
-			qcol = getMainColor();
-		else {
-			LDColor* col = getColor (obj->color());
-			
-			if (col)
-				qcol = col->faceColor;
-		}
-		
-		if (obj->color() == edgecolor) {
-			qcol = luma (m_bgcolor) < 40 ? QColor (64, 64, 64) : Qt::black;
-			LDColor* col;
-			
-			if (!gl_blackedges && obj->parent() && (col = getColor (obj->parent()->color())))
-				qcol = col->edgeColor;
-		}
-		
-		if (qcol.isValid() == false) {
-			// The color was unknown. Use main color to make the object at least
-			// not appear pitch-black.
-			if (obj->color() != edgecolor)
-				qcol = getMainColor();
-			
-			// Warn about the unknown colors, but only once.
-			for (short i : g_warnedColors)
-				if (obj->color() == i)
-					return;
-			
-			printf ("%s: Unknown color %d!\n", __func__, obj->color());
-			g_warnedColors << obj->color();
-			return;
-		}
-	}
-	
-	long r = qcol.red(),
-		g = qcol.green(),
-		b = qcol.blue(),
-		a = qcol.alpha();
-	
-	if (obj->topLevelParent()->selected()) {
-		// Brighten it up for the select list.
-		const uchar add = 51;
-		
-		r = min (r + add, 255l);
-		g = min (g + add, 255l);
-		b = min (b + add, 255l);
-	}
-	
-	glColor4f (
-		((double) r) / 255.0f,
-		((double) g) / 255.0f,
-		((double) b) / 255.0f,
-		((double) a) / 255.0f);
-}
-
-// =============================================================================
-// -----------------------------------------------------------------------------
 void GLRenderer::refresh() {
 	update();
 	swapBuffers();
@@ -325,7 +241,7 @@
 // =============================================================================
 // -----------------------------------------------------------------------------
 void GLRenderer::hardRefresh() {
-	compileAllObjects();
+	g_vertexCompiler.compileFile();
 	refresh();
 	
 	glLineWidth (gl_linethickness);
@@ -373,7 +289,7 @@
 		}
 		
 		// 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);
 		}
@@ -389,34 +305,32 @@
 		glRotatef (m_rotZ, 0.0f, 0.0f, 1.0f);
 	}
 	
-	const GL::ListType list = (!drawOnly() && m_picking) ? PickList : NormalList;
+	// Draw the polygons
+	glEnableClientState (GL_VERTEX_ARRAY);
+	glEnableClientState (GL_COLOR_ARRAY);
+	glDisableClientState (GL_NORMAL_ARRAY);
 	
-	if (gl_colorbfc && !m_picking && !drawOnly()) {
+	if (gl_colorbfc) {
 		glEnable (GL_CULL_FACE);
-		
-		for (LDObject* obj : file()->objects()) {
-			if (obj->hidden())
-				continue;
-			
-			glCullFace (GL_BACK);
-			glCallList (obj->glLists[BFCFrontList]);
-			
-			glCullFace (GL_FRONT);
-			glCallList (obj->glLists[BFCBackList]);
-		}
-		
+		glCullFace (GL_CCW);
+	} else
 		glDisable (GL_CULL_FACE);
-	} else {
-		for (LDObject* obj : file()->objects()) {
-			if (obj->hidden())
-				continue;
-			
-			glCallList (obj->glLists[list]);
-		}
-	}
 	
-	if (gl_axes && !m_picking && !drawOnly())
-		glCallList (m_axeslist);
+	const VertexCompiler::Array* array = g_vertexCompiler.getMergedBuffer (
+		(m_picking) ? VertexCompiler::PickArray :
+		(gl_colorbfc) ? VertexCompiler::BFCArray :
+			VertexCompiler::MainArray);
+	glVertexPointer (3, GL_FLOAT, sizeof (VertexCompiler::Vertex), &array->data()[0].x);
+	glColorPointer (4, GL_UNSIGNED_BYTE, sizeof (VertexCompiler::Vertex), &array->data()[0].color);
+	glDrawArrays (GL_TRIANGLES, 0, array->writtenSize() / sizeof (VertexCompiler::Vertex));
+	
+	// Draw edge lines
+	array = g_vertexCompiler.getMergedBuffer (
+		(m_picking) ? VertexCompiler::EdgePickArray :
+			VertexCompiler::EdgeArray);
+	glVertexPointer (3, GL_FLOAT, sizeof (VertexCompiler::Vertex), &array->data()[0].x);
+	glColorPointer (4, GL_UNSIGNED_BYTE, sizeof (VertexCompiler::Vertex), &array->data()[0].color);
+	glDrawArrays (GL_LINES, 0, array->writtenSize() / sizeof (VertexCompiler::Vertex));
 	
 	glPopMatrix();
 	glMatrixMode (GL_MODELVIEW);
@@ -675,118 +589,7 @@
 // =============================================================================
 // -----------------------------------------------------------------------------
 void GLRenderer::compileAllObjects() {
-	if (!file())
-		return;
-	
-	// Compiling all is a big job, use a busy cursor
-	setCursor (Qt::BusyCursor);
-	
-	m_knownVerts.clear();
-	
-	for (LDObject* obj : file()->objects())
-		compileObject (obj);
-	
-	// Compile axes
-	glDeleteLists (m_axeslist, 1);
-	m_axeslist = glGenLists (1);
-	glNewList (m_axeslist, GL_COMPILE);
-	glBegin (GL_LINES);
-	
-	for (const GLAxis& ax : g_GLAxes) {
-		qglColor (ax.col);
-		compileVertex (ax.vert);
-		compileVertex (-ax.vert);
-	}
-	
-	glEnd();
-	glEndList();
-	
-	setCursor (Qt::ArrowCursor);
-}
-
-// =============================================================================
-// -----------------------------------------------------------------------------
-void GLRenderer::compileSubObject (LDObject* obj, const GLenum gltype) {
-	glBegin (gltype);
-	
-	const short numverts = (obj->getType() != LDObject::CndLine) ? obj->vertices() : 2;
-	
-	if (g_glInvert == false)
-		for (short i = 0; i < numverts; ++i)
-			compileVertex (obj->m_coords[i]);
-	else
-		for (short i = numverts - 1; i >= 0; --i)
-			compileVertex (obj->m_coords[i]);
-	
-	glEnd();
-}
-
-// =============================================================================
-// -----------------------------------------------------------------------------
-void GLRenderer::compileList (LDObject* obj, const GLRenderer::ListType list) {
-	setObjectColor (obj, list);
-	
-	switch (obj->getType()) {
-	case LDObject::Line:
-		compileSubObject (obj, GL_LINES);
-		break;
-	
-	case LDObject::CndLine:
-		// Draw conditional lines with a dash pattern - however, use a full
-		// line when drawing a pick list to make selecting them easier.
-		if (list != GL::PickList) {
-			glLineStipple (1, 0x6666);
-			glEnable (GL_LINE_STIPPLE);
-		}
-		
-		compileSubObject (obj, GL_LINES);
-		
-		glDisable (GL_LINE_STIPPLE);
-		break;
-	
-	case LDObject::Triangle:
-		compileSubObject (obj, GL_TRIANGLES);
-		break;
-	
-	case LDObject::Quad:
-		compileSubObject (obj, GL_QUADS);
-		break;
-	
-	case LDObject::Subfile: {
-			LDSubfile* ref = static_cast<LDSubfile*> (obj);
-			List<LDObject*> objs;
-			
-			objs = ref->inlineContents (
-				LDSubfile::DeepInline |
-				LDSubfile::CacheInline |
-				LDSubfile::RendererInline);
-			bool oldinvert = g_glInvert;
-			
-			if (ref->transform().determinant() < 0)
-				g_glInvert = !g_glInvert;
-			
-			LDObject* prev = ref->prev();
-			if (prev && prev->getType() == LDObject::BFC && static_cast<LDBFC*> (prev)->type == LDBFC::InvertNext)
-				g_glInvert = !g_glInvert;
-			
-			for (LDObject* obj : objs) {
-				compileList (obj, list);
-				delete obj;
-			}
-			
-			g_glInvert = oldinvert;
-		}
-		break;
-	
-	default:
-		break;
-	}
-}
-
-// =============================================================================
-// -----------------------------------------------------------------------------
-void GLRenderer::compileVertex (const vertex& vrt) {
-	glVertex3d (vrt[X], -vrt[Y], -vrt[Z]);
+	g_vertexCompiler.compileFile();
 }
 
 // =============================================================================
@@ -1103,6 +906,7 @@
 			(*(pixelptr + 0) * 0x10000) +
 			(*(pixelptr + 1) * 0x00100) +
 			(*(pixelptr + 2) * 0x00001);
+		
 		pixelptr += 4;
 		
 		if (idx == 0xFFFFFF)
@@ -1209,6 +1013,7 @@
 // -----------------------------------------------------------------------------
 SET_ACCESSOR (LDFile*, GLRenderer::setFile) {
 	m_file = val;
+	g_vertexCompiler.setFile (val);
 	
 	if (val != null)
 		overlaysFromObjects();
@@ -1298,26 +1103,7 @@
 // =============================================================================
 // -----------------------------------------------------------------------------
 void GLRenderer::compileObject (LDObject* obj) {
-	deleteLists (obj);
-	
-	for (const GL::ListType listType : g_glListTypes) {
-		if (drawOnly() && listType != GL::NormalList)
-			continue;
-		
-		GLuint list = glGenLists (1);
-		glNewList (list, GL_COMPILE);
-		
-		obj->glLists[listType] = list;
-		compileList (obj, listType);
-		
-		glEndList();
-	}
-	
-	// Mark in known vertices of this object
-	List<vertex> verts = getVertices (obj);
-	m_knownVerts << verts;
-	m_knownVerts.makeUnique();
-	
+	g_vertexCompiler.compileObject (obj, obj);
 	obj->m_glinit = true;
 }
 
--- a/src/gldraw.h	Wed Sep 04 11:54:17 2013 +0300
+++ b/src/gldraw.h	Sat Sep 07 13:23:09 2013 +0300
@@ -79,7 +79,7 @@
 	double         depthValue() const;
 	void           drawGLScene();
 	void           endDraw (bool accept);
-	QColor         getMainColor();
+	static QColor  getMainColor();
 	overlayMeta&   getOverlay (int newcam);
 	void           hardRefresh();
 	void           initGLData();
@@ -145,15 +145,11 @@
 	void           addDrawnVertex (vertex m_hoverpos);
 	void           calcCameraIcons();                                      // Compute geometry for camera icons
 	void           clampAngle (double& angle) const;                       // Clamps an angle to [0, 360]
-	void           compileList (LDObject* obj, const ListType list);       // Compile one of the lists of an object
-	void           compileSubObject (LDObject* obj, const GLenum gltype);  // Sub-routine for object compiling
-	void           compileVertex (const vertex& vrt);                      // Compile a single vertex to a list
 	vertex         coordconv2_3 (const QPoint& pos2d, bool snap) const;    // Convert a 2D point to a 3D point
 	QPoint         coordconv3_2 (const vertex& pos3d) const;               // Convert a 3D point to a 2D point
-	LDOverlay* findOverlayObject (Camera cam);
+	LDOverlay*     findOverlayObject (Camera cam);
 	void           updateRectVerts();
 	void           pick (uint mouseX, uint mouseY);                        // Perform object selection
-	void           setObjectColor (LDObject* obj, const ListType list);    // Set the color to an object list
 	QColor         getTextPen() const;                                     // Determine which color to draw text with
 	
 private slots:
--- a/src/types.h	Wed Sep 04 11:54:17 2013 +0300
+++ b/src/types.h	Sat Sep 07 13:23:09 2013 +0300
@@ -279,6 +279,11 @@
 	std::deque<T> m_vect;
 };
 
+template<class T> static inline T& operator>> (const T& a, List<T>& b) {
+	b.insert (0, a);
+	return b;
+}
+
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================

mercurial