|
1 /* |
|
2 * LDForge: LDraw parts authoring CAD |
|
3 * Copyright (C) 2013 - 2018 Teemu Piippo |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation, either version 3 of the License, or |
|
8 * (at your option) any later version. |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 */ |
|
18 |
|
19 #pragma once |
|
20 #include <QColor> |
|
21 #include <QOpenGLFunctions> |
|
22 #include <QGenericMatrix> |
|
23 #include "basics.h" |
|
24 #include "colors.h" |
|
25 #include "vertex.h" |
|
26 |
|
27 namespace gl |
|
28 { |
|
29 // Transformation matrices for projection cameras. |
|
30 static const QMatrix4x4 topCameraMatrix = {}; |
|
31 static const QMatrix4x4 frontCameraMatrix = {1, 0, 0, 0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1}; |
|
32 static const QMatrix4x4 leftCameraMatrix = {0, -1, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, 0, 0, 1}; |
|
33 static const QMatrix4x4 bottomCameraMatrix = {1, 0, 0, 0, 0, -1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1}; |
|
34 static const QMatrix4x4 backCameraMatrix = {-1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1}; |
|
35 static const QMatrix4x4 rightCameraMatrix = {0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1}; |
|
36 |
|
37 // Conversion matrix from LDraw to OpenGL coordinates. |
|
38 static const QMatrix4x4 ldrawToGL = {1, 0, 0, 0, 0, 0, -1, 0, 0, 1, 0, 0, 0, 0, 0, 1}; |
|
39 |
|
40 static constexpr QRgb BlackRgb = 0xff000000; |
|
41 static constexpr GLfloat near = 1.0f; |
|
42 static constexpr GLfloat far = 10000.0f; |
|
43 struct Polygon; |
|
44 } |
|
45 |
|
46 struct gl::Polygon |
|
47 { |
|
48 enum Type : qint8 |
|
49 { |
|
50 EdgeLine, |
|
51 Triangle, |
|
52 Quadrilateral, |
|
53 ConditionalEdge |
|
54 }; |
|
55 Type type; |
|
56 Point3D vertices[4]; |
|
57 Color color; |
|
58 |
|
59 /** |
|
60 * @return amount of vertices used for geometry |
|
61 */ |
|
62 inline int numPolygonVertices() const |
|
63 { |
|
64 if (type == Type::ConditionalEdge) |
|
65 return 2; |
|
66 else |
|
67 return numVertices(); |
|
68 } |
|
69 |
|
70 /** |
|
71 * @return amount of vertices |
|
72 */ |
|
73 inline int numVertices() const |
|
74 { |
|
75 switch (type) |
|
76 { |
|
77 case Type::EdgeLine: |
|
78 return 2; |
|
79 case Type::Triangle: |
|
80 return 3; |
|
81 case Type::ConditionalEdge: |
|
82 case Type::Quadrilateral: |
|
83 return 4; |
|
84 default: |
|
85 return 0; |
|
86 } |
|
87 } |
|
88 }; |
|
89 |
|
90 Q_DECLARE_METATYPE(gl::Polygon) |
|
91 |
|
92 namespace gl |
|
93 { |
|
94 inline Polygon edgeLine(const Point3D& v_1, const Point3D& v_2, Color color) |
|
95 { |
|
96 return {Polygon::EdgeLine, {v_1, v_2}, color}; |
|
97 } |
|
98 |
|
99 inline Polygon triangle(const Point3D& v_1, const Point3D& v_2, const Point3D& v_3, Color color) |
|
100 { |
|
101 return {Polygon::Triangle, {v_1, v_2, v_3}, color}; |
|
102 } |
|
103 |
|
104 inline Polygon quadrilateral( |
|
105 const Point3D& v_1, |
|
106 const Point3D& v_2, |
|
107 const Point3D& v_3, |
|
108 const Point3D& v_4, |
|
109 Color color) |
|
110 { |
|
111 return {Polygon::Quadrilateral, {v_1, v_2, v_3, v_4}, color}; |
|
112 } |
|
113 |
|
114 inline Polygon conditionalEdge( |
|
115 const Point3D& v_1, |
|
116 const Point3D& v_2, |
|
117 const Point3D& control_1, |
|
118 const Point3D& control_2, |
|
119 Color color) |
|
120 { |
|
121 return {Polygon::ConditionalEdge, {v_1, v_2, control_1, control_2}, color}; |
|
122 } |
|
123 |
|
124 // Vbo names |
|
125 enum class VboClass |
|
126 { |
|
127 Lines, |
|
128 Triangles, |
|
129 Quads, |
|
130 ConditionalLines |
|
131 }; |
|
132 constexpr int numVboClasses = 4; |
|
133 |
|
134 // Types of vbo per object |
|
135 enum class VboSubclass |
|
136 { |
|
137 Surfaces, |
|
138 RegularColors, |
|
139 PickColors, |
|
140 BfcFrontColors, |
|
141 BfcBackColors, |
|
142 RandomColors, |
|
143 Normals, |
|
144 InvertedNormals |
|
145 }; |
|
146 constexpr int numVboSubclasses = 8; |
|
147 |
|
148 // Amount of vbos overall |
|
149 constexpr int numVbos = gl::numVboClasses * gl::numVboSubclasses; |
|
150 |
|
151 enum class RenderStyle |
|
152 { |
|
153 Normal, |
|
154 Wireframe, |
|
155 BfcRedGreen, |
|
156 RandomColors |
|
157 }; |
|
158 } |