Further work on VAO rendering

Sat, 07 Sep 2013 16:40:05 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Sat, 07 Sep 2013 16:40:05 +0300
changeset 489
0b32138fedcc
parent 488
0ea49207a4ec
child 490
fff86085017e

Further work on VAO rendering

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/gui.cpp file | annotate | diff | comparison | revisions
src/ldtypes.cpp file | annotate | diff | comparison | revisions
--- a/src/gldata.cpp	Sat Sep 07 14:21:33 2013 +0300
+++ b/src/gldata.cpp	Sat Sep 07 16:40:05 2013 +0300
@@ -7,6 +7,7 @@
 
 cfg (Bool, gl_blackedges, false);
 static List<short> g_warnedColors;
+VertexCompiler g_vertexCompiler;
 
 // =============================================================================
 // -----------------------------------------------------------------------------
@@ -155,8 +156,8 @@
 // =============================================================================
 // -----------------------------------------------------------------------------
 void VertexCompiler::compileObject (LDObject* obj, LDObject* topobj) {
-	print ("compile %1 (%2, %3)\n", obj->id(), obj->typeName(), topobj->id());
 	List<LDObject*> objs;
+	m_objArrays[obj].clear();
 	
 	switch (obj->getType()) {
 	case LDObject::Triangle:
@@ -183,8 +184,6 @@
 		break;
 	}
 	
-	print ("-> %1\n", m_objArrays[obj].size());
-	
 	// Set all of m_changed to true
 	memset (m_changed, 0xFF, sizeof m_changed);
 }
@@ -210,8 +209,8 @@
 
 // =============================================================================
 // -----------------------------------------------------------------------------
