Picking now works with the VAO setup

Fri, 09 Aug 2013 04:29:37 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Fri, 09 Aug 2013 04:29:37 +0300
changeset 442
4852e815df29
parent 441
a958f6925088
child 443
a70dd25dd4bb

Picking now works with the VAO setup

src/gldata.cpp file | annotate | diff | comparison | revisions
src/gldata.h file | annotate | diff | comparison | revisions
src/gldraw.cpp file | annotate | diff | comparison | revisions
--- a/src/gldata.cpp	Fri Aug 09 03:09:08 2013 +0300
+++ b/src/gldata.cpp	Fri Aug 09 04:29:37 2013 +0300
@@ -144,27 +144,34 @@
 // =============================================================================
 // -----------------------------------------------------------------------------
 void VertexCompiler::compilePolygon (
-	LDObject* obj,
-	VertexCompiler::Array** arrays
+	LDObject* drawobj,
+	LDObject* trueobj
 ) {
-	LDObject::Type objtype = obj->getType();
+	// 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.
+	Array** arrays = m_objArrays[trueobj];
+	LDObject::Type objtype = drawobj->getType();
 	bool isline = (objtype == LDObject::Line || objtype == LDObject::CondLine);
-	int verts = isline ? 2 : obj->vertices();
+	int verts = isline ? 2 : drawobj->vertices();
+	
+	QColor normalColor = getObjectColor (trueobj, Normal),
+		pickColor = getObjectColor (trueobj, PickColor);
 	
 	for (int i = 0; i < verts; ++i) {
-		compileVertex (obj->getVertex (i), getObjectColor (obj, Normal),
-			arrays[isline ? EdgeArray : MainArray]);
+		compileVertex (drawobj->getVertex (i), normalColor, arrays[isline ? EdgeArray : MainArray]);
+		compileVertex (drawobj->getVertex (i), pickColor, arrays[isline ? EdgePickArray : PickArray]);
 	}
 	
 	// For non-lines, compile BFC data
 	if (!isline) {
-		QColor col = getObjectColor (obj, BFCFront);
+		QColor col = getObjectColor (trueobj, BFCFront);
 		for (int i = 0; i < verts; ++i)
-			compileVertex (obj->getVertex(i), col, arrays[BFCArray]);
+			compileVertex (drawobj->getVertex(i), col, arrays[BFCArray]);
 		
-		col = getObjectColor (obj, BFCBack);
+		col = getObjectColor (trueobj, BFCBack);
 		for (int i = verts - 1; i >= 0; --i)
-			compileVertex (obj->getVertex(i), col, arrays[BFCArray]);
+			compileVertex (drawobj->getVertex(i), col, arrays[BFCArray]);
 	}
 }
 
@@ -185,18 +192,18 @@
 	
 	switch (obj->getType()) {
 	case LDObject::Triangle:
-		compilePolygon (obj, m_objArrays[obj]);
+		compilePolygon (obj, obj);
 		m_changed[MainArray] = true;
 		break;
 	
 	case LDObject::Quad:
 		for (LDTriangleObject* triangle : static_cast<LDQuadObject*> (obj)->splitToTriangles())
-			compilePolygon (triangle, m_objArrays[obj]);
+			compilePolygon (triangle, obj);
 		m_changed[MainArray] = true;
 		break;
 	
 	case LDObject::Line:
-		compilePolygon (obj, m_objArrays[obj]);
+		compilePolygon (obj, obj);
 		break;
 	
 	default:
@@ -265,8 +272,7 @@
 	if (!obj->isColored())
 		return QColor();
 	
-/*
-	if (list == GL::PickList) {
+	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
@@ -276,14 +282,12 @@
 		// 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,
+		int r = (i / (256 * 256)) % 256,
 			g = (i / 256) % 256,
 			b = i % 256;
 		
-		qglColor (QColor (r, g, b));
-		return;
+		return QColor (r, g, b);
 	}
-*/
 	
 	if ((colotype == BFCFront || colotype == BFCBack) &&
 		obj->getType() != LDObject::Line &&
--- a/src/gldata.h	Fri Aug 09 03:09:08 2013 +0300
+++ b/src/gldata.h	Fri Aug 09 04:29:37 2013 +0300
@@ -13,12 +13,15 @@
 		Normal,
 		BFCFront,
 		BFCBack,
+		PickColor,
 	};
 	
 	enum MergedArrayType {
 		MainArray,
 		EdgeArray,
 		BFCArray,
+		PickArray,
+		EdgePickArray,
 		NumArrays
 	};
 	
@@ -62,7 +65,7 @@
 	QColor getObjectColor (LDObject* obj, ColorType list) const;
 	
 private:
-	void compilePolygon (LDObject* obj, Array** arrays);
+	void compilePolygon (LDObject* drawobj, LDObject* trueobj);
 	void compileVertex (vertex v, QColor col, VertexCompiler::Array* array);
 	
 	QMap<LDObject*, Array**> m_objArrays;
--- a/src/gldraw.cpp	Fri Aug 09 03:09:08 2013 +0300
+++ b/src/gldraw.cpp	Fri Aug 09 04:29:37 2013 +0300
@@ -295,8 +295,6 @@
 		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);
@@ -309,13 +307,17 @@
 		glDisable (GL_CULL_FACE);
 	
 	VertexCompiler::Array* array = g_vertexCompiler.getMergedBuffer (
-		(gl_colorbfc) ? VertexCompiler::BFCArray : VertexCompiler::MainArray);
+		(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 (VertexCompiler::EdgeArray);
+	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));
@@ -886,6 +888,7 @@
 			(*(pixelptr + 0) * 0x10000) +
 			(*(pixelptr + 1) * 0x00100) +
 			(*(pixelptr + 2) * 0x00001);
+		
 		pixelptr += 4;
 		
 		if (idx == 0xFFFFFF)

mercurial