src/gl/common.h

Fri, 07 Feb 2020 01:58:34 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 07 Feb 2020 01:58:34 +0200
changeset 48
3c10f0e2fbe0
parent 46
98645c8e7704
child 53
3af627f7a40f
permissions
-rw-r--r--

added selection highlighting

/*
 *  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 <QColor>
#include <QOpenGLFunctions>
#include <QGenericMatrix>
#include "basics.h"
#include "colors.h"

namespace gl
{
	// Transformation matrices for projection cameras.
	static const QMatrix4x4 topCameraMatrix = {};
	static const QMatrix4x4 frontCameraMatrix = {1, 0, 0, 0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1};
	static const QMatrix4x4 leftCameraMatrix = {0, -1, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, 0, 0, 1};
	static const QMatrix4x4 bottomCameraMatrix = {1, 0, 0, 0, 0, -1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1};
	static const QMatrix4x4 backCameraMatrix = {-1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1};
	static const QMatrix4x4 rightCameraMatrix = {0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1};
	static constexpr QRgb BlackRgb = 0xff000000;
	struct Polygon;
}

struct gl::Polygon
{
	enum Type : qint8
	{
		EdgeLine,
		Triangle,
		Quadrilateral,
		ConditionalEdge
	};
	Type type;
	glm::vec3 vertices[4];
	ldraw::Color color;
	ldraw::Id id;

	/**
	 * @return amount of vertices used for geometry
	 */
	inline unsigned int numPolygonVertices() const
	{
		if (type == Type::ConditionalEdge)
			return 2;
		else
			return numVertices();
	}

	/**
	 * @return amount of vertices
	 */
	inline unsigned int numVertices() const
	{
		switch (type)
		{
		case Type::EdgeLine:
			return 2;
		case Type::Triangle:
			return 3;
		case Type::ConditionalEdge:
		case Type::Quadrilateral:
			return 4;
		}
		return 0;
	}
};

Q_DECLARE_METATYPE(gl::Polygon)

namespace gl
{
	inline Polygon edgeLine(const glm::vec3& v_1, const glm::vec3& v_2, ldraw::Color color, ldraw::Id id)
	{
		return {Polygon::EdgeLine, {v_1, v_2}, color, id};
	}

	inline Polygon triangle(
		const glm::vec3& v_1,
		const glm::vec3& v_2,
		const glm::vec3& v_3,
		ldraw::Color color,
		ldraw::Id id)
	{
		return {Polygon::Triangle, {v_1, v_2, v_3}, color, id};
	}

	inline Polygon quadrilateral(
		const glm::vec3& v_1,
		const glm::vec3& v_2,
		const glm::vec3& v_3,
		const glm::vec3& v_4,
		ldraw::Color color,
		ldraw::Id id)
	{
		return {Polygon::Quadrilateral, {v_1, v_2, v_3, v_4}, color, id};
	}

	inline Polygon conditionalEdge(
		const glm::vec3& v_1,
		const glm::vec3& v_2,
		const glm::vec3& control_1,
		const glm::vec3& control_2,
		ldraw::Color color,
		ldraw::Id id)
	{
		return {Polygon::ConditionalEdge, {v_1, v_2, control_1, control_2}, color, id};
	}

	// Vbo names
	enum class ArrayClass : std::uint8_t
	{
		Lines,
		Triangles,
		Quads,
		ConditionalLines
	};

	constexpr ArrayClass ARRAY_CLASSES[] = {ArrayClass::Lines, ArrayClass::Triangles, ArrayClass::Quads, ArrayClass::ConditionalLines};
	constexpr int NUM_ARRAY_CLASSES = countof(ARRAY_CLASSES);

	enum class RenderStyle
	{
		Normal,
		Wireframe,
		BfcRedGreen,
		RandomColors,
		PickScene
	};

	// These are also defined in shaders
	enum class FragmentStyle
	{
		Normal = 0,
		BfcGreen = 1,
		BfcRed = 2,
		RandomColors = 3,
		Id = 4,
	};

	// User options for rendering
	struct RenderPreferences
	{
		gl::RenderStyle style = gl::RenderStyle::Normal;
		QColor mainColor{255, 255, 64};
		QColor backgroundColor{48, 48, 48};
		QColor selectedColor{32, 32, 255};
		GLfloat lineThickness = 2.0f;
		bool lineAntiAliasing = true;
	};
}

mercurial