-const VertexCompiler::Array* VertexCompiler::getMergedBuffer (ArrayType type) {
-	assert (type < NumArrays);
+const VertexCompiler::Array* VertexCompiler::getMergedBuffer (GL::VAOType type) {
+	assert (type < GL::NumArrays);
 	
 	if (m_changed[type]) {
 		m_changed[type] = false;
@@ -221,7 +220,7 @@
 			if (!obj->isScemantic())
 				continue;
 			
-			const bool islinearray = (type == EdgeArray || type == EdgePickArray);
+			const bool islinearray = (type == GL::EdgeArray || type == GL::EdgePickArray);
 			auto it = m_objArrays.find (obj);
 			
 			if (it != m_objArrays.end()) {
@@ -229,13 +228,17 @@
 				
 				for (const CompiledTriangle& i : data) {
 					if (i.isCondLine) {
-						if (type != EdgePickArray && type != CondEdgeArray)
+						// Conditional lines go to the edge pick array and the array
+						// specifically designated for conditional lines and nowhere else.
+						if (type != GL::EdgePickArray && type != GL::CondEdgeArray)
 							continue;
 					} else {
+						// Lines and only lines go to the line array and only to the line array.
 						if ((i.numVerts == 2) ^ islinearray)
 							continue;
 						
-						if (type == CondEdgeArray)
+						// Only conditional lines go into the conditional line array
+						if (type == GL::CondEdgeArray)
 							continue;
 					}
 					
@@ -255,7 +258,7 @@
 // =============================================================================
 // This turns a compiled triangle into usable VAO vertices
 // -----------------------------------------------------------------------------
-VertexCompiler::Array* VertexCompiler::postprocess (const CompiledTriangle& triangle, ArrayType type) {
+VertexCompiler::Array* VertexCompiler::postprocess (const CompiledTriangle& triangle, GL::VAOType type) {
 	Array* va = new Array;
 	List<Vertex> verts;
 	
@@ -267,27 +270,27 @@
 		v.z = v0.z();
 		
 		switch (type) {
-		case MainArray:
-		case EdgeArray:
-		case CondEdgeArray:
+		case GL::MainArray:
+		case GL::EdgeArray:
+		case GL::CondEdgeArray:
 			v.color = triangle.rgb;
 			break;
 		
-		case PickArray:
-		case EdgePickArray:
+		case GL::PickArray:
+		case GL::EdgePickArray:
 			v.color = triangle.pickrgb;
 		
-		case BFCArray:
-			break;
+		case GL::BFCArray:
+			break; // handled separately
 		
-		case NumArrays:
+		case GL::NumArrays:
 			assert (false);
 		}
 		
 		verts << v;
 	}
 	
-	if (type == BFCArray) {
+	if (type == GL::BFCArray) {
 		int32 rgb = getObjectColor (triangle.obj, BFCFront).rgb();
 		for (Vertex v : verts) {
 			v.color = rgb;
@@ -379,7 +382,7 @@
 				if (obj->color() == i)
 					return Qt::black;
 			
-			print ("%1: Unknown color %2!\n", __func__, obj->color());
+			log ("%1: Unknown color %2!\n", __func__, obj->color());
 			g_warnedColors << obj->color();
 			return Qt::black;
 		}
--- a/src/gldata.h	Sat Sep 07 14:21:33 2013 +0300
+++ b/src/gldata.h	Sat Sep 07 16:40:05 2013 +0300
@@ -2,6 +2,7 @@
 #define LDFORGE_GLDATA_H
 
 #include "types.h"
+#include "gldraw.h"
 #include <QMap>
 #include <QRgb>
 
@@ -45,16 +46,6 @@
 		PickColor,
 	};
 	
-	enum ArrayType {
-		MainArray,
-		EdgeArray,
-		CondEdgeArray,
-		BFCArray,
-		PickArray,
-		EdgePickArray,
-		NumArrays
-	};
-	
 	struct CompiledTriangle {
 		vertex    verts[3];
 		uint8     numVerts;
@@ -99,20 +90,22 @@
 	void compileFile();
 	void compileObject (LDObject* obj, LDObject* topobj);
 	void forgetObject (LDObject* obj);
-	const Array* getMergedBuffer (ArrayType type);
+	const Array* getMergedBuffer (GL::VAOType 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);
+	Array* postprocess (const CompiledTriangle& i, GL::VAOType type);
 	
 	QMap<LDObject*, List<CompiledTriangle>> m_objArrays;
 	QMap<LDFile*, Array*> m_fileCache;
-	Array m_mainArrays[NumArrays];
+	Array m_mainArrays[GL::NumArrays];
 	LDFile* m_file;
-	bool m_changed[NumArrays];
+	bool m_changed[GL::NumArrays];
 };
 
+extern VertexCompiler g_vertexCompiler;
+
 #endif // LDFORGE_GLDATA_H
\ No newline at end of file
--- a/src/gldraw.cpp	Sat Sep 07 14:21:33 2013 +0300
+++ b/src/gldraw.cpp	Sat Sep 07 16:40:05 2013 +0300
@@ -91,14 +91,6 @@
 	{ 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;
-
 // =============================================================================
 // -----------------------------------------------------------------------------
 GLRenderer::GLRenderer (QWidget* parent) : QGLWidget (parent) {
@@ -306,7 +298,7 @@
 		glRotatef (m_rotZ, 0.0f, 0.0f, 1.0f);
 	}
 	
-	// Draw the polygons
+	// Draw the VAOs now
 	glEnableClientState (GL_VERTEX_ARRAY);
 	glEnableClientState (GL_COLOR_ARRAY);
 	glDisableClientState (GL_NORMAL_ARRAY);
@@ -317,33 +309,15 @@
 	} else
 		glDisable (GL_CULL_FACE);
 	
-	const VertexCompiler::Array* 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));
+	drawVAOs ((m_picking ? PickArray : gl_colorbfc ? BFCArray : MainArray), GL_TRIANGLES);
+	drawVAOs ((m_picking ? EdgePickArray : 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.
 	if (!m_picking) {
-		array = g_vertexCompiler.getMergedBuffer (VertexCompiler::CondEdgeArray);
 		glEnable (GL_LINE_STIPPLE);
-		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));
+		drawVAOs (CondEdgeArray, GL_LINES);
 		glDisable (GL_LINE_STIPPLE);
 	}
 	
@@ -354,6 +328,14 @@
 
 // =============================================================================
 // -----------------------------------------------------------------------------
+void GLRenderer::drawVAOs (VAOType arrayType, GLenum type) {
+	const VertexCompiler::Array* array = g_vertexCompiler.getMergedBuffer (arrayType);
+	glVertexPointer (3, GL_FLOAT, sizeof (VertexCompiler::Vertex), &array->data()[0].x);
+	glColorPointer (4, GL_UNSIGNED_BYTE, sizeof (VertexCompiler::Vertex), &array->data()[0].color);
+	glDrawArrays (type, 0, array->writtenSize() / sizeof (VertexCompiler::Vertex));
+}
+
+// =============================================================================
 // This converts a 2D point on the screen to a 3D point in the model. If 'snap'
 // is true, the 3D point will snap to the current grid.
 // -----------------------------------------------------------------------------
@@ -873,9 +855,9 @@
 	
 	glGetIntegerv (GL_VIEWPORT, viewport);
 	
-	short x0 = mouseX,
-		y0 = mouseY;
-	short x1, y1;
+	int x0 = mouseX,
+		y0 = mouseY,
+		x1, y1;
 	
 	// Determine how big an area to read - with range picking, we pick by
 	// the area given, with single pixel picking, we use an 1 x 1 area.
@@ -895,14 +877,14 @@
 		dataswap (y0, y1);
 	
 	// Clamp the values to ensure they're within bounds
-	x0 = max<short> (0, x0);
-	y0 = max<short> (0, y0);
-	x1 = min<short> (x1, m_width);
-	y1 = min<short> (y1, m_height);
+	x0 = max (0, x0);
+	y0 = max (0, y0);
+	x1 = min (x1, m_width);
+	y1 = min (y1, m_height);
 	
-	const short areawidth = (x1 - x0);
-	const short areaheight = (y1 - y0);
-	const long numpixels = areawidth * areaheight;
+	const int areawidth = (x1 - x0);
+	const int areaheight = (y1 - y0);
+	const int64 numpixels = areawidth * areaheight;
 	
 	// Allocate space for the pixel data.
 	uchar* const pixeldata = new uchar[4 * numpixels];
@@ -916,8 +898,10 @@
 	LDObject* removedObj = null;
 	
 	// Go through each pixel read and add them to the selection.
-	for (long i = 0; i < numpixels; ++i) {
-		long idx =
+	for (int64 i = 0; i < numpixels; ++i) {
+		printf ("Color: #%X%X%X\n", pixelptr[0], pixelptr[1], pixelptr[2]);
+		
+		int32 idx =
 			(*(pixelptr + 0) * 0x10000) +
 			(*(pixelptr + 1) * 0x00100) +
 			(*(pixelptr + 2) * 0x00001);
@@ -928,6 +912,10 @@
 			continue; // White is background; skip
 		
 		LDObject* obj = LDObject::fromID (idx);
+		if (!obj) {
+			log ("WARNING: Object #%1 doesn't exist!", idx);
+			continue;
+		}
 		
 		// If this is an additive single pick and the object is currently selected,
 		// we remove it from selection instead.
--- a/src/gldraw.h	Sat Sep 07 14:21:33 2013 +0300
+++ b/src/gldraw.h	Sat Sep 07 16:40:05 2013 +0300
@@ -67,6 +67,16 @@
 	enum Camera { Top, Front, Left, Bottom, Back, Right, Free };
 	enum ListType { NormalList, PickList, BFCFrontList, BFCBackList };
 	
+	enum VAOType {
+		MainArray,
+		EdgeArray,
+		CondEdgeArray,
+		BFCArray,
+		PickArray,
+		EdgePickArray,
+		NumArrays
+	};
+	
 	GLRenderer (QWidget* parent = null);
 	~GLRenderer();
 	
@@ -131,8 +141,8 @@
 	QPoint m_pos, m_globalpos, m_rangeStart;
 	QPen m_thickBorderPen, m_thinBorderPen;
 	Camera m_camera, m_toolTipCamera;
-	uint m_axeslist;
-	ushort m_width, m_height;
+	GLuint m_axeslist;
+	int m_width, m_height;
 	List<vertex> m_drawedVerts;
 	bool m_rectdraw;
 	vertex m_rectverts[4];
@@ -147,6 +157,7 @@
 	void           clampAngle (double& angle) const;                       // Clamps an angle to [0, 360]
 	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
+	void           drawVAOs (VAOType arrayType, GLenum type);              // Draw a VAO array
 	LDOverlay*     findOverlayObject (Camera cam);
 	void           updateRectVerts();
 	void           pick (uint mouseX, uint mouseY);                        // Perform object selection
--- a/src/gui.cpp	Sat Sep 07 14:21:33 2013 +0300
+++ b/src/gui.cpp	Sat Sep 07 16:40:05 2013 +0300
@@ -501,10 +501,14 @@
 void ForgeWindow::updateSelection() {
 	g_bSelectionLocked = true;
 	
+	print ("1\n");
 	for (LDObject* obj : LDFile::current()->objects())
 		obj->setSelected (false);
 	
+	print ("2\n");
 	ui->objectList->clearSelection();
+	
+	print ("3\n");
 	for (LDObject* obj : m_sel) {
 		if (obj->qObjListEntry == null)
 			continue;
@@ -513,6 +517,7 @@
 		obj->setSelected (true);
 	}
 	
+	print ("4\n");
 	g_bSelectionLocked = false;
 	slot_selectionChanged();
 }
--- a/src/ldtypes.cpp	Sat Sep 07 14:21:33 2013 +0300
+++ b/src/ldtypes.cpp	Sat Sep 07 16:40:05 2013 +0300
@@ -24,6 +24,7 @@
 #include "history.h"
 #include "gldraw.h"
 #include "colors.h"
+#include "gldata.h"
 
 cfg (String, ld_defaultname, "");
 cfg (String, ld_defaultuser, "");
@@ -265,6 +266,8 @@
 	
 	if (pos < g_LDObjects.size())
 		g_LDObjects.erase (pos);
+	
+	g_vertexCompiler.forgetObject (this);
 }
 
 // =============================================================================

mercurial