- now actually renders stuff correctly

Fri, 24 Jan 2014 22:10:49 +0200

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Fri, 24 Jan 2014 22:10:49 +0200
changeset 679
dd7545e00a8f
parent 678
0526d8404097
child 680
bf6323f2b1be

- now actually renders stuff correctly

src/GLCompiler.cc file | annotate | diff | comparison | revisions
src/GLCompiler.h file | annotate | diff | comparison | revisions
src/GLRenderer.cc file | annotate | diff | comparison | revisions
src/GLRenderer.h file | annotate | diff | comparison | revisions
--- a/src/GLCompiler.cc	Fri Jan 24 20:46:57 2014 +0200
+++ b/src/GLCompiler.cc	Fri Jan 24 22:10:49 2014 +0200
@@ -7,28 +7,76 @@
 #include "Document.h"
 #include "Misc.h"
 #include "GLRenderer.h"
+#include "Dialogs.h"
+
+static const struct
+{
+	GLenum	value;
+	QString	text;
+}
+g_GLErrors[] =
+{
+	{ GL_NO_ERROR,						"No error" },
+	{ GL_INVALID_ENUM,					"Unacceptable enumerator passed" },
+	{ GL_INVALID_VALUE,					"Numeric argument out of range" },
+	{ GL_INVALID_OPERATION,				"The operation is not allowed to be done in this state" },
+	{ GL_INVALID_FRAMEBUFFER_OPERATION,	"Framebuffer object is not complete"},
+	{ GL_OUT_OF_MEMORY,					"Out of memory" },
+	{ GL_STACK_UNDERFLOW,				"The operation would have caused an underflow" },
+	{ GL_STACK_OVERFLOW,				"The operation would have caused an overflow" },
+};
 
 #define DEBUG_PRINT(...) fprint (stdout, __VA_ARGS__)
 
 extern_cfg (Bool, gl_blackedges);
 static QList<short> g_warnedColors;
-GLCompiler g_vertexCompiler;
 
 static const QColor g_BFCFrontColor(40, 192, 0);
 static const QColor g_BFCBackColor(224, 0, 0);
 
 // =============================================================================
 //
+void checkGLError_private (const char* file, int line)
+{
+	GLenum errnum = glGetError();
+
+	if (errnum == GL_NO_ERROR)
+		return;
+
+	QString errmsg;
+
+	for (const auto& it : g_GLErrors)
+	{
+		if (it.value == errnum)
+		{
+			errmsg = it.text;
+			break;
+		}
+	}
+
+	log ("GL ERROR: %1:%2: %3", file, line, errmsg);
+}
+
+// =============================================================================
+//
 GLCompiler::GLCompiler() :
 	m_Document (null)
 {
+	needMerge();
+}
+
+// =============================================================================
+//
+void GLCompiler::initialize()
+{
 	glGenBuffers (VBO_NumArrays, &m_mainVBOs[0]);
-	needMerge();
+	checkGLError();
 }
 
 GLCompiler::~GLCompiler()
 {
 	glDeleteBuffers (VBO_NumArrays, &m_mainVBOs[0]);
+	checkGLError();
 }
 
 // =============================================================================
@@ -172,9 +220,12 @@
 		m_mainVBOData[type] += (*it)[type];
 
 	glBindBuffer (GL_ARRAY_BUFFER, m_mainVBOs[type]);
+	checkGLError();
 	glBufferData (GL_ARRAY_BUFFER, m_mainVBOData[type].size() * sizeof(float),
 		m_mainVBOData[type].constData(), GL_DYNAMIC_DRAW);
-	glBindBuffer (GL_ARRAY_BUFFER, m_mainVBOs[type]);
+	checkGLError();
+	glBindBuffer (GL_ARRAY_BUFFER, 0);
+	checkGLError();
 	m_changed[type] = false;
 	log ("VBO array %1 prepared: %2 coordinates", (int) type, m_mainVBOData[type].size());
 }
