src/gldata.h

Mon, 20 Jan 2014 15:04:26 +0200

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Mon, 20 Jan 2014 15:04:26 +0200
changeset 667
31540c1f22ea
parent 666
c595cfb4791c
child 668
6a05c4969074
permissions
-rw-r--r--

- Merge commit '5c835eb' into HEAD

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

#ifndef LDFORGE_GLDATA_H
#define LDFORGE_GLDATA_H

#include "types.h"
#include "gldraw.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,
		};

		struct CompiledTriangle
		{	vertex    verts[3];
			uint8     numVerts;    // 2 if a line
			QRgb      rgb;         // Color of this poly normally
			QRgb      pickrgb;     // Color of this poly while picking
			bool      isCondLine;  // Is this a conditional line?
			LDObject* obj;         // Pointer to the object this poly represents
		};

		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 forgetObject (LDObject* obj);
		void initObject (LDObject* obj);
		const Array* getMergedBuffer (GL::VAOType type);
		QColor getObjectColor (LDObject* obj, ColorType list) const;
		void needMerge();
		void stageForCompilation (LDObject* obj);

		static uint32 getColorRGB (QColor& color);

	private:
		void compilePolygon (LDObject* drawobj, LDObject* trueobj, QList<CompiledTriangle>& data);
		void compileObject (LDObject* obj);
		void compileSubObject (LDObject* obj, LDObject* topobj, QList<CompiledTriangle>& data);
		Array* postprocess (const CompiledTriangle& i, GL::VAOType type);

		QMap<LDObject*, Array*> m_objArrays;
		QMap<LDFile*, Array*> m_fileCache;
		Array m_mainArrays[GL::NumArrays];
		LDFile* m_file;
		bool m_changed[GL::NumArrays];
		QList<LDObject*> m_staged;
};

extern VertexCompiler g_vertexCompiler;

#endif // LDFORGE_GLDATA_H

mercurial