diff -r 918b6c0f8b5b -r ed9685f44ab3 src/gl/common.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gl/common.h Sat Dec 14 22:36:06 2019 +0200 @@ -0,0 +1,158 @@ +/* + * 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 . + */ + +#pragma once +#include +#include +#include +#include "basics.h" +#include "colors.h" +#include "vertex.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}; + + // Conversion matrix from LDraw to OpenGL coordinates. + static const QMatrix4x4 ldrawToGL = {1, 0, 0, 0, 0, 0, -1, 0, 0, 1, 0, 0, 0, 0, 0, 1}; + + static constexpr QRgb BlackRgb = 0xff000000; + static constexpr GLfloat near = 1.0f; + static constexpr GLfloat far = 10000.0f; + struct Polygon; +} + +struct gl::Polygon +{ + enum Type : qint8 + { + EdgeLine, + Triangle, + Quadrilateral, + ConditionalEdge + }; + Type type; + Point3D vertices[4]; + Color color; + + /** + * @return amount of vertices used for geometry + */ + inline int numPolygonVertices() const + { + if (type == Type::ConditionalEdge) + return 2; + else + return numVertices(); + } + + /** + * @return amount of vertices + */ + inline int numVertices() const + { + switch (type) + { + case Type::EdgeLine: + return 2; + case Type::Triangle: + return 3; + case Type::ConditionalEdge: + case Type::Quadrilateral: + return 4; + default: + return 0; + } + } +}; + +Q_DECLARE_METATYPE(gl::Polygon) + +namespace gl +{ + inline Polygon edgeLine(const Point3D& v_1, const Point3D& v_2, Color color) + { + return {Polygon::EdgeLine, {v_1, v_2}, color}; + } + + inline Polygon triangle(const Point3D& v_1, const Point3D& v_2, const Point3D& v_3, Color color) + { + return {Polygon::Triangle, {v_1, v_2, v_3}, color}; + } + + inline Polygon quadrilateral( + const Point3D& v_1, + const Point3D& v_2, + const Point3D& v_3, + const Point3D& v_4, + Color color) + { + return {Polygon::Quadrilateral, {v_1, v_2, v_3, v_4}, color}; + } + + inline Polygon conditionalEdge( + const Point3D& v_1, + const Point3D& v_2, + const Point3D& control_1, + const Point3D& control_2, + Color color) + { + return {Polygon::ConditionalEdge, {v_1, v_2, control_1, control_2}, color}; + } + + // Vbo names + enum class VboClass + { + Lines, + Triangles, + Quads, + ConditionalLines + }; + constexpr int numVboClasses = 4; + + // Types of vbo per object + enum class VboSubclass + { + Surfaces, + RegularColors, + PickColors, + BfcFrontColors, + BfcBackColors, + RandomColors, + Normals, + InvertedNormals + }; + constexpr int numVboSubclasses = 8; + + // Amount of vbos overall + constexpr int numVbos = gl::numVboClasses * gl::numVboSubclasses; + + enum class RenderStyle + { + Normal, + Wireframe, + BfcRedGreen, + RandomColors + }; +}