src/gl/compiler.h

Sun, 02 Feb 2020 00:51:06 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 02 Feb 2020 00:51:06 +0200
changeset 44
c6114b3af3a6
parent 39
caac957e9834
child 46
98645c8e7704
permissions
-rw-r--r--

added configurable line thickness

/*
 *  LDForge: LDraw parts authoring CAD
 *  Copyright (C) 2013 - 2018 Teemu Piippo
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once
#include "main.h"
#include "gl/common.h"
#include "types/boundingbox.h"
#include <glm/gtc/type_ptr.hpp>
#include <QMap>
#include <QSet>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLBuffer>
#include <QOpenGLShaderProgram>

class Model;
class DocumentManager;

namespace gl
{
	class Compiler;

	struct Vertex
	{
		glm::vec3 position;
		glm::vec4 color;
		glm::vec3 normal;
	};
}

class gl::Compiler : public QObject, protected QOpenGLFunctions
{
	Q_OBJECT
public:
	Compiler(const ldraw::ColorTable& colorTable, QObject* parent);
	~Compiler();
	void build(Model* model, DocumentManager* context, const RenderPreferences& preferences);
	void buildPolygon(Polygon polygon, std::vector<Vertex>* vboData, const gl::RenderPreferences& preferences);
	std::size_t vertexCount(gl::ArrayClass arrayClass) const;
	QColor getColorForPolygon(const gl::Polygon& polygon, const RenderPreferences& preferences);
	glm::vec3 modelCenter() const;
	double modelDistance() const;
	void initialize();
	void bindVertexArray(gl::ArrayClass arrayClass);
	void releaseVertexArray(gl::ArrayClass arrayClass);
	void buildShaders(int arrayId);

	template<typename T>
	void setUniform(const char* uniformName, T&& value)
	{
		Q_ASSERT(this->initialized);
		for (auto& object : this->glObjects)
		{
			object.program->bind();
			const int location = glGetUniformLocation(object.program->programId(), uniformName);
			Q_ASSERT(location != -1);
			object.program->setUniformValue(location, std::forward<T>(value));
			object.program->release();
		}
	}

	template<typename Float, glm::qualifier Prec>
	void setUniformMatrix(const char* uniformName, const glm::mat<4, 4, Float, Prec>& value)
	{
		const float (*array)[4][4] = reinterpret_cast<const float(*)[4][4]>(glm::value_ptr(value));
		this->setUniform(uniformName, *array);
	}
private:
	std::size_t storedVertexCounts[gl::NUM_ARRAY_CLASSES] = {0_z};
	bool initialized = false;
	BoundingBox boundingBox;
	const ldraw::ColorTable& colorTable;
	struct
	{
		QOpenGLShaderProgram* program = nullptr;
		QOpenGLBuffer buffer{QOpenGLBuffer::VertexBuffer};
		QOpenGLVertexArrayObject vertexArray;
	} glObjects[gl::NUM_ARRAY_CLASSES];
};

#define CHECK_GL_ERROR() { checkGLError(__FILE__, __LINE__); }
void checkGLError (QString file, int line);

mercurial