667 |
khor |
1 |
\documentclass[a4paper]{article}
|
|
|
2 |
\usepackage{listings}
|
|
|
3 |
\usepackage[isolatin]{inputenc}
|
|
|
4 |
\usepackage{makeidx,times}
|
|
|
5 |
\usepackage{fancyhdr}
|
|
|
6 |
\usepackage{graphicx}
|
|
|
7 |
\usepackage{float}
|
|
|
8 |
\usepackage{alltt}
|
|
|
9 |
%%\usepackage{doxygen}
|
|
|
10 |
\usepackage[pdftex,
|
|
|
11 |
pagebackref=true,
|
|
|
12 |
colorlinks=true,
|
|
|
13 |
linkcolor=blue
|
|
|
14 |
]{hyperref}
|
|
|
15 |
|
|
|
16 |
\makeindex
|
|
|
17 |
\lstset{tabsize=1,language=C++,basicstyle=\small}
|
|
|
18 |
\author{J. Andreas B{\ae}rentzen}
|
|
|
19 |
\title{Introduction to GEL\\\vspace{0.25cm} \normalsize
|
|
|
20 |
\textit{An informal introduction to the GEL library with many examples of usage}.}
|
|
|
21 |
\setcounter{tocdepth}{2}
|
|
|
22 |
\begin{document}
|
|
|
23 |
\maketitle
|
|
|
24 |
\tableofcontents
|
|
|
25 |
\newpage\section{Introduction}
|
|
|
26 |
\sloppy
|
|
|
27 |
|
|
|
28 |
GEL is an abbreviation of GEometry and Linear algebra. GEL is a C++ library with tools for manipulating digital representations of geometry such as polygonal meshes, triangle meshes, point clouds, and voxel grids. Since linear algebra tools are invariably needed for this, GEL also contains tools for linear algebra. Finally, GEL contains tools for drawing geometry using OpenGL and various utilities.
|
|
|
29 |
|
|
|
30 |
This document pertains to the version of GEL which is distributed on CampusNet for 02585. This version of GEL comes with project files for Visual Studio 9 and XCode and examples relevant for the course are included in the distribution.
|
|
|
31 |
|
|
|
32 |
GEL is not documented as well as we would like, but a fair amount of Doxygen documentation is provided for those parts of GEL which are used a lot. You will find the reference manual at \href{http://www2.imm.dtu.dk/courses/02585/GELref/index.html}{http://www2.imm.dtu.dk/courses/02585/GELref/index.html}
|
|
|
33 |
|
|
|
34 |
Please use it. Autogenerated documentation is not eminently readable, but it is much better than no documentation and it should be relatively complete for the parts of GEL needed in the course.
|
|
|
35 |
|
|
|
36 |
The goal of \textit{this} document is to provide more detail and to help people getting started. In particular, this document focuses on functionality in the CGLA and HMesh namespaces, but we shall also take a look at some other parts of GEL. Unfortunately, it is rather difficult to be complete: There is functionality in GEL not covered here. Consequently, if you are looking for something particular, we \textbf{entreat } you to look at the reference linked to above
|
|
|
37 |
\subsection{Overview of GEL}
|
|
|
38 |
|
|
|
39 |
GEL contains many tools for geometry processing and a number of data structures. Perhaps the most important is the \texttt{Manifold} class which resides in the \texttt{HMesh} namespace. A \texttt{Manifold} is a halfedge based representation of a polygonal mesh. There is also a simpler triangle mesh data structure, \texttt{TriMesh}, based on an indexed face set. There are several voxel grid data structures, a fairly efficient k-D tree for storing points, bounding box hierarchies etc.
|
|
|
40 |
|
|
|
41 |
A number of algorithms exist for manipulating these representations of geometric data. For instance, \texttt{Manifold} has a function for mesh simplification, several functions for smoothing (including anisotropic), mesh optimization, and mesh clean up.
|
|
|
42 |
Another strong point of GEL is the fact that it contains good support for converting between representations; especially between meshes and voxel grids. There are several tools for polygonizing isosurfaces and also converting meshes to distance fields. The tools pertaining to the Manifold data structure are in the \texttt{HMesh} namespace. The other geometry related tools are in the \texttt{Geometry} namespace.
|
|
|
43 |
|
|
|
44 |
There are two packages for linear algebra: \texttt{CGLA} is strictly for small vectors and matrices (dimensions 2,3, and 4). \texttt{LinAlg} is a Lapack wrapper for slightly larger problems. At this point, it is fairly simple but includes a number of functions for solving linear systems, factoring matrices and finding eigensolutions for symmetric matrices.
|
|
|
45 |
|
|
|
46 |
\texttt{GLGraphics} contains facilities for drawing entities from other parts of GEL via OpenGL. Especially \texttt{TriMesh} and Manifold have good support. For instance, there is a nice wireframe drawing class. There are also two virtual trackballs for interactive programs and some tools for viewing in interactive programs and SOIL a small open source library for image loading by Jonathan Dummer.
|
|
|
47 |
|
|
|
48 |
Finally there are some utilities in \texttt{Util}: Getting command line arguments, hash table classes, a 2D grid class, a resource manager, an XML parser by Jeppe Frisvad and other tools.
|
|
|
49 |
|
|
|
50 |
\section{CGLA}
|
|
|
51 |
|
|
|
52 |
\texttt{CGLA} is a set of numerical C++ vector and matrix classes and class
|
|
|
53 |
templates designed with computer graphics in mind. \texttt{CGLA} stands for
|
|
|
54 |
``Computer Graphics Linear Algebra''. Let us get right down to the obvious question: Why create another
|
|
|
55 |
linear algebra package?
|
|
|
56 |
Well, \texttt{CGLA} evolved from a few matrix and vector classes because I
|
|
|
57 |
didn't have anything better. Also, I created \texttt{CGLA} to experiment with
|
|
|
58 |
some template programming techniques. This led to the most important
|
|
|
59 |
feature of \texttt{CGLA}, namely the fact that all vector types are derived
|
|
|
60 |
from the same template.
|
|
|
61 |
|
|
|
62 |
This makes it easy to ensure identical semantics: Since all vectors
|
|
|
63 |
have inherited, say, the * operator from a common ancestor, it works
|
|
|
64 |
the same for all of them.
|
|
|
65 |
|
|
|
66 |
It is important to note that \texttt{CGLA} was designed for Computer Graphics
|
|
|
67 |
(not numerical computations) and this had a number of
|
|
|
68 |
implications. Since, in computer graphics we mainly need small vectors
|
|
|
69 |
of dimension 2,3, or 4 \texttt{CGLA} was designed for vectors of low
|
|
|
70 |
dimensionality. Moreover, the amount of memory allocated for a vector
|
|
|
71 |
is decided by its type at compile time. \texttt{CGLA} does not use dynamic
|
|
|
72 |
memory. \texttt{CGLA} also does not use virtual functions, and most functions
|
|
|
73 |
are inline. These features all help making \texttt{CGLA} relatively fast.
|
|
|
74 |
|
|
|
75 |
Of course, other libraries of vector templates for computer graphics
|
|
|
76 |
exist, but to my knowledge none where the fundamental templates are
|
|
|
77 |
parametrized w.r.t. dimension as well as type. In other words, we have
|
|
|
78 |
a template (\texttt{ArithVec}) that gets both type
|
|
|
79 |
(e.g. \texttt{float}) and dimension
|
|
|
80 |
(e.g. 3) as arguments. the intended use of this template is as
|
|
|
81 |
ancestor of concrete types such as \texttt{Vec3f} - a 3D floating
|
|
|
82 |
point type.
|
|
|
83 |
|
|
|
84 |
The use of just one template as basis is very important, I believe,
|
|
|
85 |
since it makes it extremely simple to add new types of
|
|
|
86 |
vectors. Another very generic template is \texttt{ArithMat} which is a
|
|
|
87 |
template for matrix classes. (and not necessarily NxN matrices).
|
|
|
88 |
|
|
|
89 |
From a users perspective \texttt{CGLA} contains a number of vector and matrix
|
|
|
90 |
classes, a quaternion and some utility classes. In summary, the most
|
|
|
91 |
important features are
|
|
|
92 |
\begin{itemize}
|
|
|
93 |
\item A number of 2, 3 and 4 d vector classes.
|
|
|
94 |
\item A number of Matrix classes.
|
|
|
95 |
\item A Quaternion class.
|
|
|
96 |
\item Some test programs.
|
|
|
97 |
\item Works well with OpenGL.
|
|
|
98 |
\end{itemize}
|
|
|
99 |
|
|
|
100 |
|
|
|
101 |
\subsection{Naming conventions}
|
|
|
102 |
|
|
|
103 |
Vectors in \texttt{CGLA} are named \texttt{VecDT} where \texttt{D} stands for
|
|
|
104 |
dimension at \texttt{T}
|
|
|
105 |
for type. For instance a 3D floating point vector is named
|
|
|
106 |
\texttt{Vec3f}. Other types are d (double), s (short), i (int), uc
|
|
|
107 |
(unsigned char), and usi (unsigned shourt int).
|
|
|
108 |
|
|
|
109 |
Matrices are similiarly named \texttt{MatDxDT}. For instance a 4D
|
|
|
110 |
\texttt{double} matrix is called \texttt{Mat4x4d}.
|
|
|
111 |
|
|
|
112 |
\subsection{How to use CGLA}
|
|
|
113 |
|
|
|
114 |
If you need a given \texttt{CGLA} class you can find the header file that
|
|
|
115 |
contains it in this document. Simply include the header file and use
|
|
|
116 |
the class. Remember also that all \texttt{CGLA} functions and classes live in
|
|
|
117 |
the \texttt{CGLA} namespace! Lastly, look at the example programs that came
|
|
|
118 |
with the code.
|
|
|
119 |
|
|
|
120 |
An important point is that you should never use the Arith... classes
|
|
|
121 |
directly. Classes whose names begin with Arith are templates used for
|
|
|
122 |
deriving concrete types. It is simpler, cleaner, and the intended
|
|
|
123 |
thing to do to only use the derived types.
|
|
|
124 |
|
|
|
125 |
In some cases, you may find that you need a vector or matrix class
|
|
|
126 |
that I haven't defined. If so, it is fortunate that \texttt{CGLA} is easy to
|
|
|
127 |
extend. Just look at, say, \texttt{Vec4f} if you need a \texttt{Vec5d}
|
|
|
128 |
class.
|
|
|
129 |
|
|
|
130 |
For some more specific help look at the next section where some of the
|
|
|
131 |
commonly used operations are shown.
|
|
|
132 |
|
|
|
133 |
|
|
|
134 |
\subsection{CGLA: Examples of Usage}
|
|
|
135 |
|
|
|
136 |
The examples below are by no means complete. Many things are
|
|
|
137 |
possible but not covered below. However, most of the common usage is
|
|
|
138 |
shown, so this should be enough to get you started. Note that in the
|
|
|
139 |
following we assume that you are \texttt{using namespace CGLA} and
|
|
|
140 |
hence don't prefix with \texttt{CGLA::}.
|
|
|
141 |
|
|
|
142 |
In short, to compile the examples below you would need the following
|
|
|
143 |
in the top of your file
|
|
|
144 |
|
|
|
145 |
\begin{verbatim}
|
|
|
146 |
#include <iostream> // For input output
|
|
|
147 |
#include "CGLA/Vec3f.h"
|
|
|
148 |
#include "CGLA/Quaternion.h"
|
|
|
149 |
#include "CGLA/Mat4x4f.h"
|
|
|
150 |
|
|
|
151 |
using namespace std; // For input output
|
|
|
152 |
using namespace CGLA;
|
|
|
153 |
\end{verbatim}
|
|
|
154 |
|
|
|
155 |
To begin with let us create 3 3D vectors. This is done as follows:
|
|
|
156 |
\begin{verbatim}
|
|
|
157 |
Vec3f p0(10,10,10);
|
|
|
158 |
Vec3f p1(20,10,10);
|
|
|
159 |
Vec3f p2(10,20,10);
|
|
|
160 |
\end{verbatim}
|
|
|
161 |
if we had left out the arguments the three vectors would have been
|
|
|
162 |
uninitialized, at least in release mode. If we compile in debug mode
|
|
|
163 |
they would have been initialized to ``Not a Number'' to aid
|
|
|
164 |
debugging. However, the $[10 \;10 \;10]^T$ vector could also have been
|
|
|
165 |
created differently, using
|
|
|
166 |
\begin{verbatim}
|
|
|
167 |
Vec3f p0(10);
|
|
|
168 |
\end{verbatim}
|
|
|
169 |
|
|
|
170 |
We often need arithmetic operators which work on vectors and matrices. This is supported by CGLA through overloaded versions of the standard arithmetic operators. Using \texttt{Vec3f} and \texttt{+} as examples, we have the following syntax:
|
|
|
171 |
\begin{verbatim}
|
|
|
172 |
Vec3f p0(0,1,2);
|
|
|
173 |
Vec3f p1(0,1,0);
|
|
|
174 |
|
|
|
175 |
Vec3f p2 = p0 + p1;
|
|
|
176 |
p2 += p1; // Same as p2 = p2 + p1
|
|
|
177 |
\end{verbatim}
|
|
|
178 |
Thus both the normal form of \texttt{+} and the assignment form are supported for \texttt{Vec3f}. The same is true of the other arithmetic operators \texttt{-*/} and the operators are supported for most CGLA entities including all matrix and vector types.
|
|
|
179 |
|
|
|
180 |
Multiplication is also supported for matrix vector products:
|
|
|
181 |
\begin{verbatim}
|
|
|
182 |
Vec4d p0(1,1,0,1);
|
|
|
183 |
|
|
|
184 |
Mat4x4d m = rotation_Mat4x4d(ZAXIS, 3.14);
|
|
|
185 |
|
|
|
186 |
Vec4d p1 = m * p0;
|
|
|
187 |
\end{verbatim}
|
|
|
188 |
|
|
|
189 |
but all vectors are column vectors, so only right multiplication is supported. We cannot multiply a vector on the left side of a matrix. However, there is, of course, a \texttt{dot} function which computes the dot product of two vectors.
|
|
|
190 |
|
|
|
191 |
A very common operation is to compute the normal of a triangle from
|
|
|
192 |
the position of its vertices. Assuming the three vectors represent the
|
|
|
193 |
vertices of a triangle, we can compute the normal by finding the
|
|
|
194 |
vector from the first vertex to the two other vertices, taking the
|
|
|
195 |
cross product and normalizing the result. This is a one-liner:
|
|
|
196 |
|
|
|
197 |
\begin{verbatim}
|
|
|
198 |
Vec3f n = normalize(cross(p1-p0,p2-p0));
|
|
|
199 |
\end{verbatim}
|
|
|
200 |
|
|
|
201 |
Quite a lot goes on in the snippet above. Observe that the - operator
|
|
|
202 |
also works for vectors. In fact almost all the arithmetic operators
|
|
|
203 |
work for vectors. You can also use assignment operators (i.e
|
|
|
204 |
\texttt{+=}) which is often faster. Then there is the function
|
|
|
205 |
\texttt{cross} which simply computes the cross product of its
|
|
|
206 |
arguments. Finally the vector is normalized using the
|
|
|
207 |
function normalize.
|
|
|
208 |
|
|
|
209 |
Of course, we can print all or at least most CGLA entities. For
|
|
|
210 |
example
|
|
|
211 |
|
|
|
212 |
\begin{verbatim}
|
|
|
213 |
cout << n << endl;
|
|
|
214 |
\end{verbatim}
|
|
|
215 |
|
|
|
216 |
will print the normal vector just computed. We can also treat a vector
|
|
|
217 |
as an array as shown below
|
|
|
218 |
|
|
|
219 |
\begin{verbatim}
|
|
|
220 |
float x = n[0];
|
|
|
221 |
\end{verbatim}
|
|
|
222 |
|
|
|
223 |
here, of course, we just extracted the first coordinate of the
|
|
|
224 |
vector.
|
|
|
225 |
|
|
|
226 |
CGLA contains a number of features that are not used very frequently,
|
|
|
227 |
but which are used frequently enough to warrent inclusion. A good
|
|
|
228 |
example is assigning to a vector using spherical coordinates:
|
|
|
229 |
|
|
|
230 |
\begin{verbatim}
|
|
|
231 |
Vec3f p;
|
|
|
232 |
p.set_spherical(0.955317, 3.1415926f/4.0f , 1);
|
|
|
233 |
\end{verbatim}
|
|
|
234 |
|
|
|
235 |
CGLA also includes a quaternion class. Here it is used to construct a
|
|
|
236 |
quaternion which will rotate the x axis into the y axis.
|
|
|
237 |
|
|
|
238 |
\begin{verbatim}
|
|
|
239 |
Quaternion q;
|
|
|
240 |
q.make_rot(Vec3f(1,0,0), Vec3f(0,1,0));
|
|
|
241 |
\end{verbatim}
|
|
|
242 |
|
|
|
243 |
Next, we construct a $4\times 4$ matrix \texttt{m} and assign a translation
|
|
|
244 |
matrix to the newly constructed matrix. After that we ask the
|
|
|
245 |
quaternion to return a $4\times 4$ matrix corresponding to its
|
|
|
246 |
rotation. This rotation matrix is then multiplied onto \texttt{m}.
|
|
|
247 |
|
|
|
248 |
\begin{verbatim}
|
|
|
249 |
Mat4x4f m = translation_Mat4x4f(Vec3f(1,2,3));
|
|
|
250 |
m *= q.get_mat4x4f();
|
|
|
251 |
\end{verbatim}
|
|
|
252 |
|
|
|
253 |
Just like for vectors, the subscript operator works on
|
|
|
254 |
matrices. However, in this case there are two indices. Just using one
|
|
|
255 |
index will return the ith row as a vector as shown on the first line
|
|
|
256 |
below. On the second line we see that using two indices will get us an
|
|
|
257 |
element in the matrix.
|
|
|
258 |
|
|
|
259 |
\begin{verbatim}
|
|
|
260 |
Vec4f v4 = m[0];
|
|
|
261 |
float c = m[0][3];
|
|
|
262 |
\end{verbatim}
|
|
|
263 |
|
|
|
264 |
There is a number of constructors for vectors. The default constructor
|
|
|
265 |
will create a null vector as we have already seen. We can also specify
|
|
|
266 |
all the coordinates. Finally, we can pass just a single number
|
|
|
267 |
$a$. This will create the $[a\:a\:a]^T$ vector. For instance, below we
|
|
|
268 |
create the $[1\: 1\: 1]^T$ vector. Subsequently, this vector is
|
|
|
269 |
multiplied onto \texttt{m}.
|
|
|
270 |
|
|
|
271 |
\begin{verbatim}
|
|
|
272 |
Vec3f p(1);
|
|
|
273 |
Vec3f p2 = m.mul_3D_point(p);
|
|
|
274 |
\end{verbatim}
|
|
|
275 |
|
|
|
276 |
Note though that \texttt{m} is a $4\times
|
|
|
277 |
4$ matrix so ... how is that possible? Well, we use the function
|
|
|
278 |
\texttt{mul\_3D\_point} which, effectively, adds a $w=1$ coordinate to p
|
|
|
279 |
making it a 4D vector. This $w$ coordinate is stripped afterwards. In
|
|
|
280 |
practice, this means that the translation part of the matrix is also
|
|
|
281 |
applied. There is a similar function \texttt{mul\_3D\_vector} if we want
|
|
|
282 |
to transform vectors without having the translation. This function,
|
|
|
283 |
effectively, sets $w=0$.
|
|
|
284 |
|
|
|
285 |
Finally, CGLA is often used together with OpenGL although there is no
|
|
|
286 |
explicit tie to the OpenGL library. However, we can call the
|
|
|
287 |
\texttt{get} function of most CGLA entities to get a pointer to the
|
|
|
288 |
contents. E.g. \texttt{p.get()} will return a pointer to the first
|
|
|
289 |
float in the 3D vector \texttt{p}. This can be used with OpenGL's
|
|
|
290 |
``v'' functions as shown below.
|
|
|
291 |
|
|
|
292 |
\begin{verbatim}
|
|
|
293 |
glVertex3fv(p.get());
|
|
|
294 |
\end{verbatim}
|
|
|
295 |
|
|
|
296 |
\section{HMesh}
|
|
|
297 |
|
|
|
298 |
HMesh's \texttt{Manifold} class is a halfedge based mesh data structure. To use the basic functionality you need to include the Manifold.h header and use the appropriate name space:
|
|
|
299 |
\begin{verbatim}
|
|
|
300 |
#include <Manifold.h>
|
|
|
301 |
using namespace HMesh;
|
|
|
302 |
\end{verbatim}
|
|
|
303 |
This type of data structure restricts you to manifolds, but allows for general N-gons for arbitrary N and makes manipulation somewhat simpler than other types of data structures for meshes. Note that this particular implementation does not allow more than one loop per face.
|
|
|
304 |
|
|
|
305 |
Since the halfedge representation can only represent polygonized manifolds, the name of the data structure is
|
|
|
306 |
\begin{verbatim}
|
|
|
307 |
Manifold m;
|
|
|
308 |
\end{verbatim}
|
|
|
309 |
The central element of the data structure is the \textit{halfedge}. A halfedge is a representation of an edge, but it is imbued with a direction: it represents the edge in the counterclockwise loop around one of the two faces that share the edge. Thus, we always have two halfedges for a given edge, and a halfedge contains an integer index that points to its opposite halfedge. Similarly, indices point to the next and the previous halfedge in a counterclockwise loop as well as the incident face and the incident vertex in the direction of the halfedge.
|
|
|
310 |
|
|
|
311 |
A vertex has an integer index referencing one of the halfedges which emanate from the vertex, and a face has the index of one of the halfedges in the loop around the face. The entities and the integer indices used to refer from one entity to another are illustrated in Figure~\ref{fig:halfedge}. Note also that the pointers from an entity to another, e.g. from a halfedge to its next edge is the mechanism that allows us to traverse the mesh.
|
|
|
312 |
\begin{figure}[h!]
|
|
|
313 |
\centering
|
|
|
314 |
\includegraphics[width=0.5\textwidth]{halfedge-entities.pdf}
|
|
|
315 |
\caption{A halfedge and related entities. The halfedge has pointers to the \texttt{next} and previous (\texttt{prev}) halfedges in the counterclockwise loop around its face. It also has a pointer to the opposite (\texttt{opp}) halfedge, which corresponds to the same geometric edge but is part of the loop around the adjacent face. Finally, the halfedge has a pointer to the next \texttt{vertex} in the direction of the halfedge and to its \texttt{face}. A face has a pointer to exactly one halfedge (\texttt{last}) and a vertex has a pointer to one outgoing halfedge (\texttt{h}). The word ``pointer'' is a bit of a misnomer. In GEL we use integer indices instead of memory pointers. }
|
|
|
316 |
\label{fig:halfedge}
|
|
|
317 |
\end{figure}
|
|
|
318 |
|
|
|
319 |
The integer indices are not only used to store references from one entity to another. In GEL, we do not directly access entities but use integer IDs to refer to them. For instance to get the position of a vertex, which is stored as a \texttt{Vec3d}, we need
|
|
|
320 |
\begin{verbatim}
|
|
|
321 |
VertexID vid;
|
|
|
322 |
// ... obtain a vertex ID
|
|
|
323 |
vec3d p = m.pos(vid);
|
|
|
324 |
\end{verbatim}
|
|
|
325 |
Note that m.pos(vid) returns a reference to the vertex identified by vid, so we can also assign the vertex position simply using
|
|
|
326 |
\begin{verbatim}
|
|
|
327 |
m.pos(vid) = p;
|
|
|
328 |
\end{verbatim}
|
|
|
329 |
\texttt{VertexID}, \texttt{FaceID}, and \texttt{HalfEdgeID} are simply indices which we use to refer to entities in the mesh. These types are used to communicate with the \texttt{Manifold} class. For instance to get the position of a vertex, but many other functions -- both inside and outside the \texttt{Manifold} class take one of the ID types as argument. For instance,
|
|
|
330 |
\begin{verbatim}
|
|
|
331 |
VertexID Manifold::split_edge(HalfEdgeID h);
|
|
|
332 |
\end{verbatim}
|
|
|
333 |
splits an edge and returns a \texttt{VertexID} for the introduced vertex. For functions outside the class, we need to also give the \texttt{Manifold} as an argument since an ID does not tell the function which mesh we are referring to. For example
|
|
|
334 |
\begin{verbatim}
|
|
|
335 |
bool boundary(const Manifold& m, HalfEdgeID h);
|
|
|
336 |
\end{verbatim}
|
|
|
337 |
returns true if the halfedge identified by \texttt{h} is a boundary edge in \texttt{m}.
|
|
|
338 |
|
|
|
339 |
A very frequent operation is that we need to iterate over all the vertices in a mesh. This is done as follows:
|
|
|
340 |
\begin{verbatim}
|
|
|
341 |
for(VertexID v : m.vertices())
|
|
|
342 |
{
|
|
|
343 |
Vec3d p = m.pos(v);
|
|
|
344 |
// Do something with the position
|
|
|
345 |
}
|
|
|
346 |
\end{verbatim}
|
|
|
347 |
Since we also have ID types for faces and half edges, similar code could have been used to iterate over these entities. As a note: there are also specific iterator classes (e.g. \texttt{VertexIDIterator}) which can be used to iterate with old fashioned for loops. However, the syntax is far more cumbersome, so we suggest you stick with the range based for loops as shown above.
|
|
|
348 |
\subsection{Walker}
|
|
|
349 |
To loop over all vertices is one way of traversing the mesh. It ensures that we visit all vertices, but it does not relate to the connectivity of the mesh.
|
|
|
350 |
To traverse the mesh in a way that respects connectivity, we use the \texttt{Walker} class. A \texttt{Walker} always points to a specific halfedge, but unlike a \texttt{HalfEdgeID}, it can be used to move from edge to edge. For instance, to compute the centroid for a given face, we can use the following code:
|
|
|
351 |
\begin{verbatim}
|
|
|
352 |
Vec3d p(0);
|
|
|
353 |
FaceID f = m.faces();
|
|
|
354 |
Walker w = m.walker(f);
|
|
|
355 |
for(; !w.full_circle(); w = w.next())
|
|
|
356 |
p += m.pos(w.vertex());
|
|
|
357 |
p /= w.no_steps();
|
|
|
358 |
\end{verbatim}
|
|
|
359 |
The code above computes the centroid for the first face in the mesh. It does so by jumping from edge to edge until it has come full circle. For each jump, we add the vertex pointed to by the halfedge to \texttt{p}. The benefit of the \texttt{Walker} is that it allows us to move from a halfedge to all connected halfedges. The simple traversal functions are \texttt{next}, \texttt{prev}, and \texttt{opp}:
|
|
|
360 |
\begin{verbatim}
|
|
|
361 |
Walker w;
|
|
|
362 |
w = w.next();
|
|
|
363 |
w = w.prev();
|
|
|
364 |
w = w.opp();
|
|
|
365 |
\end{verbatim}
|
|
|
366 |
\texttt{prev} does the same as \texttt{circulate\_face\_cw} and \texttt{next} does the same as \\\texttt{circulate\_face\_ccw}. The longer names are indicative of the geometric operation while \texttt{prev} and \texttt{next} highlight the connection with the data structure. It is important to note that all of the functions which operate on \texttt{Walker} instances return a new \texttt{Walker} and do not alter the state of the current walker. Thus to walk around a face, we use \texttt{w = w.next()} as opposed to just \texttt{w.next()} in the for loop above.
|
|
|
367 |
That might seem inconvenient but it is more flexible since we can obtain, say, a nearby vertex just by concatenating some traversal functions. For instance \texttt{w.next().opp().next().vertex()} returns an ID to a vertex nearby -- without changing \texttt{w}. The sequence \texttt{w.next().opp().next()} creates three anonymous walkers. If, on the other hand, the functions used for walking updated the state of \texttt{w}, we would have needed to first make a named copy of \texttt{w} before updating its state, so that we could get back to the original state.
|
|
|
368 |
|
|
|
369 |
\texttt{Walker} can be used to circulate around both faces and vertices. Below, we use vertex circulation to compute the average of the neighbors of a vertex
|
|
|
370 |
\begin{verbatim}
|
|
|
371 |
Vec3d p(0);
|
|
|
372 |
VertexID v = m.vertices();
|
|
|
373 |
Walker w = m.walker(v)
|
|
|
374 |
for(;!w.full_circle(); w = w.circulate_vertex_ccw()){
|
|
|
375 |
p += m.pos(w.vertex());
|
|
|
376 |
p /= w.no_steps();
|
|
|
377 |
\end{verbatim}
|
|
|
378 |
|
|
|
379 |
Again, the \texttt{full\_circle()} function tests whether the walker has returned to the original halfedge whence it was created. We can also call \texttt{no\_steps() } which returns the number of steps taken. In this regard, a step of vertex circulation is considered atomic - even though \texttt{w.circulate\_vertex\_ccw()} is the same as \texttt{w.prev().opp()}.
|
|
|
380 |
|
|
|
381 |
From a walker we can easily obtain any of the mesh entities that the halfedge it refers to are incident upon. For instance \texttt{vertex()} returns the \texttt{VertexID} of the vertex which the halfedge points to, \texttt{face()} gives us the \texttt{FaceID} of the incident face and \texttt{halfedge()} simply returns the \texttt{HalfEdgeID}. There is one more option for circulation. The following code does precisely the same as the code above, i.e. it computes the average of the neighboring vertices.
|
|
|
382 |
\begin{verbatim}
|
|
|
383 |
Vec3d p(0);
|
|
|
384 |
int n = circulate_vertex_ccw(m, v,
|
|
|
385 |
[&](VertexID v){ p += m.pos(v); });
|
|
|
386 |
return p / n;
|
|
|
387 |
\end{verbatim}
|
|
|
388 |
Here the function \texttt{circulate\_vertex\_ccw} takes three arguments. The \texttt{Manifold}, \texttt{m}, and the vertex, \texttt{v}, around which we circulate, and, finally, a function that accepts a \texttt{VertexID}. This function gets called for every vertex adjacent to \texttt{v}. In this example, the function is given as a so called \textit{lambda} function. This allows us to write things compactly and to have the body of the function where it is needed. We could also have passed a function which accepts a \texttt{FaceID} or a \texttt{HalfEdgeID} or a \texttt{Walker}. Thus, we can visit incident faces and edges during circulation if that is what we need, and if we need go get some entity other than adjacent vertices, edges, or faces, we can use the \texttt{Walker} option which is the most generic.
|
|
|
389 |
|
|
|
390 |
To summarize there are three datatypes that we use to refer to mesh entitites.
|
|
|
391 |
\begin{itemize}
|
|
|
392 |
\item IDs \texttt{VertexID}, \texttt{HalfEdgeID}, and \texttt{FaceID} are simply indices. These types are what we use when calling functions that take arguments which refer to mesh entities.
|
|
|
393 |
\item ID iterators \texttt{VertexIDIterator}, \texttt{HalfEdgeIDIterator}, and\\ \texttt{FaceIDIterator} as their names imply can be used to iterate over the entities of a mesh. As such they reflect how the mesh is stored in memory. Dereferencing an ID iterator will give an ID.
|
|
|
394 |
\item \texttt{Walker} is used to walk from halfedge to halfedge on the mesh. It utilizes the references stored in halfedges to other halfedges in order to facilitate this traversal. Again, we can obtain an ID from a walker.
|
|
|
395 |
\end{itemize}
|
|
|
396 |
It might seem tiresome that you have to deal with three different methods for referring to mesh entities, but they do very different jobs. The IDs are simply indices (wrapped in a class) referring to mesh elements. The iterators combine this indices with a reference to the actual data structures. The walker contains functions that allow us to move from halfedge to halfedge and additionally keeps track of the starting point and number of steps taken.
|
|
|
397 |
|
|
|
398 |
We could have combined two or all three of these concepts, but would have led to rather bloated datatypes.
|
|
|
399 |
|
|
|
400 |
\subsection{Attributes}
|
|
|
401 |
Given a \texttt{VertexID}, \texttt{v}, and a \texttt{Manifold}, \texttt{m}, we can obtain the geometric position of the vertex with the method call \texttt{m.pos(v)}. If is fairly important to note that a reference to the position is returned. Thus, we can assign the position of a vertex:
|
|
|
402 |
\begin{verbatim}
|
|
|
403 |
Vec3d p;
|
|
|
404 |
VertexID v;
|
|
|
405 |
// ...
|
|
|
406 |
m.pos(v) = p;
|
|
|
407 |
\end{verbatim}
|
|
|
408 |
|
|
|
409 |
Geometric position is in fact the only attribute that is stored for a vertex. That might seem strange since we would very often like to associate any number of attributes with mesh entities. For instance, we might need to associate normals, colors, curvature tensors, counters, etc. with vertices, faces, or even edges.
|
|
|
410 |
|
|
|
411 |
Unfortunately, what precise attributes we need depends greatly on the precise application. To resolve this issue, we have introduces three attribute vector class templates which allow the user to create his own attribute vectors:
|
|
|
412 |
\begin{verbatim}
|
|
|
413 |
VertexAttributeVector<T> vertex_attrib_vector;
|
|
|
414 |
FaceAttributeVector<T> face_ttrib_vector;
|
|
|
415 |
HalfEdgeAttributeVector<T> halfedge_attrib_vector;
|
|
|
416 |
\end{verbatim}
|
|
|
417 |
where \texttt{T} is a typename. Attribute vectors are indexed with indices. Thus, a
|
|
|
418 |
\texttt{VertexAttributeVector} is indexed with a \texttt{VertexID} and likewise for the other types. To give a simple example of how this is used consider smoothing a mesh. The simplest way of smoothing is to replace the vertex with the average of its neighbors. However, if we copy the average value back before the average value for some neighbor has been computed, then we clearly use one average to compute another average and thereby introduce an unfortunate dependence on the ordering of the vertices. Consequently, we need to first compute all of the averages and then update the original vertices. The code looks as follows:
|
|
|
419 |
\begin{verbatim}
|
|
|
420 |
void smooth(Manifold& m, float t)
|
|
|
421 |
{
|
|
|
422 |
VertexAttributeVector<Vec3f> pos(m.total_vertices());
|
|
|
423 |
for(VertexID v : m.vertices())
|
|
|
424 |
{
|
|
|
425 |
if(!boundary(m, v))
|
|
|
426 |
{
|
|
|
427 |
Vec3f avg_pos(0);
|
|
|
428 |
Walker w = m.walker(v)
|
|
|
429 |
for(; !w.full_circle(); w = w.circulate_vertex_cw())
|
|
|
430 |
avg_pos += m.pos(w.vertex());
|
|
|
431 |
pos[v] = avg_pos / w.no_steps();
|
|
|
432 |
}
|
|
|
433 |
}
|
|
|
434 |
|
|
|
435 |
for(VertexID v : m.vertices())
|
|
|
436 |
{
|
|
|
437 |
if(!boundary(m, v))
|
|
|
438 |
m.pos(v) = pos[v];
|
|
|
439 |
}
|
|
|
440 |
}
|
|
|
441 |
\end{verbatim}
|
|
|
442 |
|
|
|
443 |
In the smoothing example the \texttt{VertexAttributeVector} containing the new positions is initialized to a size equal to the actual number of vertices. That is not strictly necessary since the attribute vectors automatically resize if the index stored in the ID is out of bounds. Thus, we never explicitly need to increase the size of an attribute vector. What if there are suddenly fewer vertices? In that case, we can clean up the attribute vector. However, we also need to clean up the manifold. The procedure looks as follows:
|
|
|
444 |
\begin{verbatim}
|
|
|
445 |
VertexAttributeVector<T> vertex_attrib_vector;
|
|
|
446 |
Manifold m;
|
|
|
447 |
// ....
|
|
|
448 |
|
|
|
449 |
IDRemap remap;
|
|
|
450 |
m.cleanup(remap);
|
|
|
451 |
vertex_attrib_vector.cleanup(remap);
|
|
|
452 |
\end{verbatim}
|
|
|
453 |
Calling \texttt{cleanup} on the \texttt{Manifold} removes unused mesh entities, resizes, and resize the vectors containing these entities. When we subsequently call cleanup on the attribute vector, the same reorganization is carried out on the attribute vector keeping it in sync. Strictly speaking cleanup is rarely needed, but it removes unused memory which can be a performance advantage.
|
|
|
454 |
|
|
|
455 |
\subsection{Extended Example: Edge Flipping}
|
|
|
456 |
The following example demonstrates how to create a \texttt{Manifold} and add polygons (in this case triangles) and finally how to flip edges of a manifold. First, let us define some vertices
|
|
|
457 |
\begin{verbatim}
|
|
|
458 |
vector<Vec3f> vertices(3);
|
|
|
459 |
vertices[0] = p1;
|
|
|
460 |
vertices[1] = p2;
|
|
|
461 |
vertices[2] = p3;
|
|
|
462 |
\end{verbatim}
|
|
|
463 |
and then a vector of faces. The faces vector contains the number of vertices of each face in the mesh we want tor produce. Initially, we want to make a mesh with just one single triangle, so we produce a vector of one element and set that element to 3.
|
|
|
464 |
\begin{verbatim}
|
|
|
465 |
vector<int> faces(1);
|
|
|
466 |
faces[0] = 3;
|
|
|
467 |
\end{verbatim}
|
|
|
468 |
Next, we need the index vector. This vector is a long list of indices to vertices. For each face, we store the indices to its vertices in this vector. Since we just have a triangle, we store three vertices in the vector.
|
|
|
469 |
\begin{verbatim}
|
|
|
470 |
vector<int> indices(3);
|
|
|
471 |
indices[0]=0;
|
|
|
472 |
indices[1]=1;
|
|
|
473 |
indices[2]=2;
|
|
|
474 |
\end{verbatim}
|
|
|
475 |
So, if we had had two triangles, we would have stored six indices. Finally, we call \texttt{build} with the indexed face set data, we have compiled.
|
|
|
476 |
\begin{verbatim}
|
|
|
477 |
m.build(3, reinterpret_cast<float*>(&vertices[0]),1,
|
|
|
478 |
&faces[0], &indices[0]);
|
|
|
479 |
\end{verbatim}
|
|
|
480 |
The result of the above is a mesh with a single triangle.
|
|
|
481 |
Next, we try to split an edge. We grab the first halfedge and split it: Now we have two triangles. Splitting the halfedge changes the containing triangle to a quadrilateral, and we call triangulate to divide it into two triangls again.
|
|
|
482 |
\begin{verbatim}
|
|
|
483 |
HalfEdgeID h = m.halfedge();
|
|
|
484 |
m.split_edge(h);
|
|
|
485 |
triangulate_face_by_edge_split(m, m.walker(h).face());
|
|
|
486 |
\end{verbatim}
|
|
|
487 |
Now, we obtain an iterator to the first of our (two) triangles.
|
|
|
488 |
\begin{verbatim}
|
|
|
489 |
FaceID f1 = m.faces();
|
|
|
490 |
\end{verbatim}
|
|
|
491 |
We create a vertex, \texttt{v}, at centre of \texttt{f1} and insert it in that face:
|
|
|
492 |
\begin{verbatim}
|
|
|
493 |
m.split_face_by_vertex(f1);
|
|
|
494 |
\end{verbatim}
|
|
|
495 |
The code below, marks one of a pair of halfedges (with the value 1).
|
|
|
496 |
\begin{verbatim}
|
|
|
497 |
HalfEdgeAttributeVector<int> touched;
|
|
|
498 |
|
|
|
499 |
// ....
|
|
|
500 |
|
|
|
501 |
for(HalfEdgeID h : m.halfedges())
|
|
|
502 |
{
|
|
|
503 |
if(m.walker(h).opp().halfedge() < h)
|
|
|
504 |
touched[h] =0;
|
|
|
505 |
else
|
|
|
506 |
touched[h] =1;
|
|
|
507 |
}
|
|
|
508 |
\end{verbatim}
|
|
|
509 |
Next, we set the \texttt{flipper} variable to point to the first of these halfedges:
|
|
|
510 |
\begin{verbatim}
|
|
|
511 |
flipper = m.halfedges_begin();
|
|
|
512 |
\end{verbatim}
|
|
|
513 |
The long piece of code below examines a halfedge pointed to by \texttt{flipper}, and if it is not a boundary halfedge it tries to flip it. Then it increments the \texttt{flipper} variable until it lands on a halfedge marked with 1. If \texttt{flipper} reaches the end of the list of halfedges, we start over.
|
|
|
514 |
\begin{verbatim}
|
|
|
515 |
if(!boundary(m, *flipper))
|
|
|
516 |
if(precond_flip_edge(m,*flipper))
|
|
|
517 |
m.flip_edge(*flipper);
|
|
|
518 |
do
|
|
|
519 |
{
|
|
|
520 |
++flipper;
|
|
|
521 |
if(flipper==m.halfedges_end())
|
|
|
522 |
{
|
|
|
523 |
flipper = m.halfedges_begin();
|
|
|
524 |
break;
|
|
|
525 |
}
|
|
|
526 |
}
|
|
|
527 |
while(touched[*flipper] == 0);
|
|
|
528 |
\end{verbatim}
|
|
|
529 |
This example may not serve much of a purpose but illustrates how we can iterate over all edges and flip them. From this example, we are fairly close to Delaunay triangulation.
|
|
|
530 |
|
|
|
531 |
\subsection{HMesh tools}
|
|
|
532 |
|
|
|
533 |
The HMesh directory contains not only the basic data structure and functionality for traversing a mesh. There are also tools for mesh optimization through energy minimization, several algorithms for mesh smoothing, a number of tools for cleaning meshes as well as tools for simplifying meshes.
|
|
|
534 |
|
|
|
535 |
\section{GLGraphics}
|
|
|
536 |
|
|
|
537 |
\texttt{GLGraphics} contains a set of tools needed for visualization of geometrical objects. In particular, this namespace contains mesh rendering facilities, and in the following, a very simple example is given. This section is basically a walk through of the simplest GEL program that draws a mesh. Apart from GEL, we also use OpenGL and that requires a toolkit for connecting with the window system. With GEL programs, one generally uses GLUT, and this example is not an exception.
|
|
|
538 |
|
|
|
539 |
For starters, we need to include some files mostly from \texttt{GLGraphics} but also the header file for the mesh load function of \texttt{HMesh} and the \texttt{GLEW} header. Why incude \texttt{glew.h} rather than just \texttt{gl.h}? Well, \texttt{HMesh/draw.h} contains some draw functions which rely on shaders, and since \texttt{gl.h} is normally not up to date, it makes sense to use \texttt{glew.h} instead of \texttt{gl.h} even though it inflicts a dependency. We include \texttt{GLGraphics/gel\_glut.h} rather than the normal \texttt{glut.h}. That is because the GEL version works the same on Windows, OSX, Linux and probably other platforms.
|
|
|
540 |
|
|
|
541 |
We also open the namespaces we will need and declare two globals: The view controller \texttt{view\_ctrl} and the mesh \texttt{mani}. The view controller class is rather complex. It more or less insulates you from having to deal directly with the projections and transformations of OpenGL: It sets up a perspective projection and also the modelview transformation needed to view the object. From a user interface perspective, it defines a virtual trackball which allows you to rotate the object.
|
|
|
542 |
\begin{verbatim}
|
|
|
543 |
#include <GL/glew.h>
|
|
|
544 |
#include <GLGraphics/gel_glut.h>
|
|
|
545 |
#include <GLGraphics/draw.h>
|
|
|
546 |
#include <GLGraphics/GLViewController.h>
|
|
|
547 |
#include <HMesh/load.h>
|
|
|
548 |
|
|
|
549 |
using namespace std;
|
|
|
550 |
using namespace HMesh;
|
|
|
551 |
using namespace CGLA;
|
|
|
552 |
using namespace GLGraphics;
|
|
|
553 |
|
|
|
554 |
GLViewController* view_ctrl;
|
|
|
555 |
Manifold mani;
|
|
|
556 |
\end{verbatim}
|
|
|
557 |
|
|
|
558 |
Below is the display function which is called from the GLUT event loop whenever an event that requires redrawing is received. All drawing takes place inside this function. In this case, it is simple since all we need to do is clear the screen (and depth buffer), set up a new modelview transformation with the view controller and then call the draw function followed by a swap of buffers.
|
|
|
559 |
|
|
|
560 |
The draw function sends the polygons to the graphics card. It is very inefficient and uses the fixed function pipeline. At a minimum one should draw the polygons to a display list, but for a simple example, this works. The buffer swap is because we use double buffering; in other words, we draw to the back buffer.
|
|
|
561 |
\begin{verbatim}
|
|
|
562 |
void display() {
|
|
|
563 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
564 |
view_ctrl->set_gl_modelview();
|
|
|
565 |
draw(mani);
|
|
|
566 |
glutSwapBuffers();
|
|
|
567 |
}
|
|
|
568 |
|
|
|
569 |
\end{verbatim}
|
|
|
570 |
The mouse callback below is called when a mouse event has occurred. This is where user input for the virtual trackball is registered. We check whether a mouse is pressed or released. If it is pressed, we detect which button and then ``grab'' the virtual trackball in order to rotate, zoom, or pan depending on which button is pressed. If the button is released, we inform the view controller of this.
|
|
|
571 |
\begin{verbatim}
|
|
|
572 |
void mouse(int button, int state, int x, int y) {
|
|
|
573 |
Vec2i pos(x,y);
|
|
|
574 |
if (state==GLUT_DOWN)
|
|
|
575 |
{
|
|
|
576 |
if (button==GLUT_LEFT_BUTTON)
|
|
|
577 |
view_ctrl->grab_ball(ROTATE_ACTION,pos);
|
|
|
578 |
else if (button==GLUT_MIDDLE_BUTTON)
|
|
|
579 |
view_ctrl->grab_ball(ZOOM_ACTION,pos);
|
|
|
580 |
else if (button==GLUT_RIGHT_BUTTON)
|
|
|
581 |
view_ctrl->grab_ball(PAN_ACTION,pos);
|
|
|
582 |
}
|
|
|
583 |
else if (state==GLUT_UP)
|
|
|
584 |
view_ctrl->release_ball();
|
|
|
585 |
}
|
|
|
586 |
\end{verbatim}
|
|
|
587 |
If the mouse moves with a button depressed, we register motion in the function below. The new mouse position is sent to the view controller
|
|
|
588 |
which then, e.g., rotates the trackball accordingly. Note that this function ends by posting a redisplay event, i.e. informs GLUT that display should be called at the earliest convenience.
|
|
|
589 |
\begin{verbatim}
|
|
|
590 |
void motion(int x, int y) {
|
|
|
591 |
Vec2i pos(x,y);
|
|
|
592 |
view_ctrl->roll_ball(Vec2i(x,y));
|
|
|
593 |
glutPostRedisplay();
|
|
|
594 |
}
|
|
|
595 |
\end{verbatim}
|
|
|
596 |
|
|
|
597 |
Finally, in the main function, we first load the mesh and then setup glut. This mostly involves defining the callback function just described. We set up the view controller passing it window dimensions and the size and position of the bounding sphere of the object just loaded. Finally, we do some minimal OpenGL set up: Essentially clearing the depth buffer and enabling lights. Finally, we pass control to the GLUT event loop.
|
|
|
598 |
\begin{verbatim}
|
|
|
599 |
int main(int argc, char** argv) {
|
|
|
600 |
string file = "bunny-little.x3d";
|
|
|
601 |
load(file, mani);
|
|
|
602 |
|
|
|
603 |
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
|
|
|
604 |
glutInitWindowSize(800, 800);
|
|
|
605 |
glutInit(&argc, argv);
|
|
|
606 |
glutCreateWindow("GEL Example Program");
|
|
|
607 |
glutDisplayFunc(display);
|
|
|
608 |
glutMouseFunc(mouse);
|
|
|
609 |
glutMotionFunc(motion);
|
|
|
610 |
|
|
|
611 |
Vec3f bsphere_center(0,0,0);
|
|
|
612 |
float bsphere_radius = 3;
|
|
|
613 |
mani.get_bsphere(bsphere_center,bsphere_radius);
|
|
|
614 |
view_ctrl = new GLViewController(800,800,
|
|
|
615 |
bsphere_center,bsphere_radius*2);
|
|
|
616 |
|
|
|
617 |
glClearColor(0.0f, 0.0f, .5f, 0.f);
|
|
|
618 |
glEnable(GL_DEPTH_TEST);
|
|
|
619 |
glEnable(GL_LIGHTING);
|
|
|
620 |
glEnable(GL_LIGHT0);
|
|
|
621 |
|
|
|
622 |
glutMainLoop();
|
|
|
623 |
}
|
|
|
624 |
\end{verbatim}
|
|
|
625 |
Note that the mesh can be either an X3D, OBJ, PLY, or OFF files. However, only the geometry is loaded not the textures. In the case of OBJ files, we also convert all polygons to triangles, because the TriMesh loader (see the geometry class) is in fact used.
|
|
|
626 |
|
|
|
627 |
Apart from the drawing functions and the viewcontroller, GLGraphics also contains other useful pieces: A few functions which greatly simplify shader loading and SOIL. SOIL is a small library for loading images. It is designed for OpenGL and makes it easy to e.g. save screen shots or load an image for use as a texture. While the need for image loading is fairly obvious, it might be more difficult to see the need for shader loading functions. However, it \textit{is} a little tricky to make shader programs in C++ using GLSL but the problems are almost all silly little things. For instance, how do you robustly read from a text file? How do you compile a shader and check for errors? In OpenGL What is the difference between a GLSL program and a shader anyway?
|
|
|
628 |
|
|
|
629 |
The short answer to the last question is this: A "shader" can be either a vertex shader, a geometry shader (in OpenGL 2.0) or a fragment shader. A "program" is a linked combination of these three types of shaders. Note that you don't have to have a geometry shader.
|
|
|
630 |
|
|
|
631 |
These functions attempt to obviate the need for an answer to the first two questions by providing an API for loading and compiling shaders and checking for errors. However, I don't include functions for creating the program, attaching shaders and linking. Why not? Because the need for flexibility means that the API would be just as complex as just using the OpenGL functions directly! However, loading shaders and checking for errors in compiled shaders is different. It makes sense to wrap that. It also makes sense to wrap the error checking for programs that are linked, so there is a function for that too.
|
|
|
632 |
|
|
|
633 |
Since shader loading and error check sandwhich the two calls needed for compilation, the most important function in this API, \texttt{create\_glsl\_shader}, loads a shader, compiles it, checks for errors and returns the shader handle. There is also a version which creates a shader from a string.
|
|
|
634 |
|
|
|
635 |
There is some code below to illustrate usage.
|
|
|
636 |
\begin{verbatim}
|
|
|
637 |
GLuint vs = create_glsl_shader(GL_VERTEX_SHADER,
|
|
|
638 |
shader_path, "tri.vert");
|
|
|
639 |
GLuint gs = create_glsl_shader(GL_GEOMETRY_SHADER_EXT,
|
|
|
640 |
shader_path, "tri.geom");
|
|
|
641 |
GLuint fs = create_glsl_shader(GL_FRAGMENT_SHADER,
|
|
|
642 |
shader_path, "tri.frag");
|
|
|
643 |
|
|
|
644 |
prog_P0 = glCreateProgram();
|
|
|
645 |
|
|
|
646 |
if(vs) glAttachShader(prog_P0, vs);
|
|
|
647 |
if(gs) glAttachShader(prog_P0, gs);
|
|
|
648 |
if(fs) glAttachShader(prog_P0, fs);
|
|
|
649 |
|
|
|
650 |
// Specify input and output for the geometry shader.
|
|
|
651 |
// Note that this must be done before linking the program.
|
|
|
652 |
glProgramParameteriEXT(prog_P0,GL_GEOMETRY_INPUT_TYPE_EXT,
|
|
|
653 |
GL_TRIANGLES);
|
|
|
654 |
glProgramParameteriEXT(prog_P0,GL_GEOMETRY_VERTICES_OUT_EXT,3);
|
|
|
655 |
glProgramParameteriEXT(prog_P0,GL_GEOMETRY_OUTPUT_TYPE_EXT,
|
|
|
656 |
GL_TRIANGLE_STRIP);
|
|
|
657 |
|
|
|
658 |
// Link the program object and print out the info log
|
|
|
659 |
glLinkProgram(prog_P0);
|
|
|
660 |
print_glsl_program_log(prog_P0);
|
|
|
661 |
|
|
|
662 |
// Install program object as part of current state
|
|
|
663 |
glUseProgram(prog_P0);
|
|
|
664 |
|
|
|
665 |
// Set the value of a uniform
|
|
|
666 |
glUniform2f(glGetUniformLocation(prog_P0,"WIN_SCALE"),
|
|
|
667 |
win_size_x/2.0, win_size_y/2.0);
|
|
|
668 |
\end{verbatim}
|
|
|
669 |
|
|
|
670 |
\section{LinAlg}
|
|
|
671 |
Sometimes the simple 2,3,4 dimensional vectors from CGLA just don't cut it. We often need to solve large linear systems. The LinAlg namespace contains some vector and matrix classes and an interface to Lapack. This provides fairly easy way to do many numerical computations.
|
|
|
672 |
|
|
|
673 |
In the example below, we find coefficients for $a x^2 + b y^2 +cxy + dx + ey + f$ such that the surface contains six specific points. The coefficients are found by solving the linear system $\mathbf A \mathbf x = \mathbf b$ where the rows of the $\mathbf A$ matrix contains $x^2 \; y^2 \; xy \; x \; y \; 1$ computed from the $xy$ positions of each point, $\mathbf x$ contains the unknown coefficients, and $\mathbf b$ on the right hand side contains the $z$ values of the six points. Using LinAlg, we implement it as follows:
|
|
|
674 |
\begin{verbatim}
|
|
|
675 |
CMatrix A(6,6);
|
|
|
676 |
CVector b(6);
|
|
|
677 |
|
|
|
678 |
for(int i=0;i<6;++i)
|
|
|
679 |
{
|
|
|
680 |
A.set(i,0,pts[i][0]*pts[i][0]);
|
|
|
681 |
A.set(i,1,pts[i][1]*pts[i][1]);
|
|
|
682 |
A.set(i,2,pts[i][0]*pts[i][1]);
|
|
|
683 |
A.set(i,3,pts[i][0]);
|
|
|
684 |
A.set(i,4,pts[i][1]);
|
|
|
685 |
A.set(i,5,1);
|
|
|
686 |
b[i] = pts[i][2];
|
|
|
687 |
}
|
|
|
688 |
CVector x;
|
|
|
689 |
LinearSolve(A,b,x);
|
|
|
690 |
\end{verbatim}
|
|
|
691 |
While this example shows only the function for solving a system which should have a solution, there are also functions for solving overdetermined systems in the least square sense and finding the minimum norm solution to underdetermined systems using singular value decomposition. There is also a function for finding eigenvalues and eigenvectors of symmetric matrices and more.
|
|
|
692 |
\section{Geometry}
|
|
|
693 |
The Geometry namespace contains many different classes and functions. Roughly, we can divide the contents into
|
|
|
694 |
\begin{itemize}
|
|
|
695 |
\item Spatial data structures such as kd tress, bounding box hierarchies, and BSP trees.
|
|
|
696 |
\item A Triangle mesh data structure called TriMesh. TriMesh is different from HMesh in that it only contains triangles. It is much more an API for real time rendering which also facilitates material properties and not just geometry.
|
|
|
697 |
\item Classes and functions for dealing with volume data. HGrid and RGrid are, respectively, a class for hierarchically stored voxel grids (sparse grids) and regular grids. There is a number of functions for traversing voxel grids systematically and along rays.
|
|
|
698 |
\item Surprisingly, you will also find my C++ port of Jules Bloomenthals isosurface polygonizer and an implementation of Luiz Velho's parametric surface tiler in this namespace.
|
|
|
699 |
\end{itemize}
|
|
|
700 |
One of the most frequently used tools in Geometry is the kD tree class which merits an example. In the code below, we create a kD tree which had \texttt{Vec3f}s as keys and integers as data. Note that since the kD tree is a template, we could use any other type as both key and data, however, only CGLA vectors have been tested as key types, but there are no restrictions on data types.
|
|
|
701 |
\begin{verbatim}
|
|
|
702 |
KDTree<Vec3f,int> tree;
|
|
|
703 |
for(int i=0;i<N;++i)
|
|
|
704 |
{
|
|
|
705 |
Vec3f p0;
|
|
|
706 |
make_ran_point(p0);
|
|
|
707 |
tree.insert(p0, i);
|
|
|
708 |
}
|
|
|
709 |
tree.build();
|
|
|
710 |
\end{verbatim}
|
|
|
711 |
Note also, that before we use the tree it needs to be built. This is because the internal representation is a balanced binary heap which it is easier to build at the end rather than maintain during insertion of new points. To look for the points near a given point, we can call
|
|
|
712 |
\begin{verbatim}
|
|
|
713 |
Vec3f p0;
|
|
|
714 |
// ...
|
|
|
715 |
std::vector<Vec3f> keys;
|
|
|
716 |
std::vector<int> vals;
|
|
|
717 |
float radius = 1.95f;
|
|
|
718 |
int N = tree.in_sphere(p0, radius , keys, vals);
|
|
|
719 |
\end{verbatim}
|
|
|
720 |
This will find all points in a radius of 1.95 units from \texttt{p0}. The keys and values are stored in \texttt{keys} and \texttt{vals} when the functions returns. The return value of the function is the number of points found within the radius.
|
|
|
721 |
|
|
|
722 |
Sometimes, we simply want the point closest to a given point \texttt{p0} which is done using \texttt{tree.closest\_point(p0, d, pos, x)} where the two first arguments indicate the point and the maximum search radius. The two last arguments are set to the key and data of the point found to be closest to \texttt{p0}. The function returns true if a closest point was found.
|
|
|
723 |
|
|
|
724 |
|
|
|
725 |
|
|
|
726 |
|
|
|
727 |
\section{Util}
|
|
|
728 |
The Util namespace contains a rather mixed bag of utilities many of which are quite useful and very diverse.
|
|
|
729 |
\begin{trivlist}
|
|
|
730 |
\item The XML parser for instance is written from scratch and the basis for our X3D loader although it will load any XML file.
|
|
|
731 |
\item \texttt{Grid2D} is often useful when we want to deal with e.g. simulation on a 2D grid.
|
|
|
732 |
\item The \texttt{Timer} class is nearly indispensable when we need to time something, and it is very easy to use.
|
|
|
733 |
\item \texttt{ArgExtracter} is a class for getting the command line arguments to a program.
|
|
|
734 |
\item There is more - for instance a resource manager template, some string utility templates and a hash table implementation.
|
|
|
735 |
\end{trivlist}
|
|
|
736 |
Arguably, it would be more pretty if these pieces had been more thematically linked, but many are used a lot, and it seemed reasonable to have somewhere to stick various functions and classes that did not naturally belong elsewhere.
|
|
|
737 |
|
|
|
738 |
|
|
|
739 |
\section{Authors}
|
|
|
740 |
\begin{trivlist}
|
|
|
741 |
\item Andreas B{\ae}rentzen created the library and has written most of the code not contributed by the people below.
|
|
|
742 |
\item Jeppe Revall Frisvad has contributed greatly to the CGLA part of GEL and elsewhere.
|
|
|
743 |
\item Christian Thode Larsen rewrote the classes in HMesh. They are much nicer for it.
|
|
|
744 |
\item Anders Wang Kristensen has been a great help in the process of reworking HMesh and also contributed some code for an OpenGL console.
|
|
|
745 |
\item Henrik Aan{\ae}s provided the original LinAlg package, which is a set of simple but very useful Lapack bindings.
|
|
|
746 |
\item Rasmus Paulsen, Bjarke Jakobsen, Marek Misztal helped with structure and compilation on various platforms with testing and ideas.
|
|
|
747 |
\end{trivlist}
|
|
|
748 |
|
|
|
749 |
\section{License}
|
|
|
750 |
\begin{trivlist}
|
|
|
751 |
\item
|
|
|
752 |
You are allowed to use GEL for academic or commercial purposes. In particular, you are welcome to give GEL to students as a basis for academic work or to use it in your own applications.
|
|
|
753 |
\item
|
|
|
754 |
Regarding the use of GEL in other libraries. GEL does exist in slightly different versions, since we have created pared down packages for various courses. We would like to retain the prerogative of repackaging. If you would like to use GEL in its entirety in another library that is fine, but we do not allow you not to take bits and pieces and put them into a different library.
|
|
|
755 |
\item
|
|
|
756 |
If you use GEL, we would be grateful for due credit.
|
|
|
757 |
\item
|
|
|
758 |
Finally, we follow the MIT license as regards warranty:
|
|
|
759 |
\item
|
|
|
760 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
761 |
\item
|
|
|
762 |
If anything is unclear, please send an email to jab@imm.dtu.dk.
|
|
|
763 |
\end{trivlist}
|
|
|
764 |
\section{Open Source Code used in GEL}
|
|
|
765 |
|
|
|
766 |
Source code from other projects have been incorporated in GEL.
|
|
|
767 |
\begin{trivlist}
|
|
|
768 |
\item From Graphics Gems, two functions for inverting matrices and Jules Bloomenthal's polygonizer which I reworked from C to C++. This is allowed according to the license.
|
|
|
769 |
\item rply by Diego Nehab, Princeton University, and the Simple OpenGL Image Loader by Jonathan Dummer. These are MIT licensed and our use is congruent with that license.
|
|
|
770 |
\item The OpenGL Extension Wrangler Library is also included and appears to have a license identical to the MIT license. As mentioned, our use is congruent with that license.
|
|
|
771 |
\end{trivlist}
|
|
|
772 |
The incorporated code amounts to only a small fraction of the GEL source code.
|
|
|
773 |
\end{document}
|