/
render.h
359 lines (297 loc) · 9.12 KB
/
render.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
Copyright (C) 2001-2006, William Joseph.
All Rights Reserved.
This file is part of GtkRadiant.
GtkRadiant 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 2 of the License, or
(at your option) any later version.
GtkRadiant 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 GtkRadiant; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#pragma once
/// \file
/// \brief High-level constructs for efficient OpenGL rendering.
#include "render/MeshVertex.h"
#include "render/Vertex3f.h"
#include "render/TexCoord2f.h"
#include "render/VertexCb.h"
#include "irender.h"
#include "igl.h"
#include "math/FloatTools.h"
#include "math/Vector2.h"
#include "math/pi.h"
#include <vector>
typedef unsigned int RenderIndex;
const GLenum RenderIndexTypeID = GL_UNSIGNED_INT;
/// Vector of indices for use with glDrawElements
typedef std::vector<RenderIndex> IndexBuffer;
/// \brief A wrapper around a std::vector which inserts only vertices which have not already been inserted.
/// \param Vertex The vertex data type. Must support operator<, operator== and operator!=.
/// For best performance, quantise vertices before inserting them.
template<typename Vertex>
class UniqueVertexBuffer {
typedef std::vector<Vertex> Vertices;
Vertices& m_data;
struct bnode {
bnode()
: m_left(0), m_right(0) {}
RenderIndex m_left;
RenderIndex m_right;
};
std::vector<bnode> m_btree;
RenderIndex m_prev0;
RenderIndex m_prev1;
RenderIndex m_prev2;
const RenderIndex find_or_insert(const Vertex& vertex) {
RenderIndex index = 0;
while(1) {
if(vertex < m_data[index]) {
bnode& node = m_btree[index];
if(node.m_left != 0) {
index = node.m_left;
continue;
}
else {
node.m_left = RenderIndex(m_btree.size());
m_btree.push_back(bnode());
m_data.push_back(vertex);
return RenderIndex(m_btree.size()-1);
}
}
if(m_data[index] < vertex) {
bnode& node = m_btree[index];
if(node.m_right != 0) {
index = node.m_right;
continue;
}
else {
node.m_right = RenderIndex(m_btree.size());
m_btree.push_back(bnode());
m_data.push_back(vertex);
return RenderIndex(m_btree.size()-1);
}
}
return index;
}
}
public:
UniqueVertexBuffer(Vertices& data)
: m_data(data), m_prev0(0), m_prev1(0), m_prev2(0) {}
typedef typename Vertices::const_iterator iterator;
iterator begin() const {
return m_data.begin();
}
iterator end() const {
return m_data.end();
}
std::size_t size() const {
return m_data.size();
}
const Vertex* data() const {
return &(*m_data.begin());
}
Vertex& operator[](std::size_t index) {
return m_data[index];
}
const Vertex& operator[](std::size_t index) const {
return m_data[index];
}
void clear() {
m_prev0 = 0;
m_prev1 = 0;
m_prev2 = 0;
m_data.clear();
m_btree.clear();
}
void reserve(std::size_t max_vertices) {
m_data.reserve(max_vertices);
m_btree.reserve(max_vertices);
}
/// \brief Returns the index of the element equal to \p vertex.
RenderIndex insert(const Vertex& vertex) {
if(m_data.empty()) {
m_data.push_back(vertex);
m_btree.push_back(bnode());
return 0;
}
if(m_data[m_prev0] == vertex)
return m_prev0;
if(m_prev1 != m_prev0 && m_data[m_prev1] == vertex)
return m_prev1;
if(m_prev2 != m_prev0 && m_prev2 != m_prev1 && m_data[m_prev2] == vertex)
return m_prev2;
m_prev2 = m_prev1;
m_prev1 = m_prev0;
m_prev0 = find_or_insert(vertex);
return m_prev0;
}
};
// A Normal3 is just another Vertex3 (Vector3)
typedef Vertex3 Normal3;
/// \brief Returns a double-precision \p component quantised to \p precision.
inline double double_quantise(double component, double precision)
{
return float_snapped(component, precision);
}
/// \brief Returns a \p vertex quantised to \p precision.
inline Vertex3 vertex3f_quantised(const Vertex3& vertex, double precision)
{
return Vertex3(double_quantise(vertex.x(), precision), double_quantise(vertex.y(), precision), double_quantise(vertex.z(), precision));
}
const float c_quantise_vertex = 1.f / static_cast<float>(1 << 3);
/// \brief Returns \p v with vertex quantised to a fixed precision.
inline VertexCb pointvertex_quantised(const VertexCb& v) {
return VertexCb(vertex3f_quantised(v.vertex, c_quantise_vertex), v.colour);
}
/// \brief Sets up the OpenGL colour and vertex arrays for \p array.
inline void pointvertex_gl_array(const VertexCb* array)
{
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(VertexCb), &array->colour);
glVertexPointer(3, GL_DOUBLE, sizeof(VertexCb), &array->vertex);
}
/// A renderable collection of coloured vertices
class RenderablePointVector
{
public:
typedef std::vector<VertexCb> PointVertexVector;
protected:
PointVertexVector _vector;
const GLenum _mode;
public:
using value_type = PointVertexVector::value_type;
RenderablePointVector(GLenum mode) :
_mode(mode)
{}
RenderablePointVector(GLenum mode, std::size_t initialSize) :
_vector(initialSize),
_mode(mode)
{}
const PointVertexVector& getPointVector() const
{
return _vector;
}
// Convenience method to set the colour of the whole array
void setColour(const Colour4b& colour)
{
for (PointVertexVector::iterator i = _vector.begin(); i != _vector.end(); ++i)
{
i->colour = colour;
}
}
const VertexCb& operator[](std::size_t i) const
{
return _vector[i];
}
VertexCb& operator[](std::size_t i)
{
return _vector[i];
}
VertexCb& front()
{
return _vector.front();
}
const VertexCb& front() const
{
return _vector.front();
}
std::size_t size() const {
return _vector.size();
}
bool empty() const {
return _vector.empty();
}
void clear() {
_vector.clear();
}
void resize(std::size_t size) {
_vector.resize(size);
}
void reserve(std::size_t size) {
_vector.reserve(size);
}
void push_back(const VertexCb& point) {
_vector.push_back(point);
}
template<class... Args>
VertexCb& emplace_back(Args&&... args)
{
return _vector.emplace_back(std::forward<Args>(args)...);
}
};
template<typename VertexContainerT> struct RemappingTraits
{};
template<>
struct RemappingTraits<Vertex3>
{
static Vertex3& getVertex(Vertex3& vertex) { return vertex; }
};
template<>
struct RemappingTraits<VertexCb>
{
static Vertex3& getVertex(VertexCb& container) { return container.vertex; }
};
template<>
struct RemappingTraits<MeshVertex>
{
static Vertex3& getVertex(MeshVertex& container) { return container.vertex; }
};
class RemapXYZ
{
public:
template<typename VertexContainerT>
static void set(VertexContainerT& container, Vertex3::ElementType x, Vertex3::ElementType y, Vertex3::ElementType z)
{
RemappingTraits<VertexContainerT>::getVertex(container).x() = x;
RemappingTraits<VertexContainerT>::getVertex(container).y() = y;
RemappingTraits<VertexContainerT>::getVertex(container).z() = z;
}
};
class RemapYZX
{
public:
template<typename VertexContainerT>
static void set(VertexContainerT& container, Vertex3::ElementType x, Vertex3::ElementType y, Vertex3::ElementType z)
{
RemappingTraits<VertexContainerT>::getVertex(container).x() = z;
RemappingTraits<VertexContainerT>::getVertex(container).y() = x;
RemappingTraits<VertexContainerT>::getVertex(container).z() = y;
}
};
class RemapZXY
{
public:
template<typename VertexContainerT>
static void set(VertexContainerT& container, Vertex3::ElementType x, Vertex3::ElementType y, Vertex3::ElementType z)
{
RemappingTraits<VertexContainerT>::getVertex(container).x() = y;
RemappingTraits<VertexContainerT>::getVertex(container).y() = z;
RemappingTraits<VertexContainerT>::getVertex(container).z() = x;
}
};
// VertexArray must expose a value_type typedef and implement an index operator[], like std::vector
template<typename remap_policy, typename VertexArray>
inline void draw_ellipse(const std::size_t numSegments, const double radiusX, const double radiusY, VertexArray& vertices, std::size_t firstVertex = 0)
{
// Per half circle we push in (Segments x 4) vertices (the caller made room for that)
const auto numVerticesPerHalf = numSegments << 2;
const auto increment = math::PI / numVerticesPerHalf;
for (std::size_t curSegment = 0; curSegment < numVerticesPerHalf; ++curSegment)
{
auto curAngle = curSegment * increment;
auto x = radiusX * cos(curAngle);
auto y = radiusY * sin(curAngle);
remap_policy::set(vertices[firstVertex + curSegment], x, y, 0);
remap_policy::set(vertices[firstVertex + curSegment + numVerticesPerHalf], -x, -y, 0);
}
}
template<typename remap_policy, typename VertexArray>
inline void draw_circle(const std::size_t segments, const double radius, VertexArray& vertices, std::size_t firstVertex = 0)
{
draw_ellipse<remap_policy>(segments, radius, radius, vertices, firstVertex);
}