src/gl/compiler.cpp

Sun, 26 Jan 2020 01:06:27 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 26 Jan 2020 01:06:27 +0200
changeset 32
767592024ec5
parent 30
1536f23cfab7
child 33
4c41bfe2ec6e
permissions
-rw-r--r--

fix default angle

/*
 *  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/>.
 */

#define GL_GLEXT_PROTOTYPES
#include <GL/glu.h>
#include <GL/glext.h>
#include <QMessageBox>
#include "gl/compiler.h"
#include "documentmanager.h"
#include "invert.h"
#include "ring.h"

static const char* vertexShaderSource = R"(
#version 330 core

layout(location=0) in vec3 position;
layout(location=1) in vec4 color;
out vec4 vColor;
uniform mat4 CameraTransformation;

void main()
{
	vColor = color;
	gl_Position = CameraTransformation * vec4(position, 1.0);
}
)";

static const char* fragmentShaderSource = R"(
#version 330 core

in vec4 vColor;
out vec4 fColor;

void main()
{
	fColor = vColor;
}
)";

gl::Compiler::Compiler(const ColorTable& colorTable, QObject* parent) :
	QObject{parent},
	colorTable{colorTable}
{
}

gl::Compiler::~Compiler()
{
}

void gl::Compiler::initialize()
{
	if (not this->initialized)
	{
		this->initializeOpenGLFunctions();
		for (int i = 0; i < gl::NUM_ARRAY_CLASSES; i += 1)
		{
			auto& object = this->glObjects[i];
			object.program = new QOpenGLShaderProgram;
			object.program->create();
			const bool vertexShaderCompiled = object.program->addShaderFromSourceCode(QOpenGLShader::Vertex, ::vertexShaderSource);
			QString log;
			if (not vertexShaderCompiled)
			{
				log += "\n" + tr("Vertex shader:") + "\n" + object.program->log();
			}
			const bool fragmentShaderCompiled = object.program->addShaderFromSourceCode(QOpenGLShader::Fragment, ::fragmentShaderSource);
			if (not fragmentShaderCompiled)
			{
				log += "\n" + tr("Fragment shader:") + "\n" + object.program->log();
			}
			if (not vertexShaderCompiled or not fragmentShaderCompiled)
			{
				QMessageBox::critical(nullptr, tr("Shader compile error"), tr("Could not compile shaders.") + "\n" + log);
				std::exit(-1);
			}
			object.program->link();
			object.program->bind();
			object.buffer.create();
			object.buffer.bind();
			object.buffer.setUsagePattern(QOpenGLBuffer::DynamicDraw);
			object.vertexArray.create();
			object.vertexArray.bind();
			object.program->enableAttributeArray(0);
			object.program->enableAttributeArray(1);
			constexpr int stride = 7 * sizeof(GLfloat);
			object.program->setAttributeBuffer(0, GL_FLOAT, 0 * sizeof(GLfloat), 3, stride);
			object.program->setAttributeBuffer(1, GL_FLOAT, 3 * sizeof(GLfloat), 4, stride);
			object.vertexArray.release();
			object.buffer.release();
			object.program->release();
		}
		this->initialized = true;
	}
}

void gl::Compiler::build(Model* model, DocumentManager* context)
{
	this->boundingBox = {};
	std::vector<GLfloat> vboData[gl::NUM_ARRAY_CLASSES];
	const std::vector<gl::Polygon> polygons = model->getPolygons(context);
	for (const gl::Polygon& polygon : polygons)
	{
		this->buildPolygon(polygon, vboData);
	}
	for (int arrayId = 0; arrayId < gl::NUM_ARRAY_CLASSES; arrayId += 1)
	{
		auto& buffer = this->glObjects[arrayId].buffer;
		auto& vector = vboData[arrayId];
		this->storedVboSizes[arrayId] = vector.size();
		buffer.bind();
		buffer.allocate(vector.data(), static_cast<int>(vector.size() * sizeof vector[0]));
		buffer.release();
	}
}

gl::ArrayClass classifyPolygon(const gl::Polygon& polygon)
{
	switch (polygon.type)
	{
	case gl::Polygon::EdgeLine:
		return gl::ArrayClass::Lines;
	case gl::Polygon::Triangle:
		return gl::ArrayClass::Triangles;
	case gl::Polygon::Quadrilateral:
		return gl::ArrayClass::Quads;
	case gl::Polygon::ConditionalEdge:
		return gl::ArrayClass::ConditionalLines;
	}
	return gl::ArrayClass::Lines;
}

