-
Notifications
You must be signed in to change notification settings - Fork 0
/
GLRender.cpp
393 lines (353 loc) · 13.9 KB
/
GLRender.cpp
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include "GLRender.hpp"
#include "GeometryUtils.hpp"
#include "PolygonSorter.hpp"
#include <CGAL/Polygon_mesh_processing/compute_normal.h>
namespace PMP = CGAL::Polygon_mesh_processing;
////////// Basics //////////
#if defined(_WIN32)
// Windows OpenGL
#include <GL/gl.h>
#include "glext.h"
#define GL_GET_PROC_ADDR(n) wglGetProcAddress(n) // requires a valid OpenGL context
#elif defined(__APPLE__)
// Apple OpenGL (does not need anything special for extensions)
#include <OpenGL/gl.h>
#else
// Linux OpenGL
#include <GL/glx.h>
#include <GL/glxext.h>
#define GL_GET_PROC_ADDR(n) glXGetProcAddressARB((const GLubyte *)n) // does not require a valid OpenGL context
#endif
static PFNGLGENBUFFERSPROC glGenBuffers = nullptr;
static PFNGLBINDBUFFERPROC glBindBuffer = nullptr;
static PFNGLBUFFERDATAPROC glBufferData = nullptr;
static PFNGLDELETEBUFFERSPROC glDeleteBuffers = nullptr;
static PFNGLENABLEVERTEXATTRIBARRAYPROC glEnableVertexAttribArray = nullptr;
static PFNGLDISABLEVERTEXATTRIBARRAYPROC glDisableVertexAttribArray = nullptr;
static PFNGLVERTEXATTRIBPOINTERPROC glVertexAttribPointer = nullptr;
static void _gl_init()
{
#ifndef __APPLE__
if (glGenBuffers == nullptr)
{
glGenBuffers = (PFNGLGENBUFFERSPROC)GL_GET_PROC_ADDR("glGenBuffers");
glBindBuffer = (PFNGLBINDBUFFERPROC)GL_GET_PROC_ADDR("glBindBuffer");
glBufferData = (PFNGLBUFFERDATAPROC)GL_GET_PROC_ADDR("glBufferData");
glDeleteBuffers = (PFNGLDELETEBUFFERSPROC)GL_GET_PROC_ADDR("glDeleteBuffers");
glEnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC)GL_GET_PROC_ADDR("glEnableVertexAttribArray");
glDisableVertexAttribArray = (PFNGLDISABLEVERTEXATTRIBARRAYPROC)GL_GET_PROC_ADDR("glDisableVertexAttribArray");
glVertexAttribPointer = (PFNGLVERTEXATTRIBPOINTERPROC)GL_GET_PROC_ADDR("glVertexAttribPointer");
}
#endif
}
void gl_setup()
{
_gl_init();
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
glShadeModel(GL_SMOOTH);
glEnable(GL_POINT_SMOOTH);
}
inline void _gl_set_color(const QColor &c) { glColor4d(c.redF(), c.greenF(), c.blueF(), c.alphaF()); }
//inline void _gl_set_normal(const Vector3 &n) { glNormal3d(CGAL::to_double(n.dx()), CGAL::to_double(n.dy()), CGAL::to_double(n.dz())); }
inline void _gl_add_point(const Point3 &p) { glVertex3d(CGAL::to_double(p.x()), CGAL::to_double(p.y()), CGAL::to_double(p.z())); }
////////// GlPolyhedron //////////
GlPolyhedron::GlPolyhedron(const Polyhedron3* P) : nverts(0), nedges(0), nfaces(0), edges(nullptr), faces(nullptr), ps(nullptr)
{
size_t i;
this->bufs[0] = 0;
this->bufs[1] = 0;
// Get all vertices and their normals
this->nverts = P->size_of_vertices();
GLdouble* verts = new GLdouble[this->nverts*3];
GLdouble* norms = new GLdouble[this->nverts*3];
i = 0;
for (Polyhedron3::Vertex_const_iterator V = P->vertices_begin(), end = P->vertices_end(); V != end; ++V)
{
const Point3& v = V->point();
const Vector3 n = PMP::compute_vertex_normal(V, *P);
verts[i ] = CGAL::to_double(v.x());
norms[i++] = CGAL::to_double(n.dx());
verts[i ] = CGAL::to_double(v.y());
norms[i++] = CGAL::to_double(n.dy());
verts[i ] = CGAL::to_double(v.z());
norms[i++] = CGAL::to_double(n.dz());
}
// Get all edges
this->nedges = P->size_of_halfedges();
this->edges = new unsigned int[this->nedges];
i = 0;
for (Polyhedron3::Edge_const_iterator HE = P->edges_begin(), end = P->edges_end(); HE != end; ++HE)
{
this->edges[i++] = (unsigned int)HE->vertex()->id();
this->edges[i++] = (unsigned int)HE->opposite()->vertex()->id();
}
// Get all faces
this->nfaces = P->size_of_facets();
this->faces = new unsigned int[this->nfaces*3]; // used when getting sorted face order, not below
unsigned int** faces = new unsigned int*[this->nfaces];
i = 0;
for (Polyhedron3::Facet_const_iterator F = P->facets_begin(), end = P->facets_end(); F != end; ++F)
{
const Polyhedron3::Halfedge_const_handle &a = F->halfedge(), &b = a->next(), &c = b->next();
faces[i] = new unsigned int[3];
faces[i][0] = (unsigned int)a->vertex()->id();
faces[i][1] = (unsigned int)b->vertex()->id();
faces[i][2] = (unsigned int)c->vertex()->id();
++i;
}
// Save vertex and normal data to the graphics card
glGenBuffers(2, this->bufs);
glBindBuffer(GL_ARRAY_BUFFER, this->bufs[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLdouble)*nverts*3, verts, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, this->bufs[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLdouble)*nverts*3, norms, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Create the polygon sorter
this->ps = new PolygonSorter(verts, faces, this->nfaces);
// Cleanup temporary data
delete[] verts;
delete[] norms;
for (size_t i = 0; i < this->nfaces; ++i) { if (faces[i]) { delete[] faces[i]; faces[i] = nullptr; } }
delete[] faces;
}
GlPolyhedron::~GlPolyhedron()
{
glDeleteBuffers(2, this->bufs); this->bufs[0] = 0; this->bufs[1] = 0;
if (this->edges) { delete[] this->edges; this->edges = nullptr; }
if (this->faces) { delete[] this->faces; this->faces = nullptr; }
if (this->ps) { delete this->ps; this->ps = nullptr; }
}
void GlPolyhedron::render_edges(const QColor& color)
{
// Setup the GL environment
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
glLineWidth(0.25f);
_gl_set_color(color);
// Draw all edges
glBindBuffer(GL_ARRAY_BUFFER, this->bufs[0]);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_DOUBLE, GL_FALSE, 0, 0);
glDrawElements(GL_LINES, (GLsizei)this->nedges, GL_UNSIGNED_INT, this->edges);
glDisableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
//// Indirect/slow/easy way
//glBegin(GL_LINES);
//for (Polyhedron3::Edge_const_iterator he = P->edges_begin(), end = P->edges_end(); he != end; ++he)
//{
// _gl_add_point(he->vertex()->point());
// _gl_add_point(he->opposite()->vertex()->point());
//}
//glEnd();
}
void GlPolyhedron::render_faces(const Ray3& view, const QColor& color)
{
// Calculate the ordering of the faces
this->ps->query(view.source(), view.direction(), this->faces);
// Setup the GL environment
//glEnable(GL_LIGHTING); // TODO: alpha and lighting
glDisable(GL_DEPTH_TEST);
_gl_set_color(color);
// Draw all faces
glBindBuffer(GL_ARRAY_BUFFER, this->bufs[0]);
glVertexAttribPointer(0, 3, GL_DOUBLE, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, this->bufs[1]);
glVertexAttribPointer(1, 3, GL_DOUBLE, GL_FALSE, 0, 0);
glEnableVertexAttribArray(1);
glDrawElements(GL_TRIANGLES, (GLsizei)this->nfaces*3, GL_UNSIGNED_INT, this->faces);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
//// Indirect/slow/easy way
//glBegin(GL_TRIANGLES);
//for (; f != end; ++f)
//{
// // revolve around current face to get vertices
// VERTICES_AROUND_FACET(f, v)
// {
// // Gouraud (smooth) shading has 1 normal per vertex
// _gl_set_normal(PMP::compute_vertex_normal(v, *P));
// _gl_add_point(v->point());
// }
//}
//glEnd();
}
////////// GlSkeleton //////////
GlSkeleton::GlSkeleton(const Skeleton3* S) : nverts(boost::num_vertices(*S)), nedges(boost::num_edges(*S)), edges(nullptr)
{
size_t i;
this->bufs[0] = 0;
// Get all vertices
GLdouble* verts = new GLdouble[this->nverts*3];
i = 0;
Skeleton3::vertex_iterator V, Vend;
std::unordered_map<Skeleton3::vertex_descriptor, size_t> vertex_lookup;
for (boost::tie(V, Vend) = boost::vertices(*S); V != Vend; ++V)
{
vertex_lookup[*V] = i;
const Point3& v = (*S)[*V].point;
verts[3*i+0] = CGAL::to_double(v.x());
verts[3*i+1] = CGAL::to_double(v.y());
verts[3*i+2] = CGAL::to_double(v.z());
++i;
}
// Get all edges
this->edges = new unsigned int[2*this->nedges];
i = 0;
Skeleton3::edge_iterator E, Eend;
for (boost::tie(E, Eend) = boost::edges(*S); E != Eend; ++E)
{
this->edges[i++] = (unsigned int)vertex_lookup[boost::source(*E, *S)];
this->edges[i++] = (unsigned int)vertex_lookup[boost::target(*E, *S)];
}
// Save vertex data to the graphics card
glGenBuffers(1, this->bufs);
glBindBuffer(GL_ARRAY_BUFFER, this->bufs[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLdouble)*nverts*3, verts, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Cleanup temporary data
delete[] verts;
}
GlSkeleton::~GlSkeleton()
{
if (this->bufs) { glDeleteBuffers(1, this->bufs); this->bufs[0] = 0; }
if (this->edges) { delete[] this->edges; this->edges = nullptr; }
}
void GlSkeleton::render(const double weight, const QColor& color)
{
// Setup the GL environment
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
glLineWidth(weight);
_gl_set_color(color);
// Draw all edges
glBindBuffer(GL_ARRAY_BUFFER, this->bufs[0]);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_DOUBLE, GL_FALSE, 0, 0);
glDrawElements(GL_LINES, (GLsizei)this->nedges*2, GL_UNSIGNED_INT, this->edges);
glDisableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
////////// GlIntersection //////////
GlIntersection::GlIntersection(const Intersection& I) : nintersections(0), npoints(nullptr)
{
this->bufs[0] = 0;
const Plane3& h = I.plane();
// Calculate the plane corners
// TODO: instead do an off-axis best-fit box or a circle.
CGAL::Bbox_2 bb = I.bbox();
Point2 center = Point2((bb.xmax()+bb.xmin())/2, (bb.ymax()+bb.ymin())/2);
double x_radius = 0.75*(bb.xmax() - bb.xmin());
double y_radius = 0.75*(bb.ymax() - bb.ymin());
this->plane_corners[0] = h.to_3d(Point2(center.x() + x_radius, center.y() + y_radius));
this->plane_corners[1] = h.to_3d(Point2(center.x() + x_radius, center.y() - y_radius));
this->plane_corners[2] = h.to_3d(Point2(center.x() - x_radius, center.y() - y_radius));
this->plane_corners[3] = h.to_3d(Point2(center.x() - x_radius, center.y() + y_radius));
// Buffer the intersection lines
this->nintersections = I.count();
size_t pts_len = I.total_count()*3;
GLdouble* pts = new GLdouble[pts_len];
this->npoints = new size_t[this->nintersections];
this->polys = new unsigned int*[this->nintersections];
size_t i = 0, p = 0;
for (Intersection::const_iterator IP2 = I.begin(), ip2_end = I.end(); IP2 != ip2_end; ++IP2)
{
size_t start_p = p / 3;
for (Polygon2::Vertex_const_iterator V = IP2->vertices_begin(), end = IP2->vertices_end(); V != end; ++V)
{
const Point3& v = h.to_3d(*V);
pts[p++] = CGAL::to_double(v.x());
pts[p++] = CGAL::to_double(v.y());
pts[p++] = CGAL::to_double(v.z());
}
size_t n = IP2->size()*2, j = 1, end_p = p / 3, last_p;
if (IP2->is_open()) { n -= 2; end_p -= 1; last_p = end_p; }
else { last_p = start_p; }
this->npoints[i] = n;
this->polys[i] = new unsigned int[n];
this->polys[i][0] = (unsigned int)start_p;
this->polys[i][n-1] = (unsigned int)last_p;
for (size_t x = start_p + 1; x < end_p; ++x) { this->polys[i][j++] = (unsigned int)x; this->polys[i][j++] = (unsigned int)x; }
i++;
}
// Save points data to the graphics card
glGenBuffers(1, bufs);
glBindBuffer(GL_ARRAY_BUFFER, this->bufs[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLdouble)*pts_len, pts, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Cleanup temporary data
delete[] pts;
}
GlIntersection::~GlIntersection()
{
if (this->bufs) { glDeleteBuffers(1, this->bufs); this->bufs[0] = 0; }
if (this->npoints) { delete[] this->npoints; this->npoints = nullptr; }
if (this->polys) { for (size_t i = 0; i < this->nintersections; ++i) { if (this->polys[i]) { delete[] this->polys[i]; this->polys[i] = nullptr; } } delete[] this->polys; this->polys = nullptr; }
}
void GlIntersection::render_plane(const QColor& color)
{
// Setup the GL environment
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
_gl_set_color(color);
// Draw the plane
glBegin(GL_QUADS);
_gl_add_point(this->plane_corners[0]);
_gl_add_point(this->plane_corners[1]);
_gl_add_point(this->plane_corners[2]);
_gl_add_point(this->plane_corners[3]);
glEnd();
}
void GlIntersection::render_polygons(const QColor& color)
{
// Setup the GL environment
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
glLineWidth(5.0f);
_gl_set_color(color);
// Draw all edges
glBindBuffer(GL_ARRAY_BUFFER, this->bufs[0]);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_DOUBLE, GL_FALSE, 0, 0);
for (size_t i = 0; i < this->nintersections; ++i)
{
glDrawElements(GL_LINES, (GLsizei)this->npoints[i], GL_UNSIGNED_INT, this->polys[i]);
}
glDisableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
//// Indirect/slow/easy way
//glBegin(GL_LINES);
//Polygon2::Edge_const_circulator e = this->p.edges_circulator(), end = e;
//CGAL_For_all(e, end)
//{
// _gl_add_point(this->h.to_3d(e->source()));
// _gl_add_point(this->h.to_3d(e->target()));
//}
//glEnd();
}
////////// GlPoint //////////
GlPoint::GlPoint(const Point3& pt, double radius) : pt(pt), radius(radius)
{
}
GlPoint::~GlPoint()
{
}
#include <iostream>
void GlPoint::render(const QColor& color)
{
// Setup the GL environment
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
glPointSize(2*this->radius);
_gl_set_color(color);
// Draw the point
glBegin(GL_POINTS);
_gl_add_point(this->pt);
glEnd();
}