@@ -198,7 +249,7 @@
 {
 	// Ensure we have valid arrays to write to.
 	if (m_objArrays.find (obj) == m_objArrays.end())
-		m_objArrays[obj] = new QVector<float>[VBO_NumArrays];
+		m_objArrays[obj] = new QVector<GLfloat>[VBO_NumArrays];
 	else
 	{
 		// Arrays exist already, clear them.
@@ -241,15 +292,17 @@
 				default: break;
 			}
 
-			QVector<float>* ap = m_objArrays[topobj];
+			QVector<GLfloat>* ap = m_objArrays[topobj];
 			QColor normalColor = getObjectColor (obj, E_NormalColor);
 			QColor pickColor = getObjectColor (topobj, E_PickColor);
 
 			for (int i = 0; i < verts; ++i)
 			{
 				// Write coordinates
-				for (int j = 0; j < 3; ++j)
-					ap[arraynum] << obj->getVertex (i).getCoordinate (j);
+				ap[arraynum]
+					<< obj->getVertex (i).x()
+					<< -obj->getVertex (i).y()
+					<< -obj->getVertex (i).z();
 
 				// Colors
 				writeColor (ap[VBO_NormalColors], normalColor);
@@ -278,7 +331,7 @@
 
 // =============================================================================
 //
-void GLCompiler::writeColor (QVector<float>& array, const QColor& color)
+void GLCompiler::writeColor (QVector<GLfloat>& array, const QColor& color)
 {
 	array	<< color.red()
 			<< color.green()
--- a/src/GLCompiler.h	Fri Jan 24 20:46:57 2014 +0200
+++ b/src/GLCompiler.h	Fri Jan 24 22:10:49 2014 +0200
@@ -84,6 +84,7 @@
 		{
 			return m_mainVBOData[array].size() / 3;
 		}
+		void initialize();
 
 	private:
 		void			compileStaged();
@@ -91,13 +92,14 @@
 		void			compileSubObject (LDObject* obj, LDObject* topobj);
 		void			writeColor (QVector< float >& array, const QColor& color);
 
-		QMap<LDObject*, QVector<float>*>	m_objArrays;
-		QVector<float>						m_mainVBOData[VBO_NumArrays];
+		QMap<LDObject*, QVector<GLfloat>*>	m_objArrays;
+		QVector<GLfloat>					m_mainVBOData[VBO_NumArrays];
 		GLuint								m_mainVBOs[VBO_NumArrays];
 		bool								m_changed[VBO_NumArrays];
 		LDObjectList						m_staged; // Objects that need to be compiled
 };
 
-extern GLCompiler g_vertexCompiler;
+#define checkGLError() { checkGLError_private (__FILE__, __LINE__); }
+void checkGLError_private (const char* file, int line);
 
 #endif // LDFORGE_GLCOMPILER_H
--- a/src/GLRenderer.cc	Fri Jan 24 20:46:57 2014 +0200
+++ b/src/GLRenderer.cc	Fri Jan 24 22:10:49 2014 +0200
@@ -130,6 +130,7 @@
 	setMessageLog (null);
 	m_width = m_height = -1;
 	m_hoverpos = g_origin;
+	m_compiler = new GLCompiler;
 
 	m_toolTipTimer = new QTimer (this);
 	m_toolTipTimer->setSingleShot (true);
@@ -149,32 +150,6 @@
 		info->cam = cam;
 	}
 
-	// Init VBO for axes
-	float axesdata[18];
-	memset (axesdata, 0, sizeof axesdata);
-	float colordata[18];
-
-	for (int i = 0; i < 3; ++i)
-	{
-		for (int j = 0; j < 3; ++j)
-		{
-			axesdata[(i * 6) + j] = g_GLAxes[i].vert.getCoordinate (j);
-			axesdata[(i * 6) + 3 + j] = -g_GLAxes[i].vert.getCoordinate (j);
-		}
-
-		for (int j = 0; j < 2; ++j)
-		{
-			colordata[(i * 6) + (j * 3) + 0] = g_GLAxes[i].col.red();
-			colordata[(i * 6) + (j * 3) + 1] = g_GLAxes[i].col.green();
-			colordata[(i * 6) + (j * 3) + 2] = g_GLAxes[i].col.blue();
-		}
-	}
-
-	glGenBuffers (1, &g_GLAxes_VBO);
-	glBindBuffer (GL_ARRAY_BUFFER, g_GLAxes_VBO);
-	glBufferData (GL_ARRAY_BUFFER, sizeof axesdata, axesdata, GL_STATIC_DRAW);
-	glBindBuffer (GL_ARRAY_BUFFER, g_GLAxes_VBO);
-
 	calcCameraIcons();
 }
 
@@ -187,6 +162,8 @@
 
 	for (CameraIcon& info : m_cameraIcons)
 		delete info.img;
+
+	delete m_compiler;
 }
 
 // =============================================================================
@@ -280,7 +257,40 @@
 	setMouseTracking (true);
 	setFocusPolicy (Qt::WheelFocus);
 
