src/gl/compiler.h

Sun, 26 Jan 2020 00:55:36 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 26 Jan 2020 00:55:36 +0200
changeset 31
b6df269a2c6b
parent 30
1536f23cfab7
child 33
4c41bfe2ec6e
permissions
-rw-r--r--

fix remaining rendering control issues

/*
 *  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/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <QMap>
#include <QSet>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLBuffer>
#include <QOpenGLShaderProgram>

class Model;
class DocumentManager;

namespace gl
{
	class Compiler;
	class Renderer;
	struct VboAddress
	{
		ArrayClass vboClass;
		VboSubclass vboSubclass;
	};
	int vboIndex(const VboAddress vboAddress);
	QMatrix4x4 toQMatrix(const glm::mat4& matrix);
}

class gl::Compiler : public QObject, protected QOpenGLFunctions
{
	Q_OBJECT
public:
	Compiler(const ColorTable& colorTable, QObject* parent);
	~Compiler();
	void build(Model* model, DocumentManager* context);
	void buildPolygon(Polygon polygon, std::vector<GLfloat>* vboData);
	std::size_t vboSize(gl::ArrayClass arrayClass) const;
	QColor getColorForPolygon(const gl::Polygon& polygon);
	Point3D 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:
	bool m_vboChanged[gl::numVbos] = {true};
	std::size_t storedVboSizes[gl::numVbos] = {0_z};
	bool initialized = false;
	BoundingBox boundingBox;
	const 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