[[maybe_unused]]
static QColor colorFromId(linetypes::Id id)
{
	// Calculate a color based from this index. This method caters for
	// 16777216 objects. I don't think that will be exceeded anytime soon.
	const int r = (id.value / 0x10000) % 0x100;
	const int g = (id.value / 0x100) % 0x100;
	const int b = id.value % 0x100;
	return {r, g, b};
}

void gl::Compiler::buildPolygon(gl::Polygon polygon, std::vector<GLfloat>* vboData)
{
	const gl::ArrayClass vboClass = classifyPolygon(polygon);
	//std::vector<GLfloat>& vertexBuffer = vboData[gl::vboIndex({vboClass, gl::VboSubclass::VertexData})];
	std::vector<GLfloat>& vertexBuffer = vboData[static_cast<int>(vboClass)];
	/*
	std::vector<GLfloat>& normalsBuffer = vboData[gl::vboIndex({vboClass, gl::VboSubclass::Normals})];
	auto vertexRing = iter::ring(polygon.vertices, polygon.numPolygonVertices());
	reserveMore(normalsBuffer, polygon.numPolygonVertices() * 3_z);
	for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1)
	{
		const Point3D& v1 = vertexRing[i - 1];
		const Point3D& v2 = vertexRing[i];
		const Point3D& v3 = vertexRing[i + 1];
		const QVector3D normal = QVector3D::crossProduct(v3 - v2, v1 - v2).normalized();
		for (const GLfloat coord : {normal.x(), normal.y(), normal.z()})
			normalsBuffer.push_back(coord);
	}
	*/
	reserveMore(vertexBuffer, polygon.numPolygonVertices() * 7);
	const QColor color = this->getColorForPolygon(polygon);
	// Transform vertices so that they're suitable for GL rendering
	for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1)
	{
		Point3D& point = polygon.vertices[i];
		this->boundingBox.consider(polygon.vertices[i]);
		vertexBuffer.push_back(static_cast<GLfloat>(point.x));
		vertexBuffer.push_back(static_cast<GLfloat>(point.y));
		vertexBuffer.push_back(static_cast<GLfloat>(point.z));
		vertexBuffer.push_back(static_cast<GLfloat>(color.redF()));
		vertexBuffer.push_back(static_cast<GLfloat>(color.greenF()));
		vertexBuffer.push_back(static_cast<GLfloat>(color.blueF()));
		vertexBuffer.push_back(static_cast<GLfloat>(color.alphaF()));
	}
}

QColor gl::Compiler::getColorForPolygon(const gl::Polygon& polygon)
{
	QColor color;
	// For normal colors, use the polygon's color.
	if (polygon.color == colors::main)
	{
		color = {255, 255, 64}; // mainColorRepresentation();
	}
	else if (polygon.color == colors::edge)
	{
		// Edge color is black, unless we have a dark background, in which case lines need to be bright.
		color = Qt::black; //luma(config::backgroundColor()) > 40 ? Qt::black : Qt::white;
	}
	else
	{
		// Not main or edge color, use the polygon's color as is.
		color = this->colorTable[polygon.color].faceColor;
	}
	return color;
}

Point3D gl::Compiler::modelCenter() const
{
	return boxCenter(this->boundingBox);
}

double gl::Compiler::modelDistance() const
{
	return longestMeasure(this->boundingBox);
}

void gl::Compiler::bindVertexArray(gl::ArrayClass arrayClass)
{
	auto& object = this->glObjects[static_cast<int>(arrayClass)];
	object.vertexArray.bind();
	object.program->bind();
}

void gl::Compiler::releaseVertexArray(gl::ArrayClass arrayClass)
{
	auto& object = this->glObjects[static_cast<int>(arrayClass)];
	object.program->release();
	object.vertexArray.release();
}

std::size_t gl::Compiler::vboSize(const gl::ArrayClass arrayClass) const
{
	return this->storedVboSizes[static_cast<int>(arrayClass)];
}

int gl::vboIndex(const VboAddress vboAddress)
{
	return static_cast<std::uint8_t>(vboAddress.vboClass) * gl::numVboSubclasses
		+ static_cast<std::uint8_t>(vboAddress.vboSubclass);
}

QMatrix4x4 gl::toQMatrix(const glm::mat4& matrix)
{
	return  {
		matrix[0][0],
		matrix[0][1],
		matrix[0][2],
		matrix[0][3],
		matrix[1][0],
		matrix[1][1],
		matrix[1][2],
		matrix[1][3],
		matrix[2][0],
		matrix[2][1],
		matrix[2][2],
		matrix[2][3],
		matrix[3][0],
		matrix[3][1],
		matrix[3][2],
		matrix[3][3],
	};
}

mercurial