-	g_vertexCompiler.compileDocument();
+	m_compiler->initialize();
+	m_compiler->compileDocument();
+
+	initializeAxes();
+}
+
+// =============================================================================
+//
+void GLRenderer::initializeAxes()
+{
+	float axesdata[18];
+	memset (axesdata, 0, sizeof axesdata);
+	float colordata[18];
+
+	for (int i = 0; i < 3; ++i)
+	{
+		for (int j = 0; j < 3; ++j)
+		{
+			axesdata[(i * 6) + j] = g_GLAxes[i].vert.getCoordinate (j);
+			axesdata[(i * 6) + 3 + j] = -g_GLAxes[i].vert.getCoordinate (j);
+		}
+
+		for (int j = 0; j < 2; ++j)
+		{
+			colordata[(i * 6) + (j * 3) + 0] = g_GLAxes[i].col.red();
+			colordata[(i * 6) + (j * 3) + 1] = g_GLAxes[i].col.green();
+			colordata[(i * 6) + (j * 3) + 2] = g_GLAxes[i].col.blue();
+		}
+	}
+
+	glGenBuffers (1, &g_GLAxes_VBO);
+	glBindBuffer (GL_ARRAY_BUFFER, g_GLAxes_VBO);
+	glBufferData (GL_ARRAY_BUFFER, sizeof axesdata, axesdata, GL_STATIC_DRAW);
+	glBindBuffer (GL_ARRAY_BUFFER, 0);
 }
 
 // =============================================================================
@@ -324,7 +334,7 @@
 // -----------------------------------------------------------------------------
 void GLRenderer::hardRefresh()
 {
-	g_vertexCompiler.compileDocument();
+	m_compiler->compileDocument();
 	refresh();
 
 	glLineWidth (gl_linethickness);
@@ -400,8 +410,11 @@
 	if (gl_axes)
 	{
 		glBindBuffer (GL_ARRAY_BUFFER, g_GLAxes_VBO);
+		checkGLError();
 		glVertexPointer (3, GL_FLOAT, 0, NULL);
+		checkGLError();
 		glDrawArrays (GL_LINES, 0, 6);
+		checkGLError();
 	}
 
 	drawVBOs (VBO_Triangles, GL_TRIANGLES);
@@ -410,7 +423,9 @@
 	drawVBOs (VBO_CondLines, GL_LINES);
 
 	glPopMatrix();
+	glBindBuffer (GL_ARRAY_BUFFER, 0);
 	glDisableClientState (GL_VERTEX_ARRAY);
+	checkGLError();
 	glDisable (GL_CULL_FACE);
 	glMatrixMode (GL_MODELVIEW);
 	glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
@@ -420,17 +435,20 @@
 //
 void GLRenderer::drawVBOs (E_VBOArray arrayType, GLenum type)
 {
-	g_vertexCompiler.prepareVBOArray (arrayType);
-	GLuint idx = g_vertexCompiler.getVBOIndex (arrayType);
-	GLsizei count = g_vertexCompiler.getVBOCount (arrayType);
+	m_compiler->prepareVBOArray (arrayType);
+	GLuint idx = m_compiler->getVBOIndex (arrayType);
+	GLsizei count = m_compiler->getVBOCount (arrayType);
 
 	if (count > 0)
 	{
 		glBindBuffer (GL_ARRAY_BUFFER, idx);
+		checkGLError();
 		glVertexPointer (3, GL_FLOAT, 0, null);
+		checkGLError();
 		// glColorPointer (4, GL_UNSIGNED_BYTE, sizeof (GLCompiler::VAO), &array->data()[0].color);
 		// glVertexAttribPointer (idx, 3, GL_FLOAT, GL_FALSE, 0, null);
 		glDrawArrays (type, 0, count);
+		checkGLError();
 	}
 }
 
@@ -829,7 +847,7 @@
 //
 void GLRenderer::compileAllObjects()
 {
-	g_vertexCompiler.compileDocument();
+	m_compiler->compileDocument();
 }
 
 // =============================================================================
@@ -1285,7 +1303,7 @@
 void GLRenderer::setFile (LDDocument* const& a)
 {
 	m_File = a;
-	g_vertexCompiler.setDocument (a);
+	m_compiler->setDocument (a);
 
 	if (a != null)
 	{
@@ -1559,7 +1577,7 @@
 //
 void GLRenderer::compileObject (LDObject* obj)
 {
-	g_vertexCompiler.stageForCompilation (obj);
+	m_compiler->stageForCompilation (obj);
 
 	// Mark in known vertices of this object
 	QList<Vertex> verts = getVertices (obj);
--- a/src/GLRenderer.h	Fri Jan 24 20:46:57 2014 +0200
+++ b/src/GLRenderer.h	Fri Jan 24 22:10:49 2014 +0200
@@ -25,6 +25,7 @@
 #include "Document.h"
 #include "GLShared.h"
 
+class GLCompiler;
 class MessageManager;
 class QDialogButtonBox;
 class RadioGroup;
@@ -217,6 +218,7 @@
 		Vertex						m_rectverts[4];
 		QColor						m_bgcolor;
 		QList<Vertex>				m_knownVerts;
+		GLCompiler*					m_compiler;
 
 		void           addDrawnVertex (Vertex m_hoverpos);
 		LDOverlay*     findOverlayObject (EFixedCamera cam);
@@ -297,6 +299,7 @@
 
 	private slots:
 		void           slot_toolTipTimer();
+		void initializeAxes();
 };
 
 // Alias for short namespaces

mercurial